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 "internet_addressing.h"
61 struct rssroom *rooms;
66 struct CtdlMessage *msg;
70 struct rssroom *rooms;
74 struct CitContext rss_CC;
75 struct rssurl *rsstodo = NULL;
78 // This handler is called whenever an XML tag opens.
80 void rss_start_element(void *data, const char *el, const char **attribute)
82 struct rssparser *r = (struct rssparser *)data;
86 (!strcasecmp(el, "entry"))
87 || (!strcasecmp(el, "item"))
89 // this is the start of a new item(rss) or entry(atom)
94 r->msg = malloc(sizeof(struct CtdlMessage));
95 memset(r->msg, 0, sizeof(struct CtdlMessage));
96 r->msg->cm_magic = CTDLMESSAGE_MAGIC;
97 r->msg->cm_anon_type = MES_NORMAL;
98 r->msg->cm_format_type = FMT_RFC822;
101 else if (!strcasecmp(el, "link")) { // atom feeds have the link as an attribute
102 for(i = 0; attribute[i]; i += 2) {
103 if (!strcasecmp(attribute[i], "href")) {
104 if (r->link != NULL) {
108 r->link = strdup(attribute[i+1]);
116 // This handler is called whenever an XML tag closes.
118 void rss_end_element(void *data, const char *el)
120 struct rssparser *r = (struct rssparser *)data;
122 syslog(LOG_DEBUG, "end: %s", el);
123 if ( // end of a new item(rss) or entry(atom)
124 (!strcasecmp(el, "entry"))
125 || (!strcasecmp(el, "item"))
128 if (r->msg != NULL) { // Save the message to the rooms
130 // use the link as an item id if nothing else is available
131 if ((r->item_id == NULL) && (r->link != NULL)) {
132 r->item_id = strdup(r->link);
135 // check the use table
136 StrBuf *u = NewStrBuf();
137 StrBufAppendPrintf(u, "rss/%s", r->item_id);
138 time_t already_seen = CheckIfAlreadySeen("rss", u, time(NULL), 604800, eUpdate, 0, 0);
141 if (already_seen == 0) {
143 // Compose the message text
144 StrBuf *TheMessage = NewStrBuf();
145 StrBufAppendPrintf(TheMessage,
146 "Content-type: text/html\n\n"
148 "<html><head></head><body>"
151 if (r->description != NULL) {
152 StrBufAppendPrintf(TheMessage, "%s<br><br>\r\n", r->description);
153 free(r->description);
154 r->description = NULL;
157 if (r->link != NULL) {
158 StrBufAppendPrintf(TheMessage, "<a href=\"%s\">%s</a>\r\n", r->link, r->link);
163 StrBufAppendPrintf(TheMessage, "</body></html>\r\n");
164 CM_SetField(r->msg, eMesageText, ChrPtr(TheMessage), StrLength(TheMessage));
165 FreeStrBuf(&TheMessage);
167 if (CM_IsEmpty(r->msg, eAuthor)) {
168 CM_SetField(r->msg, eAuthor, HKEY("rss"));
171 if (CM_IsEmpty(r->msg, eTimestamp)) {
172 CM_SetFieldLONG(r->msg, eTimestamp, time(NULL));
175 // Save it to the room(s)
176 struct rssroom *rr = NULL;
178 for (rr=r->rooms; rr!=NULL; rr=rr->next) {
179 if (rr == r->rooms) {
180 msgnum = CtdlSubmitMsg(r->msg, NULL, rr->room, 0);
183 CtdlSaveMsgPointerInRoom(rr->room, msgnum, 0, NULL);
185 syslog(LOG_DEBUG, "Saved message %ld to %s", msgnum, rr->room);
189 syslog(LOG_DEBUG, "%s was already seen %ld seconds ago", r->item_id, already_seen);
196 if (r->item_id != NULL) {
202 else if (!strcasecmp(el, "title")) { // item subject (rss and atom)
203 if ((r->msg != NULL) && (CM_IsEmpty(r->msg, eMsgSubject))) {
204 CM_SetField(r->msg, eMsgSubject, ChrPtr(r->CData), StrLength(r->CData));
205 striplt(r->msg->cm_fields[eMsgSubject]);
209 else if (!strcasecmp(el, "author")) { // author of item (rss and maybe atom)
210 if ((r->msg != NULL) && (CM_IsEmpty(r->msg, eAuthor))) {
211 CM_SetField(r->msg, eAuthor, ChrPtr(r->CData), StrLength(r->CData));
212 striplt(r->msg->cm_fields[eAuthor]);
216 else if (!strcasecmp(el, "pubdate")) { // date/time stamp (rss) Sat, 25 Feb 2017 14:28:01 EST
217 if ((r->msg)&&(r->msg->cm_fields[eTimestamp]==NULL)) {
218 CM_SetFieldLONG(r->msg, eTimestamp, parsedate(ChrPtr(r->CData)));
222 else if (!strcasecmp(el, "updated")) { // date/time stamp (atom) 2003-12-13T18:30:02Z
226 else if (!strcasecmp(el, "link")) { // link to story (rss)
227 if (r->link != NULL) {
231 r->link = strdup(ChrPtr(r->CData));
236 (!strcasecmp(el, "guid")) // unique item id (rss)
237 || (!strcasecmp(el, "id")) // unique item id (atom)
239 if (r->item_id != NULL) {
243 r->item_id = strdup(ChrPtr(r->CData));
248 (!strcasecmp(el, "description")) // message text (rss)
249 || (!strcasecmp(el, "summary")) // message text (atom)
251 if (r->description != NULL) {
252 free(r->description);
253 r->description = NULL;
255 r->description = strdup(ChrPtr(r->CData));
256 striplt(r->description);
259 if (r->CData != NULL) {
260 FreeStrBuf(&r->CData);
266 // This handler is called whenever data appears between opening and closing tags.
268 void rss_handle_data(void *data, const char *content, int length)
270 struct rssparser *r = (struct rssparser *)data;
272 if (r->CData == NULL) {
273 r->CData = NewStrBuf();
276 StrBufAppendBufPlain(r->CData, content, length, 0);
280 // Feed has been downloaded, now parse it.
282 void rss_parse_feed(StrBuf *Feed, struct rssroom *rooms)
286 memset(&r, 0, sizeof r);
288 XML_Parser p = XML_ParserCreate("UTF-8");
289 XML_SetElementHandler(p, rss_start_element, rss_end_element);
290 XML_SetCharacterDataHandler(p, rss_handle_data);
291 XML_SetUserData(p, (void *)&r);
292 XML_Parse(p, ChrPtr(Feed), StrLength(Feed), XML_TRUE);
297 // Add a feed/room pair into the todo list
299 void rssclient_push_todo(char *rssurl, char *roomname)
301 struct rssurl *r = NULL;
302 struct rssurl *thisone = NULL;
303 struct rssroom *newroom = NULL;
305 syslog(LOG_DEBUG, "rssclient_push_todo(%s, %s)", rssurl, roomname);
307 for (r=rsstodo; r!=NULL; r=r->next) {
308 if (!strcasecmp(r->url, rssurl)) {
312 if (thisone == NULL) {
313 thisone = malloc(sizeof(struct rssurl));
314 thisone->url = strdup(rssurl);
315 thisone->rooms = NULL;
316 thisone->next = rsstodo;
320 newroom = malloc(sizeof(struct rssroom));
321 newroom->room = strdup(roomname);
322 newroom->next = thisone->rooms;
323 thisone->rooms = newroom;
327 // Callback function for curl
329 size_t rss_pof_write_data(void *buffer, size_t size, size_t nmemb, void *userp)
331 StrBuf *Downloaded = (StrBuf *)userp;
332 size_t bytes = size * nmemb;
333 StrBufAppendBufPlain(Downloaded, buffer, bytes, 0);
338 // pull one feed (possibly multiple rooms)
340 void rss_pull_one_feed(struct rssurl *url)
344 StrBuf *Downloaded = NULL;
346 syslog(LOG_DEBUG, "rss_pull_one_feed(%s)", url->url);
348 curl = curl_easy_init();
353 Downloaded = NewStrBuf();
355 curl_easy_setopt(curl, CURLOPT_URL, url->url);
356 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Follow redirects
357 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rss_pof_write_data); // What to do with downloaded data
358 curl_easy_setopt(curl, CURLOPT_WRITEDATA, Downloaded); // Give it our StrBuf to work with
359 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L); // Time out after 20 seconds
360 res = curl_easy_perform(curl); // Perform the request
361 if (res != CURLE_OK) {
362 syslog(LOG_WARNING, "Failed to load feed: %s", curl_easy_strerror(res));
364 curl_easy_cleanup(curl);
366 rss_parse_feed(Downloaded, url->rooms); // parse the feed
367 FreeStrBuf(&Downloaded); // free the downloaded feed data
371 // We have a list, now download the feeds
373 void rss_pull_feeds(void)
378 while (rsstodo != NULL) {
379 rss_pull_one_feed(rsstodo);
381 rsstodo = rsstodo->next;
382 while (r->rooms != NULL) {
384 r->rooms = r->rooms->next;
394 // Scan a room's netconfig looking for RSS feed parsing requests
396 void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
398 char *serialized_config = NULL;
403 serialized_config = LoadRoomNetConfigFile(qrbuf->QRnumber);
404 if (!serialized_config) {
408 num_configs = num_tokens(serialized_config, '\n');
409 for (i=0; i<num_configs; ++i) {
410 extract_token(cfgline, serialized_config, i, '\n', sizeof cfgline);
411 if (!strncasecmp(cfgline, HKEY("rssclient|"))) {
412 strcpy(cfgline, &cfgline[10]);
413 char *vbar = strchr(cfgline, '|');
417 rssclient_push_todo(cfgline, qrbuf->QRname);
421 free(serialized_config);
426 * Scan for rooms that have RSS client requests configured
428 void rssclient_scan(void) {
429 time_t now = time(NULL);
431 /* Run no more than once every 15 minutes. */
432 if ((now - last_run) < 900) {
434 "Client: polling interval not yet reached; last run was %ldm%lds ago",
435 ((now - last_run) / 60),
436 ((now - last_run) % 60)
441 become_session(&rss_CC);
442 syslog(LOG_DEBUG, "rssclient started");
443 CtdlForEachRoom(rssclient_scan_room, NULL);
445 syslog(LOG_DEBUG, "rssclient ended");
446 last_run = time(NULL);
451 CTDL_MODULE_INIT(rssclient)
455 syslog(LOG_INFO, "%s", curl_version());
456 CtdlRegisterSessionHook(rssclient_scan, EVT_TIMER, PRIO_AGGR + 300);
460 CtdlFillSystemContext(&rss_CC, "rssclient");