2 * Bring external RSS feeds into rooms.
4 * Copyright (c) 2007-2017 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12 * GNU General Public License for more details.
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
33 #include <sys/types.h>
36 #include <curl/curl.h>
37 #include <libcitadel.h>
40 #include "citserver.h"
44 #include "ctdl_module.h"
46 #include "parsedate.h"
48 #include "citadel_dirs.h"
51 #include "rss_atom_parser.h"
61 struct rssroom *rooms;
66 struct CitContext rss_CC;
67 struct rssurl *rsstodo = NULL;
70 // Add a feed/room pair into the todo list
72 void rssclient_push_todo(char *rssurl, char *roomname)
74 struct rssurl *r = NULL;
75 struct rssurl *thisone = NULL;
76 struct rssroom *newroom = NULL;
78 syslog(LOG_DEBUG, "rssclient_push_todo(%s, %s)", rssurl, roomname);
80 for (r=rsstodo; r!=NULL; r=r->next) {
81 if (!strcasecmp(r->url, rssurl)) {
85 if (thisone == NULL) {
86 thisone = malloc(sizeof(struct rssurl));
87 thisone->url = strdup(rssurl);
88 thisone->rooms = NULL;
89 thisone->next = rsstodo;
93 newroom = malloc(sizeof(struct rssroom));
94 newroom->room = strdup(roomname);
95 newroom->next = thisone->rooms;
96 thisone->rooms = newroom;
100 // Callback function for curl
102 size_t rss_pof_write_data(void *buffer, size_t size, size_t nmemb, void *userp)
104 StrBuf *Downloaded = (StrBuf *)userp;
105 size_t bytes = size * nmemb;
106 StrBufAppendBufPlain(Downloaded, buffer, bytes, 0);
111 // pull one feed (possibly multiple rooms)
113 void rss_pull_one_feed(struct rssurl *url)
118 StrBuf *Downloaded = NULL;
120 syslog(LOG_DEBUG, "rss_pull_one_feed(%s)", url->url);
122 curl = curl_easy_init();
127 Downloaded = NewStrBuf();
129 curl_easy_setopt(curl, CURLOPT_URL, url->url);
130 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Follow redirects
131 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rss_pof_write_data); // What to do with downloaded data
132 curl_easy_setopt(curl, CURLOPT_WRITEDATA, Downloaded); // Give it our StrBuf to work with
133 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L); // Time out after 20 seconds
134 res = curl_easy_perform(curl); // Perform the request
135 if (res != CURLE_OK) {
136 syslog(LOG_WARNING, "Failed to load feed: %s", curl_easy_strerror(res));
138 curl_easy_cleanup(curl);
140 // FIXME parse the feed, dummeh ... it's in ChrPtr(Downloaded)
142 for (r=url->rooms; r!=NULL; r=r->next) {
143 syslog(LOG_DEBUG, "Saving item to %s", r->room);
144 // FIXME save to rooms
147 FreeStrBuf(&Downloaded);
151 // We have a list, now download the feeds
153 void rss_pull_feeds(void)
158 while (rsstodo != NULL) {
159 rss_pull_one_feed(rsstodo);
161 rsstodo = rsstodo->next;
162 while (r->rooms != NULL) {
164 r->rooms = r->rooms->next;
174 // Scan a room's netconfig looking for RSS feed parsing requests
176 void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
178 char *serialized_config = NULL;
183 serialized_config = LoadRoomNetConfigFile(qrbuf->QRnumber);
184 if (!serialized_config) {
188 num_configs = num_tokens(serialized_config, '\n');
189 for (i=0; i<num_configs; ++i) {
190 extract_token(cfgline, serialized_config, i, '\n', sizeof cfgline);
191 if (!strncasecmp(cfgline, HKEY("rssclient|"))) {
192 strcpy(cfgline, &cfgline[10]);
193 char *vbar = strchr(cfgline, '|');
197 rssclient_push_todo(cfgline, qrbuf->QRname);
201 free(serialized_config);
206 * Scan for rooms that have RSS client requests configured
208 void rssclient_scan(void) {
209 time_t now = time(NULL);
211 /* Run no more than once every 15 minutes. */
212 if ((now - last_run) < 900) {
214 "Client: polling interval not yet reached; last run was %ldm%lds ago",
215 ((now - last_run) / 60),
216 ((now - last_run) % 60)
221 become_session(&rss_CC);
222 syslog(LOG_DEBUG, "rssclient started");
223 CtdlForEachRoom(rssclient_scan_room, NULL);
225 syslog(LOG_DEBUG, "rssclient ended");
226 last_run = time(NULL);
231 CTDL_MODULE_INIT(rssclient)
235 syslog(LOG_INFO, "%s", curl_version());
236 CtdlRegisterSessionHook(rssclient_scan, EVT_TIMER, PRIO_AGGR + 300);
240 CtdlFillSystemContext(&rss_CC, "rssclient");