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