Move vi modelines closer to the beginning, so they're more likely to be actually...
[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-2009 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
30 #ifdef DEBUG
31 #include <assert.h>
32 #endif /* DEBUG */
33
34 #include <curl/curl.h>
35 #include <curl/types.h>
36 #include <curl/easy.h>
37
38 /*
39  * The following code is the conky curl thread lib, which can be re-used to
40  * create any curl-based object (see weather and rss).  Below is an
41  * implementation of a curl-only object ($curl) which can also be used as an
42  * example.
43  */
44 typedef struct _ccurl_memory_t {
45         char *memory;
46         size_t size;
47 } ccurl_memory_t;
48
49 /* finds a location based on uri in the list provided */
50 ccurl_location_t *ccurl_find_location(ccurl_location_t **locations_head, char *uri)
51 {
52         ccurl_location_t *tail = *locations_head;
53         ccurl_location_t *new = 0;
54         while (tail) {
55                 if (tail->uri &&
56                                 strcmp(tail->uri, uri) == EQUAL) {
57                         return tail;
58                 }
59                 tail = tail->next;
60         }
61         if (!tail) { /* new location!!!!!!! */
62                 DBGP("new curl location: '%s'", uri);
63                 new = malloc(sizeof(ccurl_location_t));
64                 memset(new, 0, sizeof(ccurl_location_t));
65                 new->uri = strndup(uri, text_buffer_size);
66                 tail = *locations_head;
67                 while (tail && tail->next) {
68                         tail = tail->next;
69                 }
70                 if (!tail) {
71                         /* omg the first one!!!!!!! */
72                         *locations_head = new;
73                 } else {
74                         tail->next = new;
75                 }
76         }
77         return new;
78 }
79
80 /* iterates over the list provided, frees stuff (list item, uri, result) */
81 void ccurl_free_locations(ccurl_location_t **locations_head)
82 {
83         ccurl_location_t *tail = *locations_head;
84         ccurl_location_t *last = 0;
85
86         while (tail) {
87                 if (tail->uri) free(tail->uri);
88                 if (tail->result) free(tail->result);
89                 last = tail;
90                 tail = tail->next;
91                 free(last);
92         }
93         *locations_head = 0;
94 }
95
96 /* callback used by curl for writing the received data */
97 size_t ccurl_write_memory_callback(void *ptr, size_t size, size_t nmemb, void *data)
98 {
99         size_t realsize = size * nmemb;
100         ccurl_memory_t *mem = (ccurl_memory_t*)data;
101
102         mem->memory = (char *) realloc(mem->memory, mem->size + realsize + 1);
103         if (mem->memory) {
104                 memcpy(&(mem->memory[mem->size]), ptr, realsize);
105                 mem->size += realsize;
106                 mem->memory[mem->size] = 0;
107         }
108         return realsize;
109 }
110
111
112 /* fetch our datums */
113 void ccurl_fetch_data(ccurl_location_t *curloc)
114 {
115         CURL *curl = NULL;
116         CURLcode res;
117
118         // curl temps
119         ccurl_memory_t chunk;
120
121         chunk.memory = NULL;
122         chunk.size = 0;
123
124         curl = curl_easy_init();
125         if (curl) {
126                 DBGP("reading curl data from '%s'", curloc->uri);
127                 curl_easy_setopt(curl, CURLOPT_URL, curloc->uri);
128                 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
129                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ccurl_write_memory_callback);
130                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &chunk);
131                 curl_easy_setopt(curl, CURLOPT_USERAGENT, "conky-curl/1.0");
132
133                 res = curl_easy_perform(curl);
134                 if (res == CURLE_OK && chunk.size) {
135                         timed_thread_lock(curloc->p_timed_thread);
136                         (*curloc->process_function)(curloc->result, chunk.memory);
137                         timed_thread_unlock(curloc->p_timed_thread);
138                         free(chunk.memory);
139                 } else {
140                         NORM_ERR("curl: no data from server");
141                 }
142
143                 curl_easy_cleanup(curl);
144         }
145 }
146
147 void *ccurl_thread(void *) __attribute__((noreturn));
148
149 void ccurl_init_thread(ccurl_location_t *curloc, int interval)
150 {
151 #ifdef DEBUG
152         assert(curloc->result);
153 #endif /* DEBUG */
154         curloc->p_timed_thread =
155                 timed_thread_create(&ccurl_thread,
156                                 (void *)curloc, interval * 1000000);
157
158         if (!curloc->p_timed_thread) {
159                 NORM_ERR("curl thread: error creating timed thread");
160         }
161         timed_thread_register(curloc->p_timed_thread,
162                         &curloc->p_timed_thread);
163         if (timed_thread_run(curloc->p_timed_thread)) {
164                 NORM_ERR("curl thread: error running timed thread");
165         }
166 }
167
168 void *ccurl_thread(void *arg)
169 {
170         ccurl_location_t *curloc = (ccurl_location_t*)arg;
171
172         while (1) {
173                 ccurl_fetch_data(curloc);
174                 if (timed_thread_test(curloc->p_timed_thread, 0)) {
175                         timed_thread_exit(curloc->p_timed_thread);
176                 }
177         }
178         /* never reached */
179 }
180
181
182 /*
183  * This is where the $curl section begins.
184  */
185
186 /* internal location pointer for use by $curl, no touchy */
187 static ccurl_location_t *ccurl_locations_head = 0;
188
189 /* used to free data used by $curl */
190 void ccurl_free_info(void)
191 {
192         ccurl_free_locations(&ccurl_locations_head);
193 }
194
195 /* straight copy, used by $curl */
196 void ccurl_parse_data(void *result, const char *data)
197 {
198         strncpy(result, data, max_user_text);
199 }
200
201 /* prints result data to text buffer, used by $curl */
202 void ccurl_process_info(char *p, int p_max_size, char *uri, int interval)
203 {
204         ccurl_location_t *curloc = ccurl_find_location(&ccurl_locations_head, uri);
205         if (!curloc->p_timed_thread) {
206                 curloc->result = malloc(max_user_text);
207                 memset(curloc->result, 0, max_user_text);
208                 curloc->process_function = &ccurl_parse_data;
209                 ccurl_init_thread(curloc, interval);
210                 if (!curloc->p_timed_thread) {
211                         NORM_ERR("error setting up curl thread");
212                 }
213         }
214
215         timed_thread_lock(curloc->p_timed_thread);
216         strncpy(p, curloc->result, p_max_size);
217         timed_thread_unlock(curloc->p_timed_thread);
218 }
219