2 * Bring external RSS feeds into rooms.
4 * Copyright (c) 2007-2010 by the citadel.org team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
30 # include <sys/time.h>
39 #include <sys/types.h>
42 #include <curl/curl.h>
43 #include <libcitadel.h>
46 #include "citserver.h"
50 #include "ctdl_module.h"
51 #include "clientsocket.h"
53 #include "parsedate.h"
55 #include "citadel_dirs.h"
59 typedef struct rssnetcfg rssnetcfg;
64 time_t last_error_when;
68 #define RSS_UNSET (1<<0)
69 #define RSS_RSS (1<<1)
70 #define RSS_ATOM (1<<2)
71 #define RSS_REQUIRE_BUF (1<<3)
73 typedef struct _rss_item {
84 StrBuf *channel_title;
86 StrBuf *author_or_creator;
91 typedef void (*rss_handler_func)(StrBuf *CData,
96 typedef struct __rss_xml_handler {
98 rss_handler_func Handler;
102 typedef struct _rsscollection {
109 rss_xml_handler *Current;
112 struct rssnetcfg *rnclist = NULL;
113 HashList *StartHandlers;
114 HashList *EndHandlers;
115 HashList *KnownNameSpaces;
116 void AddRSSStartHandler(rss_handler_func Handler, int Flags, const char *key, long len)
119 h = (rss_xml_handler*) malloc(sizeof (rss_xml_handler));
121 h->Handler = Handler;
122 Put(StartHandlers, key, len, h, NULL);
124 void AddRSSEndHandler(rss_handler_func Handler, int Flags, const char *key, long len)
127 h = (rss_xml_handler*) malloc(sizeof (rss_xml_handler));
129 h->Handler = Handler;
130 Put(EndHandlers, key, len, h, NULL);
139 * dug this out of the trashcan of the midgard project, lets see if it works for us.
140 * original code by Alexander Bokovoy <bokovoy@avilink.ne> distributed under GPL V2 or later
144 >= 0 - successfull, 0 means conversion doesn't use multibyte sequences
145 -1 - error during iconv_open call
146 -2 - error during iconv_close call
147 ----------------------------------
148 This function expects that multibyte encoding in 'charset' wouldn't have
149 characters with more than 3 bytes. It is not intended to convert UTF-8 because
150 we'll never receive UTF-8 in our handler (it is handled by Exat itself).
153 fill_encoding_info (const char *charset, XML_Encoding * info)
155 iconv_t cd = (iconv_t)(-1);
157 CtdlLogPrintf(0, "RSS: fill encoding info ...\n");
159 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
160 cd = iconv_open ("UCS-2LE", charset);
162 cd = iconv_open ("UCS-2BE", charset);
165 if (cd == (iconv_t) (-1))
172 unsigned char buf[4];
173 unsigned int i0, i1, i2;
176 for (i0 = 0; i0 < 0x100; i0++)
180 //result = try (cd, buf, 1, &out);
190 for (i1 = 0; i1 < 0x100; i1++)
193 ///result = try (cd, buf, 2, &out);
204 for (i2 = 0; i2 < 0x100; i2++)
207 ////result = try (cd, buf, 3, &out);
223 if (iconv_close (cd) < 0)
231 iconv_convertor (void *data, const char *s)
233 XML_Encoding *info = data;
235 CtdlLogPrintf(0, "RSS: Converting ...\n");
241 result = g_byte_array_new ();
242 if (process_block (info->data, (char *) s, strlen (s), result) == 0)
244 res = *(result->data);
245 g_byte_array_free (result, TRUE);
248 g_byte_array_free (result, TRUE);
254 my_release (void *data)
256 iconv_t cd = (iconv_t) data;
257 if (iconv_close (cd) != 0)
259 /// TODO: uh no. exit (1);
263 handle_unknown_xml_encoding (void *encodingHandleData,
264 const XML_Char * name,
268 CtdlLogPrintf(0, "RSS: unknown encoding ...\n");
269 result = fill_encoding_info (name, info);
273 Special case: client asked for reverse conversion, we'll provide him with
274 iconv descriptor which handles it. Client should release it by himself.
276 if(encodingHandleData != NULL)
277 *((iconv_t *)encodingHandleData) = iconv_open(name, "UTF-8");
279 Optimization: we do not need conversion function if encoding is one-to-one,
280 info->map table will be enough
285 info->convert = NULL;
286 info->release = NULL;
290 We do need conversion function because this encoding uses multibyte sequences
292 info->data = (void *) iconv_open ("UTF-8", name);
293 if ((int)info->data == -1)
295 info->convert = iconv_convertor;
296 info->release = my_release;
299 if(encodingHandleData != NULL)
300 *(iconv_t *)encodingHandleData = NULL;
308 * Commit a fetched and parsed RSS item to disk
310 void rss_save_item(rss_item *ri)
313 struct MD5Context md5context;
314 u_char rawdigest[MD5_DIGEST_LEN];
317 struct cdbdata *cdbut;
319 struct CtdlMessage *msg;
320 struct recptypes *recp = NULL;
323 recp = (struct recptypes *) malloc(sizeof(struct recptypes));
324 if (recp == NULL) return;
325 memset(recp, 0, sizeof(struct recptypes));
326 memset(&ut, 0, sizeof(struct UseTable));
327 recp->recp_room = strdup(ri->roomlist);
328 recp->num_room = num_tokens(ri->roomlist, '|');
329 recp->recptypes_magic = RECPTYPES_MAGIC;
331 /* Construct a GUID to use in the S_USETABLE table.
332 * If one is not present in the item itself, make one up.
334 if (ri->guid != NULL) {
335 StrBufSpaceToBlank(ri->guid);
336 StrBufTrim(ri->guid);
337 snprintf(utmsgid, sizeof utmsgid, "rss/%s", ChrPtr(ri->guid));
340 MD5Init(&md5context);
341 if (ri->title != NULL) {
342 MD5Update(&md5context, (const unsigned char*)ChrPtr(ri->title), StrLength(ri->title));
344 if (ri->link != NULL) {
345 MD5Update(&md5context, (const unsigned char*)ChrPtr(ri->link), StrLength(ri->link));
347 MD5Final(rawdigest, &md5context);
348 for (i=0; i<MD5_DIGEST_LEN; i++) {
349 sprintf(&utmsgid[i*2], "%02X", (unsigned char) (rawdigest[i] & 0xff));
350 utmsgid[i*2] = tolower(utmsgid[i*2]);
351 utmsgid[(i*2)+1] = tolower(utmsgid[(i*2)+1]);
353 strcat(utmsgid, "_rss2ctdl");
356 /* Find out if we've already seen this item */
358 cdbut = cdb_fetch(CDB_USETABLE, utmsgid, strlen(utmsgid));
361 /* Item has already been seen */
362 CtdlLogPrintf(CTDL_DEBUG, "%s has already been seen\n", utmsgid);
365 /* rewrite the record anyway, to update the timestamp */
366 strcpy(ut.ut_msgid, utmsgid);
367 ut.ut_timestamp = time(NULL);
368 cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid), &ut, sizeof(struct UseTable) );
373 /* Item has not been seen, so save it. */
374 CtdlLogPrintf(CTDL_DEBUG, "RSS: saving item...\n");
375 if (ri->description == NULL) ri->description = NewStrBufPlain(HKEY(""));
376 StrBufSpaceToBlank(ri->description);
377 msg = malloc(sizeof(struct CtdlMessage));
378 memset(msg, 0, sizeof(struct CtdlMessage));
379 msg->cm_magic = CTDLMESSAGE_MAGIC;
380 msg->cm_anon_type = MES_NORMAL;
381 msg->cm_format_type = FMT_RFC822;
383 if (ri->guid != NULL) {
384 msg->cm_fields['E'] = strdup(ChrPtr(ri->guid));
387 if (ri->author_or_creator != NULL) {
389 StrBuf *Encoded, *QPEncoded;
391 StrBuf *EmailAddress;
394 UserName = NewStrBuf();
395 EmailAddress = NewStrBuf();
396 EncBuf = NewStrBuf();
398 From = html_to_ascii(ChrPtr(ri->author_or_creator),
399 StrLength(ri->author_or_creator),
402 Encoded = NewStrBufPlain(From, -1);
405 QPEncoded = StrBufSanitizeEmailRecipientVector(Encoded, UserName, EmailAddress, EncBuf);
406 msg->cm_fields['A'] = SmashStrBuf(&QPEncoded);
408 FreeStrBuf(&Encoded);
409 FreeStrBuf(&UserName);
410 FreeStrBuf(&EmailAddress);
415 msg->cm_fields['A'] = strdup("rss");
418 msg->cm_fields['N'] = strdup(NODENAME);
419 if (ri->title != NULL) {
422 StrBuf *Encoded, *QPEncoded;
425 StrBufSpaceToBlank(ri->title);
426 len = StrLength(ri->title);
427 Sbj = html_to_ascii(ChrPtr(ri->title), len, 512, 0);
428 Encoded = NewStrBufPlain(Sbj, -1);
432 StrBufRFC2047encode(&QPEncoded, Encoded);
434 msg->cm_fields['U'] = SmashStrBuf(&QPEncoded);
435 FreeStrBuf(&Encoded);
437 msg->cm_fields['T'] = malloc(64);
438 snprintf(msg->cm_fields['T'], 64, "%ld", ri->pubdate);
439 if (ri->channel_title != NULL) {
440 if (StrLength(ri->channel_title) > 0) {
441 msg->cm_fields['O'] = strdup(ChrPtr(ri->channel_title));
444 if (ri->link == NULL)
445 ri->link = NewStrBufPlain(HKEY(""));
446 msglen += 1024 + StrLength(ri->link) + StrLength(ri->description) ;
447 msg->cm_fields['M'] = malloc(msglen);
448 snprintf(msg->cm_fields['M'], msglen,
449 "Content-type: text/html; charset=\"UTF-8\"\r\n\r\n"
452 "<a href=\"%s\">%s</a><br>\n"
453 "<a href=\"%s\">%s</a>\n"
456 ChrPtr(ri->description),
458 (StrLength(ri->linkTitle)>0)?ChrPtr(ri->linkTitle):ChrPtr(ri->link),
460 (StrLength(ri->reLinkTitle)>0)?ChrPtr(ri->reLinkTitle):"Reply to this"
463 CtdlSubmitMsg(msg, recp, NULL, 0);
464 CtdlFreeMessage(msg);
466 /* write the uidl to the use table so we don't store this item again */
467 strcpy(ut.ut_msgid, utmsgid);
468 ut.ut_timestamp = time(NULL);
469 cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid), &ut, sizeof(struct UseTable) );
471 free_recipients(recp);
477 * Convert an RDF/RSS datestamp into a time_t
479 time_t rdf_parsedate(const char *p)
485 if (strlen(p) < 10) return 0L;
487 memset(&tm, 0, sizeof tm);
490 * If the timestamp appears to be in W3C datetime format, try to
491 * parse it. See also: http://www.w3.org/TR/NOTE-datetime
493 * This code, along with parsedate.c, is a potential candidate for
494 * moving into libcitadel.
496 if ( (p[4] == '-') && (p[7] == '-') ) {
497 tm.tm_year = atoi(&p[0]) - 1900;
498 tm.tm_mon = atoi(&p[5]) - 1;
499 tm.tm_mday = atoi(&p[8]);
500 if ( (p[10] == 'T') && (p[13] == ':') ) {
501 tm.tm_hour = atoi(&p[11]);
502 tm.tm_min = atoi(&p[14]);
507 /* hmm... try RFC822 date stamp format */
510 if (t > 0) return(t);
512 /* yeesh. ok, just return the current date and time. */
516 void flush_rss_item(rss_item *ri)
518 /* Initialize the feed item data structure */
519 FreeStrBuf(&ri->guid);
520 FreeStrBuf(&ri->title);
521 FreeStrBuf(&ri->link);
522 FreeStrBuf(&ri->author_or_creator);
523 FreeStrBuf(&ri->author_url);
524 FreeStrBuf(&ri->description);
527 void rss_xml_start(void *data, const char *supplied_el, const char **attr)
530 rsscollection *rssc = (rsscollection*) data;
531 rssnetcfg *Cfg = rssc->Cfg;
532 rss_item *ri = rssc->Item;
537 /* Axe the namespace, we don't care about it */
538 /// CtdlLogPrintf(0, "RSS: supplied el %d: %s...\n", rssc->Cfg->ItemType, supplied_el);
540 while (sep = strchr(pel, ':'), sep) {
544 if (pel != supplied_el)
548 if (!GetHash(KnownNameSpaces,
550 pel - supplied_el - 1,
553 CtdlLogPrintf(0, "RSS: START ignoring because of wrong namespace [%s] = [%s]\n",
559 StrBufPlain(rssc->Key, pel, -1);
560 StrBufLowerCase(rssc->Key);
561 if (GetHash(StartHandlers, SKEY(rssc->Key), &pv))
563 rssc->Current = h = (rss_xml_handler*) pv;
565 if (((h->Flags & RSS_UNSET) != 0) &&
566 (Cfg->ItemType == RSS_UNSET))
568 h->Handler(rssc->CData, ri, Cfg, attr);
570 else if (((h->Flags & RSS_RSS) != 0) &&
571 (Cfg->ItemType == RSS_RSS))
573 h->Handler(rssc->CData, ri, Cfg, attr);
575 else if (((h->Flags & RSS_ATOM) != 0) &&
576 (Cfg->ItemType == RSS_ATOM))
578 h->Handler(rssc->CData, ri, Cfg, attr);
581 CtdlLogPrintf(0, "RSS: START unhandled: [%s] [%s]...\n", pel, supplied_el);
584 CtdlLogPrintf(0, "RSS: START unhandled: [%s] [%s]...\n", pel, supplied_el);
588 void rss_xml_end(void *data, const char *supplied_el)
591 rsscollection *rssc = (rsscollection*) data;
592 rssnetcfg *Cfg = rssc->Cfg;
593 rss_item *ri = rssc->Item;
598 /* Axe the namespace, we don't care about it */
600 while (sep = strchr(pel, ':'), sep) {
603 // CtdlLogPrintf(0, "RSS: END %s...\n", el);
604 if (pel != supplied_el)
608 if (!GetHash(KnownNameSpaces,
610 pel - supplied_el - 1,
613 CtdlLogPrintf(0, "RSS: END ignoring because of wrong namespace [%s] = [%s]\n",
614 supplied_el, ChrPtr(rssc->CData));
615 FlushStrBuf(rssc->CData);
620 StrBufPlain(rssc->Key, pel, -1);
621 StrBufLowerCase(rssc->Key);
622 if (GetHash(EndHandlers, SKEY(rssc->Key), &pv))
624 h = (rss_xml_handler*) pv;
626 if (((h->Flags & RSS_UNSET) != 0) &&
627 (Cfg->ItemType == RSS_UNSET))
629 h->Handler(rssc->CData, ri, Cfg, NULL);
631 else if (((h->Flags & RSS_RSS) != 0) &&
632 (Cfg->ItemType == RSS_RSS))
634 h->Handler(rssc->CData, ri, Cfg, NULL);
636 else if (((h->Flags & RSS_ATOM) != 0) &&
637 (Cfg->ItemType == RSS_ATOM))
639 h->Handler(rssc->CData, ri, Cfg, NULL);
642 CtdlLogPrintf(0, "RSS: END unhandled: [%s] [%s] = [%s]...\n", pel, supplied_el, ChrPtr(rssc->CData));
646 CtdlLogPrintf(0, "RSS: END unhandled: [%s] [%s] = [%s]...\n", pel, supplied_el, ChrPtr(rssc->CData));
647 FlushStrBuf(rssc->CData);
648 rssc->Current = NULL;
666 void RSS_item_rss_start (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
668 CtdlLogPrintf(CTDL_DEBUG, "RSS: This is an RSS feed.\n");
669 Cfg->ItemType = RSS_RSS;
672 void RSS_item_rdf_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
674 CtdlLogPrintf(CTDL_DEBUG, "RSS: This is an RDF feed.\n");
675 Cfg->ItemType = RSS_RSS;
678 void ATOM_item_feed_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
680 CtdlLogPrintf(CTDL_DEBUG, "RSS: This is an ATOM feed.\n");
681 Cfg->ItemType = RSS_ATOM;
685 void RSS_item_item_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
687 ri->item_tag_nesting ++;
691 void ATOM_item_entry_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
694 ri->item_tag_nesting ++;
698 void ATOM_item_link_start (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
701 const char *pHref = NULL;
702 const char *pType = NULL;
703 const char *pRel = NULL;
704 const char *pTitle = NULL;
706 for (i = 0; Attr[i] != NULL; i+=2)
708 if (!strcmp(Attr[i], "href"))
712 else if (!strcmp(Attr[i], "rel"))
716 else if (!strcmp(Attr[i], "type"))
720 else if (!strcmp(Attr[i], "title"))
726 return; /* WHUT? Pointing... where? */
727 if ((pType != NULL) && !strcasecmp(pType, "application/atom+xml"))
728 return; /* these just point to other rss resources, we're not interested in them. */
731 if (!strcasecmp (pRel, "replies"))
733 NewStrBufDupAppendFlush(&ri->reLink, NULL, pHref, -1);
734 StrBufTrim(ri->link);
735 NewStrBufDupAppendFlush(&ri->reLinkTitle, NULL, pTitle, -1);
737 else if (!strcasecmp(pRel, "alternate")) /* Alternative representation of this Item... */
739 NewStrBufDupAppendFlush(&ri->link, NULL, pHref, -1);
740 StrBufTrim(ri->link);
741 NewStrBufDupAppendFlush(&ri->linkTitle, NULL, pTitle, -1);
744 #if 0 /* these are also defined, but dunno what to do with them.. */
745 else if (!strcasecmp(pRel, "related"))
748 else if (!strcasecmp(pRel, "self"))
751 else if (!strcasecmp(pRel, "enclosure"))
752 {/* this reference can get big, and is probably the full article... */
754 else if (!strcasecmp(pRel, "via"))
755 {/* this article was provided via... */
759 else if (StrLength(ri->link) == 0)
761 NewStrBufDupAppendFlush(&ri->link, NULL, pHref, -1);
762 StrBufTrim(ri->link);
763 NewStrBufDupAppendFlush(&ri->linkTitle, NULL, pTitle, -1);
770 void ATOMRSS_item_title_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
772 if ((ri->item_tag_nesting == 0) && (StrLength(CData) > 0)) {
773 NewStrBufDupAppendFlush(&ri->channel_title, CData, NULL, 0);
774 StrBufTrim(ri->channel_title);
778 void RSS_item_guid_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
780 if (StrLength(CData) > 0) {
781 NewStrBufDupAppendFlush(&ri->guid, CData, NULL, 0);
785 void ATOM_item_id_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
787 if (StrLength(CData) > 0) {
788 NewStrBufDupAppendFlush(&ri->guid, CData, NULL, 0);
793 void RSS_item_link_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
795 if (StrLength(CData) > 0) {
796 NewStrBufDupAppendFlush(&ri->link, CData, NULL, 0);
797 StrBufTrim(ri->link);
801 void RSSATOM_item_title_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
803 if (StrLength(CData) > 0) {
804 NewStrBufDupAppendFlush(&ri->title, CData, NULL, 0);
805 StrBufTrim(ri->title);
809 void ATOM_item_content_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
811 if (StrLength(CData) > 0) {
812 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
813 StrBufTrim(ri->description);
816 void ATOM_item_summary_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
818 /* this can contain an abstract of the article. but we don't want to verwrite a full document if we already have it. */
819 if ((StrLength(CData) > 0) && (StrLength(ri->description) == 0))
821 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
822 StrBufTrim(ri->description);
826 void RSS_item_description_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
828 if (StrLength(CData) > 0) {
829 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
833 void ATOM_item_published_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
835 if (StrLength(CData) > 0) {
837 ri->pubdate = rdf_parsedate(ChrPtr(CData));
841 void ATOM_item_updated_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
843 if (StrLength(CData) > 0) {
845 ri->pubdate = rdf_parsedate(ChrPtr(CData));
849 void RSS_item_pubdate_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
851 if (StrLength(CData) > 0) {
853 ri->pubdate = rdf_parsedate(ChrPtr(CData));
858 void RSS_item_date_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
860 if (StrLength(CData) > 0) {
862 ri->pubdate = rdf_parsedate(ChrPtr(CData));
868 void RSS_item_author_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
870 if (StrLength(CData) > 0) {
871 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
872 StrBufTrim(ri->author_or_creator);
877 void ATOM_item_name_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
879 if (StrLength(CData) > 0) {
880 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
881 StrBufTrim(ri->author_or_creator);
885 void RSS_item_creator_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
887 if ((StrLength(CData) > 0) &&
888 (StrLength(ri->author_or_creator) == 0))
890 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
891 StrBufTrim(ri->author_or_creator);
896 void ATOM_item_uri_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
898 if (StrLength(CData) > 0) {
899 NewStrBufDupAppendFlush(&ri->author_url, CData, NULL, 0);
900 StrBufTrim(ri->author_url);
904 void RSS_item_item_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
906 --ri->item_tag_nesting;
911 void ATOM_item_entry_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
913 --ri->item_tag_nesting;
917 void RSS_item_rss_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
919 // CtdlLogPrintf(CTDL_DEBUG, "End of feed detected. Closing parser.\n");
920 ri->done_parsing = 1;
923 void RSS_item_rdf_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
925 // CtdlLogPrintf(CTDL_DEBUG, "End of feed detected. Closing parser.\n");
926 ri->done_parsing = 1;
930 void RSSATOM_item_ignore(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
945 * This callback stores up the data which appears in between tags.
947 void rss_xml_cdata_start(void *data)
949 rsscollection *rssc = (rsscollection*) data;
951 FlushStrBuf(rssc->CData);
954 void rss_xml_cdata_end(void *data)
956 //rsscollection *rssc = (rsscollection*) data;
957 //rss_item *ri = rssc->Item;
959 /* Hm, it seems as if in some CDATA cases expat doesn't call the handler... * /
960 if (rssc->Current == NULL)
961 NewStrBufDupAppendFlush(&ri->description, rssc->CData, NULL, 0);
964 void rss_xml_chardata(void *data, const XML_Char *s, int len)
966 rsscollection *rssc = (rsscollection*) data;
968 StrBufAppendBufPlain (rssc->CData, s, len, 0);
972 * Callback function for passing libcurl's output to expat for parsing
974 size_t rss_libcurl_callback(void *ptr, size_t size, size_t nmemb, void *stream)
976 XML_Parse((XML_Parser)stream, ptr, (size * nmemb), 0);
985 void rss_do_fetching(rssnetcfg *Cfg) {
992 char errmsg[1024] = "";
994 memset(&ri, 0, sizeof(rss_item));
998 CtdlLogPrintf(CTDL_DEBUG, "Fetching RSS feed <%s>\n", Cfg->url);
1000 curl = curl_easy_init();
1002 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
1006 xp = XML_ParserCreateNS("UTF-8", ':');
1008 CtdlLogPrintf(CTDL_ALERT, "Cannot create XML parser!\n");
1009 curl_easy_cleanup(curl);
1012 rssc.CData = NewStrBufPlain(NULL, SIZ);
1013 rssc.Key = NewStrBuf();
1014 curl_easy_setopt(curl, CURLOPT_URL, Cfg->url);
1015 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
1016 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
1017 curl_easy_setopt(curl, CURLOPT_WRITEDATA, xp);
1018 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rss_libcurl_callback);
1019 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
1020 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
1021 #ifdef CURLOPT_HTTP_CONTENT_DECODING
1022 curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
1023 curl_easy_setopt(curl, CURLOPT_ENCODING, "");
1025 curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
1026 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180); /* die after 180 seconds */
1028 (!IsEmptyStr(config.c_ip_addr))
1029 && (strcmp(config.c_ip_addr, "*"))
1030 && (strcmp(config.c_ip_addr, "::"))
1031 && (strcmp(config.c_ip_addr, "0.0.0.0"))
1033 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
1036 memset(&ri, 0, sizeof(rss_item));
1037 ri.roomlist = Cfg->rooms;
1040 XML_SetUnknownEncodingHandler(xp,
1041 handle_unknown_xml_encoding,
1045 XML_SetElementHandler(xp, rss_xml_start, rss_xml_end);
1046 XML_SetCharacterDataHandler(xp, rss_xml_chardata);
1047 XML_SetUserData(xp, &rssc);
1048 XML_SetCdataSectionHandler(xp,
1049 rss_xml_cdata_start,
1051 if (CtdlThreadCheckStop())
1054 curl_easy_cleanup(curl);
1058 if (CtdlThreadCheckStop())
1061 res = curl_easy_perform(curl);
1063 CtdlLogPrintf(CTDL_ALERT, "libcurl error %d: %s\n", res, errmsg);
1066 if (CtdlThreadCheckStop())
1069 if (ri.done_parsing == 0)
1070 XML_Parse(xp, "", 0, 1);
1073 CtdlLogPrintf(CTDL_ALERT, "RSS: XML Status [%s] \n",
1075 XML_GetErrorCode(xp)));
1078 curl_easy_cleanup(curl);
1081 flush_rss_item(&ri);
1082 FreeStrBuf(&rssc.CData);
1083 FreeStrBuf(&rssc.Key);
1088 * Scan a room's netconfig to determine whether it is requesting any RSS feeds
1090 void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
1092 char filename[PATH_MAX];
1097 rssnetcfg *rncptr = NULL;
1098 rssnetcfg *use_this_rncptr = NULL;
1102 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1104 if (CtdlThreadCheckStop())
1107 /* Only do net processing for rooms that have netconfigs */
1108 fp = fopen(filename, "r");
1113 while (fgets(buf, sizeof buf, fp) != NULL && !CtdlThreadCheckStop()) {
1114 buf[strlen(buf)-1] = 0;
1116 extract_token(instr, buf, 0, '|', sizeof instr);
1117 if (!strcasecmp(instr, "rssclient")) {
1119 use_this_rncptr = NULL;
1121 extract_token(feedurl, buf, 1, '|', sizeof feedurl);
1123 /* If any other rooms have requested the same feed, then we will just add this
1124 * room to the target list for that client request.
1126 for (rncptr=rnclist; rncptr!=NULL; rncptr=rncptr->next) {
1127 if (!strcmp(rncptr->url, feedurl)) {
1128 use_this_rncptr = rncptr;
1132 /* Otherwise create a new client request */
1133 if (use_this_rncptr == NULL) {
1134 rncptr = (rssnetcfg *) malloc(sizeof(rssnetcfg));
1135 rncptr->ItemType = RSS_UNSET;
1136 if (rncptr != NULL) {
1137 rncptr->next = rnclist;
1138 safestrncpy(rncptr->url, feedurl, sizeof rncptr->url);
1139 rncptr->rooms = NULL;
1141 use_this_rncptr = rncptr;
1145 /* Add the room name to the request */
1146 if (use_this_rncptr != NULL) {
1147 if (use_this_rncptr->rooms == NULL) {
1148 rncptr->rooms = strdup(qrbuf->QRname);
1151 len = strlen(use_this_rncptr->rooms) + strlen(qrbuf->QRname) + 5;
1152 ptr = realloc(use_this_rncptr->rooms, len);
1155 strcat(ptr, qrbuf->QRname);
1156 use_this_rncptr->rooms = ptr;
1169 * Scan for rooms that have RSS client requests configured
1171 void *rssclient_scan(void *args) {
1172 static time_t last_run = 0L;
1173 static int doing_rssclient = 0;
1174 rssnetcfg *rptr = NULL;
1175 CitContext rssclientCC;
1177 /* Give this thread its own private CitContext */
1178 CtdlFillSystemContext(&rssclientCC, "rssclient");
1179 citthread_setspecific(MyConKey, (void *)&rssclientCC );
1182 * This is a simple concurrency check to make sure only one rssclient run
1183 * is done at a time. We could do this with a mutex, but since we
1184 * don't really require extremely fine granularity here, we'll do it
1185 * with a static variable instead.
1187 if (doing_rssclient) return NULL;
1188 doing_rssclient = 1;
1190 CtdlLogPrintf(CTDL_DEBUG, "rssclient started\n");
1191 CtdlForEachRoom(rssclient_scan_room, NULL);
1193 while (rnclist != NULL && !CtdlThreadCheckStop()) {
1194 rss_do_fetching(rnclist);
1196 rnclist = rnclist->next;
1197 if (rptr->rooms != NULL) free(rptr->rooms);
1201 CtdlLogPrintf(CTDL_DEBUG, "rssclient ended\n");
1202 last_run = time(NULL);
1203 doing_rssclient = 0;
1204 if (!CtdlThreadCheckStop())
1205 CtdlThreadSchedule ("RSS Client", CTDLTHREAD_BIGSTACK, rssclient_scan, NULL, last_run + config.c_net_freq);
1207 CtdlLogPrintf(CTDL_DEBUG, "rssclient: Task STOPPED.\n");
1208 CtdlClearSystemContext();
1213 CTDL_MODULE_INIT(rssclient)
1217 CtdlLogPrintf(CTDL_INFO, "%s\n", curl_version());
1218 CtdlThreadSchedule ("RSS Client", CTDLTHREAD_BIGSTACK, rssclient_scan, NULL, 0);
1221 StartHandlers = NewHash(1, NULL);
1222 EndHandlers = NewHash(1, NULL);
1224 AddRSSStartHandler(RSS_item_rss_start, RSS_UNSET, HKEY("rss"));
1225 AddRSSStartHandler(RSS_item_rdf_start, RSS_UNSET, HKEY("rdf"));
1226 AddRSSStartHandler(ATOM_item_feed_start, RSS_UNSET, HKEY("feed"));
1227 AddRSSStartHandler(RSS_item_item_start, RSS_RSS, HKEY("item"));
1228 AddRSSStartHandler(ATOM_item_entry_start, RSS_ATOM, HKEY("entry"));
1229 AddRSSStartHandler(ATOM_item_link_start, RSS_ATOM, HKEY("link"));
1231 AddRSSEndHandler(ATOMRSS_item_title_end, RSS_ATOM|RSS_RSS|RSS_REQUIRE_BUF, HKEY("title"));
1232 AddRSSEndHandler(RSS_item_guid_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("guid"));
1233 AddRSSEndHandler(ATOM_item_id_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("id"));
1234 AddRSSEndHandler(RSS_item_link_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("link"));
1235 AddRSSEndHandler(RSSATOM_item_title_end, RSS_ATOM|RSS_RSS|RSS_REQUIRE_BUF, HKEY("title"));
1236 AddRSSEndHandler(ATOM_item_content_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("content"));
1237 AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("encoded"));
1238 AddRSSEndHandler(ATOM_item_summary_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("summary"));
1239 AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("description"));
1240 AddRSSEndHandler(ATOM_item_published_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("published"));
1241 AddRSSEndHandler(ATOM_item_updated_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("updated"));
1242 AddRSSEndHandler(RSS_item_pubdate_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("pubdate"));
1243 AddRSSEndHandler(RSS_item_date_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("date"));
1244 AddRSSEndHandler(RSS_item_author_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("author"));
1245 AddRSSEndHandler(RSS_item_creator_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("creator"));
1246 AddRSSEndHandler(ATOM_item_name_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("name"));
1247 AddRSSEndHandler(ATOM_item_uri_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("uri"));
1249 AddRSSEndHandler(RSS_item_item_end, RSS_RSS, HKEY("item"));
1250 AddRSSEndHandler(RSS_item_rss_end, RSS_RSS, HKEY("rss"));
1251 AddRSSEndHandler(RSS_item_rdf_end, RSS_RSS, HKEY("rdf"));
1252 AddRSSEndHandler(ATOM_item_entry_end, RSS_ATOM, HKEY("entry"));
1255 /* at the start of atoms: <seq> <li>link to resource</li></seq> ignore them. */
1256 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("seq"));
1257 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("seq"));
1258 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("li"));
1259 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("li"));
1261 /* links to other feed generators... */
1262 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("feedflare"));
1263 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("feedflare"));
1264 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("browserfriendly"));
1265 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("browserfriendly"));
1267 KnownNameSpaces = NewHash(1, NULL);
1268 Put(KnownNameSpaces, HKEY("http://a9.com/-/spec/opensearch/1.1/"), NULL, reference_free_handler);
1269 Put(KnownNameSpaces, HKEY("http://a9.com/-/spec/opensearchrss/1.0/"), NULL, reference_free_handler);
1270 Put(KnownNameSpaces, HKEY("http://backend.userland.com/creativeCommonsRssModule"), NULL, reference_free_handler);
1271 Put(KnownNameSpaces, HKEY("http://purl.org/atom/ns#"), NULL, reference_free_handler);
1272 Put(KnownNameSpaces, HKEY("http://purl.org/dc/elements/1.1/"), NULL, reference_free_handler);
1273 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/"), NULL, reference_free_handler);
1274 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/content/"), NULL, reference_free_handler);
1275 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/slash/"), NULL, reference_free_handler);
1276 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/syndication/"), NULL, reference_free_handler);
1277 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/"), NULL, reference_free_handler);
1278 Put(KnownNameSpaces, HKEY("http://purl.org/syndication/thread/1.0"), NULL, reference_free_handler);
1279 Put(KnownNameSpaces, HKEY("http://rssnamespace.org/feedburner/ext/1.0"), NULL, reference_free_handler);
1280 Put(KnownNameSpaces, HKEY("http://schemas.google.com/g/2005"), NULL, reference_free_handler);
1281 Put(KnownNameSpaces, HKEY("http://webns.net/mvcb/"), NULL, reference_free_handler);
1282 Put(KnownNameSpaces, HKEY("http://web.resource.org/cc/"), NULL, reference_free_handler);
1283 Put(KnownNameSpaces, HKEY("http://wellformedweb.org/CommentAPI/"), NULL, reference_free_handler);
1284 Put(KnownNameSpaces, HKEY("http://www.georss.org/georss"), NULL, reference_free_handler);
1285 Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/xhtml"), NULL, reference_free_handler);
1286 Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), NULL, reference_free_handler);
1287 Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), NULL, reference_free_handler);
1288 Put(KnownNameSpaces, HKEY("http://www.w3.org/2003/01/geo/wgs84_pos#"), NULL, reference_free_handler);
1289 Put(KnownNameSpaces, HKEY("http://www.w3.org/2005/Atom"), NULL, reference_free_handler);
1290 Put(KnownNameSpaces, HKEY("urn:flickr:"), NULL, reference_free_handler);
1292 /* we don't like these namespaces because of they shadow our usefull parameters. */
1293 Put(KnownNameSpaces, HKEY("http://search.yahoo.com/mrss/"), NULL, reference_free_handler);