Merge branch 'master' of ssh://git.citadel.org/appl/gitroot/citadel
[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 #ifndef DEBUG_RSS
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 #ifdef DEBUG_RSS
554                         CtdlLogPrintf(0, "RSS: START ignoring because of wrong namespace [%s] = [%s]\n", 
555                                       supplied_el);
556 #endif
557                         return;
558                 }
559         }
560
561         StrBufPlain(rssc->Key, pel, -1);
562         StrBufLowerCase(rssc->Key);
563         if (GetHash(StartHandlers, SKEY(rssc->Key), &pv))
564         {
565                 rssc->Current = h = (rss_xml_handler*) pv;
566
567                 if (((h->Flags & RSS_UNSET) != 0) && 
568                     (Cfg->ItemType == RSS_UNSET))
569                 {
570                         h->Handler(rssc->CData, ri, Cfg, attr);
571                 }
572                 else if (((h->Flags & RSS_RSS) != 0) &&
573                     (Cfg->ItemType == RSS_RSS))
574                 {
575                         h->Handler(rssc->CData, ri, Cfg, attr);
576                 }
577                 else if (((h->Flags & RSS_ATOM) != 0) &&
578                          (Cfg->ItemType == RSS_ATOM))
579                 {
580                         h->Handler(rssc->CData, ri, Cfg, attr);                 
581                 }
582 #ifdef DEBUG_RSS
583                 else 
584                         CtdlLogPrintf(0, "RSS: START unhandled: [%s] [%s]...\n", pel, supplied_el);
585 #endif
586         }
587 #ifdef DEBUG_RSS
588         else 
589                 CtdlLogPrintf(0, "RSS: START unhandled: [%s] [%s]...\n", pel,  supplied_el);
590 #endif
591 }
592
593 void rss_xml_end(void *data, const char *supplied_el)
594 {
595         rss_xml_handler *h;
596         rsscollection   *rssc = (rsscollection*) data;
597         rssnetcfg       *Cfg = rssc->Cfg;
598         rss_item        *ri = rssc->Item;
599         const char      *pel;
600         char            *sep = NULL;
601         void            *pv;
602
603         /* Axe the namespace, we don't care about it */
604         pel = supplied_el;
605         while (sep = strchr(pel, ':'), sep) {
606                 pel = sep + 1;
607         }
608 //      CtdlLogPrintf(0, "RSS: END %s...\n", el);
609         if (pel != supplied_el)
610         {
611                 void *v;
612                 
613                 if (!GetHash(KnownNameSpaces, 
614                              supplied_el, 
615                              pel - supplied_el - 1,
616                              &v))
617                 {
618 #ifdef DEBUG_RSS
619                         CtdlLogPrintf(0, "RSS: END ignoring because of wrong namespace [%s] = [%s]\n", 
620                                       supplied_el, ChrPtr(rssc->CData));
621 #endif
622                         FlushStrBuf(rssc->CData);
623                         return;
624                 }
625         }
626
627         StrBufPlain(rssc->Key, pel, -1);
628         StrBufLowerCase(rssc->Key);
629         if (GetHash(EndHandlers, SKEY(rssc->Key), &pv))
630         {
631                 h = (rss_xml_handler*) pv;
632
633                 if (((h->Flags & RSS_UNSET) != 0) && 
634                     (Cfg->ItemType == RSS_UNSET))
635                 {
636                         h->Handler(rssc->CData, ri, Cfg, NULL);
637                 }
638                 else if (((h->Flags & RSS_RSS) != 0) &&
639                     (Cfg->ItemType == RSS_RSS))
640                 {
641                         h->Handler(rssc->CData, ri, Cfg, NULL);
642                 }
643                 else if (((h->Flags & RSS_ATOM) != 0) &&
644                          (Cfg->ItemType == RSS_ATOM))
645                 {
646                         h->Handler(rssc->CData, ri, Cfg, NULL);
647                 }
648 #ifdef DEBUG_RSS
649                 else 
650                         CtdlLogPrintf(0, "RSS: END   unhandled: [%s]  [%s] = [%s]...\n", pel, supplied_el, ChrPtr(rssc->CData));
651 #endif
652         }
653 #ifdef DEBUG_RSS
654         else 
655                 CtdlLogPrintf(0, "RSS: END   unhandled: [%s]  [%s] = [%s]...\n", pel, supplied_el, ChrPtr(rssc->CData));
656 #endif
657         FlushStrBuf(rssc->CData);
658         rssc->Current = NULL;
659 }
660
661
662
663
664
665 void RSS_item_rss_start (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
666 {
667         CtdlLogPrintf(CTDL_DEBUG, "RSS: This is an RSS feed.\n");
668         Cfg->ItemType = RSS_RSS;
669 }
670
671 void RSS_item_rdf_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
672 {
673         CtdlLogPrintf(CTDL_DEBUG, "RSS: This is an RDF feed.\n");
674         Cfg->ItemType = RSS_RSS;
675 }
676
677 void ATOM_item_feed_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
678 {
679         CtdlLogPrintf(CTDL_DEBUG, "RSS: This is an ATOM feed.\n");
680         Cfg->ItemType = RSS_ATOM;
681 }
682
683
684 void RSS_item_item_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
685 {
686         ri->item_tag_nesting ++;
687         flush_rss_item(ri);
688 }
689
690 void ATOM_item_entry_start(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
691 {
692 /* Atom feed... */
693         ri->item_tag_nesting ++;
694         flush_rss_item(ri);
695 }
696
697 void ATOM_item_link_start (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
698 {
699         int i;
700         const char *pHref = NULL;
701         const char *pType = NULL;
702         const char *pRel = NULL;
703         const char *pTitle = NULL;
704
705         for (i = 0; Attr[i] != NULL; i+=2)
706         {
707                 if (!strcmp(Attr[i], "href"))
708                 {
709                         pHref = Attr[i+1];
710                 }
711                 else if (!strcmp(Attr[i], "rel"))
712                 {
713                         pRel = Attr[i+1];
714                 }
715                 else if (!strcmp(Attr[i], "type"))
716                 {
717                         pType = Attr[i+1];
718                 }
719                 else if (!strcmp(Attr[i], "title"))
720                 {
721                         pTitle = Attr[i+1];
722                 }
723         }
724         if (pHref == NULL)
725                 return; /* WHUT? Pointing... where? */
726         if ((pType != NULL) && !strcasecmp(pType, "application/atom+xml"))
727                 return; /* these just point to other rss resources, we're not interested in them. */
728         if (pRel != NULL)
729         {
730                 if (!strcasecmp (pRel, "replies"))
731                 {
732                         NewStrBufDupAppendFlush(&ri->reLink, NULL, pHref, -1);
733                         StrBufTrim(ri->link);
734                         NewStrBufDupAppendFlush(&ri->reLinkTitle, NULL, pTitle, -1);
735                 }
736                 else if (!strcasecmp(pRel, "alternate")) /* Alternative representation of this Item... */
737                 {
738                         NewStrBufDupAppendFlush(&ri->link, NULL, pHref, -1);
739                         StrBufTrim(ri->link);
740                         NewStrBufDupAppendFlush(&ri->linkTitle, NULL, pTitle, -1);
741
742                 }
743 #if 0 /* these are also defined, but dunno what to do with them.. */
744                 else if (!strcasecmp(pRel, "related"))
745                 {
746                 }
747                 else if (!strcasecmp(pRel, "self"))
748                 {
749                 }
750                 else if (!strcasecmp(pRel, "enclosure"))
751                 {/* this reference can get big, and is probably the full article... */
752                 }
753                 else if (!strcasecmp(pRel, "via"))
754                 {/* this article was provided via... */
755                 }
756 #endif
757         }
758         else if (StrLength(ri->link) == 0)
759         {
760                 NewStrBufDupAppendFlush(&ri->link, NULL, pHref, -1);
761                 StrBufTrim(ri->link);
762                 NewStrBufDupAppendFlush(&ri->linkTitle, NULL, pTitle, -1);
763         }
764 }
765
766
767
768
769 void ATOMRSS_item_title_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
770 {
771         if ((ri->item_tag_nesting == 0) && (StrLength(CData) > 0)) {
772                 NewStrBufDupAppendFlush(&ri->channel_title, CData, NULL, 0);
773                 StrBufTrim(ri->channel_title);
774         }
775 }
776
777 void RSS_item_guid_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
778 {
779         if (StrLength(CData) > 0) {
780                 NewStrBufDupAppendFlush(&ri->guid, CData, NULL, 0);
781         }
782 }
783
784 void ATOM_item_id_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
785 {
786         if (StrLength(CData) > 0) {
787                 NewStrBufDupAppendFlush(&ri->guid, CData, NULL, 0);
788         }
789 }
790
791
792 void RSS_item_link_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
793 {
794         if (StrLength(CData) > 0) {
795                 NewStrBufDupAppendFlush(&ri->link, CData, NULL, 0);
796                 StrBufTrim(ri->link);
797         }
798 }
799
800 void RSSATOM_item_title_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
801 {
802         if (StrLength(CData) > 0) {
803                 NewStrBufDupAppendFlush(&ri->title, CData, NULL, 0);
804                 StrBufTrim(ri->title);
805         }
806 }
807
808 void ATOM_item_content_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
809 {
810         if (StrLength(CData) > 0) {
811                 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
812                 StrBufTrim(ri->description);
813         }
814 }
815 void ATOM_item_summary_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
816 {
817         /* this can contain an abstract of the article. but we don't want to verwrite a full document if we already have it. */
818         if ((StrLength(CData) > 0) && (StrLength(ri->description) == 0))
819         {
820                 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
821                 StrBufTrim(ri->description);
822         }
823 }
824
825 void RSS_item_description_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
826 {
827         if (StrLength(CData) > 0) {
828                 NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
829         }
830 }
831
832 void ATOM_item_published_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
833 {                 
834         if (StrLength(CData) > 0) {
835                 StrBufTrim(CData);
836                 ri->pubdate = rdf_parsedate(ChrPtr(CData));
837         }
838 }
839
840 void ATOM_item_updated_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
841 {
842         if (StrLength(CData) > 0) {
843                 StrBufTrim(CData);
844                 ri->pubdate = rdf_parsedate(ChrPtr(CData));
845         }
846 }
847
848 void RSS_item_pubdate_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
849 {
850         if (StrLength(CData) > 0) {
851                 StrBufTrim(CData);
852                 ri->pubdate = rdf_parsedate(ChrPtr(CData));
853         }
854 }
855
856
857 void RSS_item_date_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
858 {
859         if (StrLength(CData) > 0) {
860                 StrBufTrim(CData);
861                 ri->pubdate = rdf_parsedate(ChrPtr(CData));
862         }
863 }
864
865
866
867 void RSS_item_author_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
868 {
869         if (StrLength(CData) > 0) {
870                 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
871                 StrBufTrim(ri->author_or_creator);
872         }
873 }
874
875
876 void ATOM_item_name_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
877 {
878         if (StrLength(CData) > 0) {
879                 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
880                 StrBufTrim(ri->author_or_creator);
881         }
882 }
883
884 void RSS_item_creator_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
885 {
886         if ((StrLength(CData) > 0) && 
887             (StrLength(ri->author_or_creator) == 0))
888         {
889                 NewStrBufDupAppendFlush(&ri->author_or_creator, CData, NULL, 0);
890                 StrBufTrim(ri->author_or_creator);
891         }
892 }
893
894
895 void ATOM_item_uri_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
896 {
897         if (StrLength(CData) > 0) {
898                 NewStrBufDupAppendFlush(&ri->author_url, CData, NULL, 0);
899                 StrBufTrim(ri->author_url);
900         }
901 }
902
903 void RSS_item_item_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
904 {
905         --ri->item_tag_nesting;
906         rss_save_item(ri);
907 }
908
909
910 void ATOM_item_entry_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
911 {
912         --ri->item_tag_nesting;
913         rss_save_item(ri);
914 }
915
916 void RSS_item_rss_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
917 {
918 //              CtdlLogPrintf(CTDL_DEBUG, "End of feed detected.  Closing parser.\n");
919         ri->done_parsing = 1;
920         
921 }
922 void RSS_item_rdf_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
923 {
924 //              CtdlLogPrintf(CTDL_DEBUG, "End of feed detected.  Closing parser.\n");
925         ri->done_parsing = 1;
926 }
927
928
929 void RSSATOM_item_ignore(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
930 {
931 }
932
933
934
935 /*
936  * This callback stores up the data which appears in between tags.
937  */
938 void rss_xml_cdata_start(void *data) 
939 {
940         rsscollection *rssc = (rsscollection*) data;
941
942         FlushStrBuf(rssc->CData);
943 }
944
945 void rss_xml_cdata_end(void *data) 
946 {
947 }
948 void rss_xml_chardata(void *data, const XML_Char *s, int len) 
949 {
950         rsscollection *rssc = (rsscollection*) data;
951
952         StrBufAppendBufPlain (rssc->CData, s, len, 0);
953 }
954
955 /*
956  * Callback function for passing libcurl's output to expat for parsing
957  */
958 size_t rss_libcurl_callback(void *ptr, size_t size, size_t nmemb, void *stream)
959 {
960         XML_Parse((XML_Parser)stream, ptr, (size * nmemb), 0);
961         return (size*nmemb);
962 }
963
964
965
966 /*
967  * Begin a feed parse
968  */
969 void rss_do_fetching(rssnetcfg *Cfg) {
970         rsscollection rssc;
971         rss_item ri;
972         XML_Parser xp;
973
974         CURL *curl;
975         CURLcode res;
976         char errmsg[1024] = "";
977
978         memset(&ri, 0, sizeof(rss_item));
979         rssc.Item = &ri;
980         rssc.Cfg = Cfg;
981
982         CtdlLogPrintf(CTDL_DEBUG, "Fetching RSS feed <%s>\n", Cfg->url);
983
984         curl = curl_easy_init();
985         if (!curl) {
986                 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
987                 return;
988         }
989
990         xp = XML_ParserCreateNS("UTF-8", ':');
991         if (!xp) {
992                 CtdlLogPrintf(CTDL_ALERT, "Cannot create XML parser!\n");
993                 curl_easy_cleanup(curl);
994                 return;
995         }
996         rssc.CData = NewStrBufPlain(NULL, SIZ);
997         rssc.Key = NewStrBuf();
998         curl_easy_setopt(curl, CURLOPT_URL, Cfg->url);
999         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
1000         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
1001         curl_easy_setopt(curl, CURLOPT_WRITEDATA, xp);
1002         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rss_libcurl_callback);
1003         curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
1004         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
1005 #ifdef CURLOPT_HTTP_CONTENT_DECODING
1006         curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
1007         curl_easy_setopt(curl, CURLOPT_ENCODING, "");
1008 #endif
1009         curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
1010         curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180);           /* die after 180 seconds */
1011         if (
1012                 (!IsEmptyStr(config.c_ip_addr))
1013                 && (strcmp(config.c_ip_addr, "*"))
1014                 && (strcmp(config.c_ip_addr, "::"))
1015                 && (strcmp(config.c_ip_addr, "0.0.0.0"))
1016         ) {
1017                 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
1018         }
1019
1020         memset(&ri, 0, sizeof(rss_item));
1021         ri.roomlist = Cfg->rooms;
1022 #ifdef HAVE_ICONV
1023 #if 0
1024         XML_SetUnknownEncodingHandler(xp,
1025                                       handle_unknown_xml_encoding,
1026                                       NULL);
1027 #endif
1028 #endif
1029         XML_SetElementHandler(xp, rss_xml_start, rss_xml_end);
1030         XML_SetCharacterDataHandler(xp, rss_xml_chardata);
1031         XML_SetUserData(xp, &rssc);
1032         XML_SetCdataSectionHandler(xp,
1033                                    rss_xml_cdata_start,
1034                                    rss_xml_cdata_end);
1035         if (CtdlThreadCheckStop())
1036         {
1037                 XML_ParserFree(xp);
1038                 curl_easy_cleanup(curl);
1039                 return;
1040         }
1041         
1042         if (CtdlThreadCheckStop())
1043                 goto shutdown ;
1044
1045         res = curl_easy_perform(curl);
1046         if (res) {
1047                 CtdlLogPrintf(CTDL_ALERT, "libcurl error %d: %s\n", res, errmsg);
1048         }
1049
1050         if (CtdlThreadCheckStop())
1051                 goto shutdown ;
1052
1053         if (ri.done_parsing == 0)
1054                 XML_Parse(xp, "", 0, 1);
1055
1056
1057         CtdlLogPrintf(CTDL_ALERT, "RSS: XML Status [%s] \n", 
1058                       XML_ErrorString(
1059                               XML_GetErrorCode(xp)));
1060
1061 shutdown:
1062         curl_easy_cleanup(curl);
1063         XML_ParserFree(xp);
1064
1065         flush_rss_item(&ri);
1066         FreeStrBuf(&rssc.CData);
1067         FreeStrBuf(&rssc.Key);
1068 }
1069
1070
1071 /*
1072  * Scan a room's netconfig to determine whether it is requesting any RSS feeds
1073  */
1074 void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
1075 {
1076         char filename[PATH_MAX];
1077         char buf[1024];
1078         char instr[32];
1079         FILE *fp;
1080         char feedurl[256];
1081         rssnetcfg *rncptr = NULL;
1082         rssnetcfg *use_this_rncptr = NULL;
1083         int len = 0;
1084         char *ptr = NULL;
1085
1086         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1087
1088         if (CtdlThreadCheckStop())
1089                 return;
1090                 
1091         /* Only do net processing for rooms that have netconfigs */
1092         fp = fopen(filename, "r");
1093         if (fp == NULL) {
1094                 return;
1095         }
1096
1097         while (fgets(buf, sizeof buf, fp) != NULL && !CtdlThreadCheckStop()) {
1098                 buf[strlen(buf)-1] = 0;
1099
1100                 extract_token(instr, buf, 0, '|', sizeof instr);
1101                 if (!strcasecmp(instr, "rssclient")) {
1102
1103                         use_this_rncptr = NULL;
1104
1105                         extract_token(feedurl, buf, 1, '|', sizeof feedurl);
1106
1107                         /* If any other rooms have requested the same feed, then we will just add this
1108                          * room to the target list for that client request.
1109                          */
1110                         for (rncptr=rnclist; rncptr!=NULL; rncptr=rncptr->next) {
1111                                 if (!strcmp(rncptr->url, feedurl)) {
1112                                         use_this_rncptr = rncptr;
1113                                 }
1114                         }
1115
1116                         /* Otherwise create a new client request */
1117                         if (use_this_rncptr == NULL) {
1118                                 rncptr = (rssnetcfg *) malloc(sizeof(rssnetcfg));
1119                                 rncptr->ItemType = RSS_UNSET;
1120                                 if (rncptr != NULL) {
1121                                         rncptr->next = rnclist;
1122                                         safestrncpy(rncptr->url, feedurl, sizeof rncptr->url);
1123                                         rncptr->rooms = NULL;
1124                                         rnclist = rncptr;
1125                                         use_this_rncptr = rncptr;
1126                                 }
1127                         }
1128
1129                         /* Add the room name to the request */
1130                         if (use_this_rncptr != NULL) {
1131                                 if (use_this_rncptr->rooms == NULL) {
1132                                         rncptr->rooms = strdup(qrbuf->QRname);
1133                                 }
1134                                 else {
1135                                         len = strlen(use_this_rncptr->rooms) + strlen(qrbuf->QRname) + 5;
1136                                         ptr = realloc(use_this_rncptr->rooms, len);
1137                                         if (ptr != NULL) {
1138                                                 strcat(ptr, "|");
1139                                                 strcat(ptr, qrbuf->QRname);
1140                                                 use_this_rncptr->rooms = ptr;
1141                                         }
1142                                 }
1143                         }
1144                 }
1145
1146         }
1147
1148         fclose(fp);
1149
1150 }
1151
1152 /*
1153  * Scan for rooms that have RSS client requests configured
1154  */
1155 void *rssclient_scan(void *args) {
1156         static time_t last_run = 0L;
1157         static int doing_rssclient = 0;
1158         rssnetcfg *rptr = NULL;
1159         CitContext rssclientCC;
1160
1161         /* Give this thread its own private CitContext */
1162         CtdlFillSystemContext(&rssclientCC, "rssclient");
1163         citthread_setspecific(MyConKey, (void *)&rssclientCC );
1164
1165         /*
1166          * This is a simple concurrency check to make sure only one rssclient run
1167          * is done at a time.  We could do this with a mutex, but since we
1168          * don't really require extremely fine granularity here, we'll do it
1169          * with a static variable instead.
1170          */
1171         if (doing_rssclient) return NULL;
1172         doing_rssclient = 1;
1173
1174         CtdlLogPrintf(CTDL_DEBUG, "rssclient started\n");
1175         CtdlForEachRoom(rssclient_scan_room, NULL);
1176
1177         while (rnclist != NULL && !CtdlThreadCheckStop()) {
1178                 rss_do_fetching(rnclist);
1179                 rptr = rnclist;
1180                 rnclist = rnclist->next;
1181                 if (rptr->rooms != NULL) free(rptr->rooms);
1182                 free(rptr);
1183         }
1184
1185         CtdlLogPrintf(CTDL_DEBUG, "rssclient ended\n");
1186         last_run = time(NULL);
1187         doing_rssclient = 0;
1188         if (!CtdlThreadCheckStop())
1189                 CtdlThreadSchedule ("RSS Client", CTDLTHREAD_BIGSTACK, rssclient_scan, NULL, last_run + config.c_net_freq);
1190         else
1191                 CtdlLogPrintf(CTDL_DEBUG, "rssclient: Task STOPPED.\n");
1192         CtdlClearSystemContext();
1193         return NULL;
1194 }
1195
1196
1197 CTDL_MODULE_INIT(rssclient)
1198 {
1199         if (threading)
1200         {
1201                 CtdlLogPrintf(CTDL_INFO, "%s\n", curl_version());
1202                 CtdlThreadSchedule ("RSS Client", CTDLTHREAD_BIGSTACK, rssclient_scan, NULL, 0);
1203         }
1204
1205         StartHandlers = NewHash(1, NULL);
1206         EndHandlers = NewHash(1, NULL);
1207
1208         AddRSSStartHandler(RSS_item_rss_start,     RSS_UNSET, HKEY("rss"));
1209         AddRSSStartHandler(RSS_item_rdf_start,     RSS_UNSET, HKEY("rdf"));
1210         AddRSSStartHandler(ATOM_item_feed_start,    RSS_UNSET, HKEY("feed"));
1211         AddRSSStartHandler(RSS_item_item_start,    RSS_RSS, HKEY("item"));
1212         AddRSSStartHandler(ATOM_item_entry_start,  RSS_ATOM, HKEY("entry"));
1213         AddRSSStartHandler(ATOM_item_link_start,   RSS_ATOM, HKEY("link"));
1214
1215         AddRSSEndHandler(ATOMRSS_item_title_end,   RSS_ATOM|RSS_RSS|RSS_REQUIRE_BUF, HKEY("title"));
1216         AddRSSEndHandler(RSS_item_guid_end,        RSS_RSS|RSS_REQUIRE_BUF, HKEY("guid"));
1217         AddRSSEndHandler(ATOM_item_id_end,         RSS_ATOM|RSS_REQUIRE_BUF, HKEY("id"));
1218         AddRSSEndHandler(RSS_item_link_end,        RSS_RSS|RSS_REQUIRE_BUF, HKEY("link"));
1219         AddRSSEndHandler(RSSATOM_item_title_end,   RSS_ATOM|RSS_RSS|RSS_REQUIRE_BUF, HKEY("title"));
1220         AddRSSEndHandler(ATOM_item_content_end,    RSS_ATOM|RSS_REQUIRE_BUF, HKEY("content"));
1221         AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("encoded"));
1222         AddRSSEndHandler(ATOM_item_summary_end,    RSS_ATOM|RSS_REQUIRE_BUF, HKEY("summary"));
1223         AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("description"));
1224         AddRSSEndHandler(ATOM_item_published_end,  RSS_ATOM|RSS_REQUIRE_BUF, HKEY("published"));
1225         AddRSSEndHandler(ATOM_item_updated_end,    RSS_ATOM|RSS_REQUIRE_BUF, HKEY("updated"));
1226         AddRSSEndHandler(RSS_item_pubdate_end,     RSS_RSS|RSS_REQUIRE_BUF, HKEY("pubdate"));
1227         AddRSSEndHandler(RSS_item_date_end,        RSS_RSS|RSS_REQUIRE_BUF, HKEY("date"));
1228         AddRSSEndHandler(RSS_item_author_end,      RSS_RSS|RSS_REQUIRE_BUF, HKEY("author"));
1229         AddRSSEndHandler(RSS_item_creator_end,     RSS_RSS|RSS_REQUIRE_BUF, HKEY("creator"));
1230         AddRSSEndHandler(ATOM_item_name_end,       RSS_ATOM|RSS_REQUIRE_BUF, HKEY("name"));
1231         AddRSSEndHandler(ATOM_item_uri_end,        RSS_ATOM|RSS_REQUIRE_BUF, HKEY("uri"));
1232
1233         AddRSSEndHandler(RSS_item_item_end,        RSS_RSS, HKEY("item"));
1234         AddRSSEndHandler(RSS_item_rss_end,         RSS_RSS, HKEY("rss"));
1235         AddRSSEndHandler(RSS_item_rdf_end,         RSS_RSS, HKEY("rdf"));
1236         AddRSSEndHandler(ATOM_item_entry_end,      RSS_ATOM, HKEY("entry"));
1237
1238
1239 /* at the start of atoms: <seq> <li>link to resource</li></seq> ignore them. */
1240         AddRSSStartHandler(RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("seq"));
1241         AddRSSEndHandler  (RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("seq"));
1242         AddRSSStartHandler(RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("li"));
1243         AddRSSEndHandler  (RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("li"));
1244
1245 /* links to other feed generators... */
1246         AddRSSStartHandler(RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("feedflare"));
1247         AddRSSEndHandler  (RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("feedflare"));
1248         AddRSSStartHandler(RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("browserfriendly"));
1249         AddRSSEndHandler  (RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("browserfriendly"));
1250
1251         KnownNameSpaces = NewHash(1, NULL);
1252         Put(KnownNameSpaces, HKEY("http://a9.com/-/spec/opensearch/1.1/"), NULL, reference_free_handler);
1253         Put(KnownNameSpaces, HKEY("http://a9.com/-/spec/opensearchrss/1.0/"), NULL, reference_free_handler);
1254         Put(KnownNameSpaces, HKEY("http://backend.userland.com/creativeCommonsRssModule"), NULL, reference_free_handler);
1255         Put(KnownNameSpaces, HKEY("http://purl.org/atom/ns#"), NULL, reference_free_handler);
1256         Put(KnownNameSpaces, HKEY("http://purl.org/dc/elements/1.1/"), NULL, reference_free_handler);
1257         Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/"), NULL, reference_free_handler);
1258         Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/content/"), NULL, reference_free_handler);
1259         Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/slash/"), NULL, reference_free_handler);
1260         Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/syndication/"), NULL, reference_free_handler);
1261         Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/"), NULL, reference_free_handler);
1262         Put(KnownNameSpaces, HKEY("http://purl.org/syndication/thread/1.0"), NULL, reference_free_handler);
1263         Put(KnownNameSpaces, HKEY("http://rssnamespace.org/feedburner/ext/1.0"), NULL, reference_free_handler);
1264         Put(KnownNameSpaces, HKEY("http://schemas.google.com/g/2005"), NULL, reference_free_handler);
1265         Put(KnownNameSpaces, HKEY("http://webns.net/mvcb/"), NULL, reference_free_handler);
1266         Put(KnownNameSpaces, HKEY("http://web.resource.org/cc/"), NULL, reference_free_handler);
1267         Put(KnownNameSpaces, HKEY("http://wellformedweb.org/CommentAPI/"), NULL, reference_free_handler);
1268         Put(KnownNameSpaces, HKEY("http://www.georss.org/georss"), NULL, reference_free_handler);
1269         Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/xhtml"), NULL, reference_free_handler);
1270         Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), NULL, reference_free_handler);
1271         Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), NULL, reference_free_handler);
1272         Put(KnownNameSpaces, HKEY("http://www.w3.org/2003/01/geo/wgs84_pos#"), NULL, reference_free_handler);
1273         Put(KnownNameSpaces, HKEY("http://www.w3.org/2005/Atom"), NULL, reference_free_handler);
1274         Put(KnownNameSpaces, HKEY("urn:flickr:"), NULL, reference_free_handler);
1275 #if 0
1276         /* we don't like these namespaces because of they shadow our usefull parameters. */
1277         Put(KnownNameSpaces, HKEY("http://search.yahoo.com/mrss/"), NULL, reference_free_handler);
1278 #endif
1279         return "rssclient";
1280 }