Fix broken RSS code.
[monky] / src / rss.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) 2007 Toni Spets
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  * vim: ts=4 sw=4 noet ai cindent syntax=c
25  *
26  */
27
28 #include "conky.h"
29 #include "logging.h"
30 #include "prss.h"
31 #include "ccurl_thread.h"
32 #include <time.h>
33 #include <assert.h>
34
35 static ccurl_location_t *locations_head = 0;
36
37 void rss_free_info(void)
38 {
39         ccurl_location_t *tail = locations_head;
40
41         while (tail) {
42                 if (tail->result) prss_free((PRSS*)tail->result);       /* clean up old data */
43                 tail = tail->next;
44         }
45         ccurl_free_locations(&locations_head);
46 }
47
48 void rss_process_info(char *p, int p_max_size, char *uri, char *action, int
49                 act_par, int interval, unsigned int nrspaces)
50 {
51         PRSS *data;
52         char *str;
53
54         ccurl_location_t *curloc = ccurl_find_location(&locations_head, uri);
55
56         assert(act_par >= 0 && action);
57
58         if (!curloc->p_timed_thread) {
59                 curloc->result = malloc(sizeof(PRSS));
60                 memset(curloc->result, 0, sizeof(PRSS));
61                 curloc->process_function = &prss_parse_data;
62                 ccurl_init_thread(curloc, interval);
63                 if (!curloc->p_timed_thread) {
64                         NORM_ERR("error setting up RSS thread");
65                 }
66         }
67
68         timed_thread_lock(curloc->p_timed_thread);
69         data = (PRSS*)curloc->result;
70
71         if (data == NULL || data->item_count < 1) {
72                 *p = 0;
73         } else {
74                 /*
75                  * XXX: Refactor this so that we can retrieve any of the fields in the
76                  * PRSS struct (in prss.h).
77                  */
78                 if (strcmp(action, "feed_title") == EQUAL) {
79                         str = data->title;
80                         if (str && strlen(str) > 0) {
81                                 // remove trailing new line if one exists
82                                 if (str[strlen(str) - 1] == '\n') {
83                                         str[strlen(str) - 1] = 0;
84                                 }
85                                 snprintf(p, p_max_size, "%s", str);
86                         }
87                 } else if (strcmp(action, "item_title") == EQUAL) {
88                         if (act_par < data->item_count) {
89                                 str = data->items[act_par].title;
90                                 // remove trailing new line if one exists
91                                 if (str && strlen(str) > 0) {
92                                         if (str[strlen(str) - 1] == '\n') {
93                                                 str[strlen(str) - 1] = 0;
94                                         }
95                                         snprintf(p, p_max_size, "%s", str);
96                                 }
97                         }
98                 } else if (strcmp(action, "item_desc") == EQUAL) {
99                         if (act_par < data->item_count) {
100                                 str =
101                                         data->items[act_par].description;
102                                 // remove trailing new line if one exists
103                                 if (str && strlen(str) > 0) {
104                                         if (str[strlen(str) - 1] == '\n') {
105                                                 str[strlen(str) - 1] = 0;
106                                         }
107                                         snprintf(p, p_max_size, "%s", str);
108                                 }
109                         }
110                 } else if (strcmp(action, "item_titles") == EQUAL) {
111                         if (data->item_count > 0) {
112                                 int itmp;
113                                 int show;
114                                 //'tmpspaces' is a string with spaces too be placed in front of each title
115                                 char *tmpspaces = malloc(nrspaces + 1);
116                                 memset(tmpspaces, ' ', nrspaces);
117                                 tmpspaces[nrspaces]=0;
118
119                                 if (act_par > data->item_count) {
120                                         show = data->item_count;
121                                 } else {
122                                         show = act_par;
123                                 }
124                                 for (itmp = 0; itmp < show; itmp++) {
125                                         PRSS_Item *item = &data->items[itmp];
126
127                                         str = item->title;
128                                         if (str) {
129                                                 // don't add new line before first item
130                                                 if (itmp > 0) {
131                                                         strncat(p, "\n", p_max_size);
132                                                 }
133                                                 /* remove trailing new line if one exists,
134                                                  * we have our own */
135                                                 if (strlen(str) > 0 && str[strlen(str) - 1] == '\n') {
136                                                         str[strlen(str) - 1] = 0;
137                                                 }
138                                                 strncat(p, tmpspaces, p_max_size);
139                                                 strncat(p, str, p_max_size);
140                                         }
141                                 }
142                                 free(tmpspaces);
143                         }
144                 } else {
145                         NORM_ERR("rss: Invalid action '%s'", action);
146                 }
147         }
148         timed_thread_unlock(curloc->p_timed_thread);
149 }
150