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 if ( // end of a new item(rss) or entry(atom)
123 (!strcasecmp(el, "entry"))
124 || (!strcasecmp(el, "item"))
127 if (r->msg != NULL) { // Save the message to the rooms
129 // use the link as an item id if nothing else is available
130 if ((r->item_id == NULL) && (r->link != NULL)) {
131 r->item_id = strdup(r->link);
134 // check the use table
135 StrBuf *u = NewStrBuf();
136 StrBufAppendPrintf(u, "rss/%s", r->item_id);
137 time_t already_seen = CheckIfAlreadySeen("rss", u, time(NULL), 604800, eUpdate, 0, 0);
140 if (already_seen == 0) {
142 // Compose the message text
143 StrBuf *TheMessage = NewStrBuf();
144 StrBufAppendPrintf(TheMessage,
145 "Content-type: text/html\n\n"
147 "<html><head></head><body>"
150 if (r->description != NULL) {
151 StrBufAppendPrintf(TheMessage, "%s<br><br>\r\n", r->description);
152 free(r->description);
153 r->description = NULL;
156 if (r->link != NULL) {
157 StrBufAppendPrintf(TheMessage, "<a href=\"%s\">%s</a>\r\n", r->link, r->link);
162 StrBufAppendPrintf(TheMessage, "</body></html>\r\n");
163 CM_SetField(r->msg, eMesageText, ChrPtr(TheMessage), StrLength(TheMessage));
164 FreeStrBuf(&TheMessage);
166 if (CM_IsEmpty(r->msg, eAuthor)) {
167 CM_SetField(r->msg, eAuthor, HKEY("rss"));
170 if (CM_IsEmpty(r->msg, eTimestamp)) {
171 CM_SetFieldLONG(r->msg, eTimestamp, time(NULL));
174 // Save it to the room(s)
175 struct rssroom *rr = NULL;
177 for (rr=r->rooms; rr!=NULL; rr=rr->next) {
178 if (rr == r->rooms) {
179 msgnum = CtdlSubmitMsg(r->msg, NULL, rr->room, 0);
182 CtdlSaveMsgPointerInRoom(rr->room, msgnum, 0, NULL);
184 syslog(LOG_DEBUG, "Saved message %ld to %s", msgnum, rr->room);
188 syslog(LOG_DEBUG, "%s was already seen %ld seconds ago", r->item_id, already_seen);
195 if (r->item_id != NULL) {
201 else if (!strcasecmp(el, "title")) { // item subject (rss and atom)
202 if ((r->msg != NULL) && (CM_IsEmpty(r->msg, eMsgSubject))) {
203 CM_SetField(r->msg, eMsgSubject, ChrPtr(r->CData), StrLength(r->CData));
204 striplt(r->msg->cm_fields[eMsgSubject]);
208 else if (!strcasecmp(el, "author")) { // author of item (rss and maybe atom)
209 if ((r->msg != NULL) && (CM_IsEmpty(r->msg, eAuthor))) {
210 CM_SetField(r->msg, eAuthor, ChrPtr(r->CData), StrLength(r->CData));
211 striplt(r->msg->cm_fields[eAuthor]);
215 else if (!strcasecmp(el, "pubdate")) { // date/time stamp (rss) Sat, 25 Feb 2017 14:28:01 EST
216 if (CM_IsEmpty(r->msg, eTimestamp)) {
217 CM_SetFieldLONG(r->msg, eTimestamp, parsedate(ChrPtr(r->CData)));
221 else if (!strcasecmp(el, "updated")) { // date/time stamp (atom) 2003-12-13T18:30:02Z
225 else if (!strcasecmp(el, "link")) { // link to story (rss)
226 if (r->link != NULL) {
230 r->link = strdup(ChrPtr(r->CData));
235 (!strcasecmp(el, "guid")) // unique item id (rss)
236 || (!strcasecmp(el, "id")) // unique item id (atom)
238 if (r->item_id != NULL) {
242 r->item_id = strdup(ChrPtr(r->CData));
247 (!strcasecmp(el, "description")) // message text (rss)
248 || (!strcasecmp(el, "summary")) // message text (atom)
250 if (r->description != NULL) {
251 free(r->description);
252 r->description = NULL;
254 r->description = strdup(ChrPtr(r->CData));
255 striplt(r->description);
258 if (r->CData != NULL) {
259 FreeStrBuf(&r->CData);
265 // This handler is called whenever data appears between opening and closing tags.
267 void rss_handle_data(void *data, const char *content, int length)
269 struct rssparser *r = (struct rssparser *)data;
271 if (r->CData == NULL) {
272 r->CData = NewStrBuf();
275 StrBufAppendBufPlain(r->CData, content, length, 0);
279 // Feed has been downloaded, now parse it.
281 void rss_parse_feed(StrBuf *Feed, struct rssroom *rooms)
285 memset(&r, 0, sizeof r);
287 XML_Parser p = XML_ParserCreateNS("UTF-8", ':');
288 XML_SetElementHandler(p, rss_start_element, rss_end_element);
289 XML_SetCharacterDataHandler(p, rss_handle_data);
290 XML_SetUserData(p, (void *)&r);
291 XML_Parse(p, ChrPtr(Feed), StrLength(Feed), XML_TRUE);
296 // Add a feed/room pair into the todo list
298 void rssclient_push_todo(char *rssurl, char *roomname)
300 struct rssurl *r = NULL;
301 struct rssurl *thisone = NULL;
302 struct rssroom *newroom = NULL;
304 syslog(LOG_DEBUG, "rssclient_push_todo(%s, %s)", rssurl, roomname);
306 for (r=rsstodo; r!=NULL; r=r->next) {
307 if (!strcasecmp(r->url, rssurl)) {
311 if (thisone == NULL) {
312 thisone = malloc(sizeof(struct rssurl));
313 thisone->url = strdup(rssurl);
314 thisone->rooms = NULL;
315 thisone->next = rsstodo;
319 newroom = malloc(sizeof(struct rssroom));
320 newroom->room = strdup(roomname);
321 newroom->next = thisone->rooms;
322 thisone->rooms = newroom;
326 // Callback function for curl
328 size_t rss_pof_write_data(void *buffer, size_t size, size_t nmemb, void *userp)
330 StrBuf *Downloaded = (StrBuf *)userp;
331 size_t bytes = size * nmemb;
332 StrBufAppendBufPlain(Downloaded, buffer, bytes, 0);
337 // pull one feed (possibly multiple rooms)
339 void rss_pull_one_feed(struct rssurl *url)
343 StrBuf *Downloaded = NULL;
345 syslog(LOG_DEBUG, "rss_pull_one_feed(%s)", url->url);
347 curl = curl_easy_init();
352 Downloaded = NewStrBuf();
354 curl_easy_setopt(curl, CURLOPT_URL, url->url);
355 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Follow redirects
356 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rss_pof_write_data); // What to do with downloaded data
357 curl_easy_setopt(curl, CURLOPT_WRITEDATA, Downloaded); // Give it our StrBuf to work with
358 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L); // Time out after 20 seconds
359 res = curl_easy_perform(curl); // Perform the request
360 if (res != CURLE_OK) {
361 syslog(LOG_WARNING, "Failed to load feed: %s", curl_easy_strerror(res));
363 curl_easy_cleanup(curl);
365 rss_parse_feed(Downloaded, url->rooms); // parse the feed
366 FreeStrBuf(&Downloaded); // free the downloaded feed data
370 // We have a list, now download the feeds
372 void rss_pull_feeds(void)
377 while (rsstodo != NULL) {
378 rss_pull_one_feed(rsstodo);
380 rsstodo = rsstodo->next;
381 while (r->rooms != NULL) {
383 r->rooms = r->rooms->next;
393 // Scan a room's netconfig looking for RSS feed parsing requests
395 void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
397 char *serialized_config = NULL;
402 serialized_config = LoadRoomNetConfigFile(qrbuf->QRnumber);
403 if (!serialized_config) {
407 num_configs = num_tokens(serialized_config, '\n');
408 for (i=0; i<num_configs; ++i) {
409 extract_token(cfgline, serialized_config, i, '\n', sizeof cfgline);
410 if (!strncasecmp(cfgline, HKEY("rssclient|"))) {
411 strcpy(cfgline, &cfgline[10]);
412 char *vbar = strchr(cfgline, '|');
416 rssclient_push_todo(cfgline, qrbuf->QRname);
420 free(serialized_config);
425 * Scan for rooms that have RSS client requests configured
427 void rssclient_scan(void) {
428 time_t now = time(NULL);
430 /* Run no more than once every 15 minutes. */
431 if ((now - last_run) < 900) {
433 "Client: polling interval not yet reached; last run was %ldm%lds ago",
434 ((now - last_run) / 60),
435 ((now - last_run) % 60)
440 become_session(&rss_CC);
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, "%s", curl_version());
455 CtdlRegisterSessionHook(rssclient_scan, EVT_TIMER, PRIO_AGGR + 300);
459 CtdlFillSystemContext(&rss_CC, "rssclient");