Fix broken RSS code.
[monky] / src / weather.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  *
3  * Conky, a system monitor, based on torsmo
4  *
5  * Please see COPYING for details
6  *
7  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
8  *      (see AUTHORS)
9  * All rights reserved.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * vim: ts=4 sw=4 noet ai cindent syntax=c
24  *
25  */
26
27 #include "conky.h"
28 #include "logging.h"
29 #include "weather.h"
30 #include "temphelper.h"
31 #include "ccurl_thread.h"
32 #include <time.h>
33 #include <ctype.h>
34 #ifdef MATH
35 #include <math.h>
36 #endif /* MATH */
37 #ifdef XOAP
38 #include <libxml/parser.h>
39 #include <libxml/xpath.h>
40
41 /* Xpath expressions for XOAP xml parsing */
42 #define NUM_XPATH_EXPRESSIONS_CC 8
43 const char *xpath_expression_cc[NUM_XPATH_EXPRESSIONS_CC] = {
44         "/weather/cc/lsup", "/weather/cc/tmp", "/weather/cc/t",
45         "/weather/cc/bar/r", "/weather/cc/wind/s", "/weather/cc/wind/d",
46         "/weather/cc/hmid", "/weather/cc/icon"
47 };
48
49 #define NUM_XPATH_EXPRESSIONS_DF 8
50 const char *xpath_expression_df[NUM_XPATH_EXPRESSIONS_DF] = {
51         "/weather/dayf/day[*]/hi", "/weather/dayf/day[*]/low",
52         "/weather/dayf/day[*]/part[1]/icon", "/weather/dayf/day[*]/part[1]/t",
53         "/weather/dayf/day[*]/part[1]/wind/s","/weather/dayf/day[*]/part[1]/wind/d",
54         "/weather/dayf/day[*]/part[1]/ppcp", "/weather/dayf/day[*]/part[1]/hmid"
55 };
56 #endif /* XOAP */
57
58 /* Possible sky conditions */
59 #define NUM_CC_CODES 6
60 const char *CC_CODES[NUM_CC_CODES] = {
61         "SKC", "CLR", "FEW", "SCT", "BKN", "OVC"
62 };
63
64 /* Possible weather modifiers */
65 #define NUM_WM_CODES 9
66 const char *WM_CODES[NUM_WM_CODES] = {
67         "VC", "MI", "BC", "PR", "TS", "BL",
68         "SH", "DR", "FZ"
69 };
70
71 /* Possible weather conditions */
72 #define NUM_WC_CODES 17
73 const char *WC_CODES[NUM_WC_CODES] = {
74         "DZ", "RA", "GR", "GS", "SN", "SG",
75         "FG", "HZ", "FU", "BR", "DU", "SA",
76         "FC", "PO", "SQ", "SS", "DS"
77 };
78
79 static ccurl_location_t *locations_head_cc = 0;
80 #ifdef XOAP
81 static ccurl_location_t *locations_head_df = 0;
82 #endif
83
84 void weather_free_info(void)
85 {
86         ccurl_free_locations(&locations_head_cc);
87 #ifdef XOAP
88         ccurl_free_locations(&locations_head_df);
89 #endif
90 }
91
92 int rel_humidity(int dew_point, int air) {
93         const float a = 17.27f;
94         const float b = 237.7f;
95
96         float diff = a*(dew_point/(b+dew_point)-air/(b+air));
97 #ifdef MATH
98         return (int)(100.f*expf(diff));
99 #else
100         return (int)(16.666667163372f*(6.f+diff*(6.f+diff*(3.f+diff))));
101 #endif /* MATH */
102 }
103
104 #ifdef XOAP
105 static void parse_df(PWEATHER_FORECAST *res, xmlXPathContextPtr xpathCtx)
106 {
107         int i, j, k;
108         char *content;
109         xmlXPathObjectPtr xpathObj;
110
111         for (i = 0; i < NUM_XPATH_EXPRESSIONS_DF; i++) {
112                 xpathObj = xmlXPathEvalExpression((const xmlChar *)xpath_expression_df[i], xpathCtx);
113                 if (xpathObj != NULL) {
114                         xmlNodeSetPtr nodes = xpathObj->nodesetval;
115                         k = 0;
116                         for (j = 0; j < nodes->nodeNr; ++j) {
117                                 if (nodes->nodeTab[j]->type == XML_ELEMENT_NODE) {
118                                         content = (char *)xmlNodeGetContent(nodes->nodeTab[k]);
119                                         switch(i) {
120                                         case 0:
121                                                 res->hi[k] = atoi(content);
122                                                 break;
123                                         case 1:
124                                                 res->low[k] = atoi(content);
125                                                 break;
126                                         case 2:
127                                                 strncpy(res->icon[k], content, 2);
128                                         case 3:
129                                                 strncpy(res->xoap_t[k], content, 31);
130                                                 break;
131                                         case 4:
132                                                 res->wind_s[k] = atoi(content);
133                                                 break;
134                                         case 5:
135                                                 res->wind_d[k] = atoi(content);
136                                                 break;
137                                         case 6:
138                                                 res->ppcp[k] = atoi(content);
139                                                 break;
140                                         case 7:
141                                                 res->hmid[k] = atoi(content);
142                                         }
143                                         xmlFree(content);
144                                         if (++k == FORECAST_DAYS) break;
145                                 }
146                         }
147                         xmlXPathFreeObject(xpathObj);
148                 }
149         }
150         return;
151 }
152
153 static void parse_weather_forecast_xml(PWEATHER_FORECAST *res, const char *data)
154 {
155         xmlDocPtr doc;
156         xmlXPathContextPtr xpathCtx;
157
158         if (!(doc = xmlReadMemory(data, strlen(data), "", NULL, 0))) {
159                 NORM_ERR("weather_forecast: can't read xml data");
160                 return;
161         }
162
163         xpathCtx = xmlXPathNewContext(doc);
164         if(xpathCtx == NULL) {
165                 NORM_ERR("weather_forecast: unable to create new XPath context");
166                 xmlFreeDoc(doc);
167                 return;
168         }
169
170         parse_df(res, xpathCtx);
171         xmlXPathFreeContext(xpathCtx);
172         xmlFreeDoc(doc);
173         return;
174 }
175
176 static void parse_cc(PWEATHER *res, xmlXPathContextPtr xpathCtx)
177 {
178         int i;
179         char *content;
180         xmlXPathObjectPtr xpathObj;
181
182         xpathObj = xmlXPathEvalExpression((const xmlChar *)"/error/err", xpathCtx);
183         if (xpathObj && xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0 &&
184                         xpathObj->nodesetval->nodeTab[0]->type == XML_ELEMENT_NODE) {
185                 content = (char *)xmlNodeGetContent(xpathObj->nodesetval->nodeTab[0]);
186                 NORM_ERR("XOAP error: %s", content);
187                 xmlFree(content);
188                 xmlXPathFreeObject(xpathObj);
189                 return;
190         }
191         xmlXPathFreeObject(xpathObj);
192
193         for (i = 0; i < NUM_XPATH_EXPRESSIONS_CC; i++) {
194                 xpathObj = xmlXPathEvalExpression((const xmlChar *)xpath_expression_cc[i], xpathCtx);
195                 if (xpathObj && xpathObj->nodesetval && xpathObj->nodesetval->nodeNr >0 &&
196                                 xpathObj->nodesetval->nodeTab[0]->type ==
197                                 XML_ELEMENT_NODE) {
198                         content = (char *)xmlNodeGetContent(xpathObj->nodesetval->nodeTab[0]);
199                         switch(i) {
200                                 case 0:
201                                         strncpy(res->lastupd, content, 31);
202                                         break;
203                                 case 1:
204                                         res->temp = atoi(content);
205                                         break;
206                                 case 2:
207                                         strncpy(res->xoap_t, content, 31);
208                                         break;
209                                 case 3:
210                                         res->bar = atoi(content);
211                                         break;
212                                 case 4:
213                                         res->wind_s = atoi(content);
214                                         break;
215                                 case 5:
216                                         res->wind_d = atoi(content);
217                                         break;
218                                 case 6:
219                                         res->hmid = atoi(content);
220                                         break;
221                                 case 7:
222                                         strncpy(res->icon, content, 2);
223                         }
224                         xmlFree(content);
225                 }
226                 xmlXPathFreeObject(xpathObj);
227         }
228         return;
229 }
230
231 static void parse_weather_xml(PWEATHER *res, const char *data)
232 {
233         xmlDocPtr doc;
234         xmlXPathContextPtr xpathCtx;
235
236         if (!(doc = xmlReadMemory(data, strlen(data), "", NULL, 0))) {
237                 NORM_ERR("weather: can't read xml data");
238                 return;
239         }
240
241         xpathCtx = xmlXPathNewContext(doc);
242         if(xpathCtx == NULL) {
243                 NORM_ERR("weather: unable to create new XPath context");
244                 xmlFreeDoc(doc);
245                 return;
246         }
247
248         parse_cc(res, xpathCtx);
249         xmlXPathFreeContext(xpathCtx);
250         xmlFreeDoc(doc);
251         return;
252 }
253 #endif /* XOAP */
254
255 /*
256  * Horrible hack to avoid using regexes
257  *
258  */
259
260 static inline void parse_token(PWEATHER *res, char *token) {
261
262         int i;
263         char s_tmp[64];
264
265         switch (strlen(token)) {
266
267                 //Check all tokens 2 chars long
268                 case 2:
269
270                         //Check if token is a weather condition
271                         for (i=0; i<2; i++) {
272                                 if (!isalpha(token[i])) break;
273                         }
274                         if (i==2) {
275                                 for(i=0; i<NUM_WC_CODES; i++) {
276                                         if (!strncmp(token, WC_CODES[i], 2)) {
277                                                 res->wc=i+1;
278                                                 break;
279                                         }
280                                 }
281                                 return;
282                         }
283
284                         //Check for CB
285                         if (!strcmp(token, "CB")) {
286                                 res->cc = 8;
287                                 return;
288                         }
289
290                         break;
291
292                         //Check all tokens 3 chars long
293                 case 3:
294
295                         //Check if token is a modified weather condition
296                         if ((token[0] == '+') || (token[0] == '-')) {
297                                 for (i=1; i<3; i++) {
298                                         if (!isalpha(token[i])) break;
299                                 }
300                                 if (i==3) {
301                                         for(i=0; i<NUM_WC_CODES; i++) {
302                                                 if (!strncmp(&token[1], WC_CODES[i], 2)) {
303                                                         res->wc=i+1;
304                                                         break;
305                                                 }
306                                         }
307                                         return;
308                                 }
309                         }
310
311                         //Check for NCD or NSC
312                         if ((!strcmp(token, "NCD")) || (!strcmp(token, "NSC"))) {
313                                 res->cc = 1;
314                                 return;
315                         }
316
317                         //Check for TCU
318                         if (!strcmp(token, "TCU")) {
319                                 res->cc = 7;
320                                 return;
321                         }
322
323                         break;
324
325                         //Check all tokens 4 chars long
326                 case 4:
327
328                         //Check if token is a modified weather condition
329                         for(i=0; i<NUM_WM_CODES; i++) {
330                                 if (!strncmp(token, WM_CODES[i], 2)) {
331                                         for(i=0; i<NUM_WC_CODES; i++) {
332                                                 if (!strncmp(&token[2], WC_CODES[i], 2)) {
333                                                         res->wc=i+1;
334                                                         return;
335                                                 }
336                                         }
337                                         break;
338                                 }
339                         }
340
341                         break;
342
343                         //Check all tokens 5 chars long
344                 case 5:
345
346                         //Check for CAVOK
347                         if (!strcmp(token, "CAVOK")) {
348                                 res->cc = 1;
349                                 return;
350                         }
351
352                         //Check if token is the temperature
353                         for (i=0; i<2; i++) {
354                                 if (!isdigit(token[i])) break;
355                         }
356                         if ((i==2) && (token[2] == '/')) {
357                                 for (i=3; i<5; i++) {
358                                         if (!isdigit(token[i])) break;
359                                 }
360                                 if (i==5) {
361                                         //First 2 digits gives the air temperature
362                                         res->temp=atoi(token);
363
364                                         //4th and 5th digits gives the dew point temperature
365                                         res->dew=atoi(&token[3]);
366
367                                         //Compute humidity
368                                         res->hmid = rel_humidity(res->dew, res->temp);
369
370                                         return;
371                                 }
372                         }
373
374                         //Check if token is the pressure
375                         if ((token[0] == 'Q') || (token[0] == 'A')) {
376                                 for (i=1; i<5; i++) {
377                                         if (!isdigit(token[i])) break;
378                                 }
379                                 if (i==5) {
380                                         if (token[0] == 'A') {
381                                                 //Convert inches of mercury to mbar
382                                                 res->bar = (int)(atoi(&token[1])*0.338637526f);
383                                                 return;
384                                         }
385
386                                         //Last 4 digits is pressure im mbar
387                                         res->bar = atoi(&token[1]);
388                                         return;
389                                 }
390                         }
391
392                         //Check if token is a modified weather condition
393                         if ((token[0] == '+') || (token[0] == '-')) {
394                                 for(i=0; i<NUM_WM_CODES; i++) {
395                                         if (!strncmp(&token[1], WM_CODES[i], 2)) {
396                                                 for(i=0; i<NUM_WC_CODES; i++) {
397                                                         if (!strncmp(&token[3], WC_CODES[i], 2)) {
398                                                                 res->wc=i+1;
399                                                                 return;
400                                                         }
401                                                 }
402                                                 break;
403                                         }
404                                 }
405                         }
406                         break;
407
408                         //Check all tokens 6 chars long
409                 case 6:
410
411                         //Check if token is the cloud cover
412                         for (i=0; i<3; i++) {
413                                 if (!isalpha(token[i])) break;
414                         }
415                         if (i==3) {
416                                 for (i=3; i<6; i++) {
417                                         if (!isdigit(token[i])) break;
418                                 }
419                                 if (i==6) {
420                                         //Check if first 3 digits gives the cloud cover condition
421                                         for(i=0; i<NUM_CC_CODES; i++) {
422                                                 if (!strncmp(token, CC_CODES[i], 3)) {
423                                                         res->cc=i+1;
424                                                         break;
425                                                 }
426                                         }
427                                         return;
428                                 }
429                         }
430
431                         //Check if token is positive temp and negative dew
432                         for (i=0; i<2; i++) {
433                                 if (!isdigit(token[i])) break;
434                         }
435                         if ((i==2) && (token[2] == '/')  && (token[3] == 'M')) {
436                                 for (i=4; i<6; i++) {
437                                         if (!isdigit(token[i])) break;
438                                 }
439                                 if (i==6) {
440                                         //1st and 2nd digits gives the temperature
441                                         res->temp = atoi(token);
442
443                                         //5th and 6th digits gives the dew point temperature
444                                         res->dew = -atoi(&token[4]);
445
446                                         //Compute humidity
447                                         res->hmid = rel_humidity(res->dew, res->temp);
448
449                                         return;
450                                 }
451                         }
452
453                         break;
454
455                         //Check all tokens 7 chars long
456                 case 7:
457
458                         //Check if token is the observation time
459                         for (i=0; i<6; i++) {
460                                 if (!isdigit(token[i])) break;
461                         }
462                         if ((i==6) && (token[6] == 'Z')) return;
463
464                         //Check if token is the wind speed/direction in knots
465                         for (i=0; i<5; i++) {
466                                 if (!isdigit(token[i])) break;
467                         }
468                         if ((i==5) && (token[5] == 'K') &&  (token[6] == 'T')) {
469
470                                 //First 3 digits are wind direction
471                                 strncpy(s_tmp, token, 3);
472                                 s_tmp[3]='\0';
473                                 res->wind_d=atoi(s_tmp);
474
475                                 //4th and 5th digit are wind speed in knots (convert to km/hr)
476                                 res->wind_s = (int)(atoi(&token[3])*1.852);
477
478                                 return;
479                         }
480
481                         //Check if token is negative temperature
482                         if ((token[0] == 'M') && (token[4] == 'M')) {
483                                 for (i=1; i<3; i++) {
484                                         if (!isdigit(token[i])) break;
485                                 }
486                                 if ((i==3) && (token[3] == '/')) {
487                                         for (i=5; i<7; i++) {
488                                                 if (!isdigit(token[i])) break;
489                                         }
490                                         if (i==7) {
491                                                 //2nd and 3rd digits gives the temperature
492                                                 res->temp = -atoi(&token[1]);
493
494                                                 //6th and 7th digits gives the dew point temperature
495                                                 res->dew = -atoi(&token[5]);
496
497                                                 //Compute humidity
498                                                 res->hmid = rel_humidity(res->dew, res->temp);
499
500                                                 return;
501                                         }
502                                 }
503                         }
504
505                         //Check if token is wind variability
506                         for (i=0; i<3; i++) {
507                                 if (!isdigit(token[i])) break;
508                         }
509                         if ((i==3) && (token[3] == 'V')) {
510                                 for (i=4; i<7; i++) {
511                                         if (!isdigit(token[i])) break;
512                                 }
513                                 if (i==7) return;
514                         }
515
516                         break;
517
518                         //Check all tokens 8 chars long
519                 case 8:
520
521                         //Check if token is the wind speed/direction in m/s
522                         for (i=0; i<5; i++) {
523                                 if (!isdigit(token[i])) break;
524                         }
525                         if ((i==5)&&(token[5] == 'M')&&(token[6] == 'P')&&(token[7] == 'S')) {
526
527                                 //First 3 digits are wind direction
528                                 strncpy(s_tmp, token, 3);
529                                 s_tmp[3]='\0';
530                                 res->wind_d=atoi(s_tmp);
531
532                                 //4th and 5th digit are wind speed in m/s (convert to km/hr)
533                                 res->wind_s = (int)(atoi(&token[3])*3.6);
534
535                                 return;
536                         }
537
538                 default:
539
540                         //printf("token : %s\n", token);
541                         break;
542         }
543 }
544
545 #ifdef XOAP
546 void parse_weather_forecast(void *result, const char *data)
547 {
548         PWEATHER_FORECAST *res = (PWEATHER_FORECAST*)result;
549         /* Reset results */
550         memset(res, 0, sizeof(PWEATHER_FORECAST));
551
552         //Check if it is an xml file
553         if ( strncmp(data, "<?xml ", 6) == 0 ) {
554                 parse_weather_forecast_xml(res, data);
555         }
556 }
557 #endif /* XOAP */
558
559 void parse_weather(void *result, const char *data)
560 {
561         PWEATHER *res = (PWEATHER*)result;
562         /* Reset results */
563         memset(res, 0, sizeof(PWEATHER));
564
565 #ifdef XOAP
566         //Check if it is an xml file
567         if ( strncmp(data, "<?xml ", 6) == 0 ) {
568                 parse_weather_xml(res, data);
569         } else
570 #endif /* XOAP */
571         {
572                 //We assume its a text file
573                 char s_tmp[256];
574                 const char delim[] = " ";
575
576                 //Divide time stamp and metar data
577                 if (sscanf(data, "%[^'\n']\n%[^'\n']", res->lastupd, s_tmp) == 2) {
578
579                         //Process all tokens
580                         char *p_tok = NULL;
581                         char *p_save = NULL;
582
583                         if ((strtok_r(s_tmp, delim, &p_save)) != NULL) {
584
585                                 //Jump first token, must be icao
586                                 p_tok = strtok_r(NULL, delim, &p_save);
587
588                                 do {
589
590                                         parse_token(res, p_tok);
591                                         p_tok = strtok_r(NULL, delim, &p_save);
592
593                                 } while (p_tok != NULL);
594                         }
595                         return;
596                 }
597                 else {
598                         return;
599                 }
600         }
601 }
602
603 void wind_deg_to_dir(char *p, int p_max_size, int wind_deg) {
604         if ((wind_deg >= 349) || (wind_deg < 12)) {
605                 strncpy(p, "N", p_max_size);
606         } else if (wind_deg < 33) {
607                 strncpy(p, "NNE", p_max_size);
608         } else if (wind_deg < 57) {
609                 strncpy(p, "NE", p_max_size);
610         } else if (wind_deg < 79) {
611                 strncpy(p, "ENE", p_max_size);
612         } else if (wind_deg < 102) {
613                 strncpy(p, "E", p_max_size);
614         } else if (wind_deg < 124) {
615                 strncpy(p, "ESE", p_max_size);
616         } else if (wind_deg < 147) {
617                 strncpy(p, "SE", p_max_size);
618         } else if (wind_deg < 169) {
619                 strncpy(p, "SSE", p_max_size);
620         } else if (wind_deg < 192) {
621                 strncpy(p, "S", p_max_size);
622         } else if (wind_deg < 214) {
623                 strncpy(p, "SSW", p_max_size);
624         } else if (wind_deg < 237) {
625                 strncpy(p, "SW", p_max_size);
626         } else if (wind_deg < 259) {
627                 strncpy(p, "WSW", p_max_size);
628         } else if (wind_deg < 282) {
629                         strncpy(p, "W", p_max_size);
630         } else if (wind_deg < 304) {
631                 strncpy(p, "WNW", p_max_size);
632         } else if (wind_deg < 327) {
633                 strncpy(p, "NW", p_max_size);
634         } else if (wind_deg < 349) {
635                 strncpy(p, "NNW", p_max_size);
636         };
637 }
638
639 #ifdef XOAP
640 void weather_forecast_process_info(char *p, int p_max_size, char *uri, unsigned int day, char *data_type, int interval)
641 {
642         PWEATHER_FORECAST *data;
643
644         ccurl_location_t *curloc = ccurl_find_location(&locations_head_df, uri);
645         if (!curloc->p_timed_thread) {
646                 curloc->result = malloc(sizeof(PWEATHER_FORECAST));
647                 memset(curloc->result, 0, sizeof(PWEATHER_FORECAST));
648                 curloc->process_function = &parse_weather_forecast;
649                 ccurl_init_thread(curloc, interval);
650                 if (!curloc->p_timed_thread) {
651                         NORM_ERR("error setting up weather_forecast thread");
652                 }
653         }
654
655         timed_thread_lock(curloc->p_timed_thread);
656         data = (PWEATHER_FORECAST*)curloc->result;
657         if (strcmp(data_type, "hi") == EQUAL) {
658                 temp_print(p, p_max_size, data->hi[day], TEMP_CELSIUS);
659         } else if (strcmp(data_type, "low") == EQUAL) {
660                 temp_print(p, p_max_size, data->low[day], TEMP_CELSIUS);
661         } else if (strcmp(data_type, "icon") == EQUAL) {
662                 strncpy(p, data->icon[day], p_max_size);
663         } else if (strcmp(data_type, "forecast") == EQUAL) {
664                 strncpy(p, data->xoap_t[day], p_max_size);
665         } else if (strcmp(data_type, "wind_speed") == EQUAL) {
666                 snprintf(p, p_max_size, "%d", data->wind_s[day]);
667         } else if (strcmp(data_type, "wind_dir") == EQUAL) {
668                 wind_deg_to_dir(p, p_max_size, data->wind_d[day]);
669         } else if (strcmp(data_type, "wind_dir_DEG") == EQUAL) {
670                 snprintf(p, p_max_size, "%d", data->wind_d[day]);
671         } else if (strcmp(data_type, "humidity") == EQUAL) {
672                 snprintf(p, p_max_size, "%d", data->hmid[day]);
673         } else if (strcmp(data_type, "precipitation") == EQUAL) {
674                 snprintf(p, p_max_size, "%d", data->ppcp[day]);
675         }
676
677         timed_thread_unlock(curloc->p_timed_thread);
678 }
679 #endif /* XOAP */
680
681 void weather_process_info(char *p, int p_max_size, char *uri, char *data_type, int interval)
682 {
683         static const char *wc[] = {
684                 "", "drizzle", "rain", "hail", "soft hail",
685                 "snow", "snow grains", "fog", "haze", "smoke",
686                 "mist", "dust", "sand", "funnel cloud tornado",
687                 "dust/sand", "squall", "sand storm", "dust storm"
688         };
689         PWEATHER *data;
690
691         ccurl_location_t *curloc = ccurl_find_location(&locations_head_cc, uri);
692         if (!curloc->p_timed_thread) {
693                 curloc->result = malloc(sizeof(PWEATHER));
694                 memset(curloc->result, 0, sizeof(PWEATHER));
695                 curloc->process_function = &parse_weather;
696                 ccurl_init_thread(curloc, interval);
697                 if (!curloc->p_timed_thread) {
698                         NORM_ERR("error setting up weather thread");
699                 }
700         }
701
702         timed_thread_lock(curloc->p_timed_thread);
703         data = (PWEATHER*)curloc->result;
704         if (strcmp(data_type, "last_update") == EQUAL) {
705                 strncpy(p, data->lastupd, p_max_size);
706         } else if (strcmp(data_type, "temperature") == EQUAL) {
707                 temp_print(p, p_max_size, data->temp, TEMP_CELSIUS);
708         } else if (strcmp(data_type, "cloud_cover") == EQUAL) {
709 #ifdef XOAP
710                 if (data->xoap_t[0] != '\0') {
711                         char *s = p;
712                         strncpy(p, data->xoap_t, p_max_size);
713                         while (*s) {
714                                 *s = tolower(*s);
715                                 s++;
716                         }
717                 } else
718 #endif /* XOAP */
719                         if (data->cc == 0) {
720                                 strncpy(p, "", p_max_size);
721                         } else if (data->cc < 3) {
722                                 strncpy(p, "clear", p_max_size);
723                         } else if (data->cc < 5) {
724                                 strncpy(p, "partly cloudy", p_max_size);
725                         } else if (data->cc == 5) {
726                                 strncpy(p, "cloudy", p_max_size);
727                         } else if (data->cc == 6) {
728                                 strncpy(p, "overcast", p_max_size);
729                         } else if (data->cc == 7) {
730                                 strncpy(p, "towering cumulus", p_max_size);
731                         } else  {
732                                 strncpy(p, "cumulonimbus", p_max_size);
733                         }
734 #ifdef XOAP
735         } else if (strcmp(data_type, "icon") == EQUAL) {
736                 strncpy(p, data->icon, p_max_size);
737 #endif /* XOAP */
738         } else if (strcmp(data_type, "pressure") == EQUAL) {
739                 snprintf(p, p_max_size, "%d", data->bar);
740         } else if (strcmp(data_type, "wind_speed") == EQUAL) {
741                 snprintf(p, p_max_size, "%d", data->wind_s);
742         } else if (strcmp(data_type, "wind_dir") == EQUAL) {
743                 wind_deg_to_dir(p, p_max_size, data->wind_d);
744         } else if (strcmp(data_type, "wind_dir_DEG") == EQUAL) {
745                 snprintf(p, p_max_size, "%d", data->wind_d);
746
747         } else if (strcmp(data_type, "humidity") == EQUAL) {
748                 snprintf(p, p_max_size, "%d", data->hmid);
749         } else if (strcmp(data_type, "weather") == EQUAL) {
750                 strncpy(p, wc[data->wc], p_max_size);
751         }
752
753         timed_thread_unlock(curloc->p_timed_thread);
754 }
755
756 #ifdef XOAP
757
758 /* xoap suffix for weather from weather.com */
759 static char *xoap_cc = NULL;
760 static char *xoap_df = NULL;
761
762 /*
763  * TODO: make the xoap keys file readable from the config file
764  *       make the keys directly readable from the config file
765  *       make the xoap keys file giveable as a command line option
766  */
767 void load_xoap_keys(void)
768 {
769         FILE *fp;
770         char *par  = (char *) malloc(11 * sizeof(char));
771         char *key  = (char *) malloc(17 * sizeof(char));
772         char *xoap = (char *) malloc(64 * sizeof(char));
773
774         to_real_path(xoap, XOAP_FILE);
775         fp = fopen(xoap, "r");
776         if (fp != NULL) {
777                 if (fscanf(fp, "%10s %16s", par, key) == 2) {
778                         xoap_cc = (char *) malloc(128 * sizeof(char));
779                         xoap_df = (char *) malloc(128 * sizeof(char));
780
781                         strcpy(xoap_cc, "?cc=*&link=xoap&prod=xoap&par=");
782                         strcat(xoap_cc, par);
783                         strcat(xoap_cc, "&key=");
784                         strcat(xoap_cc, key);
785                         strcat(xoap_cc, "&unit=m");
786
787                         /* TODO: Use FORECAST_DAYS instead of 5 */
788                         strcpy(xoap_df, "?dayf=5&link=xoap&prod=xoap&par=");
789                         strcat(xoap_df, par);
790                         strcat(xoap_df, "&key=");
791                         strcat(xoap_df, key);
792                         strcat(xoap_df, "&unit=m");
793                 }
794                 fclose(fp);
795         }
796         free(par);
797         free(key);
798         free(xoap);
799 }
800 #endif /* XOAP */
801
802 int process_weather_uri(char *uri, char *locID, int dayf UNUSED_ATTR)
803 {
804         /* locID MUST BE upper-case */
805         char *tmp_p = locID;
806
807         while (*tmp_p) {
808                 *tmp_p = toupper(*tmp_p);
809                 tmp_p++;
810         }
811
812         /* Construct complete uri */
813 #ifdef XOAP
814         if (strstr(uri, "xoap.weather.com")) {
815                 if ((dayf == 0) && (xoap_cc != NULL)) {
816                         strcat(uri, locID);
817                         strcat(uri, xoap_cc);
818                 } else if ((dayf == 1) && (xoap_df != NULL)) {
819                         strcat(uri, locID);
820                         strcat(uri, xoap_df);
821                 } else {
822                         free(uri);
823                         uri = NULL;
824                 }
825         } else
826 #endif /* XOAP */
827         if (strstr(uri, "weather.noaa.gov")) {
828                 strcat(uri, locID);
829                 strcat(uri, ".TXT");
830         } else  if (!strstr(uri, "localhost") && !strstr(uri, "127.0.0.1")) {
831                 return -1;
832         }
833         return 0;
834 }