Bugfix: RSS interval was in secs instead of mins
[monky] / src / rss.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) 2007 Toni Spets
9  * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
10  *      (see AUTHORS)
11  * All rights reserved.
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26
27 #include "conky.h"
28 #include "logging.h"
29 #include "prss.h"
30 #include "text_object.h"
31 #include "ccurl_thread.h"
32 #include <time.h>
33 #include <assert.h>
34
35 struct rss_data {
36         char uri[128];
37         char action[64];
38         int act_par;
39         float interval;
40         unsigned int nrspaces;
41 };
42
43 static ccurl_location_t *locations_head = 0;
44
45 void rss_free_info(void)
46 {
47         ccurl_location_t *tail = locations_head;
48
49         while (tail) {
50                 if (tail->result) prss_free((PRSS*)tail->result);       /* clean up old data */
51                 tail = tail->next;
52         }
53         ccurl_free_locations(&locations_head);
54 }
55
56 static void rss_process_info(char *p, int p_max_size, char *uri, char *action, int
57                 act_par, int interval, unsigned int nrspaces)
58 {
59         PRSS *data;
60         char *str;
61
62         ccurl_location_t *curloc = ccurl_find_location(&locations_head, uri);
63
64         assert(act_par >= 0 && action);
65
66         if (!curloc->p_timed_thread) {
67                 curloc->result = malloc(sizeof(PRSS));
68                 memset(curloc->result, 0, sizeof(PRSS));
69                 curloc->process_function = &prss_parse_data;
70                 ccurl_init_thread(curloc, interval * 60);
71                 if (!curloc->p_timed_thread) {
72                         NORM_ERR("error setting up RSS thread");
73                 }
74         }
75
76         timed_thread_lock(curloc->p_timed_thread);
77         data = (PRSS*)curloc->result;
78
79         if (data == NULL || data->item_count < 1) {
80                 *p = 0;
81         } else {
82                 /*
83                  * XXX: Refactor this so that we can retrieve any of the fields in the
84                  * PRSS struct (in prss.h).
85                  */
86                 if (strcmp(action, "feed_title") == EQUAL) {
87                         str = data->title;
88                         if (str && strlen(str) > 0) {
89                                 // remove trailing new line if one exists
90                                 if (str[strlen(str) - 1] == '\n') {
91                                         str[strlen(str) - 1] = 0;
92                                 }
93                                 snprintf(p, p_max_size, "%s", str);
94                         }
95                 } else if (strcmp(action, "item_title") == EQUAL) {
96                         if (act_par < data->item_count) {
97                                 str = data->items[act_par].title;
98                                 // remove trailing new line if one exists
99                                 if (str && strlen(str) > 0) {
100                                         if (str[strlen(str) - 1] == '\n') {
101                                                 str[strlen(str) - 1] = 0;
102                                         }
103                                         snprintf(p, p_max_size, "%s", str);
104                                 }
105                         }
106                 } else if (strcmp(action, "item_desc") == EQUAL) {
107                         if (act_par < data->item_count) {
108                                 str =
109                                         data->items[act_par].description;
110                                 // remove trailing new line if one exists
111                                 if (str && strlen(str) > 0) {
112                                         if (str[strlen(str) - 1] == '\n') {
113                                                 str[strlen(str) - 1] = 0;
114                                         }
115                                         snprintf(p, p_max_size, "%s", str);
116                                 }
117                         }
118                 } else if (strcmp(action, "item_titles") == EQUAL) {
119                         if (data->item_count > 0) {
120                                 int itmp;
121                                 int show;
122                                 //'tmpspaces' is a string with spaces too be placed in front of each title
123                                 char *tmpspaces = malloc(nrspaces + 1);
124                                 memset(tmpspaces, ' ', nrspaces);
125                                 tmpspaces[nrspaces]=0;
126
127                                 if (act_par > data->item_count) {
128                                         show = data->item_count;
129                                 } else {
130                                         show = act_par;
131                                 }
132                                 for (itmp = 0; itmp < show; itmp++) {
133                                         PRSS_Item *item = &data->items[itmp];
134
135                                         str = item->title;
136                                         if (str) {
137                                                 // don't add new line before first item
138                                                 if (itmp > 0) {
139                                                         strncat(p, "\n", p_max_size);
140                                                 }
141                                                 /* remove trailing new line if one exists,
142                                                  * we have our own */
143                                                 if (strlen(str) > 0 && str[strlen(str) - 1] == '\n') {
144                                                         str[strlen(str) - 1] = 0;
145                                                 }
146                                                 strncat(p, tmpspaces, p_max_size);
147                                                 strncat(p, str, p_max_size);
148                                         }
149                                 }
150                                 free(tmpspaces);
151                         }
152                 } else {
153                         NORM_ERR("rss: Invalid action '%s'", action);
154                 }
155         }
156         timed_thread_unlock(curloc->p_timed_thread);
157 }
158
159 void rss_scan_arg(struct text_object *obj, const char *arg)
160 {
161         int argc;
162         struct rss_data *rd;
163
164         rd = malloc(sizeof(struct rss_data));
165         memset(rd, 0, sizeof(struct rss_data));
166
167         argc = sscanf(arg, "%127s %f %63s %d %u", rd->uri, &rd->interval, rd->action,
168                         &rd->act_par, &rd->nrspaces);
169         if (argc < 3) {
170                 NORM_ERR("wrong number of arguments for $rss");
171                 free(rd);
172                 return;
173         }
174         obj->data.opaque = rd;
175 }
176
177 void rss_print_info(struct text_object *obj, char *p, int p_max_size)
178 {
179         struct rss_data *rd = obj->data.opaque;
180
181         if (!rd) {
182                 NORM_ERR("error processing RSS data");
183                 return;
184         }
185         rss_process_info(p, p_max_size, rd->uri, rd->action,
186                         rd->act_par, rd->interval, rd->nrspaces);
187 }
188
189 void rss_free_obj_info(struct text_object *obj)
190 {
191         if (obj->data.opaque) {
192                 free(obj->data.opaque);
193                 obj->data.opaque = NULL;
194         }
195 }