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