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;
92 typedef void (*rss_handler_func)(StrBuf *CData,
97 typedef struct __rss_xml_handler {
99 rss_handler_func Handler;
103 typedef struct _rsscollection {
110 rss_xml_handler *Current;
113 struct rssnetcfg *rnclist = NULL;
114 HashList *StartHandlers;
115 HashList *EndHandlers;
116 HashList *KnownNameSpaces;
117 void AddRSSStartHandler(rss_handler_func Handler, int Flags, const char *key, long len)
120 h = (rss_xml_handler*) malloc(sizeof (rss_xml_handler));
122 h->Handler = Handler;
123 Put(StartHandlers, key, len, h, NULL);
125 void AddRSSEndHandler(rss_handler_func Handler, int Flags, const char *key, long len)
128 h = (rss_xml_handler*) malloc(sizeof (rss_xml_handler));
130 h->Handler = Handler;
131 Put(EndHandlers, key, len, h, NULL);
140 * dug this out of the trashcan of the midgard project, lets see if it works for us.
141 * original code by Alexander Bokovoy <bokovoy@avilink.ne> distributed under GPL V2 or later
145 >= 0 - successfull, 0 means conversion doesn't use multibyte sequences
146 -1 - error during iconv_open call
147 -2 - error during iconv_close call
148 ----------------------------------
149 This function expects that multibyte encoding in 'charset' wouldn't have
150 characters with more than 3 bytes. It is not intended to convert UTF-8 because
151 we'll never receive UTF-8 in our handler (it is handled by Exat itself).
154 fill_encoding_info (const char *charset, XML_Encoding * info)
156 iconv_t cd = (iconv_t)(-1);
158 CtdlLogPrintf(0, "RSS: fill encoding info ...\n");
160 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
161 cd = iconv_open ("UCS-2LE", charset);
163 cd = iconv_open ("UCS-2BE", charset);
166 if (cd == (iconv_t) (-1))
173 unsigned char buf[4];
174 unsigned int i0, i1, i2;
177 for (i0 = 0; i0 < 0x100; i0++)
181 //result = try (cd, buf, 1, &out);
191 for (i1 = 0; i1 < 0x100; i1++)
194 ///result = try (cd, buf, 2, &out);
205 for (i2 = 0; i2 < 0x100; i2++)
208 ////result = try (cd, buf, 3, &out);
224 if (iconv_close (cd) < 0)
232 iconv_convertor (void *data, const char *s)
234 XML_Encoding *info = data;
236 CtdlLogPrintf(0, "RSS: Converting ...\n");
242 result = g_byte_array_new ();
243 if (process_block (info->data, (char *) s, strlen (s), result) == 0)
245 res = *(result->data);
246 g_byte_array_free (result, TRUE);
249 g_byte_array_free (result, TRUE);
255 my_release (void *data)
257 iconv_t cd = (iconv_t) data;
258 if (iconv_close (cd) != 0)
260 /// TODO: uh no. exit (1);
264 handle_unknown_xml_encoding (void *encodingHandleData,
265 const XML_Char * name,
269 CtdlLogPrintf(0, "RSS: unknown encoding ...\n");
270 result = fill_encoding_info (name, info);
274 Special case: client asked for reverse conversion, we'll provide him with
275 iconv descriptor which handles it. Client should release it by himself.
277 if(encodingHandleData != NULL)
278 *((iconv_t *)encodingHandleData) = iconv_open(name, "UTF-8");
280 Optimization: we do not need conversion function if encoding is one-to-one,
281 info->map table will be enough
286 info->convert = NULL;
287 info->release = NULL;
291 We do need conversion function because this encoding uses multibyte sequences
293 info->data = (void *) iconv_open ("UTF-8", name);
294 if ((int)info->data == -1)
296 info->convert = iconv_convertor;
297 info->release = my_release;
300 if(encodingHandleData != NULL)
301 *(iconv_t *)encodingHandleData = NULL;
308 void AppendLink(StrBuf *Message, StrBuf *link, StrBuf *LinkTitle, const char *Title)
310 if (StrLength(link) > 0)
312 StrBufAppendBufPlain(Message, HKEY("<a href=\""), 0);
313 StrBufAppendBuf(Message, link, 0);
314 StrBufAppendBufPlain(Message, HKEY("\">"), 0);
315 if (StrLength(LinkTitle) > 0)
316 StrBufAppendBuf(Message, LinkTitle, 0);
317 else if ((Title != NULL) && !IsEmptyStr(Title))
318 StrBufAppendBufPlain(Message, Title, -1, 0);
320 StrBufAppendBuf(Message, link, 0);
321 StrBufAppendBufPlain(Message, HKEY("</a><br>\n"), 0);
325 * Commit a fetched and parsed RSS item to disk
327 void rss_save_item(rss_item *ri)
330 struct MD5Context md5context;
331 u_char rawdigest[MD5_DIGEST_LEN];
334 struct cdbdata *cdbut;
336 struct CtdlMessage *msg;
337 struct recptypes *recp = NULL;
341 recp = (struct recptypes *) malloc(sizeof(struct recptypes));
342 if (recp == NULL) return;
343 memset(recp, 0, sizeof(struct recptypes));
344 memset(&ut, 0, sizeof(struct UseTable));
345 recp->recp_room = strdup(ri->roomlist);
346 recp->num_room = num_tokens(ri->roomlist, '|');
347 recp->recptypes_magic = RECPTYPES_MAGIC;
349 /* Construct a GUID to use in the S_USETABLE table.
350 * If one is not present in the item itself, make one up.
352 if (ri->guid != NULL) {
353 StrBufSpaceToBlank(ri->guid);
354 StrBufTrim(ri->guid);
355 snprintf(utmsgid, sizeof utmsgid, "rss/%s", ChrPtr(ri->guid));
358 MD5Init(&md5context);
359 if (ri->title != NULL) {
360 MD5Update(&md5context, (const unsigned char*)ChrPtr(ri->title), StrLength(ri->title));
362 if (ri->link != NULL) {
363 MD5Update(&md5context, (const unsigned char*)ChrPtr(ri->link), StrLength(ri->link));
365 MD5Final(rawdigest, &md5context);
366 for (i=0; i<MD5_DIGEST_LEN; i++) {
367 sprintf(&utmsgid[i*2], "%02X", (unsigned char) (rawdigest[i] & 0xff));
368 utmsgid[i*2] = tolower(utmsgid[i*2]);
369 utmsgid[(i*2)+1] = tolower(utmsgid[(i*2)+1]);
371 strcat(utmsgid, "_rss2ctdl");
374 /* Find out if we've already seen this item */
376 cdbut = cdb_fetch(CDB_USETABLE, utmsgid, strlen(utmsgid));
379 /* Item has already been seen */
380 CtdlLogPrintf(CTDL_DEBUG, "%s has already been seen\n", utmsgid);
383 /* rewrite the record anyway, to update the timestamp */
384 strcpy(ut.ut_msgid, utmsgid);
385 ut.ut_timestamp = time(NULL);
386 cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid), &ut, sizeof(struct UseTable) );
391 /* Item has not been seen, so save it. */
392 CtdlLogPrintf(CTDL_DEBUG, "RSS: saving item...\n");
393 if (ri->description == NULL) ri->description = NewStrBufPlain(HKEY(""));
394 StrBufSpaceToBlank(ri->description);
395 msg = malloc(sizeof(struct CtdlMessage));
396 memset(msg, 0, sizeof(struct CtdlMessage));
397 msg->cm_magic = CTDLMESSAGE_MAGIC;
398 msg->cm_anon_type = MES_NORMAL;
399 msg->cm_format_type = FMT_RFC822;
401 if (ri->guid != NULL) {
402 msg->cm_fields['E'] = strdup(ChrPtr(ri->guid));
405 if (ri->author_or_creator != NULL) {
407 StrBuf *Encoded, *QPEncoded;
409 StrBuf *EmailAddress;
414 UserName = NewStrBuf();
415 EmailAddress = NewStrBuf();
416 EncBuf = NewStrBuf();
418 StrBufTrim(ri->author_or_creator);
419 From = html_to_ascii(ChrPtr(ri->author_or_creator),
420 StrLength(ri->author_or_creator),
422 FromLen = strlen(From);
423 if (From[FromLen - 1] == '\n')
425 From[FromLen - 1] = '\0';
427 FromAt = strchr(From, '@') != NULL;
428 if (!FromAt && StrLength (ri->author_email) > 0)
430 Encoded = NewStrBuf();
431 if (!IsEmptyStr(From))
433 StrBufPrintf(Encoded,
436 ChrPtr(ri->author_email));
440 StrBufPrintf(Encoded,
442 ChrPtr(ri->author_email));
448 Encoded = NewStrBufPlain(From, -1);
451 Encoded = NewStrBuf();
452 StrBufPrintf(Encoded,
455 "rss@localhost"); /// TODO: get hostname?
460 QPEncoded = StrBufSanitizeEmailRecipientVector(Encoded, UserName, EmailAddress, EncBuf);
461 msg->cm_fields['A'] = SmashStrBuf(&QPEncoded);
463 FreeStrBuf(&Encoded);
464 FreeStrBuf(&UserName);
465 FreeStrBuf(&EmailAddress);
470 msg->cm_fields['A'] = strdup("rss");
473 msg->cm_fields['N'] = strdup(NODENAME);
474 if (ri->title != NULL) {
477 StrBuf *Encoded, *QPEncoded;
480 StrBufSpaceToBlank(ri->title);
481 len = StrLength(ri->title);
482 Sbj = html_to_ascii(ChrPtr(ri->title), len, 512, 0);
484 if (Sbj[len - 1] == '\n')
489 Encoded = NewStrBufPlain(Sbj, len);
493 StrBufRFC2047encode(&QPEncoded, Encoded);
495 msg->cm_fields['U'] = SmashStrBuf(&QPEncoded);
496 FreeStrBuf(&Encoded);
498 msg->cm_fields['T'] = malloc(64);
499 snprintf(msg->cm_fields['T'], 64, "%ld", ri->pubdate);
500 if (ri->channel_title != NULL) {
501 if (StrLength(ri->channel_title) > 0) {
502 msg->cm_fields['O'] = strdup(ChrPtr(ri->channel_title));
505 if (ri->link == NULL)
506 ri->link = NewStrBufPlain(HKEY(""));
507 msglen += 1024 + StrLength(ri->link) + StrLength(ri->description) ;
509 Message = NewStrBufPlain(NULL, StrLength(ri->description));
511 StrBufPlain(Message, HKEY(
512 "Content-type: text/html; charset=\"UTF-8\"\r\n\r\n"
515 StrBufAppendBuf(Message, ri->description, 0);
516 StrBufAppendBufPlain(Message, HKEY("<br><br>\n"), 0);
518 AppendLink(Message, ri->link, ri->linkTitle, NULL);
519 AppendLink(Message, ri->reLink, ri->reLinkTitle, "Reply to this");
520 StrBufAppendBufPlain(Message, HKEY("</body></html>\n"), 0);
522 msg->cm_fields['M'] = SmashStrBuf(&Message);
524 CtdlSubmitMsg(msg, recp, NULL, 0);
525 CtdlFreeMessage(msg);
527 /* write the uidl to the use table so we don't store this item again */
528 strcpy(ut.ut_msgid, utmsgid);
529 ut.ut_timestamp = time(NULL);
530 cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid), &ut, sizeof(struct UseTable) );
532 free_recipients(recp);
538 * Convert an RDF/RSS datestamp into a time_t
540 time_t rdf_parsedate(const char *p)
546 if (strlen(p) < 10) return 0L;
548 memset(&tm, 0, sizeof tm);
551 * If the timestamp appears to be in W3C datetime format, try to
552 * parse it. See also: http://www.w3.org/TR/NOTE-datetime
554 * This code, along with parsedate.c, is a potential candidate for
555 * moving into libcitadel.
557 if ( (p[4] == '-') && (p[7] == '-') ) {
558 tm.tm_year = atoi(&p[0]) - 1900;
559 tm.tm_mon = atoi(&p[5]) - 1;
560 tm.tm_mday = atoi(&p[8]);
561 if ( (p[10] == 'T') && (p[13] == ':') ) {
562 tm.tm_hour = atoi(&p[11]);
563 tm.tm_min = atoi(&p[14]);
568 /* hmm... try RFC822 date stamp format */
571 if (t > 0) return(t);
573 /* yeesh. ok, just return the current date and time. */
577 void flush_rss_item(rss_item *ri)
579 /* Initialize the feed item data structure */
580 FreeStrBuf(&ri->guid);
581 FreeStrBuf(&ri->title);
582 FreeStrBuf(&ri->link);
583 FreeStrBuf(&ri->author_or_creator);
584 FreeStrBuf(&ri->author_email);
585 FreeStrBuf(&ri->author_url);
586 FreeStrBuf(&ri->description);
589 void rss_xml_start(void *data, const char *supplied_el, const char **attr)
592 rsscollection *rssc = (rsscollection*) data;
593 rssnetcfg *Cfg = rssc->Cfg;
594 rss_item *ri = rssc->Item;
599 /* Axe the namespace, we don't care about it */
600 /// CtdlLogPrintf(0, "RSS: supplied el %d: %s...\n", rssc->Cfg->ItemType, supplied_el);
602 while (sep = strchr(pel, ':'), sep) {
606 if (pel != supplied_el)
610 if (!GetHash(KnownNameSpaces,
612 pel - supplied_el - 1,
616 CtdlLogPrintf(0, "RSS: START ignoring because of wrong namespace [%s] = [%s]\n",
623 StrBufPlain(rssc->Key, pel, -1);
624 StrBufLowerCase(rssc->Key);
625 if (GetHash(StartHandlers, SKEY(rssc->Key), &pv))
627 rssc->Current = h = (rss_xml_handler*) pv;
629 if (((h->Flags & RSS_UNSET) != 0) &&
630 (Cfg->ItemType == RSS_UNSET))
632 h->Handler(rssc->CData, ri, Cfg, attr);
634 else if (((h->Flags & RSS_RSS) != 0) &&
635 (Cfg->ItemType == RSS_RSS))
637 h->Handler(rssc->CData, ri, Cfg, attr);
639 else if (((h->Flags & RSS_ATOM) != 0) &&
640 (Cfg->ItemType == RSS_ATOM))
642 h->Handler(rssc->CData, ri, Cfg, attr);
646 CtdlLogPrintf(0, "RSS: START unhandled: [%s] [%s]...\n", pel, supplied_el);
651 CtdlLogPrintf(0, "RSS: START unhandled: [%s] [%s]...\n", pel, supplied_el);
655 void rss_xml_end(void *data, const char *supplied_el)
658 rsscollection *rssc = (rsscollection*) data;
659 rssnetcfg *Cfg = rssc->Cfg;
660 rss_item *ri = rssc->Item;
665 /* Axe the namespace, we don't care about it */
667 while (sep = strchr(pel, ':'), sep) {
670 // CtdlLogPrintf(0, "RSS: END %s...\n", el);
671 if (pel != supplied_el)
675 if (!GetHash(KnownNameSpaces,
677 pel - supplied_el - 1,
681 CtdlLogPrintf(0, "RSS: END ignoring because of wrong namespace [%s] = [%s]\n",
682 supplied_el, ChrPtr(rssc->CData));
684 FlushStrBuf(rssc->CData);
689 StrBufPlain(rssc->Key, pel, -1);
690 StrBufLowerCase(rssc->Key);
691 if (GetHash(EndHandlers, SKEY(rssc->Key), &pv))
693 h = (rss_xml_handler*) pv;
695 if (((h->Flags & RSS_UNSET) != 0) &&
696 (Cfg->ItemType == RSS_UNSET))
698 h->Handler(rssc->CData, ri, Cfg, NULL);
700 else if (((h->Flags & RSS_RSS) != 0) &&
701 (Cfg->ItemType == RSS_RSS))
703 h->Handler(rssc->CData, ri, Cfg, NULL);
705 else if (((h->Flags & RSS_ATOM) != 0) &&
706 (Cfg->ItemType == RSS_ATOM))
708 h->Handler(rssc->CData, ri, Cfg, NULL);
712 CtdlLogPrintf(0, "RSS: END unhandled: [%s] [%s] = [%s]...\n", pel, supplied_el, ChrPtr(rssc->CData));
717 CtdlLogPrintf(0, "RSS: END unhandled: [%s] [%s] = [%s]...\n", pel, supplied_el, ChrPtr(rssc->CData));
719 FlushStrBuf(rssc->CData);
720 rssc->Current = NULL;
727 void RSS_item_rss_start (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
729 CtdlLogPrintf(CTDL_DEBUG, "RSS: This is an RSS feed.\n");
730 Cfg->ItemType = RSS_RSS;
733 void RSS_item_rdf_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
735 CtdlLogPrintf(CTDL_DEBUG, "RSS: This is an RDF feed.\n");
736 Cfg->ItemType = RSS_RSS;
739 void ATOM_item_feed_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
741 CtdlLogPrintf(CTDL_DEBUG, "RSS: This is an ATOM feed.\n");
742 Cfg->ItemType = RSS_ATOM;
746 void RSS_item_item_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
748 ri->item_tag_nesting ++;
752 void ATOM_item_entry_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
755 ri->item_tag_nesting ++;
759 void ATOM_item_link_start (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
762 const char *pHref = NULL;
763 const char *pType = NULL;
764 const char *pRel = NULL;
765 const char *pTitle = NULL;
767 for (i = 0; Attr[i] != NULL; i+=2)
769 if (!strcmp(Attr[i], "href"))
773 else if (!strcmp(Attr[i], "rel"))
777 else if (!strcmp(Attr[i], "type"))
781 else if (!strcmp(Attr[i], "title"))
787 return; /* WHUT? Pointing... where? */
788 if ((pType != NULL) && !strcasecmp(pType, "application/atom+xml"))
789 return; /* these just point to other rss resources, we're not interested in them. */
792 if (!strcasecmp (pRel, "replies"))
794 NewStrBufDupAppendFlush(&ri->reLink, NULL, pHref, -1);
795 StrBufTrim(ri->link);
796 NewStrBufDupAppendFlush(&ri->reLinkTitle, NULL, pTitle, -1);
798 else if (!strcasecmp(pRel, "alternate")) /* Alternative representation of this Item... */
800 NewStrBufDupAppendFlush(&ri->link, NULL, pHref, -1);
801 StrBufTrim(ri->link);
802 NewStrBufDupAppendFlush(&ri->linkTitle, NULL, pTitle, -1);
805 #if 0 /* these are also defined, but dunno what to do with them.. */
806 else if (!strcasecmp(pRel, "related"))
809 else if (!strcasecmp(pRel, "self"))
812 else if (!strcasecmp(pRel, "enclosure"))
813 {/* this reference can get big, and is probably the full article... */
815 else if (!strcasecmp(pRel, "via"))
816 {/* this article was provided via... */
820 else if (StrLength(ri->link) == 0)
822 NewStrBufDupAppendFlush(&ri->link, NULL, pHref, -1);
823 StrBufTrim(ri->link);
824 NewStrBufDupAppendFlush(&ri->linkTitle, NULL, pTitle, -1);
831 void ATOMRSS_item_title_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
833 if ((ri->item_tag_nesting == 0) && (StrLength(CData) > 0)) {
834 NewStrBufDupAppendFlush(&ri->channel_title, CData, NULL, 0);
835 StrBufTrim(ri->channel_title);
839 void RSS_item_guid_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
841 if (StrLength(CData) > 0) {
842 NewStrBufDupAppendFlush(&ri->guid, CData, NULL, 0);
846 void ATOM_item_id_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
848 if (StrLength(CData) > 0) {
849 NewStrBufDupAppendFlush(&ri->guid, CData, NULL, 0);
854 void RSS_item_link_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
856 if (StrLength(CData) > 0) {
857 NewStrBufDupAppendFlush(&ri->link, CData, NULL, 0);
858 StrBufTrim(ri->link);
861 void RSS_item_relink_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
863 if (StrLength(CData) > 0) {
864 NewStrBufDupAppendFlush(&ri->reLink, CData, NULL, 0);
865 StrBufTrim(ri->reLink);
869 void RSSATOM_item_title_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
871 if (StrLength(CData) > 0) {
872 NewStrBufDupAppendFlush(&ri->title, CData, NULL, 0);
873 StrBufTrim(ri->title);
877 void ATOM_item_content_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
879 long olen = StrLength (ri->description);
880 long clen = StrLength (CData);
884 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
885 StrBufTrim(ri->description);
887 else if (olen < clen) {
888 FlushStrBuf(ri->description);
889 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
890 StrBufTrim(ri->description);
894 void ATOM_item_summary_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
896 /* this can contain an abstract of the article. but we don't want to verwrite a full document if we already have it. */
897 if ((StrLength(CData) > 0) && (StrLength(ri->description) == 0))
899 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
900 StrBufTrim(ri->description);
904 void RSS_item_description_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
906 long olen = StrLength (ri->description);
907 long clen = StrLength (CData);
911 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
912 StrBufTrim(ri->description);
914 else if (olen < clen) {
915 FlushStrBuf(ri->description);
916 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
917 StrBufTrim(ri->description);
922 void ATOM_item_published_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
924 if (StrLength(CData) > 0) {
926 ri->pubdate = rdf_parsedate(ChrPtr(CData));
930 void ATOM_item_updated_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
932 if (StrLength(CData) > 0) {
934 ri->pubdate = rdf_parsedate(ChrPtr(CData));
938 void RSS_item_pubdate_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
940 if (StrLength(CData) > 0) {
942 ri->pubdate = rdf_parsedate(ChrPtr(CData));
947 void RSS_item_date_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
949 if (StrLength(CData) > 0) {
951 ri->pubdate = rdf_parsedate(ChrPtr(CData));
957 void RSS_item_author_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
959 if (StrLength(CData) > 0) {
960 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
961 StrBufTrim(ri->author_or_creator);
966 void ATOM_item_name_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
968 if (StrLength(CData) > 0) {
969 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
970 StrBufTrim(ri->author_or_creator);
974 void ATOM_item_email_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
976 if (StrLength(CData) > 0) {
977 NewStrBufDupAppendFlush(&ri->author_email, CData, NULL, 0);
978 StrBufTrim(ri->author_email);
982 void RSS_item_creator_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
984 if ((StrLength(CData) > 0) &&
985 (StrLength(ri->author_or_creator) == 0))
987 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
988 StrBufTrim(ri->author_or_creator);
993 void ATOM_item_uri_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
995 if (StrLength(CData) > 0) {
996 NewStrBufDupAppendFlush(&ri->author_url, CData, NULL, 0);
997 StrBufTrim(ri->author_url);
1001 void RSS_item_item_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
1003 --ri->item_tag_nesting;
1008 void ATOM_item_entry_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
1010 --ri->item_tag_nesting;
1014 void RSS_item_rss_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
1016 // CtdlLogPrintf(CTDL_DEBUG, "End of feed detected. Closing parser.\n");
1017 ri->done_parsing = 1;
1020 void RSS_item_rdf_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
1022 // CtdlLogPrintf(CTDL_DEBUG, "End of feed detected. Closing parser.\n");
1023 ri->done_parsing = 1;
1027 void RSSATOM_item_ignore(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
1034 * This callback stores up the data which appears in between tags.
1036 void rss_xml_cdata_start(void *data)
1038 rsscollection *rssc = (rsscollection*) data;
1040 FlushStrBuf(rssc->CData);
1043 void rss_xml_cdata_end(void *data)
1046 void rss_xml_chardata(void *data, const XML_Char *s, int len)
1048 rsscollection *rssc = (rsscollection*) data;
1050 StrBufAppendBufPlain (rssc->CData, s, len, 0);
1054 * Callback function for passing libcurl's output to expat for parsing
1056 size_t rss_libcurl_callback(void *ptr, size_t size, size_t nmemb, void *stream)
1058 XML_Parse((XML_Parser)stream, ptr, (size * nmemb), 0);
1059 return (size*nmemb);
1065 * Begin a feed parse
1067 void rss_do_fetching(rssnetcfg *Cfg) {
1075 char errmsg[1024] = "";
1080 memset(&ri, 0, sizeof(rss_item));
1084 CtdlLogPrintf(CTDL_DEBUG, "Fetching RSS feed <%s>\n", Cfg->url);
1086 curl = curl_easy_init();
1088 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
1091 Answer = NewStrBufPlain(NULL, SIZ);
1093 curl_easy_setopt(curl, CURLOPT_URL, Cfg->url);
1094 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
1095 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
1096 curl_easy_setopt(curl, CURLOPT_WRITEDATA, Answer);
1097 // curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rss_libcurl_callback);
1098 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
1099 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
1100 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
1101 #ifdef CURLOPT_HTTP_CONTENT_DECODING
1102 curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
1103 curl_easy_setopt(curl, CURLOPT_ENCODING, "");
1105 curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
1106 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180); /* die after 180 seconds */
1108 (!IsEmptyStr(config.c_ip_addr))
1109 && (strcmp(config.c_ip_addr, "*"))
1110 && (strcmp(config.c_ip_addr, "::"))
1111 && (strcmp(config.c_ip_addr, "0.0.0.0"))
1113 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
1116 if (CtdlThreadCheckStop())
1118 curl_easy_cleanup(curl);
1122 if (CtdlThreadCheckStop())
1125 res = curl_easy_perform(curl);
1127 CtdlLogPrintf(CTDL_ALERT, "libcurl error %d: %s\n", res, errmsg);
1130 if (CtdlThreadCheckStop())
1136 memset(&ri, 0, sizeof(rss_item));
1137 ri.roomlist = Cfg->rooms;
1138 rssc.CData = NewStrBufPlain(NULL, SIZ);
1139 rssc.Key = NewStrBuf();
1141 StrBufSipLine(rssc.Key, Answer, &at);
1144 #define encoding "encoding=\""
1145 ptr = strstr(ChrPtr(rssc.Key), encoding);
1150 ptr += sizeof (encoding) - 1;
1151 pche = strchr(ptr, '"');
1153 StrBufCutAt(rssc.Key, -1, pche);
1161 xp = XML_ParserCreateNS(ptr, ':');
1163 CtdlLogPrintf(CTDL_ALERT, "Cannot create XML parser!\n");
1166 FlushStrBuf(rssc.Key);
1169 XML_SetUnknownEncodingHandler(xp,
1170 handle_unknown_xml_encoding,
1174 XML_SetElementHandler(xp, rss_xml_start, rss_xml_end);
1175 XML_SetCharacterDataHandler(xp, rss_xml_chardata);
1176 XML_SetUserData(xp, &rssc);
1177 XML_SetCdataSectionHandler(xp,
1178 rss_xml_cdata_start,
1182 len = StrLength(Answer);
1183 ptr = SmashStrBuf(&Answer);
1184 XML_Parse(xp, ptr, len, 0);
1186 if (ri.done_parsing == 0)
1187 XML_Parse(xp, "", 0, 1);
1190 CtdlLogPrintf(CTDL_ALERT, "RSS: XML Status [%s] \n",
1192 XML_GetErrorCode(xp)));
1195 curl_easy_cleanup(curl);
1198 flush_rss_item(&ri);
1199 FreeStrBuf(&rssc.CData);
1200 FreeStrBuf(&rssc.Key);
1205 * Scan a room's netconfig to determine whether it is requesting any RSS feeds
1207 void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
1209 char filename[PATH_MAX];
1214 rssnetcfg *rncptr = NULL;
1215 rssnetcfg *use_this_rncptr = NULL;
1219 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1221 if (CtdlThreadCheckStop())
1224 /* Only do net processing for rooms that have netconfigs */
1225 fp = fopen(filename, "r");
1230 while (fgets(buf, sizeof buf, fp) != NULL && !CtdlThreadCheckStop()) {
1231 buf[strlen(buf)-1] = 0;
1233 extract_token(instr, buf, 0, '|', sizeof instr);
1234 if (!strcasecmp(instr, "rssclient")) {
1236 use_this_rncptr = NULL;
1238 extract_token(feedurl, buf, 1, '|', sizeof feedurl);
1240 /* If any other rooms have requested the same feed, then we will just add this
1241 * room to the target list for that client request.
1243 for (rncptr=rnclist; rncptr!=NULL; rncptr=rncptr->next) {
1244 if (!strcmp(rncptr->url, feedurl)) {
1245 use_this_rncptr = rncptr;
1249 /* Otherwise create a new client request */
1250 if (use_this_rncptr == NULL) {
1251 rncptr = (rssnetcfg *) malloc(sizeof(rssnetcfg));
1252 rncptr->ItemType = RSS_UNSET;
1253 if (rncptr != NULL) {
1254 rncptr->next = rnclist;
1255 safestrncpy(rncptr->url, feedurl, sizeof rncptr->url);
1256 rncptr->rooms = NULL;
1258 use_this_rncptr = rncptr;
1262 /* Add the room name to the request */
1263 if (use_this_rncptr != NULL) {
1264 if (use_this_rncptr->rooms == NULL) {
1265 rncptr->rooms = strdup(qrbuf->QRname);
1268 len = strlen(use_this_rncptr->rooms) + strlen(qrbuf->QRname) + 5;
1269 ptr = realloc(use_this_rncptr->rooms, len);
1272 strcat(ptr, qrbuf->QRname);
1273 use_this_rncptr->rooms = ptr;
1286 * Scan for rooms that have RSS client requests configured
1288 void rssclient_scan(void *args) {
1289 static time_t last_run = 0L;
1290 static int doing_rssclient = 0;
1291 rssnetcfg *rptr = NULL;
1294 * This is a simple concurrency check to make sure only one rssclient run
1295 * is done at a time. We could do this with a mutex, but since we
1296 * don't really require extremely fine granularity here, we'll do it
1297 * with a static variable instead.
1299 if (doing_rssclient) return NULL;
1300 doing_rssclient = 1;
1302 CtdlLogPrintf(CTDL_DEBUG, "rssclient started\n");
1303 CtdlForEachRoom(rssclient_scan_room, NULL);
1305 while (rnclist != NULL && !CtdlThreadCheckStop()) {
1306 rss_do_fetching(rnclist);
1308 rnclist = rnclist->next;
1309 if (rptr->rooms != NULL) free(rptr->rooms);
1313 CtdlLogPrintf(CTDL_DEBUG, "rssclient ended\n");
1314 last_run = time(NULL);
1315 doing_rssclient = 0;
1320 CTDL_MODULE_INIT(rssclient)
1324 CtdlLogPrintf(CTDL_INFO, "%s\n", curl_version());
1325 CtdlRegisterSessionHook(rssclient_scan, EVT_TIMER);
1328 StartHandlers = NewHash(1, NULL);
1329 EndHandlers = NewHash(1, NULL);
1331 AddRSSStartHandler(RSS_item_rss_start, RSS_UNSET, HKEY("rss"));
1332 AddRSSStartHandler(RSS_item_rdf_start, RSS_UNSET, HKEY("rdf"));
1333 AddRSSStartHandler(ATOM_item_feed_start, RSS_UNSET, HKEY("feed"));
1334 AddRSSStartHandler(RSS_item_item_start, RSS_RSS, HKEY("item"));
1335 AddRSSStartHandler(ATOM_item_entry_start, RSS_ATOM, HKEY("entry"));
1336 AddRSSStartHandler(ATOM_item_link_start, RSS_ATOM, HKEY("link"));
1338 AddRSSEndHandler(ATOMRSS_item_title_end, RSS_ATOM|RSS_RSS|RSS_REQUIRE_BUF, HKEY("title"));
1339 AddRSSEndHandler(RSS_item_guid_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("guid"));
1340 AddRSSEndHandler(ATOM_item_id_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("id"));
1341 AddRSSEndHandler(RSS_item_link_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("link"));
1343 // hm, rss to the comments of that blog, might be interesting in future, but...
1344 AddRSSEndHandler(RSS_item_relink_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("commentrss"));
1346 AddRSSEndHandler(RSS_item_relink_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("comments"));
1348 AddRSSEndHandler(RSSATOM_item_title_end, RSS_ATOM|RSS_RSS|RSS_REQUIRE_BUF, HKEY("title"));
1349 AddRSSEndHandler(ATOM_item_content_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("content"));
1350 AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_ATOM|RSS_REQUIRE_BUF, HKEY("encoded"));
1351 AddRSSEndHandler(ATOM_item_summary_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("summary"));
1352 AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("description"));
1353 AddRSSEndHandler(ATOM_item_published_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("published"));
1354 AddRSSEndHandler(ATOM_item_updated_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("updated"));
1355 AddRSSEndHandler(RSS_item_pubdate_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("pubdate"));
1356 AddRSSEndHandler(RSS_item_date_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("date"));
1357 AddRSSEndHandler(RSS_item_author_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("author"));
1358 AddRSSEndHandler(RSS_item_creator_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("creator"));
1360 AddRSSEndHandler(ATOM_item_email_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("email"));
1361 AddRSSEndHandler(ATOM_item_name_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("name"));
1362 AddRSSEndHandler(ATOM_item_uri_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("uri"));
1364 AddRSSEndHandler(RSS_item_item_end, RSS_RSS, HKEY("item"));
1365 AddRSSEndHandler(RSS_item_rss_end, RSS_RSS, HKEY("rss"));
1366 AddRSSEndHandler(RSS_item_rdf_end, RSS_RSS, HKEY("rdf"));
1367 AddRSSEndHandler(ATOM_item_entry_end, RSS_ATOM, HKEY("entry"));
1370 /* at the start of atoms: <seq> <li>link to resource</li></seq> ignore them. */
1371 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("seq"));
1372 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("seq"));
1373 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("li"));
1374 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("li"));
1376 /* links to other feed generators... */
1377 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("feedflare"));
1378 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("feedflare"));
1379 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("browserfriendly"));
1380 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("browserfriendly"));
1382 KnownNameSpaces = NewHash(1, NULL);
1383 Put(KnownNameSpaces, HKEY("http://a9.com/-/spec/opensearch/1.1/"), NULL, reference_free_handler);
1384 Put(KnownNameSpaces, HKEY("http://a9.com/-/spec/opensearchrss/1.0/"), NULL, reference_free_handler);
1385 Put(KnownNameSpaces, HKEY("http://backend.userland.com/creativeCommonsRssModule"), NULL, reference_free_handler);
1386 Put(KnownNameSpaces, HKEY("http://purl.org/atom/ns#"), NULL, reference_free_handler);
1387 Put(KnownNameSpaces, HKEY("http://purl.org/dc/elements/1.1/"), NULL, reference_free_handler);
1388 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/"), NULL, reference_free_handler);
1389 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/content/"), NULL, reference_free_handler);
1390 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/slash/"), NULL, reference_free_handler);
1391 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/syndication/"), NULL, reference_free_handler);
1392 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/"), NULL, reference_free_handler);
1393 Put(KnownNameSpaces, HKEY("http://purl.org/syndication/thread/1.0"), NULL, reference_free_handler);
1394 Put(KnownNameSpaces, HKEY("http://rssnamespace.org/feedburner/ext/1.0"), NULL, reference_free_handler);
1395 Put(KnownNameSpaces, HKEY("http://schemas.google.com/g/2005"), NULL, reference_free_handler);
1396 Put(KnownNameSpaces, HKEY("http://webns.net/mvcb/"), NULL, reference_free_handler);
1397 Put(KnownNameSpaces, HKEY("http://web.resource.org/cc/"), NULL, reference_free_handler);
1398 Put(KnownNameSpaces, HKEY("http://wellformedweb.org/CommentAPI/"), NULL, reference_free_handler);
1399 Put(KnownNameSpaces, HKEY("http://www.georss.org/georss"), NULL, reference_free_handler);
1400 Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/xhtml"), NULL, reference_free_handler);
1401 Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), NULL, reference_free_handler);
1402 Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), NULL, reference_free_handler);
1403 Put(KnownNameSpaces, HKEY("http://www.w3.org/2003/01/geo/wgs84_pos#"), NULL, reference_free_handler);
1404 Put(KnownNameSpaces, HKEY("http://www.w3.org/2005/Atom"), NULL, reference_free_handler);
1405 Put(KnownNameSpaces, HKEY("urn:flickr:"), NULL, reference_free_handler);
1407 /* we don't like these namespaces because of they shadow our usefull parameters. */
1408 Put(KnownNameSpaces, HKEY("http://search.yahoo.com/mrss/"), NULL, reference_free_handler);