Fix warning.
[monky] / src / ccurl_thread.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Please see COPYING for details
7  *
8  * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
9  *      (see AUTHORS)
10  * All rights reserved.
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  *
24  */
25
26 #include "conky.h"
27 #include "logging.h"
28 #include "ccurl_thread.h"
29 #include "text_object.h"
30
31 #ifdef DEBUG
32 #include <assert.h>
33 #endif /* DEBUG */
34
35 #include <curl/curl.h>
36 #include <curl/types.h>
37 #include <curl/easy.h>
38
39 /*
40  * The following code is the conky curl thread lib, which can be re-used to
41  * create any curl-based object (see weather and rss).  Below is an
42  * implementation of a curl-only object ($curl) which can also be used as an
43  * example.
44  */
45 typedef struct _ccurl_memory_t {
46         char *memory;
47         size_t size;
48 } ccurl_memory_t;
49
50 /* finds a location based on uri in the list provided */
51 ccurl_location_t *ccurl_find_location(ccurl_location_t **locations_head, char *uri)
52 {
53         ccurl_location_t *tail = *locations_head;
54         ccurl_location_t *new = 0;
55         while (tail) {
56                 if (tail->uri &&
57                                 strcmp(tail->uri, uri) == EQUAL) {
58                         return tail;
59                 }
60                 tail = tail->next;
61         }
62         if (!tail) { /* new location!!!!!!! */
63                 DBGP("new curl location: '%s'", uri);
64                 new = malloc(sizeof(ccurl_location_t));
65                 memset(new, 0, sizeof(ccurl_location_t));
66                 new->uri = strndup(uri, text_buffer_size);
67                 tail = *locations_head;
68                 while (tail && tail->next) {
69                         tail = tail->next;
70                 }
71                 if (!tail) {
72                         /* omg the first one!!!!!!! */
73                         *locations_head = new;
74                 } else {
75                         tail->next = new;
76                 }
77         }
78         return new;
79 }
80
81 /* iterates over the list provided, frees stuff (list item, uri, result) */
82 void ccurl_free_locations(ccurl_location_t **locations_head)
83 {
84         ccurl_location_t *tail = *locations_head;
85         ccurl_location_t *last = 0;
86
87         while (tail) {
88                 if (tail->uri) free(tail->uri);
89                 if (tail->result) free(tail->result);
90                 last = tail;
91                 tail = tail->next;
92                 free(last);
93         }
94         *locations_head = 0;
95 }
96
97 /* callback used by curl for writing the received data */
98 size_t ccurl_write_memory_callback(void *ptr, size_t size, size_t nmemb, void *data)
99 {
100         size_t realsize = size * nmemb;
101         ccurl_memory_t *mem = (ccurl_memory_t*)data;
102
103         mem->memory = (char *) realloc(mem->memory, mem->size + realsize + 1);
104         if (mem->memory) {
105                 memcpy(&(mem->memory[mem->size]), ptr, realsize);
106                 mem->size += realsize;
107                 mem->memory[mem->size] = 0;
108         }
109         return realsize;
110 }
111
112
113 /* fetch our datums */
114 void ccurl_fetch_data(ccurl_location_t *curloc)
115 {
116         CURL *curl = NULL;
117         CURLcode res;
118
119         // curl temps
120         ccurl_memory_t chunk;
121
122         chunk.memory = NULL;
123         chunk.size = 0;
124
125         curl = curl_easy_init();
126         if (curl) {
127                 DBGP("reading curl data from '%s'", curloc->uri);
128                 curl_easy_setopt(curl, CURLOPT_URL, curloc->uri);
129                 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
130                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ccurl_write_memory_callback);
131                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &chunk);
132                 curl_easy_setopt(curl, CURLOPT_USERAGENT, "conky-curl/1.0");
133                 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
134                 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1000);
135                 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 60);
136
137                 res = curl_easy_perform(curl);
138                 if (res == CURLE_OK && chunk.size) {
139                         long http_status_code;
140
141                         if (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE,
142                                                 &http_status_code) == CURLE_OK) {
143                                 switch (http_status_code) {
144                                         case 200:
145                                                 timed_thread_lock(curloc->p_timed_thread);
146                                                 (*curloc->process_function)(curloc->result, chunk.memory);
147                                                 timed_thread_unlock(curloc->p_timed_thread);
148                                                 break;
149                                         case 304:
150                                                 break;
151                                         default:
152                                                 NORM_ERR("curl: no data from server, got HTTP status %ld",
153                                                                 http_status_code);
154                                                 break;
155                                 }
156                         } else {
157                                 NORM_ERR("curl: no HTTP status from server");
158                         }
159                         free(chunk.memory);
160                 } else {
161                         NORM_ERR("curl: no data from server");
162                 }
163
164                 curl_easy_cleanup(curl);
165         }
166 }
167
168 void *ccurl_thread(void *) __attribute__((noreturn));
169
170 void ccurl_init_thread(ccurl_location_t *curloc, int interval)
171 {
172 #ifdef DEBUG
173         assert(curloc->result);
174 #endif /* DEBUG */
175         curloc->p_timed_thread =
176                 timed_thread_create(&ccurl_thread,
177                                 (void *)curloc, interval * 1000000);
178
179         if (!curloc->p_timed_thread) {
180                 NORM_ERR("curl thread: error creating timed thread");
181         }
182         timed_thread_register(curloc->p_timed_thread,
183                         &curloc->p_timed_thread);
184         if (timed_thread_run(curloc->p_timed_thread)) {
185                 NORM_ERR("curl thread: error running timed thread");
186         }
187 }
188
189 void *ccurl_thread(void *arg)
190 {
191         ccurl_location_t *curloc = (ccurl_location_t*)arg;
192
193         while (1) {
194                 ccurl_fetch_data(curloc);
195                 if (timed_thread_test(curloc->p_timed_thread, 0)) {
196                         timed_thread_exit(curloc->p_timed_thread);
197                 }
198         }
199         /* never reached */
200 }
201
202
203 /*
204  * This is where the $curl section begins.
205  */
206
207 struct curl_data {
208         char uri[128];
209         float interval;
210 };
211
212 /* internal location pointer for use by $curl, no touchy */
213 static ccurl_location_t *ccurl_locations_head = 0;
214
215 /* used to free data used by $curl */
216 void ccurl_free_info(void)
217 {
218         ccurl_free_locations(&ccurl_locations_head);
219 }
220
221 /* straight copy, used by $curl */
222 static void ccurl_parse_data(void *result, const char *data)
223 {
224         strncpy(result, data, max_user_text);
225 }
226
227 /* prints result data to text buffer, used by $curl */
228 void ccurl_process_info(char *p, int p_max_size, char *uri, int interval)
229 {
230         ccurl_location_t *curloc = ccurl_find_location(&ccurl_locations_head, uri);
231         if (!curloc->p_timed_thread) {
232                 curloc->result = malloc(max_user_text);
233                 memset(curloc->result, 0, max_user_text);
234                 curloc->process_function = &ccurl_parse_data;
235                 ccurl_init_thread(curloc, interval);
236                 if (!curloc->p_timed_thread) {
237                         NORM_ERR("error setting up curl thread");
238                 }
239         }
240
241         timed_thread_lock(curloc->p_timed_thread);
242         strncpy(p, curloc->result, p_max_size);
243         timed_thread_unlock(curloc->p_timed_thread);
244 }
245
246 void curl_parse_arg(struct text_object *obj, const char *arg)
247 {
248         int argc;
249         struct curl_data *cd;
250         float interval = 0;
251
252         cd = malloc(sizeof(struct curl_data));
253         memset(cd, 0, sizeof(struct curl_data));
254
255         argc = sscanf(arg, "%127s %f", cd->uri, &interval);
256         if (argc < 1) {
257                 free(cd);
258                 NORM_ERR("wrong number of arguments for $curl");
259                 return;
260         }
261         cd->interval = interval > 0 ? interval * 60 : 15*60;
262         obj->data.opaque = cd;
263 }
264
265 void curl_print(struct text_object *obj, char *p, int p_max_size)
266 {
267         struct curl_data *cd = obj->data.opaque;
268
269         if (!cd || !cd->uri) {
270                 NORM_ERR("error processing Curl data");
271                 return;
272         }
273         ccurl_process_info(p, p_max_size, cd->uri, cd->interval);
274 }
275
276 void curl_obj_free(struct text_object *obj)
277 {
278         if (obj->data.opaque) {
279                 free(obj->data.opaque);
280                 obj->data.opaque = NULL;
281         }
282 }