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