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         StrBuf *Answer;
974
975         CURL *curl;
976         CURLcode res;
977         char errmsg[1024] = "";
978         char *ptr;
979         const char *at;
980         long len;
981
982         memset(&ri, 0, sizeof(rss_item));
983         rssc.Item = &ri;
984         rssc.Cfg = Cfg;
985
986         CtdlLogPrintf(CTDL_DEBUG, "Fetching RSS feed <%s>\n", Cfg->url);
987
988         curl = curl_easy_init();
989         if (!curl) {
990                 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
991                 return;
992         }
993         Answer = NewStrBufPlain(NULL, SIZ);
994
995         curl_easy_setopt(curl, CURLOPT_URL, Cfg->url);
996         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
997         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
998         curl_easy_setopt(curl, CURLOPT_WRITEDATA, Answer);
999 //      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rss_libcurl_callback);
1000         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
1001         curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
1002         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
1003 #ifdef CURLOPT_HTTP_CONTENT_DECODING
1004         curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
1005         curl_easy_setopt(curl, CURLOPT_ENCODING, "");
1006 #endif
1007         curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
1008         curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180);           /* die after 180 seconds */
1009         if (
1010                 (!IsEmptyStr(config.c_ip_addr))
1011                 && (strcmp(config.c_ip_addr, "*"))
1012                 && (strcmp(config.c_ip_addr, "::"))
1013                 && (strcmp(config.c_ip_addr, "0.0.0.0"))
1014         ) {
1015                 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
1016         }
1017
1018         if (CtdlThreadCheckStop())
1019         {
1020                 curl_easy_cleanup(curl);
1021                 return;
1022         }
1023         
1024         if (CtdlThreadCheckStop())
1025                 goto shutdown ;
1026
1027         res = curl_easy_perform(curl);
1028         if (res) {
1029                 CtdlLogPrintf(CTDL_ALERT, "libcurl error %d: %s\n", res, errmsg);
1030         }
1031
1032         if (CtdlThreadCheckStop())
1033                 goto shutdown ;
1034
1035
1036
1037
1038         memset(&ri, 0, sizeof(rss_item));
1039         ri.roomlist = Cfg->rooms;
1040         rssc.CData = NewStrBufPlain(NULL, SIZ);
1041         rssc.Key = NewStrBuf();
1042         at = NULL;
1043         StrBufSipLine(rssc.Key, Answer, &at);
1044         ptr = NULL;
1045
1046 #define encoding "encoding=\""
1047         ptr = strstr(ChrPtr(rssc.Key), encoding);
1048         if (ptr != NULL)
1049         {
1050                 char *pche;
1051
1052                 ptr += sizeof (encoding) - 1;
1053                 pche = strchr(ptr, '"');
1054                 if (pche != NULL)
1055                         StrBufCutAt(rssc.Key, -1, pche);
1056                 else 
1057                         ptr = "UTF-8";
1058         }
1059         else
1060                 ptr = "UTF-8";
1061
1062
1063         xp = XML_ParserCreateNS(ptr, ':');
1064         if (!xp) {
1065                 CtdlLogPrintf(CTDL_ALERT, "Cannot create XML parser!\n");
1066                 goto shutdown;
1067         }
1068         FlushStrBuf(rssc.Key);
1069 //#ifdef HAVE_ICONV
1070 #if 0
1071         XML_SetUnknownEncodingHandler(xp,
1072                                       handle_unknown_xml_encoding,
1073                                       &rssc);
1074 #endif
1075 //#endif
1076         XML_SetElementHandler(xp, rss_xml_start, rss_xml_end);
1077         XML_SetCharacterDataHandler(xp, rss_xml_chardata);
1078         XML_SetUserData(xp, &rssc);
1079         XML_SetCdataSectionHandler(xp,
1080                                    rss_xml_cdata_start,
1081                                    rss_xml_cdata_end);
1082
1083
1084         len = StrLength(Answer);
1085         ptr = SmashStrBuf(&Answer);
1086         XML_Parse(xp, ptr, len, 0);
1087         free (ptr);
1088         if (ri.done_parsing == 0)
1089                 XML_Parse(xp, "", 0, 1);
1090
1091
1092         CtdlLogPrintf(CTDL_ALERT, "RSS: XML Status [%s] \n", 
1093                       XML_ErrorString(
1094                               XML_GetErrorCode(xp)));
1095
1096 shutdown:
1097         curl_easy_cleanup(curl);
1098         XML_ParserFree(xp);
1099
1100         flush_rss_item(&ri);
1101         FreeStrBuf(&rssc.CData);
1102         FreeStrBuf(&rssc.Key);
1103 }
1104
1105
1106 /*
1107  * Scan a room's netconfig to determine whether it is requesting any RSS feeds
1108  */
1109 void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
1110 {
1111         char filename[PATH_MAX];
1112         char buf[1024];
1113         char instr[32];
1114         FILE *fp;
1115         char feedurl[256];
1116         rssnetcfg *rncptr = NULL;
1117         rssnetcfg *use_this_rncptr = NULL;
1118         int len = 0;
1119         char *ptr = NULL;
1120
1121         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
1122
1123         if (CtdlThreadCheckStop())
1124                 return;
1125                 
1126         /* Only do net processing for rooms that have netconfigs */
1127         fp = fopen(filename, "r");
1128         if (fp == NULL) {
1129                 return;
1130         }
1131
1132         while (fgets(buf, sizeof buf, fp) != NULL && !CtdlThreadCheckStop()) {
1133                 buf[strlen(buf)-1] = 0;
1134
1135                 extract_token(instr, buf, 0, '|', sizeof instr);
1136                 if (!strcasecmp(instr, "rssclient")) {
1137
1138                         use_this_rncptr = NULL;
1139
1140                         extract_token(feedurl, buf, 1, '|', sizeof feedurl);
1141
1142                         /* If any other rooms have requested the same feed, then we will just add this
1143                          * room to the target list for that client request.
1144                          */
1145                         for (rncptr=rnclist; rncptr!=NULL; rncptr=rncptr->next) {
1146                                 if (!strcmp(rncptr->url, feedurl)) {
1147                                         use_this_rncptr = rncptr;
1148                                 }
1149                         }
1150
1151                         /* Otherwise create a new client request */
1152                         if (use_this_rncptr == NULL) {
1153                                 rncptr = (rssnetcfg *) malloc(sizeof(rssnetcfg));
1154                                 rncptr->ItemType = RSS_UNSET;
1155                                 if (rncptr != NULL) {
1156                                         rncptr->next = rnclist;
1157                                         safestrncpy(rncptr->url, feedurl, sizeof rncptr->url);
1158                                         rncptr->rooms = NULL;
1159                                         rnclist = rncptr;
1160                                         use_this_rncptr = rncptr;
1161                                 }
1162                         }
1163
1164                         /* Add the room name to the request */
1165                         if (use_this_rncptr != NULL) {
1166                                 if (use_this_rncptr->rooms == NULL) {
1167                                         rncptr->rooms = strdup(qrbuf->QRname);
1168                                 }
1169                                 else {
1170                                         len = strlen(use_this_rncptr->rooms) + strlen(qrbuf->QRname) + 5;
1171                                         ptr = realloc(use_this_rncptr->rooms, len);
1172                                         if (ptr != NULL) {
1173                                                 strcat(ptr, "|");
1174                                                 strcat(ptr, qrbuf->QRname);
1175                                                 use_this_rncptr->rooms = ptr;
1176                                         }
1177                                 }
1178                         }
1179                 }
1180
1181         }
1182
1183         fclose(fp);
1184
1185 }
1186
1187 /*
1188  * Scan for rooms that have RSS client requests configured
1189  */
1190 void *rssclient_scan(void *args) {
1191         static time_t last_run = 0L;
1192         static int doing_rssclient = 0;
1193         rssnetcfg *rptr = NULL;
1194         CitContext rssclientCC;
1195
1196         /* Give this thread its own private CitContext */
1197         CtdlFillSystemContext(&rssclientCC, "rssclient");
1198         citthread_setspecific(MyConKey, (void *)&rssclientCC );
1199
1200         /*
1201          * This is a simple concurrency check to make sure only one rssclient run
1202          * is done at a time.  We could do this with a mutex, but since we
1203          * don't really require extremely fine granularity here, we'll do it
1204          * with a static variable instead.
1205          */
1206         if (doing_rssclient) return NULL;
1207         doing_rssclient = 1;
1208
1209         CtdlLogPrintf(CTDL_DEBUG, "rssclient started\n");
1210         CtdlForEachRoom(rssclient_scan_room, NULL);
1211
1212         while (rnclist != NULL && !CtdlThreadCheckStop()) {
1213                 rss_do_fetching(rnclist);
1214                 rptr = rnclist;
1215                 rnclist = rnclist->next;
1216                 if (rptr->rooms != NULL) free(rptr->rooms);
1217                 free(rptr);
1218         }
1219
1220         CtdlLogPrintf(CTDL_DEBUG, "rssclient ended\n");
1221         last_run = time(NULL);
1222         doing_rssclient = 0;
1223         if (!CtdlThreadCheckStop())
1224                 CtdlThreadSchedule ("RSS Client", CTDLTHREAD_BIGSTACK, rssclient_scan, NULL, last_run + config.c_net_freq);
1225         else
1226                 CtdlLogPrintf(CTDL_DEBUG, "rssclient: Task STOPPED.\n");
1227         CtdlClearSystemContext();
1228         return NULL;
1229 }
1230
1231
1232 CTDL_MODULE_INIT(rssclient)
1233 {
1234         if (threading)
1235         {
1236                 CtdlLogPrintf(CTDL_INFO, "%s\n", curl_version());
1237                 CtdlThreadSchedule ("RSS Client", CTDLTHREAD_BIGSTACK, rssclient_scan, NULL, 0);
1238         }
1239
1240         StartHandlers = NewHash(1, NULL);
1241         EndHandlers = NewHash(1, NULL);
1242
1243         AddRSSStartHandler(RSS_item_rss_start,     RSS_UNSET, HKEY("rss"));
1244         AddRSSStartHandler(RSS_item_rdf_start,     RSS_UNSET, HKEY("rdf"));
1245         AddRSSStartHandler(ATOM_item_feed_start,    RSS_UNSET, HKEY("feed"));
1246         AddRSSStartHandler(RSS_item_item_start,    RSS_RSS, HKEY("item"));
1247         AddRSSStartHandler(ATOM_item_entry_start,  RSS_ATOM, HKEY("entry"));
1248         AddRSSStartHandler(ATOM_item_link_start,   RSS_ATOM, HKEY("link"));
1249
1250         AddRSSEndHandler(ATOMRSS_item_title_end,   RSS_ATOM|RSS_RSS|RSS_REQUIRE_BUF, HKEY("title"));
1251         AddRSSEndHandler(RSS_item_guid_end,        RSS_RSS|RSS_REQUIRE_BUF, HKEY("guid"));
1252         AddRSSEndHandler(ATOM_item_id_end,         RSS_ATOM|RSS_REQUIRE_BUF, HKEY("id"));
1253         AddRSSEndHandler(RSS_item_link_end,        RSS_RSS|RSS_REQUIRE_BUF, HKEY("link"));
1254         AddRSSEndHandler(RSSATOM_item_title_end,   RSS_ATOM|RSS_RSS|RSS_REQUIRE_BUF, HKEY("title"));
1255         AddRSSEndHandler(ATOM_item_content_end,    RSS_ATOM|RSS_REQUIRE_BUF, HKEY("content"));
1256         AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("encoded"));
1257         AddRSSEndHandler(ATOM_item_summary_end,    RSS_ATOM|RSS_REQUIRE_BUF, HKEY("summary"));
1258         AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("description"));
1259         AddRSSEndHandler(ATOM_item_published_end,  RSS_ATOM|RSS_REQUIRE_BUF, HKEY("published"));
1260         AddRSSEndHandler(ATOM_item_updated_end,    RSS_ATOM|RSS_REQUIRE_BUF, HKEY("updated"));
1261         AddRSSEndHandler(RSS_item_pubdate_end,     RSS_RSS|RSS_REQUIRE_BUF, HKEY("pubdate"));
1262         AddRSSEndHandler(RSS_item_date_end,        RSS_RSS|RSS_REQUIRE_BUF, HKEY("date"));
1263         AddRSSEndHandler(RSS_item_author_end,      RSS_RSS|RSS_REQUIRE_BUF, HKEY("author"));
1264         AddRSSEndHandler(RSS_item_creator_end,     RSS_RSS|RSS_REQUIRE_BUF, HKEY("creator"));
1265         AddRSSEndHandler(ATOM_item_name_end,       RSS_ATOM|RSS_REQUIRE_BUF, HKEY("name"));
1266         AddRSSEndHandler(ATOM_item_uri_end,        RSS_ATOM|RSS_REQUIRE_BUF, HKEY("uri"));
1267
1268         AddRSSEndHandler(RSS_item_item_end,        RSS_RSS, HKEY("item"));
1269         AddRSSEndHandler(RSS_item_rss_end,         RSS_RSS, HKEY("rss"));
1270         AddRSSEndHandler(RSS_item_rdf_end,         RSS_RSS, HKEY("rdf"));
1271         AddRSSEndHandler(ATOM_item_entry_end,      RSS_ATOM, HKEY("entry"));
1272
1273
1274 /* at the start of atoms: <seq> <li>link to resource</li></seq> ignore them. */
1275         AddRSSStartHandler(RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("seq"));
1276         AddRSSEndHandler  (RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("seq"));
1277         AddRSSStartHandler(RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("li"));
1278         AddRSSEndHandler  (RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("li"));
1279
1280 /* links to other feed generators... */
1281         AddRSSStartHandler(RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("feedflare"));
1282         AddRSSEndHandler  (RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("feedflare"));
1283         AddRSSStartHandler(RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("browserfriendly"));
1284         AddRSSEndHandler  (RSSATOM_item_ignore,      RSS_RSS|RSS_ATOM, HKEY("browserfriendly"));
1285
1286         KnownNameSpaces = NewHash(1, NULL);
1287         Put(KnownNameSpaces, HKEY("http://a9.com/-/spec/opensearch/1.1/"), NULL, reference_free_handler);
1288         Put(KnownNameSpaces, HKEY("http://a9.com/-/spec/opensearchrss/1.0/"), NULL, reference_free_handler);
1289         Put(KnownNameSpaces, HKEY("http://backend.userland.com/creativeCommonsRssModule"), NULL, reference_free_handler);
1290         Put(KnownNameSpaces, HKEY("http://purl.org/atom/ns#"), NULL, reference_free_handler);
1291         Put(KnownNameSpaces, HKEY("http://purl.org/dc/elements/1.1/"), NULL, reference_free_handler);
1292         Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/"), NULL, reference_free_handler);
1293         Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/content/"), NULL, reference_free_handler);
1294         Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/slash/"), NULL, reference_free_handler);
1295         Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/modules/syndication/"), NULL, reference_free_handler);
1296         Put(KnownNameSpaces, HKEY("http://purl.org/rss/1.0/"), NULL, reference_free_handler);
1297         Put(KnownNameSpaces, HKEY("http://purl.org/syndication/thread/1.0"), NULL, reference_free_handler);
1298         Put(KnownNameSpaces, HKEY("http://rssnamespace.org/feedburner/ext/1.0"), NULL, reference_free_handler);
1299         Put(KnownNameSpaces, HKEY("http://schemas.google.com/g/2005"), NULL, reference_free_handler);
1300         Put(KnownNameSpaces, HKEY("http://webns.net/mvcb/"), NULL, reference_free_handler);
1301         Put(KnownNameSpaces, HKEY("http://web.resource.org/cc/"), NULL, reference_free_handler);
1302         Put(KnownNameSpaces, HKEY("http://wellformedweb.org/CommentAPI/"), NULL, reference_free_handler);
1303         Put(KnownNameSpaces, HKEY("http://www.georss.org/georss"), NULL, reference_free_handler);
1304         Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/xhtml"), NULL, reference_free_handler);
1305         Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), NULL, reference_free_handler);
1306         Put(KnownNameSpaces, HKEY("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), NULL, reference_free_handler);
1307         Put(KnownNameSpaces, HKEY("http://www.w3.org/2003/01/geo/wgs84_pos#"), NULL, reference_free_handler);
1308         Put(KnownNameSpaces, HKEY("http://www.w3.org/2005/Atom"), NULL, reference_free_handler);
1309         Put(KnownNameSpaces, HKEY("urn:flickr:"), NULL, reference_free_handler);
1310 #if 0
1311         /* we don't like these namespaces because of they shadow our usefull parameters. */
1312         Put(KnownNameSpaces, HKEY("http://search.yahoo.com/mrss/"), NULL, reference_free_handler);
1313 #endif
1314         return "rssclient";
1315 }