1 // Bring external RSS and/or Atom feeds into rooms. This module implements a
2 // very loose parser that scrapes both kinds of feeds and is not picky about
3 // the standards compliance of the source data.
5 // Copyright (c) 2007-2023 by the citadel.org team
7 // This program is open source software. Use, duplication, or disclosure
8 // is subject to the terms of the GNU General Public License, version 3.
17 #include <sys/types.h>
20 #include <curl/curl.h>
21 #include <libcitadel.h>
22 #include "../../citadel_defs.h"
23 #include "../../server.h"
24 #include "../../citserver.h"
25 #include "../../support.h"
26 #include "../../config.h"
27 #include "../../threads.h"
28 #include "../../ctdl_module.h"
29 #include "../../msgbase.h"
30 #include "../../parsedate.h"
31 #include "../../database.h"
32 #include "../../citadel_dirs.h"
33 #include "../../context.h"
34 #include "../../internet_addressing.h"
37 char url[SIZ]; // string containing the URL of an RSS or Atom feed
38 char room[ROOMNAMELEN]; // the name of a room which is pulling this feed
44 struct CtdlMessage *msg;
54 // This handler is called whenever an XML tag opens.
55 void rss_start_element(void *data, const char *el, const char **attribute) {
56 struct rssparser *r = (struct rssparser *)data;
59 if (server_shutting_down) return; // shunt the whole operation if we're exiting
62 (!strcasecmp(el, "entry"))
63 || (!strcasecmp(el, "item"))
65 // this is the start of a new item(rss) or entry(atom)
70 r->msg = malloc(sizeof(struct CtdlMessage));
71 memset(r->msg, 0, sizeof(struct CtdlMessage));
72 r->msg->cm_magic = CTDLMESSAGE_MAGIC;
73 r->msg->cm_anon_type = MES_NORMAL;
74 r->msg->cm_format_type = FMT_RFC822;
77 else if (!strcasecmp(el, "link")) { // atom feeds have the link as an attribute
78 for(i = 0; attribute[i]; i += 2) {
79 if (!strcasecmp(attribute[i], "href")) {
80 if (r->link != NULL) {
84 r->link = strdup(attribute[i+1]);
92 // This handler is called whenever an XML tag closes.
93 void rss_end_element(void *data, const char *el) {
94 struct rssparser *r = (struct rssparser *)data;
95 StrBuf *encoded_field;
98 if (server_shutting_down) return; // shunt the whole operation if we're exiting
100 if (StrLength(r->CData) > 0) { // strip leading/trailing whitespace from field
101 StrBufTrim(r->CData);
104 if ( // end of a new item(rss) or entry(atom)
105 (!strcasecmp(el, "entry"))
106 || (!strcasecmp(el, "item"))
108 if (r->msg != NULL) { // Save the message to the rooms
110 // use the link as an item id if nothing else is available
111 if ((r->item_id == NULL) && (r->link != NULL)) {
112 r->item_id = strdup(r->link);
115 // check the use table
116 StrBuf *u = NewStrBuf();
117 StrBufAppendPrintf(u, "rss/%s", r->item_id);
118 int already_seen = CheckIfAlreadySeen(u);
121 if (already_seen == 0) {
123 // Compose the message text
125 StrBuf *TheMessage = NewStrBuf();
126 StrBufAppendPrintf(TheMessage, "<html><head></head><body>");
128 if (r->description != NULL) {
129 StrBufAppendPrintf(TheMessage, "%s<br><br>\r\n", r->description);
130 free(r->description);
131 r->description = NULL;
134 if (r->link != NULL) {
135 StrBufAppendPrintf(TheMessage, "<a href=\"%s\">%s</a>\r\n", r->link, r->link);
140 StrBufAppendPrintf(TheMessage, "</body></html>\r\n");
142 // Quoted-Printable encode the HTML message, because RSS and Atom make no guarantee of line length limits.
143 StrBuf *TheMessage_Encoded = StrBufQuotedPrintableEncode(TheMessage);
145 // Now we reuse TheMessage -- this time it will contain the MIME headers concatenated with the encoded message.
146 FlushStrBuf(TheMessage);
147 StrBufAppendBufPlain(TheMessage, HKEY(
148 "Content-type: text/html; charset=UTF-8\r\n"
149 "Content-Transfer-Encoding: quoted-printable\r\n"
153 StrBufAppendBuf(TheMessage, TheMessage_Encoded, 0);
154 FreeStrBuf(&TheMessage_Encoded);
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));
168 for (int i=0; i<array_len(feeds); ++i) {
169 struct rssfeed *rf = (struct rssfeed *) array_get_element_at(feeds, i);
170 if (!strcmp(rf->url, r->url)) {
172 // Save the item in a room
173 msgnum = CtdlSubmitMsg(r->msg, NULL, rf->room);
176 // If the same item goes to multiple rooms, just save a pointer
177 CtdlSaveMsgPointerInRoom(rf->room, msgnum, 0, NULL);
183 syslog(LOG_DEBUG, "rssclient: already seen %s", r->item_id);
190 if (r->item_id != NULL) {
196 else if (!strcasecmp(el, "title")) { // item subject (rss and atom)
197 if ((r->msg != NULL) && (CM_IsEmpty(r->msg, eMsgSubject))) {
198 encoded_field = NewStrBuf();
199 StrBufRFC2047encode(&encoded_field, r->CData);
200 CM_SetAsFieldSB(r->msg, eMsgSubject, &encoded_field);
204 else if (!strcasecmp(el, "creator")) { // <creator> can be used if <author> is not present
205 if ((r->msg != NULL) && (CM_IsEmpty(r->msg, eAuthor))) {
206 encoded_field = NewStrBuf();
207 StrBufRFC2047encode(&encoded_field, r->CData);
208 CM_SetAsFieldSB(r->msg, eAuthor, &encoded_field);
212 else if (!strcasecmp(el, "author")) { // <author> supercedes <creator> if both are present
213 if (r->msg != NULL) {
214 encoded_field = NewStrBuf();
215 StrBufRFC2047encode(&encoded_field, r->CData);
216 CM_SetAsFieldSB(r->msg, eAuthor, &encoded_field);
220 else if (!strcasecmp(el, "pubdate")) { // date/time stamp (rss) Sat, 25 Feb 2017 14:28:01 EST
221 if ((r->msg)&&(r->msg->cm_fields[eTimestamp]==NULL)) {
222 CM_SetFieldLONG(r->msg, eTimestamp, parsedate(ChrPtr(r->CData)));
226 else if (!strcasecmp(el, "updated")) { // date/time stamp (atom) 2003-12-13T18:30:02Z
227 if ((r->msg)&&(r->msg->cm_fields[eTimestamp]==NULL)) {
230 memset(&t, 0, sizeof t);
231 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);
234 CM_SetFieldLONG(r->msg, eTimestamp, mktime(&t));
238 else if (!strcasecmp(el, "link")) { // link to story (rss)
239 if (r->link != NULL) {
243 r->link = strdup(ChrPtr(r->CData));
247 (!strcasecmp(el, "guid")) // unique item id (rss)
248 || (!strcasecmp(el, "id")) // unique item id (atom)
250 if (r->item_id != NULL) {
254 r->item_id = strdup(ChrPtr(r->CData));
258 (!strcasecmp(el, "description")) // message text (rss)
259 || (!strcasecmp(el, "summary")) // message text (atom)
260 || (!strcasecmp(el, "content")) // message text (atom)
262 if (r->description != NULL) {
263 free(r->description);
264 r->description = NULL;
266 r->description = strdup(ChrPtr(r->CData));
269 if (r->CData != NULL) {
270 FreeStrBuf(&r->CData);
276 // This handler is called whenever data appears between opening and closing tags.
277 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.
289 // `Feed` is the actual RSS downloaded from the site.
290 // `url` is a string containing the feed URL
291 void rss_parse_feed(StrBuf *Feed, char *url) {
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 // pull the first feed in the list
306 void rss_pull_one_feed(char *url) {
309 StrBuf *Downloaded = NULL;
311 curl = curl_easy_init();
316 Downloaded = NewStrBuf();
318 syslog(LOG_DEBUG, "rssclient: fetching %s", url);
319 curl_easy_setopt(curl, CURLOPT_URL, url);
320 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
321 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
322 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Follow redirects
323 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback); // What to do with downloaded data
324 curl_easy_setopt(curl, CURLOPT_WRITEDATA, Downloaded); // Give it our StrBuf to work with
325 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L); // Time out after 20 seconds
326 res = curl_easy_perform(curl); // Perform the request
327 if (res != CURLE_OK) {
328 syslog(LOG_WARNING, "rssclient: failed to load feed: %s", curl_easy_strerror(res));
330 curl_easy_cleanup(curl);
332 rss_parse_feed(Downloaded, url);
333 FreeStrBuf(&Downloaded); // free the downloaded feed data
337 // We have a list, now download the feeds
338 void rss_pull_feeds(void) {
341 while (array_len(feeds) > 0) {
342 struct rssfeed *r = (struct rssfeed *) array_get_element_at(feeds, 0);
344 rss_pull_one_feed(url);
345 while (r = array_get_element_at(feeds, 0), (r && !strcmp(r->url, url))) {
346 array_delete_element_at(feeds, 0);
353 // Scan a room's netconfig looking for RSS feed parsing requests
354 void rssclient_scan_room(struct ctdlroom *qrbuf, void *data) {
355 char *serialized_config = NULL;
358 struct rssfeed one_feed;
361 if (server_shutting_down) return;
363 serialized_config = LoadRoomNetConfigFile(qrbuf->QRnumber);
364 if (!serialized_config) {
368 num_configs = num_tokens(serialized_config, '\n');
369 for (i=0; i<num_configs; ++i) {
370 extract_token(cfgline, serialized_config, i, '\n', sizeof cfgline);
371 if (!strncasecmp(cfgline, HKEY("rssclient|"))) {
372 strcpy(cfgline, &cfgline[10]);
373 char *vbar = strchr(cfgline, '|');
377 safestrncpy(one_feed.url, cfgline, SIZ);
378 safestrncpy(one_feed.room, qrbuf->QRname, ROOMNAMELEN);
379 array_append(feeds, &one_feed);
383 free(serialized_config);
387 // Scan for rooms that have RSS client requests configured
388 void rssclient_scan(void) {
389 time_t now = time(NULL);
391 // Run no more than once every 15 minutes.
392 if ((now - last_run) < 900) {
394 "rssclient: polling interval not yet reached; last run was %ldm%lds ago",
395 ((now - last_run) / 60),
396 ((now - last_run) % 60)
401 syslog(LOG_DEBUG, "rssclient: started");
402 feeds = array_new(sizeof(struct rssfeed));
404 syslog(LOG_DEBUG, "rssclient: cannot allocate memory for feed list");
407 CtdlForEachRoom(rssclient_scan_room, NULL);
408 array_sort(feeds, (int (*)(const void *, const void *))strcmp);
411 syslog(LOG_DEBUG, "rssclient: ended");
412 last_run = time(NULL);
417 // Initialization function, called from modules_init.c
418 char *ctdl_module_init_rssclient(void) {
420 syslog(LOG_INFO, "rssclient: using %s", curl_version());
421 CtdlRegisterSessionHook(rssclient_scan, EVT_TIMER, PRIO_AGGR + 300);