2 * Bring external RSS and/or Atom feeds into rooms. This module implements a
3 * very loose parser that scrapes both kinds of feeds and is not picky about
4 * the standards compliance of the source data.
6 * Copyright (c) 2007-2020 by the citadel.org team
8 * This program is open source software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 3.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
24 #include <sys/types.h>
27 #include <curl/curl.h>
28 #include <libcitadel.h>
31 #include "citserver.h"
35 #include "ctdl_module.h"
37 #include "parsedate.h"
39 #include "citadel_dirs.h"
41 #include "internet_addressing.h"
51 struct rssroom *rooms;
56 struct CtdlMessage *msg;
60 struct rssroom *rooms;
64 struct rssurl *rsstodo = NULL;
67 // This handler is called whenever an XML tag opens.
69 void rss_start_element(void *data, const char *el, const char **attribute)
71 struct rssparser *r = (struct rssparser *)data;
75 (!strcasecmp(el, "entry"))
76 || (!strcasecmp(el, "item"))
78 // this is the start of a new item(rss) or entry(atom)
83 r->msg = malloc(sizeof(struct CtdlMessage));
84 memset(r->msg, 0, sizeof(struct CtdlMessage));
85 r->msg->cm_magic = CTDLMESSAGE_MAGIC;
86 r->msg->cm_anon_type = MES_NORMAL;
87 r->msg->cm_format_type = FMT_RFC822;
90 else if (!strcasecmp(el, "link")) { // atom feeds have the link as an attribute
91 for(i = 0; attribute[i]; i += 2) {
92 if (!strcasecmp(attribute[i], "href")) {
93 if (r->link != NULL) {
97 r->link = strdup(attribute[i+1]);
105 // This handler is called whenever an XML tag closes.
107 void rss_end_element(void *data, const char *el)
109 struct rssparser *r = (struct rssparser *)data;
110 StrBuf *encoded_field;
112 if (StrLength(r->CData) > 0) { // strip leading/trailing whitespace from field
113 StrBufTrim(r->CData);
116 if ( // end of a new item(rss) or entry(atom)
117 (!strcasecmp(el, "entry"))
118 || (!strcasecmp(el, "item"))
120 if (r->msg != NULL) { // Save the message to the rooms
122 // use the link as an item id if nothing else is available
123 if ((r->item_id == NULL) && (r->link != NULL)) {
124 r->item_id = strdup(r->link);
127 // check the use table
128 StrBuf *u = NewStrBuf();
129 StrBufAppendPrintf(u, "rss/%s", r->item_id);
130 int already_seen = CheckIfAlreadySeen(u);
133 if (already_seen == 0) {
135 // Compose the message text
136 StrBuf *TheMessage = NewStrBuf();
137 StrBufAppendPrintf(TheMessage,
138 "Content-type: text/html\n\n"
140 "<html><head></head><body>"
143 if (r->description != NULL) {
144 StrBufAppendPrintf(TheMessage, "%s<br><br>\r\n", r->description);
145 free(r->description);
146 r->description = NULL;
149 if (r->link != NULL) {
150 StrBufAppendPrintf(TheMessage, "<a href=\"%s\">%s</a>\r\n", r->link, r->link);
155 StrBufAppendPrintf(TheMessage, "</body></html>\r\n");
156 CM_SetField(r->msg, eMesageText, ChrPtr(TheMessage), StrLength(TheMessage));
157 FreeStrBuf(&TheMessage);
159 if (CM_IsEmpty(r->msg, eAuthor)) {
160 CM_SetField(r->msg, eAuthor, HKEY("rss"));
163 if (CM_IsEmpty(r->msg, eTimestamp)) {
164 CM_SetFieldLONG(r->msg, eTimestamp, time(NULL));
167 // Save it to the room(s)
168 struct rssroom *rr = NULL;
170 for (rr=r->rooms; rr!=NULL; rr=rr->next) {
171 if (rr == r->rooms) {
172 msgnum = CtdlSubmitMsg(r->msg, NULL, rr->room); // in first room, save msg
175 CtdlSaveMsgPointerInRoom(rr->room, msgnum, 0, NULL); // elsewhere, save a pointer
177 syslog(LOG_DEBUG, "rssclient: saved message %ld to %s", msgnum, rr->room);
181 syslog(LOG_DEBUG, "rssclient: already seen %s", r->item_id);
188 if (r->item_id != NULL) {
194 else if (!strcasecmp(el, "title")) { // item subject (rss and atom)
195 if ((r->msg != NULL) && (CM_IsEmpty(r->msg, eMsgSubject))) {
196 encoded_field = NewStrBuf();
197 StrBufRFC2047encode(&encoded_field, r->CData);
198 CM_SetAsFieldSB(r->msg, eMsgSubject, &encoded_field);
202 else if (!strcasecmp(el, "creator")) { // <creator> can be used if <author> is not present
203 if ((r->msg != NULL) && (CM_IsEmpty(r->msg, eAuthor))) {
204 encoded_field = NewStrBuf();
205 StrBufRFC2047encode(&encoded_field, r->CData);
206 CM_SetAsFieldSB(r->msg, eAuthor, &encoded_field);
210 else if (!strcasecmp(el, "author")) { // <author> supercedes <creator> if both are present
211 if (r->msg != NULL) {
212 encoded_field = NewStrBuf();
213 StrBufRFC2047encode(&encoded_field, r->CData);
214 CM_SetAsFieldSB(r->msg, eAuthor, &encoded_field);
218 else if (!strcasecmp(el, "pubdate")) { // date/time stamp (rss) Sat, 25 Feb 2017 14:28:01 EST
219 if ((r->msg)&&(r->msg->cm_fields[eTimestamp]==NULL)) {
220 CM_SetFieldLONG(r->msg, eTimestamp, parsedate(ChrPtr(r->CData)));
224 else if (!strcasecmp(el, "updated")) { // date/time stamp (atom) 2003-12-13T18:30:02Z
225 if ((r->msg)&&(r->msg->cm_fields[eTimestamp]==NULL)) {
228 memset(&t, 0, sizeof t);
229 sscanf(ChrPtr(r->CData), "%d-%d-%dT%d:%d:%d%c", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec, &zulu);
232 CM_SetFieldLONG(r->msg, eTimestamp, mktime(&t));
236 else if (!strcasecmp(el, "link")) { // link to story (rss)
237 if (r->link != NULL) {
241 r->link = strdup(ChrPtr(r->CData));
245 (!strcasecmp(el, "guid")) // unique item id (rss)
246 || (!strcasecmp(el, "id")) // unique item id (atom)
248 if (r->item_id != NULL) {
252 r->item_id = strdup(ChrPtr(r->CData));
256 (!strcasecmp(el, "description")) // message text (rss)
257 || (!strcasecmp(el, "summary")) // message text (atom)
258 || (!strcasecmp(el, "content")) // message text (atom)
260 if (r->description != NULL) {
261 free(r->description);
262 r->description = NULL;
264 r->description = strdup(ChrPtr(r->CData));
267 if (r->CData != NULL) {
268 FreeStrBuf(&r->CData);
274 // This handler is called whenever data appears between opening and closing tags.
276 void rss_handle_data(void *data, const char *content, int length)
278 struct rssparser *r = (struct rssparser *)data;
280 if (r->CData == NULL) {
281 r->CData = NewStrBuf();
284 StrBufAppendBufPlain(r->CData, content, length, 0);
288 // Feed has been downloaded, now parse it.
290 void rss_parse_feed(StrBuf *Feed, struct rssroom *rooms)
294 memset(&r, 0, sizeof r);
296 XML_Parser p = XML_ParserCreate("UTF-8");
297 XML_SetElementHandler(p, rss_start_element, rss_end_element);
298 XML_SetCharacterDataHandler(p, rss_handle_data);
299 XML_SetUserData(p, (void *)&r);
300 XML_Parse(p, ChrPtr(Feed), StrLength(Feed), XML_TRUE);
305 // Add a feed/room pair into the todo list
307 void rssclient_push_todo(char *rssurl, char *roomname)
309 struct rssurl *r = NULL;
310 struct rssurl *thisone = NULL;
311 struct rssroom *newroom = NULL;
313 syslog(LOG_DEBUG, "rssclient: will fetch %s to %s", rssurl, roomname);
315 for (r=rsstodo; r!=NULL; r=r->next) {
316 if (!strcasecmp(r->url, rssurl)) {
321 if (thisone == NULL) {
322 thisone = malloc(sizeof(struct rssurl));
323 thisone->url = strdup(rssurl);
324 thisone->rooms = NULL;
325 thisone->next = rsstodo;
329 newroom = malloc(sizeof(struct rssroom));
330 newroom->room = strdup(roomname);
331 newroom->next = thisone->rooms;
332 thisone->rooms = newroom;
336 // pull one feed (possibly multiple rooms)
338 void rss_pull_one_feed(struct rssurl *url)
342 StrBuf *Downloaded = NULL;
344 syslog(LOG_DEBUG, "rssclient: fetching %s", url->url);
346 curl = curl_easy_init();
351 Downloaded = NewStrBuf();
353 curl_easy_setopt(curl, CURLOPT_URL, url->url);
354 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
355 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
356 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Follow redirects
357 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback); // 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, "rssclient: 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 "rssclient: polling interval not yet reached; last run was %ldm%lds ago",
435 ((now - last_run) / 60),
436 ((now - last_run) % 60)
441 syslog(LOG_DEBUG, "rssclient: started");
442 CtdlForEachRoom(rssclient_scan_room, NULL);
444 syslog(LOG_DEBUG, "rssclient: ended");
445 last_run = time(NULL);
450 CTDL_MODULE_INIT(rssclient)
454 syslog(LOG_INFO, "rssclient: using %s", curl_version());
455 CtdlRegisterSessionHook(rssclient_scan, EVT_TIMER, PRIO_AGGR + 300);