2 * Bring external RSS feeds into rooms.
4 * Copyright (c) 2007-2012 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
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.
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"
58 #include "event_client.h"
59 #include "rss_atom_parser.h"
61 void rss_save_item(rss_item *ri, rss_aggregator *Cfg);
63 int RSSAtomParserDebugEnabled = 0;
65 #define N ((rss_aggregator*)IO->Data)->QRnumber
67 #define DBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (RSSAtomParserDebugEnabled != 0))
69 #define EVRSSATOM_syslog(LEVEL, FORMAT, ...) \
70 DBGLOG(LEVEL) syslog(LEVEL, \
71 "IO[%ld]CC[%d][%ld]RSSP" FORMAT, \
72 IO->ID, CCID, N, __VA_ARGS__)
74 #define EVRSSATOMM_syslog(LEVEL, FORMAT) \
75 DBGLOG(LEVEL) syslog(LEVEL, \
76 "IO[%ld]CC[%d][%ld]RSSP" FORMAT, \
79 #define EVRSSATOMCS_syslog(LEVEL, FORMAT, ...) \
80 DBGLOG(LEVEL) syslog(LEVEL, "IO[%ld][%ld]RSSP" FORMAT, \
81 IO->ID, N, __VA_ARGS__)
83 #define EVRSSATOMSM_syslog(LEVEL, FORMAT) \
84 DBGLOG(LEVEL) syslog(LEVEL, "IO[%ld][%ld]RSSP" FORMAT, \
88 * Convert an RDF/RSS datestamp into a time_t
90 time_t rdf_parsedate(const char *p)
96 if (strlen(p) < 10) return 0L;
98 memset(&tm, 0, sizeof tm);
101 * If the timestamp appears to be in W3C datetime format, try to
102 * parse it. See also: http://www.w3.org/TR/NOTE-datetime
104 * This code, along with parsedate.c, is a potential candidate for
105 * moving into libcitadel.
107 if ( (p[4] == '-') && (p[7] == '-') ) {
108 tm.tm_year = atoi(&p[0]) - 1900;
109 tm.tm_mon = atoi(&p[5]) - 1;
110 tm.tm_mday = atoi(&p[8]);
111 if ( (p[10] == 'T') && (p[13] == ':') ) {
112 tm.tm_hour = atoi(&p[11]);
113 tm.tm_min = atoi(&p[14]);
118 /* hmm... try RFC822 date stamp format */
121 if (t > 0) return(t);
123 /* yeesh. ok, just return the current date and time. */
127 void flush_rss_item(rss_item *ri)
129 /* Initialize the feed item data structure */
130 FreeStrBuf(&ri->guid);
131 FreeStrBuf(&ri->title);
132 FreeStrBuf(&ri->link);
133 FreeStrBuf(&ri->author_or_creator);
134 FreeStrBuf(&ri->author_email);
135 FreeStrBuf(&ri->author_url);
136 FreeStrBuf(&ri->description);
138 FreeStrBuf(&ri->linkTitle);
139 FreeStrBuf(&ri->reLink);
140 FreeStrBuf(&ri->reLinkTitle);
141 FreeStrBuf(&ri->channel_title);
145 /******************************************************************************
147 ******************************************************************************/
150 void RSS_item_rss_start (StrBuf *CData,
152 rss_aggregator *RSSAggr,
155 AsyncIO *IO = &RSSAggr->IO;
156 EVRSSATOMM_syslog(LOG_DEBUG, "RSS: This is an RSS feed.\n");
157 RSSAggr->ItemType = RSS_RSS;
160 void RSS_item_rdf_start(StrBuf *CData,
162 rss_aggregator *RSSAggr,
165 AsyncIO *IO = &RSSAggr->IO;
166 EVRSSATOMM_syslog(LOG_DEBUG, "RSS: This is an RDF feed.\n");
167 RSSAggr->ItemType = RSS_RSS;
170 void ATOM_item_feed_start(StrBuf *CData,
172 rss_aggregator *RSSAggr,
175 AsyncIO *IO = &RSSAggr->IO;
176 EVRSSATOMM_syslog(LOG_DEBUG, "RSS: This is an ATOM feed.\n");
177 RSSAggr->ItemType = RSS_ATOM;
181 void RSS_item_item_start(StrBuf *CData,
183 rss_aggregator *RSSAggr,
186 ri->item_tag_nesting ++;
190 void ATOM_item_entry_start(StrBuf *CData,
192 rss_aggregator *RSSAggr,
196 ri->item_tag_nesting ++;
200 void ATOM_item_link_start (StrBuf *CData,
202 rss_aggregator *RSSAggr,
206 const char *pHref = NULL;
207 const char *pType = NULL;
208 const char *pRel = NULL;
209 const char *pTitle = NULL;
211 for (i = 0; Attr[i] != NULL; i+=2)
213 if (!strcmp(Attr[i], "href"))
217 else if (!strcmp(Attr[i], "rel"))
221 else if (!strcmp(Attr[i], "type"))
225 else if (!strcmp(Attr[i], "title"))
231 return; /* WHUT? Pointing... where? */
232 if ((pType != NULL) && !strcasecmp(pType, "application/atom+xml"))
234 /* these just point to other rss resources,
235 we're not interested in them. */
238 if (!strcasecmp (pRel, "replies"))
240 NewStrBufDupAppendFlush(&ri->reLink, NULL, pHref, -1);
241 StrBufTrim(ri->link);
242 NewStrBufDupAppendFlush(&ri->reLinkTitle,
247 else if (!strcasecmp(pRel, "alternate"))
248 { /* Alternative representation of this Item... */
249 NewStrBufDupAppendFlush(&ri->link, NULL, pHref, -1);
250 StrBufTrim(ri->link);
251 NewStrBufDupAppendFlush(&ri->linkTitle,
257 #if 0 /* these are also defined, but dunno what to do with them.. */
258 else if (!strcasecmp(pRel, "related"))
261 else if (!strcasecmp(pRel, "self"))
264 else if (!strcasecmp(pRel, "enclosure"))
265 {/*...reference can get big, and is probably the full article*/
267 else if (!strcasecmp(pRel, "via"))
268 {/* this article was provided via... */
272 else if (StrLength(ri->link) == 0)
274 NewStrBufDupAppendFlush(&ri->link, NULL, pHref, -1);
275 StrBufTrim(ri->link);
276 NewStrBufDupAppendFlush(&ri->linkTitle, NULL, pTitle, -1);
283 void ATOMRSS_item_title_end(StrBuf *CData,
285 rss_aggregator *RSSAggr,
288 if ((ri->item_tag_nesting == 0) && (StrLength(CData) > 0)) {
289 NewStrBufDupAppendFlush(&ri->channel_title, CData, NULL, 0);
290 StrBufTrim(ri->channel_title);
294 void RSS_item_guid_end(StrBuf *CData,
296 rss_aggregator *RSSAggr,
299 if (StrLength(CData) > 0) {
300 NewStrBufDupAppendFlush(&ri->guid, CData, NULL, 0);
304 void ATOM_item_id_end(StrBuf *CData,
305 rss_item *ri, rss_aggregator *RSSAggr, const char** Attr)
307 if (StrLength(CData) > 0) {
308 NewStrBufDupAppendFlush(&ri->guid, CData, NULL, 0);
313 void RSS_item_link_end (StrBuf *CData,
315 rss_aggregator *RSSAggr,
318 if (StrLength(CData) > 0) {
319 NewStrBufDupAppendFlush(&ri->link, CData, NULL, 0);
320 StrBufTrim(ri->link);
323 void RSS_item_relink_end(StrBuf *CData,
325 rss_aggregator *RSSAggr,
328 if (StrLength(CData) > 0) {
329 NewStrBufDupAppendFlush(&ri->reLink, CData, NULL, 0);
330 StrBufTrim(ri->reLink);
334 void RSSATOM_item_title_end (StrBuf *CData,
336 rss_aggregator *RSSAggr,
339 if (StrLength(CData) > 0) {
340 NewStrBufDupAppendFlush(&ri->title, CData, NULL, 0);
341 StrBufTrim(ri->title);
345 void ATOM_item_content_end (StrBuf *CData,
347 rss_aggregator *RSSAggr,
350 long olen = StrLength (ri->description);
351 long clen = StrLength (CData);
355 NewStrBufDupAppendFlush(&ri->description,
359 StrBufTrim(ri->description);
361 else if (olen < clen) {
362 FlushStrBuf(ri->description);
363 NewStrBufDupAppendFlush(&ri->description,
368 StrBufTrim(ri->description);
372 void ATOM_item_summary_end (StrBuf *CData,
374 rss_aggregator *RSSAggr,
378 * this can contain an abstract of the article.
379 * but we don't want to verwrite a full document if we already have it.
381 if ((StrLength(CData) > 0) && (StrLength(ri->description) == 0))
383 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
384 StrBufTrim(ri->description);
388 void RSS_item_description_end (StrBuf *CData,
390 rss_aggregator *RSSAggr,
393 long olen = StrLength (ri->description);
394 long clen = StrLength (CData);
398 NewStrBufDupAppendFlush(&ri->description,
402 StrBufTrim(ri->description);
404 else if (olen < clen) {
405 FlushStrBuf(ri->description);
406 NewStrBufDupAppendFlush(&ri->description,
410 StrBufTrim(ri->description);
415 void ATOM_item_published_end (StrBuf *CData,
417 rss_aggregator *RSSAggr,
420 if (StrLength(CData) > 0) {
422 ri->pubdate = rdf_parsedate(ChrPtr(CData));
426 void ATOM_item_updated_end (StrBuf *CData,
428 rss_aggregator *RSSAggr,
431 if (StrLength(CData) > 0) {
433 ri->pubdate = rdf_parsedate(ChrPtr(CData));
437 void RSS_item_pubdate_end (StrBuf *CData,
439 rss_aggregator *RSSAggr,
442 if (StrLength(CData) > 0) {
444 ri->pubdate = rdf_parsedate(ChrPtr(CData));
449 void RSS_item_date_end (StrBuf *CData,
451 rss_aggregator *RSSAggr,
454 if (StrLength(CData) > 0) {
456 ri->pubdate = rdf_parsedate(ChrPtr(CData));
462 void RSS_item_author_end(StrBuf *CData,
464 rss_aggregator *RSSAggr,
467 if (StrLength(CData) > 0) {
468 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
469 StrBufTrim(ri->author_or_creator);
474 void ATOM_item_name_end(StrBuf *CData,
476 rss_aggregator *RSSAggr,
479 if (StrLength(CData) > 0) {
480 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
481 StrBufTrim(ri->author_or_creator);
485 void ATOM_item_email_end(StrBuf *CData,
487 rss_aggregator *RSSAggr,
490 if (StrLength(CData) > 0) {
491 NewStrBufDupAppendFlush(&ri->author_email, CData, NULL, 0);
492 StrBufTrim(ri->author_email);
496 void RSS_item_creator_end(StrBuf *CData,
498 rss_aggregator *RSSAggr,
501 if ((StrLength(CData) > 0) &&
502 (StrLength(ri->author_or_creator) == 0))
504 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
505 StrBufTrim(ri->author_or_creator);
510 void ATOM_item_uri_end(StrBuf *CData,
512 rss_aggregator *RSSAggr,
515 if (StrLength(CData) > 0) {
516 NewStrBufDupAppendFlush(&ri->author_url, CData, NULL, 0);
517 StrBufTrim(ri->author_url);
521 void RSS_item_item_end(StrBuf *CData,
523 rss_aggregator *RSSAggr,
526 --ri->item_tag_nesting;
527 rss_save_item(ri, RSSAggr);
531 void ATOM_item_entry_end(StrBuf *CData,
533 rss_aggregator *RSSAggr,
536 --ri->item_tag_nesting;
537 rss_save_item(ri, RSSAggr);
540 void RSS_item_rss_end(StrBuf *CData,
542 rss_aggregator *RSSAggr,
545 AsyncIO *IO = &RSSAggr->IO;
546 EVRSSATOMM_syslog(LOG_DEBUG, "End of feed detected. Closing parser.\n");
547 ri->done_parsing = 1;
550 void RSS_item_rdf_end(StrBuf *CData,
552 rss_aggregator *RSSAggr,
555 AsyncIO *IO = &RSSAggr->IO;
556 EVRSSATOMM_syslog(LOG_DEBUG, "End of feed detected. Closing parser.\n");
557 ri->done_parsing = 1;
561 void RSSATOM_item_ignore(StrBuf *CData,
563 rss_aggregator *RSSAggr,
571 * This callback stores up the data which appears in between tags.
573 void rss_xml_cdata_start(void *data)
575 rss_aggregator *RSSAggr = (rss_aggregator*) data;
577 FlushStrBuf(RSSAggr->CData);
580 void rss_xml_cdata_end(void *data)
583 void rss_xml_chardata(void *data, const XML_Char *s, int len)
585 rss_aggregator *RSSAggr = (rss_aggregator*) data;
587 StrBufAppendBufPlain (RSSAggr->CData, s, len, 0);
591 /******************************************************************************
593 ******************************************************************************/
595 extern pthread_mutex_t RSSQueueMutex;
597 HashList *StartHandlers = NULL;
598 HashList *EndHandlers = NULL;
599 HashList *KnownNameSpaces = NULL;
601 void FreeNetworkSaveMessage (void *vMsg)
603 networker_save_message *Msg = (networker_save_message *) vMsg;
605 CtdlFreeMessageContents(&Msg->Msg);
606 FreeStrBuf(&Msg->Message);
607 FreeStrBuf(&Msg->MsgGUID);
612 void AppendLink(StrBuf *Message,
617 if (StrLength(link) > 0)
619 StrBufAppendBufPlain(Message, HKEY("<a href=\""), 0);
620 StrBufAppendBuf(Message, link, 0);
621 StrBufAppendBufPlain(Message, HKEY("\">"), 0);
622 if (StrLength(LinkTitle) > 0)
623 StrBufAppendBuf(Message, LinkTitle, 0);
624 else if ((Title != NULL) && !IsEmptyStr(Title))
625 StrBufAppendBufPlain(Message, Title, -1, 0);
627 StrBufAppendBuf(Message, link, 0);
628 StrBufAppendBufPlain(Message, HKEY("</a><br>\n"), 0);
633 * Commit a fetched and parsed RSS item to disk
635 void rss_save_item(rss_item *ri, rss_aggregator *RSSAggr)
637 networker_save_message *SaveMsg;
638 struct MD5Context md5context;
639 u_char rawdigest[MD5_DIGEST_LEN];
643 AsyncIO *IO = &RSSAggr->IO;
647 SaveMsg = (networker_save_message *) malloc(
648 sizeof(networker_save_message));
649 memset(SaveMsg, 0, sizeof(networker_save_message));
651 /* Construct a GUID to use in the S_USETABLE table.
652 * If one is not present in the item itself, make one up.
654 if (ri->guid != NULL) {
655 StrBufSpaceToBlank(ri->guid);
656 StrBufTrim(ri->guid);
657 guid = NewStrBufPlain(HKEY("rss/"));
658 StrBufAppendBuf(guid, ri->guid, 0);
661 MD5Init(&md5context);
662 if (ri->title != NULL) {
663 MD5Update(&md5context,
664 (const unsigned char*)SKEY(ri->title));
666 if (ri->link != NULL) {
667 MD5Update(&md5context,
668 (const unsigned char*)SKEY(ri->link));
670 MD5Final(rawdigest, &md5context);
671 guid = NewStrBufPlain(NULL,
672 MD5_DIGEST_LEN * 2 + 12 /* _rss2ctdl*/);
673 StrBufHexEscAppend(guid, NULL, rawdigest, MD5_DIGEST_LEN);
674 StrBufAppendBufPlain(guid, HKEY("_rss2ctdl"), 0);
677 /* translate Item into message. */
678 EVRSSATOMM_syslog(LOG_DEBUG, "RSS: translating item...\n");
679 if (ri->description == NULL) ri->description = NewStrBufPlain(HKEY(""));
680 StrBufSpaceToBlank(ri->description);
681 SaveMsg->Msg.cm_magic = CTDLMESSAGE_MAGIC;
682 SaveMsg->Msg.cm_anon_type = MES_NORMAL;
683 SaveMsg->Msg.cm_format_type = FMT_RFC822;
685 if (ri->guid != NULL) {
686 SaveMsg->Msg.cm_fields['E'] = strdup(ChrPtr(ri->guid));
689 if (ri->author_or_creator != NULL) {
691 StrBuf *Encoded = NULL;
694 From = html_to_ascii(ChrPtr(ri->author_or_creator),
695 StrLength(ri->author_or_creator),
697 StrBufPlain(ri->author_or_creator, From, -1);
698 StrBufTrim(ri->author_or_creator);
701 FromAt = strchr(ChrPtr(ri->author_or_creator), '@') != NULL;
702 if (!FromAt && StrLength (ri->author_email) > 0)
704 StrBufRFC2047encode(&Encoded, ri->author_or_creator);
705 SaveMsg->Msg.cm_fields['A'] = SmashStrBuf(&Encoded);
706 SaveMsg->Msg.cm_fields['P'] =
707 SmashStrBuf(&ri->author_email);
713 SaveMsg->Msg.cm_fields['A'] =
714 SmashStrBuf(&ri->author_or_creator);
715 SaveMsg->Msg.cm_fields['P'] =
716 strdup(SaveMsg->Msg.cm_fields['A']);
720 StrBufRFC2047encode(&Encoded,
721 ri->author_or_creator);
722 SaveMsg->Msg.cm_fields['A'] =
723 SmashStrBuf(&Encoded);
724 SaveMsg->Msg.cm_fields['P'] =
725 strdup("rss@localhost");
728 if (ri->pubdate <= 0) {
729 ri->pubdate = time(NULL);
734 SaveMsg->Msg.cm_fields['A'] = strdup("rss");
737 SaveMsg->Msg.cm_fields['N'] = strdup(NODENAME);
738 if (ri->title != NULL) {
741 StrBuf *Encoded, *QPEncoded;
744 StrBufSpaceToBlank(ri->title);
745 len = StrLength(ri->title);
746 Sbj = html_to_ascii(ChrPtr(ri->title), len, 512, 0);
748 if (Sbj[len - 1] == '\n')
753 Encoded = NewStrBufPlain(Sbj, len);
757 StrBufRFC2047encode(&QPEncoded, Encoded);
759 SaveMsg->Msg.cm_fields['U'] = SmashStrBuf(&QPEncoded);
760 FreeStrBuf(&Encoded);
762 SaveMsg->Msg.cm_fields['T'] = malloc(64);
763 snprintf(SaveMsg->Msg.cm_fields['T'], 64, "%ld", ri->pubdate);
764 if (ri->channel_title != NULL) {
765 if (StrLength(ri->channel_title) > 0) {
766 SaveMsg->Msg.cm_fields['O'] =
767 strdup(ChrPtr(ri->channel_title));
770 if (ri->link == NULL)
771 ri->link = NewStrBufPlain(HKEY(""));
773 #if 0 /* temporarily disable shorter urls. */
774 SaveMsg->Msg.cm_fields[TMP_SHORTER_URLS] =
775 GetShorterUrls(ri->description);
778 msglen += 1024 + StrLength(ri->link) + StrLength(ri->description) ;
780 Message = NewStrBufPlain(NULL, StrLength(ri->description));
782 StrBufPlain(Message, HKEY(
783 "Content-type: text/html; charset=\"UTF-8\"\r\n\r\n"
785 #if 0 /* disable shorter url for now. */
786 SaveMsg->Msg.cm_fields[TMP_SHORTER_URL_OFFSET] = StrLength(Message);
788 StrBufAppendBuf(Message, ri->description, 0);
789 StrBufAppendBufPlain(Message, HKEY("<br><br>\n"), 0);
791 AppendLink(Message, ri->link, ri->linkTitle, NULL);
792 AppendLink(Message, ri->reLink, ri->reLinkTitle, "Reply to this");
793 StrBufAppendBufPlain(Message, HKEY("</body></html>\n"), 0);
795 SaveMsg->MsgGUID = guid;
796 SaveMsg->Message = Message;
798 n = GetCount(RSSAggr->Messages) + 1;
799 Put(RSSAggr->Messages, IKEY(n), SaveMsg, FreeNetworkSaveMessage);
803 void rss_xml_start(void *data, const char *supplied_el, const char **attr)
806 rss_aggregator *RSSAggr = (rss_aggregator*) data;
807 AsyncIO *IO = &RSSAggr->IO;
808 rss_item *ri = RSSAggr->Item;
813 /* Axe the namespace, we don't care about it */
816 "RSS: supplied el %d: %s\n", RSSAggr->RSSAggr->ItemType, supplied_el);
819 while (sep = strchr(pel, ':'), sep) {
823 if (pel != supplied_el)
827 if (!GetHash(KnownNameSpaces,
829 pel - supplied_el - 1,
832 EVRSSATOM_syslog(LOG_DEBUG,
833 "RSS: START ignoring "
834 "because of wrong namespace [%s]\n",
840 StrBufPlain(RSSAggr->Key, pel, -1);
841 StrBufLowerCase(RSSAggr->Key);
842 if (GetHash(StartHandlers, SKEY(RSSAggr->Key), &pv))
844 h = (rss_xml_handler*) pv;
846 if (((h->Flags & RSS_UNSET) != 0) &&
847 (RSSAggr->ItemType == RSS_UNSET))
849 h->Handler(RSSAggr->CData, ri, RSSAggr, attr);
851 else if (((h->Flags & RSS_RSS) != 0) &&
852 (RSSAggr->ItemType == RSS_RSS))
854 h->Handler(RSSAggr->CData, ri, RSSAggr, attr);
856 else if (((h->Flags & RSS_ATOM) != 0) &&
857 (RSSAggr->ItemType == RSS_ATOM))
859 h->Handler(RSSAggr->CData,
865 EVRSSATOM_syslog(LOG_DEBUG,
866 "RSS: START unhandled: [%s] [%s]...\n",
871 EVRSSATOM_syslog(LOG_DEBUG,
872 "RSS: START unhandled: [%s] [%s]...\n",
877 void rss_xml_end(void *data, const char *supplied_el)
880 rss_aggregator *RSSAggr = (rss_aggregator*) data;
881 AsyncIO *IO = &RSSAggr->IO;
882 rss_item *ri = RSSAggr->Item;
887 /* Axe the namespace, we don't care about it */
889 while (sep = strchr(pel, ':'), sep) {
892 EVRSSATOM_syslog(LOG_DEBUG, "RSS: END %s...\n", supplied_el);
893 if (pel != supplied_el)
897 if (!GetHash(KnownNameSpaces,
899 pel - supplied_el - 1,
902 EVRSSATOM_syslog(LOG_DEBUG,
903 "RSS: END ignoring because of wrong namespace"
906 ChrPtr(RSSAggr->CData));
907 FlushStrBuf(RSSAggr->CData);
912 StrBufPlain(RSSAggr->Key, pel, -1);
913 StrBufLowerCase(RSSAggr->Key);
914 if (GetHash(EndHandlers, SKEY(RSSAggr->Key), &pv))
916 h = (rss_xml_handler*) pv;
918 if (((h->Flags & RSS_UNSET) != 0) &&
919 (RSSAggr->ItemType == RSS_UNSET))
921 h->Handler(RSSAggr->CData, ri, RSSAggr, NULL);
923 else if (((h->Flags & RSS_RSS) != 0) &&
924 (RSSAggr->ItemType == RSS_RSS))
926 h->Handler(RSSAggr->CData, ri, RSSAggr, NULL);
928 else if (((h->Flags & RSS_ATOM) != 0) &&
929 (RSSAggr->ItemType == RSS_ATOM))
931 h->Handler(RSSAggr->CData, ri, RSSAggr, NULL);
934 EVRSSATOM_syslog(LOG_DEBUG,
935 "RSS: END unhandled: [%s] [%s] = [%s]...\n",
938 ChrPtr(RSSAggr->CData));
941 EVRSSATOM_syslog(LOG_DEBUG,
942 "RSS: END unhandled: [%s] [%s] = [%s]...\n",
945 ChrPtr(RSSAggr->CData));
946 FlushStrBuf(RSSAggr->CData);
950 * Callback function for passing libcurl's output to expat for parsing
951 * we don't do streamed parsing so expat can handle non-utf8 documents
952 size_t rss_libcurl_callback(void *ptr, size_t size, size_t nmemb, void *stream)
954 XML_Parse((XML_Parser)stream, ptr, (size * nmemb), 0);
959 eNextState RSSAggregator_ParseReply(AsyncIO *IO)
962 rss_aggregator *RSSAggr;
970 if (IO->HttpReq.httpcode != 200)
973 EVRSSATOM_syslog(LOG_ALERT, "need a 200, got a %ld !\n",
974 IO->HttpReq.httpcode);
975 // TODO: aide error message with rate limit
981 RSSAggr->CData = NewStrBufPlain(NULL, SIZ);
982 RSSAggr->Key = NewStrBuf();
984 StrBufSipLine(RSSAggr->Key, IO->HttpReq.ReplyData, &at);
987 #define encoding "encoding=\""
988 ptr = strstr(ChrPtr(RSSAggr->Key), encoding);
993 ptr += sizeof (encoding) - 1;
994 pche = strchr(ptr, '"');
996 StrBufCutAt(RSSAggr->Key, -1, pche);
1003 EVRSSATOM_syslog(LOG_DEBUG, "RSS: Now parsing [%s] \n", ChrPtr(RSSAggr->Url));
1005 RSSAggr->xp = XML_ParserCreateNS(ptr, ':');
1007 EVRSSATOMM_syslog(LOG_ALERT, "Cannot create XML parser!\n");
1010 FlushStrBuf(RSSAggr->Key);
1012 RSSAggr->Messages = NewHash(1, Flathash);
1013 XML_SetElementHandler(RSSAggr->xp, rss_xml_start, rss_xml_end);
1014 XML_SetCharacterDataHandler(RSSAggr->xp, rss_xml_chardata);
1015 XML_SetUserData(RSSAggr->xp, RSSAggr);
1016 XML_SetCdataSectionHandler(RSSAggr->xp,
1017 rss_xml_cdata_start,
1021 len = StrLength(IO->HttpReq.ReplyData);
1022 ptr = SmashStrBuf(&IO->HttpReq.ReplyData);
1023 XML_Parse(RSSAggr->xp, ptr, len, 0);
1025 if (ri->done_parsing == 0)
1026 XML_Parse(RSSAggr->xp, "", 0, 1);
1029 EVRSSATOM_syslog(LOG_DEBUG, "RSS: XML Status [%s] \n",
1030 XML_ErrorString(XML_GetErrorCode(RSSAggr->xp)));
1032 XML_ParserFree(RSSAggr->xp);
1035 Buf = NewStrBufDup(RSSAggr->rooms);
1036 RSSAggr->recp.recp_room = SmashStrBuf(&Buf);
1037 RSSAggr->recp.num_room = RSSAggr->roomlist_parts;
1038 RSSAggr->recp.recptypes_magic = RECPTYPES_MAGIC;
1040 RSSAggr->Pos = GetNewHashPos(RSSAggr->Messages, 1);
1042 //RSSAggr->next_poll = time(NULL) + config.c_net_freq;
1043 if (GetNextHashPos(RSSAggr->Messages,
1047 (void**) &RSSAggr->ThisMsg))
1048 return QueueDBOperation(IO, RSS_FetchNetworkUsetableEntry);
1054 /******************************************************************************
1055 * RSS handler registering logic *
1056 ******************************************************************************/
1058 void AddRSSStartHandler(rss_handler_func Handler,
1064 h = (rss_xml_handler*) malloc(sizeof (rss_xml_handler));
1066 h->Handler = Handler;
1067 Put(StartHandlers, key, len, h, NULL);
1070 void AddRSSEndHandler(rss_handler_func Handler,
1076 h = (rss_xml_handler*) malloc(sizeof (rss_xml_handler));
1078 h->Handler = Handler;
1079 Put(EndHandlers, key, len, h, NULL);
1082 void rss_parser_cleanup(void)
1084 DeleteHash(&StartHandlers);
1085 DeleteHash(&EndHandlers);
1086 DeleteHash(&KnownNameSpaces);
1089 void LogDebugEnableRSSATOMParser(const int n)
1091 RSSAtomParserDebugEnabled = n;
1094 CTDL_MODULE_INIT(rssparser)
1098 StartHandlers = NewHash(1, NULL);
1099 EndHandlers = NewHash(1, NULL);
1101 AddRSSStartHandler(RSS_item_rss_start, RSS_UNSET, HKEY("rss"));
1102 AddRSSStartHandler(RSS_item_rdf_start, RSS_UNSET, HKEY("rdf"));
1103 AddRSSStartHandler(ATOM_item_feed_start, RSS_UNSET, HKEY("feed"));
1104 AddRSSStartHandler(RSS_item_item_start, RSS_RSS, HKEY("item"));
1105 AddRSSStartHandler(ATOM_item_entry_start, RSS_ATOM, HKEY("entry"));
1106 AddRSSStartHandler(ATOM_item_link_start, RSS_ATOM, HKEY("link"));
1108 AddRSSEndHandler(ATOMRSS_item_title_end, RSS_ATOM|RSS_RSS|RSS_REQUIRE_BUF, HKEY("title"));
1109 AddRSSEndHandler(RSS_item_guid_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("guid"));
1110 AddRSSEndHandler(ATOM_item_id_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("id"));
1111 AddRSSEndHandler(RSS_item_link_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("link"));
1113 // hm, rss to the comments of that blog, might be interesting in future, but...
1114 AddRSSEndHandler(RSS_item_relink_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("commentrss"));
1116 AddRSSEndHandler(RSS_item_relink_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("comments"));
1118 AddRSSEndHandler(RSSATOM_item_title_end, RSS_ATOM|RSS_RSS|RSS_REQUIRE_BUF, HKEY("title"));
1119 AddRSSEndHandler(ATOM_item_content_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("content"));
1120 AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_ATOM|RSS_REQUIRE_BUF, HKEY("encoded"));
1121 AddRSSEndHandler(ATOM_item_summary_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("summary"));
1122 AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("description"));
1123 AddRSSEndHandler(ATOM_item_published_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("published"));
1124 AddRSSEndHandler(ATOM_item_updated_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("updated"));
1125 AddRSSEndHandler(RSS_item_pubdate_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("pubdate"));
1126 AddRSSEndHandler(RSS_item_date_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("date"));
1127 AddRSSEndHandler(RSS_item_author_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("author"));
1128 AddRSSEndHandler(RSS_item_creator_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("creator"));
1130 AddRSSEndHandler(ATOM_item_email_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("email"));
1131 AddRSSEndHandler(ATOM_item_name_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("name"));
1132 AddRSSEndHandler(ATOM_item_uri_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("uri"));
1134 AddRSSEndHandler(RSS_item_item_end, RSS_RSS, HKEY("item"));
1135 AddRSSEndHandler(RSS_item_rss_end, RSS_RSS, HKEY("rss"));
1136 AddRSSEndHandler(RSS_item_rdf_end, RSS_RSS, HKEY("rdf"));
1137 AddRSSEndHandler(ATOM_item_entry_end, RSS_ATOM, HKEY("entry"));
1140 /* at the start of atoms: <seq> <li>link to resource</li></seq> ignore them. */
1141 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("seq"));
1142 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("seq"));
1143 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("li"));
1144 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("li"));
1146 /* links to other feed generators... */
1147 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("feedflare"));
1148 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("feedflare"));
1149 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("browserfriendly"));
1150 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("browserfriendly"));
1152 KnownNameSpaces = NewHash(1, NULL);
1153 Put(KnownNameSpaces, HKEY("http://a9.com/-/spec/opensearch/1.1/"), NULL, reference_free_handler);
1154 Put(KnownNameSpaces, HKEY("http://a9.com/-/spec/opensearchrss/1.0/"), NULL, reference_free_handler);
1155 Put(KnownNameSpaces, HKEY("http://backend.userland.com/creativeCommonsRssModule"), NULL, reference_free_handler);
1156 Put(KnownNameSpaces, HKEY("http://purl.org/atom/ns#"), NULL, reference_free_handler);
1157 Put(KnownNameSpaces, HKEY("http://purl.org/dc/elements/1.1/"), NULL, reference_free_handler);
1158 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/"), NULL, reference_free_handler);
1159 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/content/"), NULL, reference_free_handler);
1160 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/slash/"), NULL, reference_free_handler);
1161 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/syndication/"), NULL, reference_free_handler);
1162 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/"), NULL, reference_free_handler);
1163 Put(KnownNameSpaces, HKEY("http://purl.org/syndication/thread/1.0"), NULL, reference_free_handler);
1164 Put(KnownNameSpaces, HKEY("http://rssnamespace.org/feedburner/ext/1.0"), NULL, reference_free_handler);
1165 Put(KnownNameSpaces, HKEY("http://schemas.google.com/g/2005"), NULL, reference_free_handler);
1166 Put(KnownNameSpaces, HKEY("http://webns.net/mvcb/"), NULL, reference_free_handler);
1167 Put(KnownNameSpaces, HKEY("http://web.resource.org/cc/"), NULL, reference_free_handler);
1168 Put(KnownNameSpaces, HKEY("http://wellformedweb.org/CommentAPI/"), NULL, reference_free_handler);
1169 Put(KnownNameSpaces, HKEY("http://www.georss.org/georss"), NULL, reference_free_handler);
1170 Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/xhtml"), NULL, reference_free_handler);
1171 Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), NULL, reference_free_handler);
1172 Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), NULL, reference_free_handler);
1173 Put(KnownNameSpaces, HKEY("http://www.w3.org/2003/01/geo/wgs84_pos#"), NULL, reference_free_handler);
1174 Put(KnownNameSpaces, HKEY("http://www.w3.org/2005/Atom"), NULL, reference_free_handler);
1175 Put(KnownNameSpaces, HKEY("urn:flickr:"), NULL, reference_free_handler);
1177 /* we don't like these namespaces because of they shadow our usefull parameters. */
1178 Put(KnownNameSpaces, HKEY("http://search.yahoo.com/mrss/"), NULL, reference_free_handler);
1180 CtdlRegisterDebugFlagHook(HKEY("RSSAtomParser"), LogDebugEnableRSSATOMParser, &RSSAtomParserDebugEnabled);
1181 CtdlRegisterCleanupHook(rss_parser_cleanup);