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);
65 * Convert an RDF/RSS datestamp into a time_t
67 time_t rdf_parsedate(const char *p)
73 if (strlen(p) < 10) return 0L;
75 memset(&tm, 0, sizeof tm);
78 * If the timestamp appears to be in W3C datetime format, try to
79 * parse it. See also: http://www.w3.org/TR/NOTE-datetime
81 * This code, along with parsedate.c, is a potential candidate for
82 * moving into libcitadel.
84 if ( (p[4] == '-') && (p[7] == '-') ) {
85 tm.tm_year = atoi(&p[0]) - 1900;
86 tm.tm_mon = atoi(&p[5]) - 1;
87 tm.tm_mday = atoi(&p[8]);
88 if ( (p[10] == 'T') && (p[13] == ':') ) {
89 tm.tm_hour = atoi(&p[11]);
90 tm.tm_min = atoi(&p[14]);
95 /* hmm... try RFC822 date stamp format */
100 /* yeesh. ok, just return the current date and time. */
104 void flush_rss_item(rss_item *ri)
106 /* Initialize the feed item data structure */
107 FreeStrBuf(&ri->guid);
108 FreeStrBuf(&ri->title);
109 FreeStrBuf(&ri->link);
110 FreeStrBuf(&ri->author_or_creator);
111 FreeStrBuf(&ri->author_email);
112 FreeStrBuf(&ri->author_url);
113 FreeStrBuf(&ri->description);
115 FreeStrBuf(&ri->linkTitle);
116 FreeStrBuf(&ri->reLink);
117 FreeStrBuf(&ri->reLinkTitle);
118 FreeStrBuf(&ri->channel_title);
122 /******************************************************************************
124 ******************************************************************************/
127 void RSS_item_rss_start (StrBuf *CData,
132 syslog(LOG_DEBUG, "RSS: This is an RSS feed.\n");
133 Cfg->ItemType = RSS_RSS;
136 void RSS_item_rdf_start(StrBuf *CData,
141 syslog(LOG_DEBUG, "RSS: This is an RDF feed.\n");
142 Cfg->ItemType = RSS_RSS;
145 void ATOM_item_feed_start(StrBuf *CData,
150 syslog(LOG_DEBUG, "RSS: This is an ATOM feed.\n");
151 Cfg->ItemType = RSS_ATOM;
155 void RSS_item_item_start(StrBuf *CData,
160 ri->item_tag_nesting ++;
164 void ATOM_item_entry_start(StrBuf *CData,
170 ri->item_tag_nesting ++;
174 void ATOM_item_link_start (StrBuf *CData,
180 const char *pHref = NULL;
181 const char *pType = NULL;
182 const char *pRel = NULL;
183 const char *pTitle = NULL;
185 for (i = 0; Attr[i] != NULL; i+=2)
187 if (!strcmp(Attr[i], "href"))
191 else if (!strcmp(Attr[i], "rel"))
195 else if (!strcmp(Attr[i], "type"))
199 else if (!strcmp(Attr[i], "title"))
205 return; /* WHUT? Pointing... where? */
206 if ((pType != NULL) && !strcasecmp(pType, "application/atom+xml"))
208 /* these just point to other rss resources,
209 we're not interested in them. */
212 if (!strcasecmp (pRel, "replies"))
214 NewStrBufDupAppendFlush(&ri->reLink, NULL, pHref, -1);
215 StrBufTrim(ri->link);
216 NewStrBufDupAppendFlush(&ri->reLinkTitle,
221 else if (!strcasecmp(pRel, "alternate"))
222 { /* Alternative representation of this Item... */
223 NewStrBufDupAppendFlush(&ri->link, NULL, pHref, -1);
224 StrBufTrim(ri->link);
225 NewStrBufDupAppendFlush(&ri->linkTitle,
231 #if 0 /* these are also defined, but dunno what to do with them.. */
232 else if (!strcasecmp(pRel, "related"))
235 else if (!strcasecmp(pRel, "self"))
238 else if (!strcasecmp(pRel, "enclosure"))
239 {/*...reference can get big, and is probably the full article*/
241 else if (!strcasecmp(pRel, "via"))
242 {/* this article was provided via... */
246 else if (StrLength(ri->link) == 0)
248 NewStrBufDupAppendFlush(&ri->link, NULL, pHref, -1);
249 StrBufTrim(ri->link);
250 NewStrBufDupAppendFlush(&ri->linkTitle, NULL, pTitle, -1);
257 void ATOMRSS_item_title_end(StrBuf *CData,
262 if ((ri->item_tag_nesting == 0) && (StrLength(CData) > 0)) {
263 NewStrBufDupAppendFlush(&ri->channel_title, CData, NULL, 0);
264 StrBufTrim(ri->channel_title);
268 void RSS_item_guid_end(StrBuf *CData,
273 if (StrLength(CData) > 0) {
274 NewStrBufDupAppendFlush(&ri->guid, CData, NULL, 0);
278 void ATOM_item_id_end(StrBuf *CData,
279 rss_item *ri, rss_aggregator *Cfg, const char** Attr)
281 if (StrLength(CData) > 0) {
282 NewStrBufDupAppendFlush(&ri->guid, CData, NULL, 0);
287 void RSS_item_link_end (StrBuf *CData,
292 if (StrLength(CData) > 0) {
293 NewStrBufDupAppendFlush(&ri->link, CData, NULL, 0);
294 StrBufTrim(ri->link);
297 void RSS_item_relink_end(StrBuf *CData,
302 if (StrLength(CData) > 0) {
303 NewStrBufDupAppendFlush(&ri->reLink, CData, NULL, 0);
304 StrBufTrim(ri->reLink);
308 void RSSATOM_item_title_end (StrBuf *CData,
313 if (StrLength(CData) > 0) {
314 NewStrBufDupAppendFlush(&ri->title, CData, NULL, 0);
315 StrBufTrim(ri->title);
319 void ATOM_item_content_end (StrBuf *CData,
324 long olen = StrLength (ri->description);
325 long clen = StrLength (CData);
329 NewStrBufDupAppendFlush(&ri->description,
333 StrBufTrim(ri->description);
335 else if (olen < clen) {
336 FlushStrBuf(ri->description);
337 NewStrBufDupAppendFlush(&ri->description,
342 StrBufTrim(ri->description);
346 void ATOM_item_summary_end (StrBuf *CData,
352 * this can contain an abstract of the article.
353 * but we don't want to verwrite a full document if we already have it.
355 if ((StrLength(CData) > 0) && (StrLength(ri->description) == 0))
357 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
358 StrBufTrim(ri->description);
362 void RSS_item_description_end (StrBuf *CData,
367 long olen = StrLength (ri->description);
368 long clen = StrLength (CData);
372 NewStrBufDupAppendFlush(&ri->description,
376 StrBufTrim(ri->description);
378 else if (olen < clen) {
379 FlushStrBuf(ri->description);
380 NewStrBufDupAppendFlush(&ri->description,
384 StrBufTrim(ri->description);
389 void ATOM_item_published_end (StrBuf *CData,
394 if (StrLength(CData) > 0) {
396 ri->pubdate = rdf_parsedate(ChrPtr(CData));
400 void ATOM_item_updated_end (StrBuf *CData,
405 if (StrLength(CData) > 0) {
407 ri->pubdate = rdf_parsedate(ChrPtr(CData));
411 void RSS_item_pubdate_end (StrBuf *CData,
416 if (StrLength(CData) > 0) {
418 ri->pubdate = rdf_parsedate(ChrPtr(CData));
423 void RSS_item_date_end (StrBuf *CData,
428 if (StrLength(CData) > 0) {
430 ri->pubdate = rdf_parsedate(ChrPtr(CData));
436 void RSS_item_author_end(StrBuf *CData,
441 if (StrLength(CData) > 0) {
442 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
443 StrBufTrim(ri->author_or_creator);
448 void ATOM_item_name_end(StrBuf *CData,
453 if (StrLength(CData) > 0) {
454 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
455 StrBufTrim(ri->author_or_creator);
459 void ATOM_item_email_end(StrBuf *CData,
464 if (StrLength(CData) > 0) {
465 NewStrBufDupAppendFlush(&ri->author_email, CData, NULL, 0);
466 StrBufTrim(ri->author_email);
470 void RSS_item_creator_end(StrBuf *CData,
475 if ((StrLength(CData) > 0) &&
476 (StrLength(ri->author_or_creator) == 0))
478 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
479 StrBufTrim(ri->author_or_creator);
484 void ATOM_item_uri_end(StrBuf *CData,
489 if (StrLength(CData) > 0) {
490 NewStrBufDupAppendFlush(&ri->author_url, CData, NULL, 0);
491 StrBufTrim(ri->author_url);
495 void RSS_item_item_end(StrBuf *CData,
500 --ri->item_tag_nesting;
501 rss_save_item(ri, Cfg);
505 void ATOM_item_entry_end(StrBuf *CData,
510 --ri->item_tag_nesting;
511 rss_save_item(ri, Cfg);
514 void RSS_item_rss_end(StrBuf *CData,
519 // syslog(LOG_DEBUG, "End of feed detected. Closing parser.\n");
520 ri->done_parsing = 1;
523 void RSS_item_rdf_end(StrBuf *CData,
528 // syslog(LOG_DEBUG, "End of feed detected. Closing parser.\n");
529 ri->done_parsing = 1;
533 void RSSATOM_item_ignore(StrBuf *CData,
543 * This callback stores up the data which appears in between tags.
545 void rss_xml_cdata_start(void *data)
547 rss_aggregator *RSSAggr = (rss_aggregator*) data;
549 FlushStrBuf(RSSAggr->CData);
552 void rss_xml_cdata_end(void *data)
555 void rss_xml_chardata(void *data, const XML_Char *s, int len)
557 rss_aggregator *RSSAggr = (rss_aggregator*) data;
559 StrBufAppendBufPlain (RSSAggr->CData, s, len, 0);
563 /******************************************************************************
565 ******************************************************************************/
567 extern pthread_mutex_t RSSQueueMutex;
569 HashList *StartHandlers = NULL;
570 HashList *EndHandlers = NULL;
571 HashList *KnownNameSpaces = NULL;
573 void FreeNetworkSaveMessage (void *vMsg)
575 networker_save_message *Msg = (networker_save_message *) vMsg;
577 CtdlFreeMessageContents(&Msg->Msg);
578 FreeStrBuf(&Msg->Message);
579 FreeStrBuf(&Msg->MsgGUID);
584 void AppendLink(StrBuf *Message,
589 if (StrLength(link) > 0)
591 StrBufAppendBufPlain(Message, HKEY("<a href=\""), 0);
592 StrBufAppendBuf(Message, link, 0);
593 StrBufAppendBufPlain(Message, HKEY("\">"), 0);
594 if (StrLength(LinkTitle) > 0)
595 StrBufAppendBuf(Message, LinkTitle, 0);
596 else if ((Title != NULL) && !IsEmptyStr(Title))
597 StrBufAppendBufPlain(Message, Title, -1, 0);
599 StrBufAppendBuf(Message, link, 0);
600 StrBufAppendBufPlain(Message, HKEY("</a><br>\n"), 0);
605 * Commit a fetched and parsed RSS item to disk
607 void rss_save_item(rss_item *ri, rss_aggregator *Cfg)
609 networker_save_message *SaveMsg;
610 struct MD5Context md5context;
611 u_char rawdigest[MD5_DIGEST_LEN];
615 AsyncIO *IO = &Cfg->IO;
619 SaveMsg = (networker_save_message *) malloc(
620 sizeof(networker_save_message));
621 memset(SaveMsg, 0, sizeof(networker_save_message));
623 /* Construct a GUID to use in the S_USETABLE table.
624 * If one is not present in the item itself, make one up.
626 if (ri->guid != NULL) {
627 StrBufSpaceToBlank(ri->guid);
628 StrBufTrim(ri->guid);
629 guid = NewStrBufPlain(HKEY("rss/"));
630 StrBufAppendBuf(guid, ri->guid, 0);
633 MD5Init(&md5context);
634 if (ri->title != NULL) {
635 MD5Update(&md5context,
636 (const unsigned char*)SKEY(ri->title));
638 if (ri->link != NULL) {
639 MD5Update(&md5context,
640 (const unsigned char*)SKEY(ri->link));
642 MD5Final(rawdigest, &md5context);
643 guid = NewStrBufPlain(NULL,
644 MD5_DIGEST_LEN * 2 + 12 /* _rss2ctdl*/);
645 StrBufHexEscAppend(guid, NULL, rawdigest, MD5_DIGEST_LEN);
646 StrBufAppendBufPlain(guid, HKEY("_rss2ctdl"), 0);
649 /* translate Item into message. */
650 EVM_syslog(LOG_DEBUG, "RSS: translating item...\n");
651 if (ri->description == NULL) ri->description = NewStrBufPlain(HKEY(""));
652 StrBufSpaceToBlank(ri->description);
653 SaveMsg->Msg.cm_magic = CTDLMESSAGE_MAGIC;
654 SaveMsg->Msg.cm_anon_type = MES_NORMAL;
655 SaveMsg->Msg.cm_format_type = FMT_RFC822;
657 if (ri->guid != NULL) {
658 SaveMsg->Msg.cm_fields['E'] = strdup(ChrPtr(ri->guid));
661 if (ri->author_or_creator != NULL) {
663 StrBuf *Encoded = NULL;
666 From = html_to_ascii(ChrPtr(ri->author_or_creator),
667 StrLength(ri->author_or_creator),
669 StrBufPlain(ri->author_or_creator, From, -1);
670 StrBufTrim(ri->author_or_creator);
673 FromAt = strchr(ChrPtr(ri->author_or_creator), '@') != NULL;
674 if (!FromAt && StrLength (ri->author_email) > 0)
676 StrBufRFC2047encode(&Encoded, ri->author_or_creator);
677 SaveMsg->Msg.cm_fields['A'] = SmashStrBuf(&Encoded);
678 SaveMsg->Msg.cm_fields['P'] =
679 SmashStrBuf(&ri->author_email);
685 SaveMsg->Msg.cm_fields['A'] =
686 SmashStrBuf(&ri->author_or_creator);
687 SaveMsg->Msg.cm_fields['P'] =
688 strdup(SaveMsg->Msg.cm_fields['A']);
692 StrBufRFC2047encode(&Encoded,
693 ri->author_or_creator);
694 SaveMsg->Msg.cm_fields['A'] =
695 SmashStrBuf(&Encoded);
696 SaveMsg->Msg.cm_fields['P'] =
697 strdup("rss@localhost");
700 if (ri->pubdate <= 0) {
701 ri->pubdate = time(NULL);
706 SaveMsg->Msg.cm_fields['A'] = strdup("rss");
709 SaveMsg->Msg.cm_fields['N'] = strdup(NODENAME);
710 if (ri->title != NULL) {
713 StrBuf *Encoded, *QPEncoded;
716 StrBufSpaceToBlank(ri->title);
717 len = StrLength(ri->title);
718 Sbj = html_to_ascii(ChrPtr(ri->title), len, 512, 0);
720 if (Sbj[len - 1] == '\n')
725 Encoded = NewStrBufPlain(Sbj, len);
729 StrBufRFC2047encode(&QPEncoded, Encoded);
731 SaveMsg->Msg.cm_fields['U'] = SmashStrBuf(&QPEncoded);
732 FreeStrBuf(&Encoded);
734 SaveMsg->Msg.cm_fields['T'] = malloc(64);
735 snprintf(SaveMsg->Msg.cm_fields['T'], 64, "%ld", ri->pubdate);
736 if (ri->channel_title != NULL) {
737 if (StrLength(ri->channel_title) > 0) {
738 SaveMsg->Msg.cm_fields['O'] =
739 strdup(ChrPtr(ri->channel_title));
742 if (ri->link == NULL)
743 ri->link = NewStrBufPlain(HKEY(""));
745 #if 0 /* temporarily disable shorter urls. */
746 SaveMsg->Msg.cm_fields[TMP_SHORTER_URLS] =
747 GetShorterUrls(ri->description);
750 msglen += 1024 + StrLength(ri->link) + StrLength(ri->description) ;
752 Message = NewStrBufPlain(NULL, StrLength(ri->description));
754 StrBufPlain(Message, HKEY(
755 "Content-type: text/html; charset=\"UTF-8\"\r\n\r\n"
757 #if 0 /* disable shorter url for now. */
758 SaveMsg->Msg.cm_fields[TMP_SHORTER_URL_OFFSET] = StrLength(Message);
760 StrBufAppendBuf(Message, ri->description, 0);
761 StrBufAppendBufPlain(Message, HKEY("<br><br>\n"), 0);
763 AppendLink(Message, ri->link, ri->linkTitle, NULL);
764 AppendLink(Message, ri->reLink, ri->reLinkTitle, "Reply to this");
765 StrBufAppendBufPlain(Message, HKEY("</body></html>\n"), 0);
767 SaveMsg->MsgGUID = guid;
768 SaveMsg->Message = Message;
770 n = GetCount(Cfg->Messages) + 1;
771 Put(Cfg->Messages, IKEY(n), SaveMsg, FreeNetworkSaveMessage);
775 void rss_xml_start(void *data, const char *supplied_el, const char **attr)
778 rss_aggregator *RSSAggr = (rss_aggregator*) data;
779 rss_item *ri = RSSAggr->Item;
784 /* Axe the namespace, we don't care about it */
787 "RSS: supplied el %d: %s\n", RSSAggr->Cfg->ItemType, supplied_el);
790 while (sep = strchr(pel, ':'), sep) {
794 if (pel != supplied_el)
798 if (!GetHash(KnownNameSpaces,
800 pel - supplied_el - 1,
805 "RSS: START ignoring "
806 "because of wrong namespace [%s]\n",
813 StrBufPlain(RSSAggr->Key, pel, -1);
814 StrBufLowerCase(RSSAggr->Key);
815 if (GetHash(StartHandlers, SKEY(RSSAggr->Key), &pv))
817 h = (rss_xml_handler*) pv;
819 if (((h->Flags & RSS_UNSET) != 0) &&
820 (RSSAggr->ItemType == RSS_UNSET))
822 h->Handler(RSSAggr->CData, ri, RSSAggr, attr);
824 else if (((h->Flags & RSS_RSS) != 0) &&
825 (RSSAggr->ItemType == RSS_RSS))
827 h->Handler(RSSAggr->CData, ri, RSSAggr, attr);
829 else if (((h->Flags & RSS_ATOM) != 0) &&
830 (RSSAggr->ItemType == RSS_ATOM))
832 h->Handler(RSSAggr->CData,
840 "RSS: START unhandled: [%s] [%s]...\n",
848 "RSS: START unhandled: [%s] [%s]...\n",
854 void rss_xml_end(void *data, const char *supplied_el)
857 rss_aggregator *RSSAggr = (rss_aggregator*) data;
858 rss_item *ri = RSSAggr->Item;
863 /* Axe the namespace, we don't care about it */
865 while (sep = strchr(pel, ':'), sep) {
868 // syslog(LOG_DEBUG, "RSS: END %s...\n", el);
869 if (pel != supplied_el)
873 if (!GetHash(KnownNameSpaces,
875 pel - supplied_el - 1,
880 "RSS: END ignoring because of wrong namespace"
883 ChrPtr(RSSAggr->CData));
885 FlushStrBuf(RSSAggr->CData);
890 StrBufPlain(RSSAggr->Key, pel, -1);
891 StrBufLowerCase(RSSAggr->Key);
892 if (GetHash(EndHandlers, SKEY(RSSAggr->Key), &pv))
894 h = (rss_xml_handler*) pv;
896 if (((h->Flags & RSS_UNSET) != 0) &&
897 (RSSAggr->ItemType == RSS_UNSET))
899 h->Handler(RSSAggr->CData, ri, RSSAggr, NULL);
901 else if (((h->Flags & RSS_RSS) != 0) &&
902 (RSSAggr->ItemType == RSS_RSS))
904 h->Handler(RSSAggr->CData, ri, RSSAggr, NULL);
906 else if (((h->Flags & RSS_ATOM) != 0) &&
907 (RSSAggr->ItemType == RSS_ATOM))
909 h->Handler(RSSAggr->CData, ri, RSSAggr, NULL);
914 "RSS: END unhandled: [%s] [%s] = [%s]...\n",
917 ChrPtr(RSSAggr->CData));
923 "RSS: END unhandled: [%s] [%s] = [%s]...\n",
926 ChrPtr(RSSAggr->CData));
928 FlushStrBuf(RSSAggr->CData);
932 * Callback function for passing libcurl's output to expat for parsing
933 * we don't do streamed parsing so expat can handle non-utf8 documents
934 size_t rss_libcurl_callback(void *ptr, size_t size, size_t nmemb, void *stream)
936 XML_Parse((XML_Parser)stream, ptr, (size * nmemb), 0);
941 eNextState RSSAggregator_ParseReply(AsyncIO *IO)
944 rss_aggregator *RSSAggr;
952 if (IO->HttpReq.httpcode != 200)
955 EV_syslog(LOG_DEBUG, "need a 200, got a %ld !\n",
956 IO->HttpReq.httpcode);
957 // TODO: aide error message with rate limit
963 RSSAggr->CData = NewStrBufPlain(NULL, SIZ);
964 RSSAggr->Key = NewStrBuf();
966 StrBufSipLine(RSSAggr->Key, IO->HttpReq.ReplyData, &at);
969 #define encoding "encoding=\""
970 ptr = strstr(ChrPtr(RSSAggr->Key), encoding);
975 ptr += sizeof (encoding) - 1;
976 pche = strchr(ptr, '"');
978 StrBufCutAt(RSSAggr->Key, -1, pche);
985 syslog(LOG_DEBUG, "RSS: Now parsing [%s] \n", ChrPtr(RSSAggr->Url));
987 RSSAggr->xp = XML_ParserCreateNS(ptr, ':');
989 syslog(LOG_DEBUG, "Cannot create XML parser!\n");
992 FlushStrBuf(RSSAggr->Key);
994 RSSAggr->Messages = NewHash(1, Flathash);
995 XML_SetElementHandler(RSSAggr->xp, rss_xml_start, rss_xml_end);
996 XML_SetCharacterDataHandler(RSSAggr->xp, rss_xml_chardata);
997 XML_SetUserData(RSSAggr->xp, RSSAggr);
998 XML_SetCdataSectionHandler(RSSAggr->xp,
1003 len = StrLength(IO->HttpReq.ReplyData);
1004 ptr = SmashStrBuf(&IO->HttpReq.ReplyData);
1005 XML_Parse(RSSAggr->xp, ptr, len, 0);
1007 if (ri->done_parsing == 0)
1008 XML_Parse(RSSAggr->xp, "", 0, 1);
1011 syslog(LOG_DEBUG, "RSS: XML Status [%s] \n",
1012 XML_ErrorString(XML_GetErrorCode(RSSAggr->xp)));
1014 XML_ParserFree(RSSAggr->xp);
1017 Buf = NewStrBufDup(RSSAggr->rooms);
1018 RSSAggr->recp.recp_room = SmashStrBuf(&Buf);
1019 RSSAggr->recp.num_room = RSSAggr->roomlist_parts;
1020 RSSAggr->recp.recptypes_magic = RECPTYPES_MAGIC;
1022 RSSAggr->Pos = GetNewHashPos(RSSAggr->Messages, 1);
1024 //Cfg->next_poll = time(NULL) + config.c_net_freq;
1025 if (GetNextHashPos(RSSAggr->Messages,
1029 (void**) &RSSAggr->ThisMsg))
1030 return QueueDBOperation(IO, RSS_FetchNetworkUsetableEntry);
1036 /******************************************************************************
1037 * RSS handler registering logic *
1038 ******************************************************************************/
1040 void AddRSSStartHandler(rss_handler_func Handler,
1046 h = (rss_xml_handler*) malloc(sizeof (rss_xml_handler));
1048 h->Handler = Handler;
1049 Put(StartHandlers, key, len, h, NULL);
1052 void AddRSSEndHandler(rss_handler_func Handler,
1058 h = (rss_xml_handler*) malloc(sizeof (rss_xml_handler));
1060 h->Handler = Handler;
1061 Put(EndHandlers, key, len, h, NULL);
1064 void rss_parser_cleanup(void)
1066 DeleteHash(&StartHandlers);
1067 DeleteHash(&EndHandlers);
1068 DeleteHash(&KnownNameSpaces);
1072 CTDL_MODULE_INIT(rssparser)
1076 StartHandlers = NewHash(1, NULL);
1077 EndHandlers = NewHash(1, NULL);
1079 AddRSSStartHandler(RSS_item_rss_start, RSS_UNSET, HKEY("rss"));
1080 AddRSSStartHandler(RSS_item_rdf_start, RSS_UNSET, HKEY("rdf"));
1081 AddRSSStartHandler(ATOM_item_feed_start, RSS_UNSET, HKEY("feed"));
1082 AddRSSStartHandler(RSS_item_item_start, RSS_RSS, HKEY("item"));
1083 AddRSSStartHandler(ATOM_item_entry_start, RSS_ATOM, HKEY("entry"));
1084 AddRSSStartHandler(ATOM_item_link_start, RSS_ATOM, HKEY("link"));
1086 AddRSSEndHandler(ATOMRSS_item_title_end, RSS_ATOM|RSS_RSS|RSS_REQUIRE_BUF, HKEY("title"));
1087 AddRSSEndHandler(RSS_item_guid_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("guid"));
1088 AddRSSEndHandler(ATOM_item_id_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("id"));
1089 AddRSSEndHandler(RSS_item_link_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("link"));
1091 // hm, rss to the comments of that blog, might be interesting in future, but...
1092 AddRSSEndHandler(RSS_item_relink_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("commentrss"));
1094 AddRSSEndHandler(RSS_item_relink_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("comments"));
1096 AddRSSEndHandler(RSSATOM_item_title_end, RSS_ATOM|RSS_RSS|RSS_REQUIRE_BUF, HKEY("title"));
1097 AddRSSEndHandler(ATOM_item_content_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("content"));
1098 AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_ATOM|RSS_REQUIRE_BUF, HKEY("encoded"));
1099 AddRSSEndHandler(ATOM_item_summary_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("summary"));
1100 AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("description"));
1101 AddRSSEndHandler(ATOM_item_published_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("published"));
1102 AddRSSEndHandler(ATOM_item_updated_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("updated"));
1103 AddRSSEndHandler(RSS_item_pubdate_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("pubdate"));
1104 AddRSSEndHandler(RSS_item_date_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("date"));
1105 AddRSSEndHandler(RSS_item_author_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("author"));
1106 AddRSSEndHandler(RSS_item_creator_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("creator"));
1108 AddRSSEndHandler(ATOM_item_email_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("email"));
1109 AddRSSEndHandler(ATOM_item_name_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("name"));
1110 AddRSSEndHandler(ATOM_item_uri_end, RSS_ATOM|RSS_REQUIRE_BUF, HKEY("uri"));
1112 AddRSSEndHandler(RSS_item_item_end, RSS_RSS, HKEY("item"));
1113 AddRSSEndHandler(RSS_item_rss_end, RSS_RSS, HKEY("rss"));
1114 AddRSSEndHandler(RSS_item_rdf_end, RSS_RSS, HKEY("rdf"));
1115 AddRSSEndHandler(ATOM_item_entry_end, RSS_ATOM, HKEY("entry"));
1118 /* at the start of atoms: <seq> <li>link to resource</li></seq> ignore them. */
1119 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("seq"));
1120 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("seq"));
1121 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("li"));
1122 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("li"));
1124 /* links to other feed generators... */
1125 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("feedflare"));
1126 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("feedflare"));
1127 AddRSSStartHandler(RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("browserfriendly"));
1128 AddRSSEndHandler (RSSATOM_item_ignore, RSS_RSS|RSS_ATOM, HKEY("browserfriendly"));
1130 KnownNameSpaces = NewHash(1, NULL);
1131 Put(KnownNameSpaces, HKEY("http://a9.com/-/spec/opensearch/1.1/"), NULL, reference_free_handler);
1132 Put(KnownNameSpaces, HKEY("http://a9.com/-/spec/opensearchrss/1.0/"), NULL, reference_free_handler);
1133 Put(KnownNameSpaces, HKEY("http://backend.userland.com/creativeCommonsRssModule"), NULL, reference_free_handler);
1134 Put(KnownNameSpaces, HKEY("http://purl.org/atom/ns#"), NULL, reference_free_handler);
1135 Put(KnownNameSpaces, HKEY("http://purl.org/dc/elements/1.1/"), NULL, reference_free_handler);
1136 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/"), NULL, reference_free_handler);
1137 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/content/"), NULL, reference_free_handler);
1138 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/slash/"), NULL, reference_free_handler);
1139 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/syndication/"), NULL, reference_free_handler);
1140 Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/"), NULL, reference_free_handler);
1141 Put(KnownNameSpaces, HKEY("http://purl.org/syndication/thread/1.0"), NULL, reference_free_handler);
1142 Put(KnownNameSpaces, HKEY("http://rssnamespace.org/feedburner/ext/1.0"), NULL, reference_free_handler);
1143 Put(KnownNameSpaces, HKEY("http://schemas.google.com/g/2005"), NULL, reference_free_handler);
1144 Put(KnownNameSpaces, HKEY("http://webns.net/mvcb/"), NULL, reference_free_handler);
1145 Put(KnownNameSpaces, HKEY("http://web.resource.org/cc/"), NULL, reference_free_handler);
1146 Put(KnownNameSpaces, HKEY("http://wellformedweb.org/CommentAPI/"), NULL, reference_free_handler);
1147 Put(KnownNameSpaces, HKEY("http://www.georss.org/georss"), NULL, reference_free_handler);
1148 Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/xhtml"), NULL, reference_free_handler);
1149 Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), NULL, reference_free_handler);
1150 Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), NULL, reference_free_handler);
1151 Put(KnownNameSpaces, HKEY("http://www.w3.org/2003/01/geo/wgs84_pos#"), NULL, reference_free_handler);
1152 Put(KnownNameSpaces, HKEY("http://www.w3.org/2005/Atom"), NULL, reference_free_handler);
1153 Put(KnownNameSpaces, HKEY("urn:flickr:"), NULL, reference_free_handler);
1155 /* we don't like these namespaces because of they shadow our usefull parameters. */
1156 Put(KnownNameSpaces, HKEY("http://search.yahoo.com/mrss/"), NULL, reference_free_handler);
1158 CtdlRegisterCleanupHook(rss_parser_cleanup);