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,
554 CtdlLogPrintf(0, "RSS: START ignoring because of wrong namespace [%s] = [%s]\n",
561 StrBufPlain(rssc->Key, pel, -1);
562 StrBufLowerCase(rssc->Key);
563 if (GetHash(StartHandlers, SKEY(rssc->Key), &pv))
565 rssc->Current = h = (rss_xml_handler*) pv;
567 if (((h->Flags & RSS_UNSET) != 0) &&
568 (Cfg->ItemType == RSS_UNSET))
570 h->Handler(rssc->CData, ri, Cfg, attr);
572 else if (((h->Flags & RSS_RSS) != 0) &&
573 (Cfg->ItemType == RSS_RSS))
575 h->Handler(rssc->CData, ri, Cfg, attr);
577 else if (((h->Flags & RSS_ATOM) != 0) &&
578 (Cfg->ItemType == RSS_ATOM))
580 h->Handler(rssc->CData, ri, Cfg, attr);
584 CtdlLogPrintf(0, "RSS: START unhandled: [%s] [%s]...\n", pel, supplied_el);
589 CtdlLogPrintf(0, "RSS: START unhandled: [%s] [%s]...\n", pel, supplied_el);
593 void rss_xml_end(void *data, const char *supplied_el)
596 rsscollection *rssc = (rsscollection*) data;
597 rssnetcfg *Cfg = rssc->Cfg;
598 rss_item *ri = rssc->Item;
603 /* Axe the namespace, we don't care about it */
605 while (sep = strchr(pel, ':'), sep) {
608 // CtdlLogPrintf(0, "RSS: END %s...\n", el);
609 if (pel != supplied_el)
613 if (!GetHash(KnownNameSpaces,
615 pel - supplied_el - 1,
619 CtdlLogPrintf(0, "RSS: END ignoring because of wrong namespace [%s] = [%s]\n",
620 supplied_el, ChrPtr(rssc->CData));
622 FlushStrBuf(rssc->CData);
627 StrBufPlain(rssc->Key, pel, -1);
628 StrBufLowerCase(rssc->Key);
629 if (GetHash(EndHandlers, SKEY(rssc->Key), &pv))
631 h = (rss_xml_handler*) pv;
633 if (((h->Flags & RSS_UNSET) != 0) &&
634 (Cfg->ItemType == RSS_UNSET))
636 h->Handler(rssc->CData, ri, Cfg, NULL);
638 else if (((h->Flags & RSS_RSS) != 0) &&
639 (Cfg->ItemType == RSS_RSS))
641 h->Handler(rssc->CData, ri, Cfg, NULL);
643 else if (((h->Flags & RSS_ATOM) != 0) &&
644 (Cfg->ItemType == RSS_ATOM))
646 h->Handler(rssc->CData, ri, Cfg, NULL);
650 CtdlLogPrintf(0, "RSS: END unhandled: [%s] [%s] = [%s]...\n", pel, supplied_el, ChrPtr(rssc->CData));
655 CtdlLogPrintf(0, "RSS: END unhandled: [%s] [%s] = [%s]...\n", pel, supplied_el, ChrPtr(rssc->CData));
657 FlushStrBuf(rssc->CData);
658 rssc->Current = NULL;
665 void RSS_item_rss_start (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
667 CtdlLogPrintf(CTDL_DEBUG, "RSS: This is an RSS feed.\n");
668 Cfg->ItemType = RSS_RSS;
671 void RSS_item_rdf_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
673 CtdlLogPrintf(CTDL_DEBUG, "RSS: This is an RDF feed.\n");
674 Cfg->ItemType = RSS_RSS;
677 void ATOM_item_feed_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
679 CtdlLogPrintf(CTDL_DEBUG, "RSS: This is an ATOM feed.\n");
680 Cfg->ItemType = RSS_ATOM;
684 void RSS_item_item_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
686 ri->item_tag_nesting ++;
690 void ATOM_item_entry_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
693 ri->item_tag_nesting ++;
697 void ATOM_item_link_start (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
700 const char *pHref = NULL;
701 const char *pType = NULL;
702 const char *pRel = NULL;
703 const char *pTitle = NULL;
705 for (i = 0; Attr[i] != NULL; i+=2)
707 if (!strcmp(Attr[i], "href"))
711 else if (!strcmp(Attr[i], "rel"))
715 else if (!strcmp(Attr[i], "type"))
719 else if (!strcmp(Attr[i], "title"))
725 return; /* WHUT? Pointing... where? */
726 if ((pType != NULL) && !strcasecmp(pType, "application/atom+xml"))
727 return; /* these just point to other rss resources, we're not interested in them. */
730 if (!strcasecmp (pRel, "replies"))
732 NewStrBufDupAppendFlush(&ri->reLink, NULL, pHref, -1);
733 StrBufTrim(ri->link);
734 NewStrBufDupAppendFlush(&ri->reLinkTitle, NULL, pTitle, -1);
736 else if (!strcasecmp(pRel, "alternate")) /* Alternative representation of this Item... */
738 NewStrBufDupAppendFlush(&ri->link, NULL, pHref, -1);
739 StrBufTrim(ri->link);
740 NewStrBufDupAppendFlush(&ri->linkTitle, NULL, pTitle, -1);
743 #if 0 /* these are also defined, but dunno what to do with them.. */
744 else if (!strcasecmp(pRel, "related"))
747 else if (!strcasecmp(pRel, "self"))
750 else if (!strcasecmp(pRel, "enclosure"))
751 {/* this reference can get big, and is probably the full article... */
753 else if (!strcasecmp(pRel, "via"))
754 {/* this article was provided via... */
758 else if (StrLength(ri->link) == 0)
760 NewStrBufDupAppendFlush(&ri->link, NULL, pHref, -1);
761 StrBufTrim(ri->link);
762 NewStrBufDupAppendFlush(&ri->linkTitle, NULL, pTitle, -1);
769 void ATOMRSS_item_title_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
771 if ((ri->item_tag_nesting == 0) && (StrLength(CData) > 0)) {
772 NewStrBufDupAppendFlush(&ri->channel_title, CData, NULL, 0);
773 StrBufTrim(ri->channel_title);
777 void RSS_item_guid_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
779 if (StrLength(CData) > 0) {
780 NewStrBufDupAppendFlush(&ri->guid, CData, NULL, 0);
784 void ATOM_item_id_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
786 if (StrLength(CData) > 0) {
787 NewStrBufDupAppendFlush(&ri->guid, CData, NULL, 0);
792 void RSS_item_link_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
794 if (StrLength(CData) > 0) {
795 NewStrBufDupAppendFlush(&ri->link, CData, NULL, 0);
796 StrBufTrim(ri->link);
800 void RSSATOM_item_title_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
802 if (StrLength(CData) > 0) {
803 NewStrBufDupAppendFlush(&ri->title, CData, NULL, 0);
804 StrBufTrim(ri->title);
808 void ATOM_item_content_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
810 if (StrLength(CData) > 0) {
811 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
812 StrBufTrim(ri->description);
815 void ATOM_item_summary_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
817 /* this can contain an abstract of the article. but we don't want to verwrite a full document if we already have it. */
818 if ((StrLength(CData) > 0) && (StrLength(ri->description) == 0))
820 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
821 StrBufTrim(ri->description);
825 void RSS_item_description_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
827 if (StrLength(CData) > 0) {
828 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
832 void ATOM_item_published_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
834 if (StrLength(CData) > 0) {
836 ri->pubdate = rdf_parsedate(ChrPtr(CData));
840 void ATOM_item_updated_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
842 if (StrLength(CData) > 0) {
844 ri->pubdate = rdf_parsedate(ChrPtr(CData));
848 void RSS_item_pubdate_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
850 if (StrLength(CData) > 0) {
852 ri->pubdate = rdf_parsedate(ChrPtr(CData));
857 void RSS_item_date_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
859 if (StrLength(CData) > 0) {
861 ri->pubdate = rdf_parsedate(ChrPtr(CData));
867 void RSS_item_author_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
869 if (StrLength(CData) > 0) {
870 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
871 StrBufTrim(ri->author_or_creator);
876 void ATOM_item_name_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
878 if (StrLength(CData) > 0) {
879 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
880 StrBufTrim(ri->author_or_creator);
884 void RSS_item_creator_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
886 if ((StrLength(CData) > 0) &&
887 (StrLength(ri->author_or_creator) == 0))
889 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
890 StrBufTrim(ri->author_or_creator);
895 void ATOM_item_uri_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
897 if (StrLength(CData) > 0) {
898 NewStrBufDupAppendFlush(&ri->author_url, CData, NULL, 0);
899 StrBufTrim(ri->author_url);
903 void RSS_item_item_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
905 --ri->item_tag_nesting;
910 void ATOM_item_entry_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
912 --ri->item_tag_nesting;
916 void RSS_item_rss_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
918 // CtdlLogPrintf(CTDL_DEBUG, "End of feed detected. Closing parser.\n");
919 ri->done_parsing = 1;
922 void RSS_item_rdf_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
924 // CtdlLogPrintf(CTDL_DEBUG, "End of feed detected. Closing parser.\n");
925 ri->done_parsing = 1;
929 void RSSATOM_item_ignore(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
936 * This callback stores up the data which appears in between tags.
938 void rss_xml_cdata_start(void *data)
940 rsscollection *rssc = (rsscollection*) data;
942 FlushStrBuf(rssc->CData);
945 void rss_xml_cdata_end(void *data)
948 void rss_xml_chardata(void *data, const XML_Char *s, int len)
950 rsscollection *rssc = (rsscollection*) data;
952 StrBufAppendBufPlain (rssc->CData, s, len, 0);
956 * Callback function for passing libcurl's output to expat for parsing
958 size_t rss_libcurl_callback(void *ptr, size_t size, size_t nmemb, void *stream)
960 XML_Parse((XML_Parser)stream, ptr, (size * nmemb), 0);
969 void rss_do_fetching(rssnetcfg *Cfg) {
977 char errmsg[1024] = "";
982 memset(&ri, 0, sizeof(rss_item));
986 CtdlLogPrintf(CTDL_DEBUG, "Fetching RSS feed <%s>\n", Cfg->url);
988 curl = curl_easy_init();
990 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
993 Answer = NewStrBufPlain(NULL, SIZ);
995 curl_easy_setopt(curl, CURLOPT_URL, Cfg->url);
996 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
997 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
998 curl_easy_setopt(curl, CURLOPT_WRITEDATA, Answer);
999 // curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rss_libcurl_callback);
1000 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
1001 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
1002 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
1003 #ifdef CURLOPT_HTTP_CONTENT_DECODING
1004 curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
1005 curl_easy_setopt(curl, CURLOPT_ENCODING, "");
1007 curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
1008 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180); /* die after 180 seconds */
1010 (!IsEmptyStr(config.c_ip_addr))
1011 && (strcmp(config.c_ip_addr, "*"))
1012 && (strcmp(config.c_ip_addr, "::"))
1013 && (strcmp(config.c_ip_addr, "0.0.0.0"))
1015 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
1018 if (CtdlThreadCheckStop())
1020 curl_easy_cleanup(curl);
1024 if (CtdlThreadCheckStop())
1027 res = curl_easy_perform(curl);
1029 CtdlLogPrintf(CTDL_ALERT, "libcurl error %d: %s\n", res, errmsg);
1032 if (CtdlThreadCheckStop())
1038 memset(&ri, 0, sizeof(rss_item));
1039 ri.roomlist = Cfg->rooms;
1040 rssc.CData = NewStrBufPlain(NULL, SIZ);
1041 rssc.Key = NewStrBuf();
1043 StrBufSipLine(rssc.Key, Answer, &at);
1046 #define encoding "encoding=\""
1047 ptr = strstr(ChrPtr(rssc.Key), encoding);
1052 ptr += sizeof (encoding) - 1;
1053 pche = strchr(ptr, '"');
1055 StrBufCutAt(rssc.Key, -1, pche);
1063 xp = XML_ParserCreateNS(ptr, ':');
1065 CtdlLogPrintf(CTDL_ALERT, "Cannot create XML parser!\n");
1068 FlushStrBuf(rssc.Key);
1071 XML_SetUnknownEncodingHandler(xp,
1072 handle_unknown_xml_encoding,
1076 XML_SetElementHandler(xp, rss_xml_start, rss_xml_end);
1077 XML_SetCharacterDataHandler(xp, rss_xml_chardata);
1078 XML_SetUserData(xp, &rssc);
1079 XML_SetCdataSectionHandler(xp,
1080 rss_xml_cdata_start,
1084 len = StrLength(Answer);
1085 ptr = SmashStrBuf(&Answer);
1086 XML_Parse(xp, ptr, len, 0);
1088 if (ri.done_parsing == 0)
1089 XML_Parse(xp, "", 0, 1);
1092 CtdlLogPrintf(CTDL_ALERT, "RSS: XML Status [%s] \n",
1094 XML_GetErrorCode(xp)));
1097 curl_easy_cleanup(curl);
1100 flush_rss_item(&ri);
1101 FreeStrBuf(&rssc.CData);
1102 FreeStrBuf(&rssc.Key);
1107 * Scan a room's netconfig to determine whether it is requesting any RSS feeds
1109 void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
1111 char filename[PATH_MAX];
1116 rssnetcfg *rncptr = NULL;
1117 rssnetcfg *use_this_rncptr = NULL;
1121 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1123 if (CtdlThreadCheckStop())
1126 /* Only do net processing for rooms that have netconfigs */
1127 fp = fopen(filename, "r");
1132 while (fgets(buf, sizeof buf, fp) != NULL && !CtdlThreadCheckStop()) {
1133 buf[strlen(buf)-1] = 0;
1135 extract_token(instr, buf, 0, '|', sizeof instr);
1136 if (!strcasecmp(instr, "rssclient")) {
1138 use_this_rncptr = NULL;
1140 extract_token(feedurl, buf, 1, '|', sizeof feedurl);
1142 /* If any other rooms have requested the same feed, then we will just add this
1143 * room to the target list for that client request.
1145 for (rncptr=rnclist; rncptr!=NULL; rncptr=rncptr->next) {
1146 if (!strcmp(rncptr->url, feedurl)) {
1147 use_this_rncptr = rncptr;
1151 /* Otherwise create a new client request */
1152 if (use_this_rncptr == NULL) {
1153 rncptr = (rssnetcfg *) malloc(sizeof(rssnetcfg));
1154 rncptr->ItemType = RSS_UNSET;
1155 if (rncptr != NULL) {
1156 rncptr->next = rnclist;
1157 safestrncpy(rncptr->url, feedurl, sizeof rncptr->url);
1158 rncptr->rooms = NULL;
1160 use_this_rncptr = rncptr;
1164 /* Add the room name to the request */
1165 if (use_this_rncptr != NULL) {
1166 if (use_this_rncptr->rooms == NULL) {
1167 rncptr->rooms = strdup(qrbuf->QRname);
1170 len = strlen(use_this_rncptr->rooms) + strlen(qrbuf->QRname) + 5;
1171 ptr = realloc(use_this_rncptr->rooms, len);
1174 strcat(ptr, qrbuf->QRname);
1175 use_this_rncptr->rooms = ptr;
1188 * Scan for rooms that have RSS client requests configured
1190 void *rssclient_scan(void *args) {
1191 static time_t last_run = 0L;
1192 static int doing_rssclient = 0;
1193 rssnetcfg *rptr = NULL;
1194 CitContext rssclientCC;
1196 /* Give this thread its own private CitContext */
1197 CtdlFillSystemContext(&rssclientCC, "rssclient");
1198 citthread_setspecific(MyConKey, (void *)&rssclientCC );
1201 * This is a simple concurrency check to make sure only one rssclient run
1202 * is done at a time. We could do this with a mutex, but since we
1203 * don't really require extremely fine granularity here, we'll do it
1204 * with a static variable instead.
1206 if (doing_rssclient) return NULL;
1207 doing_rssclient = 1;
1209 CtdlLogPrintf(CTDL_DEBUG, "rssclient started\n");
1210 CtdlForEachRoom(rssclient_scan_room, NULL);
1212 while (rnclist != NULL && !CtdlThreadCheckStop()) {
1213 rss_do_fetching(rnclist);
1215 rnclist = rnclist->next;
1216 if (rptr->rooms != NULL) free(rptr->rooms);
1220 CtdlLogPrintf(CTDL_DEBUG, "rssclient ended\n");
1221 last_run = time(NULL);
1222 doing_rssclient = 0;
1223 if (!CtdlThreadCheckStop())
1224 CtdlThreadSchedule ("RSS Client", CTDLTHREAD_BIGSTACK, rssclient_scan, NULL, last_run + config.c_net_freq);
1226 CtdlLogPrintf(CTDL_DEBUG, "rssclient: Task STOPPED.\n");
1227 CtdlClearSystemContext();
1232 CTDL_MODULE_INIT(rssclient)
1236 CtdlLogPrintf(CTDL_INFO, "%s\n", curl_version());
1237 CtdlThreadSchedule ("RSS Client", CTDLTHREAD_BIGSTACK, rssclient_scan, NULL, 0);
1240 StartHandlers = NewHash(1, NULL);
1241 EndHandlers = NewHash(1, NULL);
1243 AddRSSStartHandler(RSS_item_rss_start, RSS_UNSET, HKEY("rss"));
1244 AddRSSStartHandler(RSS_item_rdf_start, RSS_UNSET, HKEY("rdf"));
1245 AddRSSStartHandler(ATOM_item_feed_start, RSS_UNSET, HKEY("feed"));
1246 AddRSSStartHandler(RSS_item_item_start, RSS_RSS, HKEY("item"));
1247 AddRSSStartHandler(ATOM_item_entry_start, RSS_ATOM, HKEY("entry"));
1248 AddRSSStartHandler(ATOM_item_link_start, RSS_ATOM, HKEY("link"));
1250 AddRSSEndHandler(ATOMRSS_item_title_end, RSS_ATOM|RSS_RSS|RSS_REQUIRE_BUF, HKEY("title"));
1251 AddRSSEndHandler(RSS_item_guid_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("guid"));
1252 AddRSSEndHandler(ATOM_item_id_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("id"));
1253 AddRSSEndHandler(RSS_item_link_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("link"));
1254 AddRSSEndHandler(RSSATOM_item_title_end, RSS_ATOM|RSS_RSS|RSS_REQUIRE_BUF, HKEY("title"));
1255 AddRSSEndHandler(ATOM_item_content_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("content"));
1256 AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("encoded"));
1257 AddRSSEndHandler(ATOM_item_summary_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("summary"));
1258 AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("description"));
1259 AddRSSEndHandler(ATOM_item_published_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("published"));
1260 AddRSSEndHandler(ATOM_item_updated_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("updated"));
1261 AddRSSEndHandler(RSS_item_pubdate_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("pubdate"));
1262 AddRSSEndHandler(RSS_item_date_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("date"));
1263 AddRSSEndHandler(RSS_item_author_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("author"));
1264 AddRSSEndHandler(RSS_item_creator_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("creator"));
1265 AddRSSEndHandler(ATOM_item_name_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("name"));
1266 AddRSSEndHandler(ATOM_item_uri_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("uri"));
1268 AddRSSEndHandler(RSS_item_item_end, RSS_RSS, HKEY("item"));
1269 AddRSSEndHandler(RSS_item_rss_end, RSS_RSS, HKEY("rss"));
1270 AddRSSEndHandler(RSS_item_rdf_end, RSS_RSS, HKEY("rdf"));
1271 AddRSSEndHandler(ATOM_item_entry_end, RSS_ATOM, HKEY("entry"));
1274 /* at the start of atoms: <seq> <li>link to resource</li></seq> ignore them. */
1275 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("seq"));
1276 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("seq"));
1277 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("li"));
1278 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("li"));
1280 /* links to other feed generators... */
1281 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("feedflare"));
1282 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("feedflare"));
1283 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("browserfriendly"));
1284 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("browserfriendly"));
1286 KnownNameSpaces = NewHash(1, NULL);
1287 Put(KnownNameSpaces, HKEY("http://a9.com/-/spec/opensearch/1.1/"), NULL, reference_free_handler);
1288 Put(KnownNameSpaces, HKEY("http://a9.com/-/spec/opensearchrss/1.0/"), NULL, reference_free_handler);
1289 Put(KnownNameSpaces, HKEY("http://backend.userland.com/creativeCommonsRssModule"), NULL, reference_free_handler);
1290 Put(KnownNameSpaces, HKEY("http://purl.org/atom/ns#"), NULL, reference_free_handler);
1291 Put(KnownNameSpaces, HKEY("http://purl.org/dc/elements/1.1/"), NULL, reference_free_handler);
1292 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/"), NULL, reference_free_handler);
1293 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/content/"), NULL, reference_free_handler);
1294 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/slash/"), NULL, reference_free_handler);
1295 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/syndication/"), NULL, reference_free_handler);
1296 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/"), NULL, reference_free_handler);
1297 Put(KnownNameSpaces, HKEY("http://purl.org/syndication/thread/1.0"), NULL, reference_free_handler);
1298 Put(KnownNameSpaces, HKEY("http://rssnamespace.org/feedburner/ext/1.0"), NULL, reference_free_handler);
1299 Put(KnownNameSpaces, HKEY("http://schemas.google.com/g/2005"), NULL, reference_free_handler);
1300 Put(KnownNameSpaces, HKEY("http://webns.net/mvcb/"), NULL, reference_free_handler);
1301 Put(KnownNameSpaces, HKEY("http://web.resource.org/cc/"), NULL, reference_free_handler);
1302 Put(KnownNameSpaces, HKEY("http://wellformedweb.org/CommentAPI/"), NULL, reference_free_handler);
1303 Put(KnownNameSpaces, HKEY("http://www.georss.org/georss"), NULL, reference_free_handler);
1304 Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/xhtml"), NULL, reference_free_handler);
1305 Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), NULL, reference_free_handler);
1306 Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), NULL, reference_free_handler);
1307 Put(KnownNameSpaces, HKEY("http://www.w3.org/2003/01/geo/wgs84_pos#"), NULL, reference_free_handler);
1308 Put(KnownNameSpaces, HKEY("http://www.w3.org/2005/Atom"), NULL, reference_free_handler);
1309 Put(KnownNameSpaces, HKEY("urn:flickr:"), NULL, reference_free_handler);
1311 /* we don't like these namespaces because of they shadow our usefull parameters. */
1312 Put(KnownNameSpaces, HKEY("http://search.yahoo.com/mrss/"), NULL, reference_free_handler);