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"
60 struct rssroom *rooms;
64 struct CitContext rss_CC;
65 struct rssurl *rsstodo = NULL;
68 // Add a feed/room pair into the todo list
70 void rssclient_push_todo(char *rssurl, char *roomname)
72 struct rssurl *r = NULL;
73 struct rssurl *thisone = NULL;
74 struct rssroom *newroom = NULL;
76 syslog(LOG_DEBUG, "rssclient_push_todo(%s, %s)", rssurl, roomname);
78 for (r=rsstodo; r!=NULL; r=r->next) {
79 if (!strcasecmp(r->url, rssurl)) {
83 if (thisone == NULL) {
84 thisone = malloc(sizeof(struct rssurl));
85 thisone->url = strdup(rssurl);
86 thisone->rooms = NULL;
87 thisone->next = rsstodo;
91 newroom = malloc(sizeof(struct rssroom));
92 newroom->room = strdup(roomname);
93 newroom->next = thisone->rooms;
94 thisone->rooms = newroom;
98 // Callback function for curl
100 size_t rss_pof_write_data(void *buffer, size_t size, size_t nmemb, void *userp)
102 StrBuf *Downloaded = (StrBuf *)userp;
103 size_t bytes = size * nmemb;
104 StrBufAppendBufPlain(Downloaded, buffer, bytes, 0);
109 // pull one feed (possibly multiple rooms)
111 void rss_pull_one_feed(struct rssurl *url)
116 StrBuf *Downloaded = NULL;
118 syslog(LOG_DEBUG, "rss_pull_one_feed(%s)", url->url);
120 curl = curl_easy_init();
125 Downloaded = NewStrBuf();
127 curl_easy_setopt(curl, CURLOPT_URL, url->url);
128 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Follow redirects
129 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rss_pof_write_data); // What to do with downloaded data
130 curl_easy_setopt(curl, CURLOPT_WRITEDATA, Downloaded); // Give it our StrBuf to work with
131 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L); // Time out after 20 seconds
132 res = curl_easy_perform(curl); // Perform the request
133 if (res != CURLE_OK) {
134 syslog(LOG_WARNING, "Failed to load feed: %s", curl_easy_strerror(res));
136 curl_easy_cleanup(curl);
138 // FIXME parse the feed, dummeh ... it's in ChrPtr(Downloaded)
140 for (r=url->rooms; r!=NULL; r=r->next) {
141 syslog(LOG_DEBUG, "Saving item to %s", r->room);
142 // FIXME save to rooms
145 FreeStrBuf(&Downloaded);
149 // We have a list, now download the feeds
151 void rss_pull_feeds(void)
156 while (rsstodo != NULL) {
157 rss_pull_one_feed(rsstodo);
159 rsstodo = rsstodo->next;
160 while (r->rooms != NULL) {
162 r->rooms = r->rooms->next;
172 // Scan a room's netconfig looking for RSS feed parsing requests
174 void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
176 char *serialized_config = NULL;
181 serialized_config = LoadRoomNetConfigFile(qrbuf->QRnumber);
182 if (!serialized_config) {
186 num_configs = num_tokens(serialized_config, '\n');
187 for (i=0; i<num_configs; ++i) {
188 extract_token(cfgline, serialized_config, i, '\n', sizeof cfgline);
189 if (!strncasecmp(cfgline, HKEY("rssclient|"))) {
190 strcpy(cfgline, &cfgline[10]);
191 char *vbar = strchr(cfgline, '|');
195 rssclient_push_todo(cfgline, qrbuf->QRname);
199 free(serialized_config);
204 * Scan for rooms that have RSS client requests configured
206 void rssclient_scan(void) {
207 time_t now = time(NULL);
209 /* Run no more than once every 15 minutes. */
210 if ((now - last_run) < 900) {
212 "Client: polling interval not yet reached; last run was %ldm%lds ago",
213 ((now - last_run) / 60),
214 ((now - last_run) % 60)
219 become_session(&rss_CC);
220 syslog(LOG_DEBUG, "rssclient started");
221 CtdlForEachRoom(rssclient_scan_room, NULL);
223 syslog(LOG_DEBUG, "rssclient ended");
224 last_run = time(NULL);
229 CTDL_MODULE_INIT(rssclient)
233 syslog(LOG_INFO, "%s", curl_version());
234 CtdlRegisterSessionHook(rssclient_scan, EVT_TIMER, PRIO_AGGR + 300);
238 CtdlFillSystemContext(&rss_CC, "rssclient");