* rewrite rss parser:
[citadel.git] / citadel / modules / rssclient / serv_rssclient.c
1 /*
2  * Bring external RSS feeds into rooms.
3  *
4  * Copyright (c) 2007-2010 by the citadel.org team
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <stdio.h>
24
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
27 # include <time.h>
28 #else
29 # if HAVE_SYS_TIME_H
30 #  include <sys/time.h>
31 # else
32 #  include <time.h>
33 # endif
34 #endif
35
36 #include <ctype.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <expat.h>
42 #include <curl/curl.h>
43 #include <libcitadel.h>
44 #include "citadel.h"
45 #include "server.h"
46 #include "citserver.h"
47 #include "support.h"
48 #include "config.h"
49 #include "threads.h"
50 #include "ctdl_module.h"
51 #include "clientsocket.h"
52 #include "msgbase.h"
53 #include "parsedate.h"
54 #include "database.h"
55 #include "citadel_dirs.h"
56 #include "md5.h"
57 #include "context.h"
58
59 typedef struct rssnetcfg rssnetcfg;
60 struct rssnetcfg {
61         rssnetcfg *next;
62         char url[256];
63         char *rooms;
64         time_t last_error_when;
65         int ItemType;
66 };
67
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)
72
73 typedef struct _rss_item {
74         char *roomlist;
75         int done_parsing;
76         StrBuf *guid;
77         StrBuf *title;
78         StrBuf *link;
79         StrBuf *linkTitle;
80         StrBuf *reLink;
81         StrBuf *reLinkTitle;
82         StrBuf *description;
83         time_t pubdate;
84         StrBuf *channel_title;
85         int item_tag_nesting;
86         StrBuf *author_or_creator;
87         StrBuf *author_url;
88 }rss_item;
89
90
91 typedef void (*rss_handler_func)(StrBuf *CData, 
92                                  rss_item *ri, 
93                                  rssnetcfg *Cfg, 
94                                  const char** Attr);
95
96 typedef struct __rss_xml_handler {
97         int Flags;
98         rss_handler_func Handler;
99 }rss_xml_handler;
100
101
102 typedef struct _rsscollection {
103         StrBuf *CData;
104         StrBuf *Key;
105
106         rss_item *Item;
107         rssnetcfg *Cfg;
108         
109         rss_xml_handler *Current;
110 } rsscollection;
111
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)
117 {
118         rss_xml_handler *h;
119         h = (rss_xml_handler*) malloc(sizeof (rss_xml_handler));
120         h->Flags = Flags;
121         h->Handler = Handler;
122         Put(StartHandlers, key, len, h, NULL);
123 }
124 void AddRSSEndHandler(rss_handler_func Handler, int Flags, const char *key, long len)
125 {
126         rss_xml_handler *h;
127         h = (rss_xml_handler*) malloc(sizeof (rss_xml_handler));
128         h->Flags = Flags;
129         h->Handler = Handler;
130         Put(EndHandlers, key, len, h, NULL);
131 }
132
133 #if 0
134 #ifdef HAVE_ICONV
135 #include <iconv.h>
136
137
138 /* 
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
141  */
142
143 /* Returns: 
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). 
151 */ 
152 static int 
153 fill_encoding_info (const char *charset, XML_Encoding * info) 
154
155   iconv_t cd = (iconv_t)(-1); 
156   int flag; 
157         CtdlLogPrintf(0, "RSS: fill encoding info ...\n");
158  
159 #if G_BYTE_ORDER == G_LITTLE_ENDIAN 
160   cd = iconv_open ("UCS-2LE", charset); 
161 #else 
162   cd = iconv_open ("UCS-2BE", charset); 
163 #endif 
164  
165   if (cd == (iconv_t) (-1)) 
166     { 
167       return -1; 
168     } 
169  
170   { 
171     unsigned short out; 
172     unsigned char buf[4]; 
173     unsigned int i0, i1, i2; 
174     int result; 
175     flag = 0; 
176     for (i0 = 0; i0 < 0x100; i0++) 
177       { 
178         buf[0] = i0; 
179         info->map[i0] = 0; 
180         //result = try (cd, buf, 1, &out); 
181         if (result < 0) 
182           { 
183           } 
184         else if (result > 0) 
185           { 
186             info->map[i0] = out; 
187           } 
188         else 
189           { 
190             for (i1 = 0; i1 < 0x100; i1++) 
191               { 
192                 buf[1] = i1; 
193                 ///result = try (cd, buf, 2, &out); 
194                 if (result < 0) 
195                   { 
196                   } 
197                 else if (result > 0) 
198                   { 
199                     flag++; 
200                     info->map[i0] = -2; 
201                   } 
202                 else 
203                   { 
204                     for (i2 = 0; i2 < 0x100; i2++) 
205                       { 
206                         buf[2] = i2; 
207                         ////result = try (cd, buf, 3, &out); 
208                         if (result < 0) 
209                           { 
210                           } 
211                         else if (result > 0) 
212                           { 
213                             flag++; 
214                             info->map[i0] = -3; 
215                           } 
216                       } 
217                   } 
218               } 
219           } 
220       } 
221   } 
222  
223   if (iconv_close (cd) < 0) 
224     { 
225       return -2; 
226     } 
227   return flag; 
228
229
230 static int 
231 iconv_convertor (void *data, const char *s) 
232
233   XML_Encoding *info = data; 
234   int res; 
235         CtdlLogPrintf(0, "RSS: Converting ...\n");
236
237   if (s == NULL) 
238     return -1; 
239 /*
240   GByteArray *result; 
241   result = g_byte_array_new (); 
242   if (process_block (info->data, (char *) s, strlen (s), result) == 0) 
243     { 
244       res = *(result->data); 
245       g_byte_array_free (result, TRUE); 
246       return res; 
247     } 
248   g_byte_array_free (result, TRUE); 
249 */
250   return -1; 
251
252
253 static void 
254 my_release (void *data) 
255
256   iconv_t cd = (iconv_t) data; 
257   if (iconv_close (cd) != 0) 
258     { 
259 /// TODO: uh no.      exit (1); 
260     } 
261
262 int 
263 handle_unknown_xml_encoding (void *encodingHandleData, 
264                              const XML_Char * name, 
265                              XML_Encoding * info) 
266
267   int result; 
268   CtdlLogPrintf(0, "RSS: unknown encoding ...\n");
269   result = fill_encoding_info (name, info); 
270   if (result >= 0) 
271     { 
272       /*  
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. 
275       */ 
276       if(encodingHandleData != NULL) 
277             *((iconv_t *)encodingHandleData) = iconv_open(name, "UTF-8"); 
278       /*  
279          Optimization: we do not need conversion function if encoding is one-to-one,  
280          info->map table will be enough  
281        */ 
282       if (result == 0) 
283         { 
284           info->data = NULL; 
285           info->convert = NULL; 
286           info->release = NULL; 
287           return 1; 
288         } 
289       /*  
290          We do need conversion function because this encoding uses multibyte sequences 
291        */ 
292       info->data = (void *) iconv_open ("UTF-8", name); 
293       if ((int)info->data == -1) 
294         return -1; 
295       info->convert = iconv_convertor; 
296       info->release = my_release; 
297       return 1; 
298     } 
299   if(encodingHandleData != NULL)  
300     *(iconv_t *)encodingHandleData = NULL; 
301   return 0; 
302
303
304 #endif
305 #endif
306
307 /*
308  * Commit a fetched and parsed RSS item to disk
309  */
310 void rss_save_item(rss_item *ri)
311 {
312
313         struct MD5Context md5context;
314         u_char rawdigest[MD5_DIGEST_LEN];
315         int i;
316         char utmsgid[SIZ];
317         struct cdbdata *cdbut;
318         struct UseTable ut;
319         struct CtdlMessage *msg;
320         struct recptypes *recp = NULL;
321         int msglen = 0;
322
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;
330    
331         /* Construct a GUID to use in the S_USETABLE table.
332          * If one is not present in the item itself, make one up.
333          */
334         if (ri->guid != NULL) {
335                 StrBufSpaceToBlank(ri->guid);
336                 StrBufTrim(ri->guid);
337                 snprintf(utmsgid, sizeof utmsgid, "rss/%s", ChrPtr(ri->guid));
338         }
339         else {
340                 MD5Init(&md5context);
341                 if (ri->title != NULL) {
342                         MD5Update(&md5context, (const unsigned char*)ChrPtr(ri->title), StrLength(ri->title));
343                 }
344                 if (ri->link != NULL) {
345                         MD5Update(&md5context, (const unsigned char*)ChrPtr(ri->link), StrLength(ri->link));
346                 }
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]);
352                 }
353                 strcat(utmsgid, "_rss2ctdl");
354         }
355
356         /* Find out if we've already seen this item */
357
358         cdbut = cdb_fetch(CDB_USETABLE, utmsgid, strlen(utmsgid));
359 #if 0
360         if (cdbut != NULL) {
361                 /* Item has already been seen */
362                 CtdlLogPrintf(CTDL_DEBUG, "%s has already been seen\n", utmsgid);
363                 cdb_free(cdbut);
364
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) );
369         }
370         else 
371 #endif
372 {
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;
382
383                 if (ri->guid != NULL) {
384                         msg->cm_fields['E'] = strdup(ChrPtr(ri->guid));
385                 }
386
387                 if (ri->author_or_creator != NULL) {
388                         char *From;
389                         StrBuf *Encoded, *QPEncoded;
390                         StrBuf *UserName;
391                         StrBuf *EmailAddress;
392                         StrBuf *EncBuf;
393                         
394                         UserName = NewStrBuf();
395                         EmailAddress = NewStrBuf();
396                         EncBuf = NewStrBuf();
397 ////TODO!
398                         From = html_to_ascii(ChrPtr(ri->author_or_creator),
399                                              StrLength(ri->author_or_creator), 
400                                              512, 0);
401
402                         Encoded = NewStrBufPlain(From, -1);
403                         free(From);
404                         StrBufTrim(Encoded);
405                         QPEncoded = StrBufSanitizeEmailRecipientVector(Encoded, UserName, EmailAddress, EncBuf);
406                         msg->cm_fields['A'] = SmashStrBuf(&QPEncoded);
407
408                         FreeStrBuf(&Encoded);
409                         FreeStrBuf(&UserName);
410                         FreeStrBuf(&EmailAddress);
411                         FreeStrBuf(&EncBuf);
412
413                 }
414                 else {
415                         msg->cm_fields['A'] = strdup("rss");
416                 }
417
418                 msg->cm_fields['N'] = strdup(NODENAME);
419                 if (ri->title != NULL) {
420                         long len;
421                         char *Sbj;
422                         StrBuf *Encoded, *QPEncoded;
423
424                         QPEncoded = NULL;
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);
429                         free(Sbj);
430
431                         StrBufTrim(Encoded);
432                         StrBufRFC2047encode(&QPEncoded, Encoded);
433
434                         msg->cm_fields['U'] = SmashStrBuf(&QPEncoded);
435                         FreeStrBuf(&Encoded);
436                 }
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));
442                         }
443                 }
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"
450                          "<html><body>\n"
451                          "%s<br><br>\n"
452                          "<a href=\"%s\">%s</a><br>\n"
453                          "<a href=\"%s\">%s</a>\n"
454                          "</body></html>\n"
455                          ,
456                          ChrPtr(ri->description),
457                          ChrPtr(ri->link),
458                          (StrLength(ri->linkTitle)>0)?ChrPtr(ri->linkTitle):ChrPtr(ri->link),
459                          ChrPtr(ri->reLink),
460                          (StrLength(ri->reLinkTitle)>0)?ChrPtr(ri->reLinkTitle):"Reply to this"
461                 );
462
463                 CtdlSubmitMsg(msg, recp, NULL, 0);
464                 CtdlFreeMessage(msg);
465
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) );
470         }
471         free_recipients(recp);
472 }
473
474
475
476 /*
477  * Convert an RDF/RSS datestamp into a time_t
478  */
479 time_t rdf_parsedate(const char *p)
480 {
481         struct tm tm;
482         time_t t = 0;
483
484         if (!p) return 0L;
485         if (strlen(p) < 10) return 0L;
486
487         memset(&tm, 0, sizeof tm);
488
489         /*
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
492          *
493          * This code, along with parsedate.c, is a potential candidate for
494          * moving into libcitadel.
495          */
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]);
503                 }
504                 return mktime(&tm);
505         }
506
507         /* hmm... try RFC822 date stamp format */
508
509         t = parsedate(p);
510         if (t > 0) return(t);
511
512         /* yeesh.  ok, just return the current date and time. */
513         return(time(NULL));
514 }
515
516 void flush_rss_item(rss_item *ri)
517 {
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);
525 }
526
527 void rss_xml_start(void *data, const char *supplied_el, const char **attr)
528 {
529         rss_xml_handler *h;
530         rsscollection   *rssc = (rsscollection*) data;
531         rssnetcfg       *Cfg = rssc->Cfg;
532         rss_item        *ri = rssc->Item;
533         void            *pv;
534         const char      *pel;
535         char            *sep = NULL;
536
537         /* Axe the namespace, we don't care about it */
538 ///     CtdlLogPrintf(0, "RSS: supplied el %d: %s...\n", rssc->Cfg->ItemType, supplied_el);
539         pel = supplied_el;
540         while (sep = strchr(pel, ':'), sep) {
541                 pel = sep + 1;
542         }
543
544         if (pel != supplied_el)
545         {
546                 void *v;
547                 
548                 if (!GetHash(KnownNameSpaces, 
549                              supplied_el, 
550                              pel - supplied_el - 1,
551                              &v))
552                 {
553                         CtdlLogPrintf(0, "RSS: START ignoring because of wrong namespace [%s] = [%s]\n", 
554                                       supplied_el);
555                         return;
556                 }
557         }
558
559         StrBufPlain(rssc->Key, pel, -1);
560         StrBufLowerCase(rssc->Key);
561         if (GetHash(StartHandlers, SKEY(rssc->Key), &pv))
562         {
563                 rssc->Current = h = (rss_xml_handler*) pv;
564
565                 if (((h->Flags & RSS_UNSET) != 0) && 
566                     (Cfg->ItemType == RSS_UNSET))
567                 {
568                         h->Handler(rssc->CData, ri, Cfg, attr);
569                 }
570                 else if (((h->Flags & RSS_RSS) != 0) &&
571                     (Cfg->ItemType == RSS_RSS))
572                 {
573                         h->Handler(rssc->CData, ri, Cfg, attr);
574                 }
575                 else if (((h->Flags & RSS_ATOM) != 0) &&
576                          (Cfg->ItemType == RSS_ATOM))
577                 {
578                         h->Handler(rssc->CData, ri, Cfg, attr);                 
579                 }
580                 else 
581                         CtdlLogPrintf(0, "RSS: START unhandled: [%s] [%s]...\n", pel, supplied_el);
582         }
583         else 
584                 CtdlLogPrintf(0, "RSS: START unhandled: [%s] [%s]...\n", pel,  supplied_el);
585
586 }
587
588 void rss_xml_end(void *data, const char *supplied_el)
589 {
590         rss_xml_handler *h;
591         rsscollection   *rssc = (rsscollection*) data;
592         rssnetcfg       *Cfg = rssc->Cfg;
593         rss_item        *ri = rssc->Item;
594         const char      *pel;
595         char            *sep = NULL;
596         void            *pv;
597
598         /* Axe the namespace, we don't care about it */
599         pel = supplied_el;
600         while (sep = strchr(pel, ':'), sep) {
601                 pel = sep + 1;
602         }
603 //      CtdlLogPrintf(0, "RSS: END %s...\n", el);
604         if (pel != supplied_el)
605         {
606                 void *v;
607                 
608                 if (!GetHash(KnownNameSpaces, 
609                              supplied_el, 
610                              pel - supplied_el - 1,
611                              &v))
612                 {
613                         CtdlLogPrintf(0, "RSS: END ignoring because of wrong namespace [%s] = [%s]\n", 
614                                       supplied_el, ChrPtr(rssc->CData));
615                         FlushStrBuf(rssc->CData);
616                         return;
617                 }
618         }
619
620         StrBufPlain(rssc->Key, pel, -1);
621         StrBufLowerCase(rssc->Key);
622         if (GetHash(EndHandlers, SKEY(rssc->Key), &pv))
623         {
624                 h = (rss_xml_handler*) pv;
625
626                 if (((h->Flags & RSS_UNSET) != 0) && 
627                     (Cfg->ItemType == RSS_UNSET))
628                 {
629                         h->Handler(rssc->CData, ri, Cfg, NULL);
630                 }
631                 else if (((h->Flags & RSS_RSS) != 0) &&
632                     (Cfg->ItemType == RSS_RSS))
633                 {
634                         h->Handler(rssc->CData, ri, Cfg, NULL);
635                 }
636                 else if (((h->Flags & RSS_ATOM) != 0) &&
637                          (Cfg->ItemType == RSS_ATOM))
638                 {
639                         h->Handler(rssc->CData, ri, Cfg, NULL);
640                 }
641                 else 
642                         CtdlLogPrintf(0, "RSS: END   unhandled: [%s]  [%s] = [%s]...\n", pel, supplied_el, ChrPtr(rssc->CData));
643                         
644         }
645         else 
646                 CtdlLogPrintf(0, "RSS: END   unhandled: [%s]  [%s] = [%s]...\n", pel, supplied_el, ChrPtr(rssc->CData));
647         FlushStrBuf(rssc->CData);
648         rssc->Current = NULL;
649 }
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666 void RSS_item_rss_start (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
667 {
668         CtdlLogPrintf(CTDL_DEBUG, "RSS: This is an RSS feed.\n");
669         Cfg->ItemType = RSS_RSS;
670 }
671
672 void RSS_item_rdf_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
673 {
674         CtdlLogPrintf(CTDL_DEBUG, "RSS: This is an RDF feed.\n");
675         Cfg->ItemType = RSS_RSS;
676 }
677
678 void ATOM_item_feed_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
679 {
680         CtdlLogPrintf(CTDL_DEBUG, "RSS: This is an ATOM feed.\n");
681         Cfg->ItemType = RSS_ATOM;
682 }
683
684
685 void RSS_item_item_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
686 {
687         ri->item_tag_nesting ++;
688         flush_rss_item(ri);
689 }
690
691 void ATOM_item_entry_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
692 {
693 /* Atom feed... */
694         ri->item_tag_nesting ++;
695         flush_rss_item(ri);
696 }
697
698 void ATOM_item_link_start (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
699 {
700         int i;
701         const char *pHref = NULL;
702         const char *pType = NULL;
703         const char *pRel = NULL;
704         const char *pTitle = NULL;
705
706         for (i = 0; Attr[i] != NULL; i+=2)
707         {
708                 if (!strcmp(Attr[i], "href"))
709                 {
710                         pHref = Attr[i+1];
711                 }
712                 else if (!strcmp(Attr[i], "rel"))
713                 {
714                         pRel = Attr[i+1];
715                 }
716                 else if (!strcmp(Attr[i], "type"))
717                 {
718                         pType = Attr[i+1];
719                 }
720                 else if (!strcmp(Attr[i], "title"))
721                 {
722                         pTitle = Attr[i+1];
723                 }
724         }
725         if (pHref == NULL)
726                 return; /* WHUT? Pointing... where? */
727         if ((pType != NULL) && !strcasecmp(pType, "application/atom+xml"))
728                 return; /* these just point to other rss resources, we're not interested in them. */
729         if (pRel != NULL)
730         {
731                 if (!strcasecmp (pRel, "replies"))
732                 {
733                         NewStrBufDupAppendFlush(&ri->reLink, NULL, pHref, -1);
734                         StrBufTrim(ri->link);
735                         NewStrBufDupAppendFlush(&ri->reLinkTitle, NULL, pTitle, -1);
736                 }
737                 else if (!strcasecmp(pRel, "alternate")) /* Alternative representation of this Item... */
738                 {
739                         NewStrBufDupAppendFlush(&ri->link, NULL, pHref, -1);
740                         StrBufTrim(ri->link);
741                         NewStrBufDupAppendFlush(&ri->linkTitle, NULL, pTitle, -1);
742
743                 }
744 #if 0 /* these are also defined, but dunno what to do with them.. */
745                 else if (!strcasecmp(pRel, "related"))
746                 {
747                 }
748                 else if (!strcasecmp(pRel, "self"))
749                 {
750                 }
751                 else if (!strcasecmp(pRel, "enclosure"))
752                 {/* this reference can get big, and is probably the full article... */
753                 }
754                 else if (!strcasecmp(pRel, "via"))
755                 {/* this article was provided via... */
756                 }
757 #endif
758         }
759         else if (StrLength(ri->link) == 0)
760         {
761                 NewStrBufDupAppendFlush(&ri->link, NULL, pHref, -1);
762                 StrBufTrim(ri->link);
763                 NewStrBufDupAppendFlush(&ri->linkTitle, NULL, pTitle, -1);
764         }
765 }
766
767
768
769
770 void ATOMRSS_item_title_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
771 {
772         if ((ri->item_tag_nesting == 0) && (StrLength(CData) > 0)) {
773                 NewStrBufDupAppendFlush(&ri->channel_title, CData, NULL, 0);
774                 StrBufTrim(ri->channel_title);
775         }
776 }
777
778 void RSS_item_guid_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
779 {
780         if (StrLength(CData) > 0) {
781                 NewStrBufDupAppendFlush(&ri->guid, CData, NULL, 0);
782         }
783 }
784
785 void ATOM_item_id_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
786 {
787         if (StrLength(CData) > 0) {
788                 NewStrBufDupAppendFlush(&ri->guid, CData, NULL, 0);
789         }
790 }
791
792
793 void RSS_item_link_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
794 {
795         if (StrLength(CData) > 0) {
796                 NewStrBufDupAppendFlush(&ri->link, CData, NULL, 0);
797                 StrBufTrim(ri->link);
798         }
799 }
800
801 void RSSATOM_item_title_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
802 {
803         if (StrLength(CData) > 0) {
804                 NewStrBufDupAppendFlush(&ri->title, CData, NULL, 0);
805                 StrBufTrim(ri->title);
806         }
807 }
808
809 void ATOM_item_content_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
810 {
811         if (StrLength(CData) > 0) {
812                 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
813                 StrBufTrim(ri->description);
814         }
815 }
816 void ATOM_item_summary_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
817 {
818         /* this can contain an abstract of the article. but we don't want to verwrite a full document if we already have it. */
819         if ((StrLength(CData) > 0) && (StrLength(ri->description) == 0))
820         {
821                 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
822                 StrBufTrim(ri->description);
823         }
824 }
825
826 void RSS_item_description_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
827 {
828         if (StrLength(CData) > 0) {
829                 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
830         }
831 }
832
833 void ATOM_item_published_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
834 {                 
835         if (StrLength(CData) > 0) {
836                 StrBufTrim(CData);
837                 ri->pubdate = rdf_parsedate(ChrPtr(CData));
838         }
839 }
840
841 void ATOM_item_updated_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
842 {
843         if (StrLength(CData) > 0) {
844                 StrBufTrim(CData);
845                 ri->pubdate = rdf_parsedate(ChrPtr(CData));
846         }
847 }
848
849 void RSS_item_pubdate_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
850 {
851         if (StrLength(CData) > 0) {
852                 StrBufTrim(CData);
853                 ri->pubdate = rdf_parsedate(ChrPtr(CData));
854         }
855 }
856
857
858 void RSS_item_date_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
859 {
860         if (StrLength(CData) > 0) {
861                 StrBufTrim(CData);
862                 ri->pubdate = rdf_parsedate(ChrPtr(CData));
863         }
864 }
865
866
867
868 void RSS_item_author_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
869 {
870         if (StrLength(CData) > 0) {
871                 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
872                 StrBufTrim(ri->author_or_creator);
873         }
874 }
875
876
877 void ATOM_item_name_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
878 {
879         if (StrLength(CData) > 0) {
880                 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
881                 StrBufTrim(ri->author_or_creator);
882         }
883 }
884
885 void RSS_item_creator_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
886 {
887         if ((StrLength(CData) > 0) && 
888             (StrLength(ri->author_or_creator) == 0))
889         {
890                 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
891                 StrBufTrim(ri->author_or_creator);
892         }
893 }
894
895
896 void ATOM_item_uri_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
897 {
898         if (StrLength(CData) > 0) {
899                 NewStrBufDupAppendFlush(&ri->author_url, CData, NULL, 0);
900                 StrBufTrim(ri->author_url);
901         }
902 }
903
904 void RSS_item_item_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
905 {
906         --ri->item_tag_nesting;
907         rss_save_item(ri);
908 }
909
910
911 void ATOM_item_entry_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
912 {
913         --ri->item_tag_nesting;
914         rss_save_item(ri);
915 }
916
917 void RSS_item_rss_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
918 {
919 //              CtdlLogPrintf(CTDL_DEBUG, "End of feed detected.  Closing parser.\n");
920         ri->done_parsing = 1;
921         
922 }
923 void RSS_item_rdf_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
924 {
925 //              CtdlLogPrintf(CTDL_DEBUG, "End of feed detected.  Closing parser.\n");
926         ri->done_parsing = 1;
927 }
928
929
930 void RSSATOM_item_ignore(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
931 {
932 }
933
934
935
936
937
938
939
940
941
942
943
944 /*
945  * This callback stores up the data which appears in between tags.
946  */
947 void rss_xml_cdata_start(void *data) 
948 {
949         rsscollection *rssc = (rsscollection*) data;
950
951         FlushStrBuf(rssc->CData);
952 }
953
954 void rss_xml_cdata_end(void *data) 
955 {
956         //rsscollection   *rssc = (rsscollection*) data;
957         //rss_item        *ri = rssc->Item;
958
959         /* Hm, it seems as if in some CDATA cases expat doesn't call the handler... * /
960         if (rssc->Current == NULL)
961                 NewStrBufDupAppendFlush(&ri->description, rssc->CData, NULL, 0);
962         */
963 }
964 void rss_xml_chardata(void *data, const XML_Char *s, int len) 
965 {
966         rsscollection *rssc = (rsscollection*) data;
967
968         StrBufAppendBufPlain (rssc->CData, s, len, 0);
969 }
970
971 /*
972  * Callback function for passing libcurl's output to expat for parsing
973  */
974 size_t rss_libcurl_callback(void *ptr, size_t size, size_t nmemb, void *stream)
975 {
976         XML_Parse((XML_Parser)stream, ptr, (size * nmemb), 0);
977         return (size*nmemb);
978 }
979
980
981
982 /*
983  * Begin a feed parse
984  */
985 void rss_do_fetching(rssnetcfg *Cfg) {
986         rsscollection rssc;
987         rss_item ri;
988         XML_Parser xp;
989
990         CURL *curl;
991         CURLcode res;
992         char errmsg[1024] = "";
993
994         memset(&ri, 0, sizeof(rss_item));
995         rssc.Item = &ri;
996         rssc.Cfg = Cfg;
997
998         CtdlLogPrintf(CTDL_DEBUG, "Fetching RSS feed <%s>\n", Cfg->url);
999
1000         curl = curl_easy_init();
1001         if (!curl) {
1002                 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
1003                 return;
1004         }
1005
1006         xp = XML_ParserCreateNS("UTF-8", ':');
1007         if (!xp) {
1008                 CtdlLogPrintf(CTDL_ALERT, "Cannot create XML parser!\n");
1009                 curl_easy_cleanup(curl);
1010                 return;
1011         }
1012         rssc.CData = NewStrBufPlain(NULL, SIZ);
1013         rssc.Key = NewStrBuf();
1014         curl_easy_setopt(curl, CURLOPT_URL, Cfg->url);
1015         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
1016         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
1017         curl_easy_setopt(curl, CURLOPT_WRITEDATA, xp);
1018         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rss_libcurl_callback);
1019         curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
1020         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
1021 #ifdef CURLOPT_HTTP_CONTENT_DECODING
1022         curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
1023         curl_easy_setopt(curl, CURLOPT_ENCODING, "");
1024 #endif
1025         curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
1026         curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180);           /* die after 180 seconds */
1027         if (
1028                 (!IsEmptyStr(config.c_ip_addr))
1029                 && (strcmp(config.c_ip_addr, "*"))
1030                 && (strcmp(config.c_ip_addr, "::"))
1031                 && (strcmp(config.c_ip_addr, "0.0.0.0"))
1032         ) {
1033                 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
1034         }
1035
1036         memset(&ri, 0, sizeof(rss_item));
1037         ri.roomlist = Cfg->rooms;
1038 #ifdef HAVE_ICONV
1039 #if 0
1040         XML_SetUnknownEncodingHandler(xp,
1041                                       handle_unknown_xml_encoding,
1042                                       NULL);
1043 #endif
1044 #endif
1045         XML_SetElementHandler(xp, rss_xml_start, rss_xml_end);
1046         XML_SetCharacterDataHandler(xp, rss_xml_chardata);
1047         XML_SetUserData(xp, &rssc);
1048         XML_SetCdataSectionHandler(xp,
1049                                    rss_xml_cdata_start,
1050                                    rss_xml_cdata_end);
1051         if (CtdlThreadCheckStop())
1052         {
1053                 XML_ParserFree(xp);
1054                 curl_easy_cleanup(curl);
1055                 return;
1056         }
1057         
1058         if (CtdlThreadCheckStop())
1059                 goto shutdown ;
1060
1061         res = curl_easy_perform(curl);
1062         if (res) {
1063                 CtdlLogPrintf(CTDL_ALERT, "libcurl error %d: %s\n", res, errmsg);
1064         }
1065
1066         if (CtdlThreadCheckStop())
1067                 goto shutdown ;
1068
1069         if (ri.done_parsing == 0)
1070                 XML_Parse(xp, "", 0, 1);
1071
1072
1073         CtdlLogPrintf(CTDL_ALERT, "RSS: XML Status [%s] \n", 
1074                       XML_ErrorString(
1075                               XML_GetErrorCode(xp)));
1076
1077 shutdown:
1078         curl_easy_cleanup(curl);
1079         XML_ParserFree(xp);
1080
1081         flush_rss_item(&ri);
1082         FreeStrBuf(&rssc.CData);
1083         FreeStrBuf(&rssc.Key);
1084 }
1085
1086
1087 /*
1088  * Scan a room's netconfig to determine whether it is requesting any RSS feeds
1089  */
1090 void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
1091 {
1092         char filename[PATH_MAX];
1093         char buf[1024];
1094         char instr[32];
1095         FILE *fp;
1096         char feedurl[256];
1097         rssnetcfg *rncptr = NULL;
1098         rssnetcfg *use_this_rncptr = NULL;
1099         int len = 0;
1100         char *ptr = NULL;
1101
1102         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1103
1104         if (CtdlThreadCheckStop())
1105                 return;
1106                 
1107         /* Only do net processing for rooms that have netconfigs */
1108         fp = fopen(filename, "r");
1109         if (fp == NULL) {
1110                 return;
1111         }
1112
1113         while (fgets(buf, sizeof buf, fp) != NULL && !CtdlThreadCheckStop()) {
1114                 buf[strlen(buf)-1] = 0;
1115
1116                 extract_token(instr, buf, 0, '|', sizeof instr);
1117                 if (!strcasecmp(instr, "rssclient")) {
1118
1119                         use_this_rncptr = NULL;
1120
1121                         extract_token(feedurl, buf, 1, '|', sizeof feedurl);
1122
1123                         /* If any other rooms have requested the same feed, then we will just add this
1124                          * room to the target list for that client request.
1125                          */
1126                         for (rncptr=rnclist; rncptr!=NULL; rncptr=rncptr->next) {
1127                                 if (!strcmp(rncptr->url, feedurl)) {
1128                                         use_this_rncptr = rncptr;
1129                                 }
1130                         }
1131
1132                         /* Otherwise create a new client request */
1133                         if (use_this_rncptr == NULL) {
1134                                 rncptr = (rssnetcfg *) malloc(sizeof(rssnetcfg));
1135                                 rncptr->ItemType = RSS_UNSET;
1136                                 if (rncptr != NULL) {
1137                                         rncptr->next = rnclist;
1138                                         safestrncpy(rncptr->url, feedurl, sizeof rncptr->url);
1139                                         rncptr->rooms = NULL;
1140                                         rnclist = rncptr;
1141                                         use_this_rncptr = rncptr;
1142                                 }
1143                         }
1144
1145                         /* Add the room name to the request */
1146                         if (use_this_rncptr != NULL) {
1147                                 if (use_this_rncptr->rooms == NULL) {
1148                                         rncptr->rooms = strdup(qrbuf->QRname);
1149                                 }
1150                                 else {
1151                                         len = strlen(use_this_rncptr->rooms) + strlen(qrbuf->QRname) + 5;
1152                                         ptr = realloc(use_this_rncptr->rooms, len);
1153                                         if (ptr != NULL) {
1154                                                 strcat(ptr, "|");
1155                                                 strcat(ptr, qrbuf->QRname);
1156                                                 use_this_rncptr->rooms = ptr;
1157                                         }
1158                                 }
1159                         }
1160                 }
1161
1162         }
1163
1164         fclose(fp);
1165
1166 }
1167
1168 /*
1169  * Scan for rooms that have RSS client requests configured
1170  */
1171 void *rssclient_scan(void *args) {
1172         static time_t last_run = 0L;
1173         static int doing_rssclient = 0;
1174         rssnetcfg *rptr = NULL;
1175         CitContext rssclientCC;
1176
1177         /* Give this thread its own private CitContext */
1178         CtdlFillSystemContext(&rssclientCC, "rssclient");
1179         citthread_setspecific(MyConKey, (void *)&rssclientCC );
1180
1181         /*
1182          * This is a simple concurrency check to make sure only one rssclient run
1183          * is done at a time.  We could do this with a mutex, but since we
1184          * don't really require extremely fine granularity here, we'll do it
1185          * with a static variable instead.
1186          */
1187         if (doing_rssclient) return NULL;
1188         doing_rssclient = 1;
1189
1190         CtdlLogPrintf(CTDL_DEBUG, "rssclient started\n");
1191         CtdlForEachRoom(rssclient_scan_room, NULL);
1192
1193         while (rnclist != NULL && !CtdlThreadCheckStop()) {
1194                 rss_do_fetching(rnclist);
1195                 rptr = rnclist;
1196                 rnclist = rnclist->next;
1197                 if (rptr->rooms != NULL) free(rptr->rooms);
1198                 free(rptr);
1199         }
1200
1201         CtdlLogPrintf(CTDL_DEBUG, "rssclient ended\n");
1202         last_run = time(NULL);
1203         doing_rssclient = 0;
1204         if (!CtdlThreadCheckStop())
1205                 CtdlThreadSchedule ("RSS Client", CTDLTHREAD_BIGSTACK, rssclient_scan, NULL, last_run + config.c_net_freq);
1206         else
1207                 CtdlLogPrintf(CTDL_DEBUG, "rssclient: Task STOPPED.\n");
1208         CtdlClearSystemContext();
1209         return NULL;
1210 }
1211
1212
1213 CTDL_MODULE_INIT(rssclient)
1214 {
1215         if (threading)
1216         {
1217                 CtdlLogPrintf(CTDL_INFO, "%s\n", curl_version());
1218                 CtdlThreadSchedule ("RSS Client", CTDLTHREAD_BIGSTACK, rssclient_scan, NULL, 0);
1219         }
1220
1221         StartHandlers = NewHash(1, NULL);
1222         EndHandlers = NewHash(1, NULL);
1223
1224         AddRSSStartHandler(RSS_item_rss_start,     RSS_UNSET, HKEY("rss"));
1225         AddRSSStartHandler(RSS_item_rdf_start,     RSS_UNSET, HKEY("rdf"));
1226         AddRSSStartHandler(ATOM_item_feed_start,    RSS_UNSET, HKEY("feed"));
1227         AddRSSStartHandler(RSS_item_item_start,    RSS_RSS, HKEY("item"));
1228         AddRSSStartHandler(ATOM_item_entry_start,  RSS_ATOM, HKEY("entry"));
1229         AddRSSStartHandler(ATOM_item_link_start,   RSS_ATOM, HKEY("link"));
1230
1231         AddRSSEndHandler(ATOMRSS_item_title_end,   RSS_ATOM|RSS_RSS|RSS_REQUIRE_BUF, HKEY("title"));
1232         AddRSSEndHandler(RSS_item_guid_end,        RSS_RSS|RSS_REQUIRE_BUF, HKEY("guid"));
1233         AddRSSEndHandler(ATOM_item_id_end,         RSS_ATOM|RSS_REQUIRE_BUF, HKEY("id"));
1234         AddRSSEndHandler(RSS_item_link_end,        RSS_RSS|RSS_REQUIRE_BUF, HKEY("link"));
1235         AddRSSEndHandler(RSSATOM_item_title_end,   RSS_ATOM|RSS_RSS|RSS_REQUIRE_BUF, HKEY("title"));
1236         AddRSSEndHandler(ATOM_item_content_end,    RSS_ATOM|RSS_REQUIRE_BUF, HKEY("content"));
1237         AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("encoded"));
1238         AddRSSEndHandler(ATOM_item_summary_end,    RSS_ATOM|RSS_REQUIRE_BUF, HKEY("summary"));
1239         AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("description"));
1240         AddRSSEndHandler(ATOM_item_published_end,  RSS_ATOM|RSS_REQUIRE_BUF, HKEY("published"));
1241         AddRSSEndHandler(ATOM_item_updated_end,    RSS_ATOM|RSS_REQUIRE_BUF, HKEY("updated"));
1242         AddRSSEndHandler(RSS_item_pubdate_end,     RSS_RSS|RSS_REQUIRE_BUF, HKEY("pubdate"));
1243         AddRSSEndHandler(RSS_item_date_end,        RSS_RSS|RSS_REQUIRE_BUF, HKEY("date"));
1244         AddRSSEndHandler(RSS_item_author_end,      RSS_RSS|RSS_REQUIRE_BUF, HKEY("author"));
1245         AddRSSEndHandler(RSS_item_creator_end,     RSS_RSS|RSS_REQUIRE_BUF, HKEY("creator"));
1246         AddRSSEndHandler(ATOM_item_name_end,       RSS_ATOM|RSS_REQUIRE_BUF, HKEY("name"));
1247         AddRSSEndHandler(ATOM_item_uri_end,        RSS_ATOM|RSS_REQUIRE_BUF, HKEY("uri"));
1248
1249         AddRSSEndHandler(RSS_item_item_end,        RSS_RSS, HKEY("item"));
1250         AddRSSEndHandler(RSS_item_rss_end,         RSS_RSS, HKEY("rss"));
1251         AddRSSEndHandler(RSS_item_rdf_end,         RSS_RSS, HKEY("rdf"));
1252         AddRSSEndHandler(ATOM_item_entry_end,      RSS_ATOM, HKEY("entry"));
1253
1254
1255 /* at the start of atoms: <seq> <li>link to resource</li></seq> ignore them. */
1256         AddRSSStartHandler(RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("seq"));
1257         AddRSSEndHandler  (RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("seq"));
1258         AddRSSStartHandler(RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("li"));
1259         AddRSSEndHandler  (RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("li"));
1260
1261 /* links to other feed generators... */
1262         AddRSSStartHandler(RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("feedflare"));
1263         AddRSSEndHandler  (RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("feedflare"));
1264         AddRSSStartHandler(RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("browserfriendly"));
1265         AddRSSEndHandler  (RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("browserfriendly"));
1266
1267         KnownNameSpaces = NewHash(1, NULL);
1268         Put(KnownNameSpaces, HKEY("http://a9.com/-/spec/opensearch/1.1/"), NULL, reference_free_handler);
1269         Put(KnownNameSpaces, HKEY("http://a9.com/-/spec/opensearchrss/1.0/"), NULL, reference_free_handler);
1270         Put(KnownNameSpaces, HKEY("http://backend.userland.com/creativeCommonsRssModule"), NULL, reference_free_handler);
1271         Put(KnownNameSpaces, HKEY("http://purl.org/atom/ns#"), NULL, reference_free_handler);
1272         Put(KnownNameSpaces, HKEY("http://purl.org/dc/elements/1.1/"), NULL, reference_free_handler);
1273         Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/"), NULL, reference_free_handler);
1274         Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/content/"), NULL, reference_free_handler);
1275         Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/slash/"), NULL, reference_free_handler);
1276         Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/syndication/"), NULL, reference_free_handler);
1277         Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/"), NULL, reference_free_handler);
1278         Put(KnownNameSpaces, HKEY("http://purl.org/syndication/thread/1.0"), NULL, reference_free_handler);
1279         Put(KnownNameSpaces, HKEY("http://rssnamespace.org/feedburner/ext/1.0"), NULL, reference_free_handler);
1280         Put(KnownNameSpaces, HKEY("http://schemas.google.com/g/2005"), NULL, reference_free_handler);
1281         Put(KnownNameSpaces, HKEY("http://webns.net/mvcb/"), NULL, reference_free_handler);
1282         Put(KnownNameSpaces, HKEY("http://web.resource.org/cc/"), NULL, reference_free_handler);
1283         Put(KnownNameSpaces, HKEY("http://wellformedweb.org/CommentAPI/"), NULL, reference_free_handler);
1284         Put(KnownNameSpaces, HKEY("http://www.georss.org/georss"), NULL, reference_free_handler);
1285         Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/xhtml"), NULL, reference_free_handler);
1286         Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), NULL, reference_free_handler);
1287         Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), NULL, reference_free_handler);
1288         Put(KnownNameSpaces, HKEY("http://www.w3.org/2003/01/geo/wgs84_pos#"), NULL, reference_free_handler);
1289         Put(KnownNameSpaces, HKEY("http://www.w3.org/2005/Atom"), NULL, reference_free_handler);
1290         Put(KnownNameSpaces, HKEY("urn:flickr:"), NULL, reference_free_handler);
1291 #if 0
1292         /* we don't like these namespaces because of they shadow our usefull parameters. */
1293         Put(KnownNameSpaces, HKEY("http://search.yahoo.com/mrss/"), NULL, reference_free_handler);
1294 #endif
1295         return "rssclient";
1296 }