RSS was not really disabled when it was supposed to be. Experimental wireless support.
[monky] / src / rss.c
1 /*
2  * rss.c
3  * RSS stuff (prss version)
4  *
5  * prss.c and prss.h written by Sisu (Mikko Sysikaski)
6  * new rss.c written by hifi (Toni Spets)
7  */
8
9 #ifdef RSS
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <time.h>
14 #include <assert.h>
15 #include "prss.h"
16 #include <curl/curl.h>
17 #include <curl/types.h>
18 #include <curl/easy.h>
19
20 #include "conky.h"
21
22 #define MAX_FEEDS 16
23
24 struct MemoryStruct {
25         char *memory;
26         size_t size;
27 };
28
29 typedef struct feed_ {
30         char* uri;
31         int last_update;
32         PRSS* data;
33 } feed;
34
35 int num_feeds = 0;
36 feed feeds[MAX_FEEDS];
37
38 size_t
39 WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
40 {
41         size_t realsize = size * nmemb;
42         struct MemoryStruct *mem = (struct MemoryStruct *)data;
43
44         mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
45         if (mem->memory) {
46                 memcpy(&(mem->memory[mem->size]), ptr, realsize);
47                 mem->size += realsize;
48                 mem->memory[mem->size] = 0;
49         }
50         return realsize;
51 }
52
53
54 int rss_delay(int *wait, int delay)
55 {
56         time_t now = time(NULL);
57
58         // make it minutes
59         if(delay < 1) delay = 1;
60         delay *= 60;
61
62         if(!*wait) {
63                 *wait = now + delay;
64                 return 1;
65         }
66
67         if(now >= *wait + delay) {
68                 *wait = now + delay;
69                 return 1;
70         }
71
72         return 0;
73 }
74
75 void init_rss_info()
76 {
77         int i;
78         for(i = 0; i < MAX_FEEDS; i++) {
79                 feeds[i].uri = NULL;
80                 feeds[i].data = NULL;
81                 feeds[i].last_update = 0;
82         }
83 }
84
85 void free_rss_info()
86 {
87         int i;
88         for(i = 0; i < num_feeds; i++)
89                 if(feeds[i].uri != NULL)
90                         free(feeds[i].uri);
91 }
92
93 PRSS*
94 get_rss_info(char *uri, int delay)
95 {
96         CURL *curl = NULL;
97         CURLcode res;
98         // curl temps
99         struct MemoryStruct chunk;
100         chunk.memory = NULL;
101         chunk.size = 0;
102
103         // pointers to struct
104         feed *curfeed = NULL;
105         PRSS *curdata = NULL;
106         int *last_update = 0;
107
108         int i;
109
110         // first seek for the uri in list
111         for(i = 0; i < num_feeds; i++) {
112                 if(feeds[i].uri != NULL)
113                         if(!strcmp(feeds[i].uri, uri)) {
114                                 curfeed = &feeds[i];
115                                 break;
116                         }
117         }
118
119         if(!curfeed) { // new feed
120                 if(num_feeds == MAX_FEEDS-1) return NULL;
121                 curfeed = &feeds[num_feeds];
122                 curfeed->uri = strdup(uri);
123                 num_feeds++;
124         }
125
126         last_update = &curfeed->last_update;
127         curdata = curfeed->data;
128
129         if(!rss_delay(last_update, delay))
130                 return curdata; // wait for delay to pass
131
132         if(curdata != NULL) {
133                 prss_free(curdata); // clean up old data
134                 curdata = NULL;
135         }
136
137         curl = curl_easy_init();
138         if(curl) {
139                 curl_easy_setopt(curl, CURLOPT_URL, uri);
140                 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
141                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
142                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
143                 curl_easy_setopt(curl, CURLOPT_USERAGENT, "conky-rss/1.0");
144
145                 res = curl_easy_perform(curl);
146                 if(chunk.size) {
147                         curdata = prss_parse_data(chunk.memory);
148                         free(chunk.memory);
149                 } else
150                         ERR("No data from server");
151
152                 curl_easy_cleanup(curl);
153         }
154
155         curfeed->data = curdata;
156
157         return curdata;
158 }
159
160 #endif