Removed the logging facility from citserver, use syslog instead
[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 "msgbase.h"
52 #include "parsedate.h"
53 #include "database.h"
54 #include "citadel_dirs.h"
55 #include "md5.h"
56 #include "context.h"
57 #include "event_client.h"
58 #include "rss_atom_parser.h"
59
60
61 #define TMP_MSGDATA 0xFF
62 #define TMP_SHORTER_URL_OFFSET 0xFE
63 #define TMP_SHORTER_URLS 0xFD
64
65 citthread_mutex_t RSSQueueMutex; /* locks the access to the following vars: */
66 HashList *RSSQueueRooms = NULL; /* rss_room_counter */
67 HashList *RSSFetchUrls = NULL; /* -> rss_aggregator; ->RefCount access to be locked too. */
68
69 eNextState RSSAggregatorTerminate(AsyncIO *IO);
70
71
72 struct rssnetcfg *rnclist = NULL;
73 void AppendLink(StrBuf *Message, StrBuf *link, StrBuf *LinkTitle, const char *Title)
74 {
75         if (StrLength(link) > 0)
76         {
77                 StrBufAppendBufPlain(Message, HKEY("<a href=\""), 0);
78                 StrBufAppendBuf(Message, link, 0);
79                 StrBufAppendBufPlain(Message, HKEY("\">"), 0);
80                 if (StrLength(LinkTitle) > 0)
81                         StrBufAppendBuf(Message, LinkTitle, 0);
82                 else if ((Title != NULL) && !IsEmptyStr(Title))
83                         StrBufAppendBufPlain(Message, Title, -1, 0);
84                 else
85                         StrBufAppendBuf(Message, link, 0);
86                 StrBufAppendBufPlain(Message, HKEY("</a><br>\n"), 0);
87         }
88 }
89 typedef struct __networker_save_message {
90         AsyncIO IO;
91         struct CtdlMessage *Msg;
92         struct recptypes *recp;
93         rss_aggregator *Cfg;
94         StrBuf *MsgGUID;
95         StrBuf *Message;
96         struct UseTable ut;
97 } networker_save_message;
98
99
100 void DeleteRoomReference(long QRnumber)
101 {
102         HashPos *At;
103         long HKLen;
104         const char *HK;
105         void *vData = NULL;
106         rss_room_counter *pRoomC;
107
108         At = GetNewHashPos(RSSQueueRooms, 0);
109
110         GetHashPosFromKey(RSSQueueRooms, LKEY(QRnumber), At);
111         GetHashPos(RSSQueueRooms, At, &HKLen, &HK, &vData);
112         if (vData != NULL)
113         {
114                 pRoomC = (rss_room_counter *) vData;
115                 pRoomC->count --;
116                 if (pRoomC->count == 0)
117                         DeleteEntryFromHash(RSSQueueRooms, At);
118         }
119         DeleteHashPos(&At);
120 }
121
122 void UnlinkRooms(rss_aggregator *Cfg)
123 {
124         
125         DeleteRoomReference(Cfg->QRnumber);
126         if (Cfg->OtherQRnumbers != NULL)
127         {
128                 long HKLen;
129                 const char *HK;
130                 HashPos *At;
131                 void *vData;
132
133                 At = GetNewHashPos(Cfg->OtherQRnumbers, 0);
134                 while (GetNextHashPos(Cfg->OtherQRnumbers, At, &HKLen, &HK, &vData) && 
135                        (vData != NULL))
136                 {
137                         long *lData = (long*) vData;
138                         DeleteRoomReference(*lData);
139                 }
140
141                 DeleteHashPos(&At);
142         }
143
144 }
145
146 void UnlinkRSSAggregator(rss_aggregator *Cfg)
147 {
148         HashPos *At;
149
150         UnlinkRooms(Cfg);
151
152         At = GetNewHashPos(RSSFetchUrls, 0);
153         if (GetHashPosFromKey(RSSFetchUrls, SKEY(Cfg->Url), At) == 0)
154         {
155                 DeleteEntryFromHash(RSSFetchUrls, At);
156         }
157         DeleteHashPos(&At);
158 }
159
160 eNextState FreeNetworkSaveMessage (AsyncIO *IO)
161 {
162         networker_save_message *Ctx = (networker_save_message *) IO->Data;
163
164         citthread_mutex_lock(&RSSQueueMutex);
165         Ctx->Cfg->RefCount --;
166
167         if (Ctx->Cfg->RefCount == 0)
168         {
169                 UnlinkRSSAggregator(Ctx->Cfg);
170
171         }
172         citthread_mutex_unlock(&RSSQueueMutex);
173
174         CtdlFreeMessage(Ctx->Msg);
175         free_recipients(Ctx->recp);
176         FreeStrBuf(&Ctx->Message);
177         FreeStrBuf(&Ctx->MsgGUID);
178         free(Ctx);
179         return eAbort;
180 }
181
182 eNextState AbortNetworkSaveMessage (AsyncIO *IO)
183 {
184         return eAbort; ///TODO
185 }
186
187 eNextState RSSSaveMessage(AsyncIO *IO)
188 {
189         networker_save_message *Ctx = (networker_save_message *) IO->Data;
190
191         Ctx->Msg->cm_fields['M'] = SmashStrBuf(&Ctx->Message);
192
193         CtdlSubmitMsg(Ctx->Msg, Ctx->recp, NULL, 0);
194
195         /* write the uidl to the use table so we don't store this item again */
196         cdb_store(CDB_USETABLE, SKEY(Ctx->MsgGUID), &Ctx->ut, sizeof(struct UseTable) );
197
198         return eTerminateConnection;
199 }
200
201 // TODO: relink me:     ExpandShortUrls(ri->description);
202
203 eNextState RSS_FetchNetworkUsetableEntry(AsyncIO *IO)
204 {
205         struct cdbdata *cdbut;
206         networker_save_message *Ctx = (networker_save_message *) IO->Data;
207
208         /* Find out if we've already seen this item */
209         strcpy(Ctx->ut.ut_msgid, ChrPtr(Ctx->MsgGUID)); /// TODO
210         Ctx->ut.ut_timestamp = time(NULL);
211
212         cdbut = cdb_fetch(CDB_USETABLE, SKEY(Ctx->MsgGUID));
213 #ifndef DEBUG_RSS
214         if (cdbut != NULL) {
215                 /* Item has already been seen */
216                 syslog(LOG_DEBUG, "%s has already been seen\n", ChrPtr(Ctx->MsgGUID));
217                 cdb_free(cdbut);
218
219                 /* rewrite the record anyway, to update the timestamp */
220                 cdb_store(CDB_USETABLE, 
221                           SKEY(Ctx->MsgGUID), 
222                           &Ctx->ut, sizeof(struct UseTable) );
223                 return eAbort;
224         }
225         else
226 #endif
227         {
228                 NextDBOperation(IO, RSSSaveMessage);
229                 return eSendMore;
230         }
231 }
232 void RSSQueueSaveMessage(struct CtdlMessage *Msg, struct recptypes *recp, StrBuf *MsgGUID, StrBuf *MessageBody, rss_aggregator *Cfg)
233 {
234         networker_save_message *Ctx;
235
236         Ctx = (networker_save_message *) malloc(sizeof(networker_save_message));
237         memset(Ctx, 0, sizeof(networker_save_message));
238         
239         Ctx->MsgGUID = MsgGUID;
240         Ctx->Message = MessageBody;
241         Ctx->Msg = Msg;
242         Ctx->Cfg = Cfg;
243         Ctx->recp = recp;
244         Ctx->IO.Data = Ctx;
245         Ctx->IO.CitContext = CloneContext(CC);
246         Ctx->IO.Terminate = FreeNetworkSaveMessage;
247         Ctx->IO.ShutdownAbort = AbortNetworkSaveMessage;
248         QueueDBOperation(&Ctx->IO, RSS_FetchNetworkUsetableEntry);
249 }
250
251
252 /*
253  * Commit a fetched and parsed RSS item to disk
254  */
255 void rss_save_item(rss_item *ri, rss_aggregator *Cfg)
256 {
257
258         struct MD5Context md5context;
259         u_char rawdigest[MD5_DIGEST_LEN];
260         struct CtdlMessage *msg;
261         struct recptypes *recp = NULL;
262         int msglen = 0;
263         StrBuf *Message;
264         StrBuf *guid;
265         StrBuf *Buf;
266
267         recp = (struct recptypes *) malloc(sizeof(struct recptypes));
268         if (recp == NULL) return;
269         memset(recp, 0, sizeof(struct recptypes));
270         Buf = NewStrBufDup(Cfg->rooms);
271         recp->recp_room = SmashStrBuf(&Buf);
272         recp->num_room = Cfg->roomlist_parts;
273         recp->recptypes_magic = RECPTYPES_MAGIC;
274    
275         Cfg->RefCount ++;
276         /* Construct a GUID to use in the S_USETABLE table.
277          * If one is not present in the item itself, make one up.
278          */
279         if (ri->guid != NULL) {
280                 StrBufSpaceToBlank(ri->guid);
281                 StrBufTrim(ri->guid);
282                 guid = NewStrBufPlain(HKEY("rss/"));
283                 StrBufAppendBuf(guid, ri->guid, 0);
284         }
285         else {
286                 MD5Init(&md5context);
287                 if (ri->title != NULL) {
288                         MD5Update(&md5context, (const unsigned char*)ChrPtr(ri->title), StrLength(ri->title));
289                 }
290                 if (ri->link != NULL) {
291                         MD5Update(&md5context, (const unsigned char*)ChrPtr(ri->link), StrLength(ri->link));
292                 }
293                 MD5Final(rawdigest, &md5context);
294                 guid = NewStrBufPlain(NULL, MD5_DIGEST_LEN * 2 + 12 /* _rss2ctdl*/);
295                 StrBufHexEscAppend(guid, NULL, rawdigest, MD5_DIGEST_LEN);
296                 StrBufAppendBufPlain(guid, HKEY("_rss2ctdl"), 0);
297         }
298
299         /* translate Item into message. */
300         syslog(LOG_DEBUG, "RSS: translating item...\n");
301         if (ri->description == NULL) ri->description = NewStrBufPlain(HKEY(""));
302         StrBufSpaceToBlank(ri->description);
303         msg = malloc(sizeof(struct CtdlMessage));
304         memset(msg, 0, sizeof(struct CtdlMessage));
305         msg->cm_magic = CTDLMESSAGE_MAGIC;
306         msg->cm_anon_type = MES_NORMAL;
307         msg->cm_format_type = FMT_RFC822;
308
309         if (ri->guid != NULL) {
310                 msg->cm_fields['E'] = strdup(ChrPtr(ri->guid));
311         }
312
313         if (ri->author_or_creator != NULL) {
314                 char *From;
315                 StrBuf *Encoded = NULL;
316                 int FromAt;
317                         
318                 From = html_to_ascii(ChrPtr(ri->author_or_creator),
319                                      StrLength(ri->author_or_creator), 
320                                      512, 0);
321                 StrBufPlain(ri->author_or_creator, From, -1);
322                 StrBufTrim(ri->author_or_creator);
323                 free(From);
324
325                 FromAt = strchr(ChrPtr(ri->author_or_creator), '@') != NULL;
326                 if (!FromAt && StrLength (ri->author_email) > 0)
327                 {
328                         StrBufRFC2047encode(&Encoded, ri->author_or_creator);
329                         msg->cm_fields['A'] = SmashStrBuf(&Encoded);
330                         msg->cm_fields['P'] = SmashStrBuf(&ri->author_email);
331                 }
332                 else
333                 {
334                         if (FromAt)
335                                 msg->cm_fields['P'] = SmashStrBuf(&ri->author_or_creator);
336                         else 
337                         {
338                                 StrBufRFC2047encode(&Encoded, ri->author_or_creator);
339                                 msg->cm_fields['A'] = SmashStrBuf(&Encoded);
340                                 msg->cm_fields['P'] = strdup("rss@localhost");
341                         }
342                 }
343         }
344         else {
345                 msg->cm_fields['A'] = strdup("rss");
346         }
347
348         msg->cm_fields['N'] = strdup(NODENAME);
349         if (ri->title != NULL) {
350                 long len;
351                 char *Sbj;
352                 StrBuf *Encoded, *QPEncoded;
353
354                 QPEncoded = NULL;
355                 StrBufSpaceToBlank(ri->title);
356                 len = StrLength(ri->title);
357                 Sbj = html_to_ascii(ChrPtr(ri->title), len, 512, 0);
358                 len = strlen(Sbj);
359                 if (Sbj[len - 1] == '\n')
360                 {
361                         len --;
362                         Sbj[len] = '\0';
363                 }
364                 Encoded = NewStrBufPlain(Sbj, len);
365                 free(Sbj);
366
367                 StrBufTrim(Encoded);
368                 StrBufRFC2047encode(&QPEncoded, Encoded);
369
370                 msg->cm_fields['U'] = SmashStrBuf(&QPEncoded);
371                 FreeStrBuf(&Encoded);
372         }
373         msg->cm_fields['T'] = malloc(64);
374         snprintf(msg->cm_fields['T'], 64, "%ld", ri->pubdate);
375         if (ri->channel_title != NULL) {
376                 if (StrLength(ri->channel_title) > 0) {
377                         msg->cm_fields['O'] = strdup(ChrPtr(ri->channel_title));
378                 }
379         }
380         if (ri->link == NULL) 
381                 ri->link = NewStrBufPlain(HKEY(""));
382
383 #if 0 /* temporarily disable shorter urls. */
384         msg->cm_fields[TMP_SHORTER_URLS] = GetShorterUrls(ri->description);
385 #endif
386
387         msglen += 1024 + StrLength(ri->link) + StrLength(ri->description) ;
388
389         Message = NewStrBufPlain(NULL, StrLength(ri->description));
390
391         StrBufPlain(Message, HKEY(
392                             "Content-type: text/html; charset=\"UTF-8\"\r\n\r\n"
393                             "<html><body>\n"));
394 #if 0 /* disable shorter url for now. */
395         msg->cm_fields[TMP_SHORTER_URL_OFFSET] = StrLength(Message);
396 #endif
397         StrBufAppendBuf(Message, ri->description, 0);
398         StrBufAppendBufPlain(Message, HKEY("<br><br>\n"), 0);
399
400         AppendLink(Message, ri->link, ri->linkTitle, NULL);
401         AppendLink(Message, ri->reLink, ri->reLinkTitle, "Reply to this");
402         StrBufAppendBufPlain(Message, HKEY("</body></html>\n"), 0);
403
404         RSSQueueSaveMessage(msg, recp, guid, Message, Cfg);
405 }
406
407
408
409 /*
410  * Begin a feed parse
411  */
412 int rss_do_fetching(rss_aggregator *Cfg)
413 {
414         rss_item *ri;
415                 
416         time_t now;
417         AsyncIO *IO;
418
419         now = time(NULL);
420
421         if ((Cfg->next_poll != 0) && (now < Cfg->next_poll))
422                 return 0;
423         Cfg->RefCount = 1;
424
425         ri = (rss_item*) malloc(sizeof(rss_item));
426         memset(ri, 0, sizeof(rss_item));
427         Cfg->Item = ri;
428         IO = &Cfg->IO;
429         IO->CitContext = CloneContext(CC);
430         IO->Data = Cfg;
431
432
433         syslog(LOG_DEBUG, "Fetching RSS feed <%s>\n", ChrPtr(Cfg->Url));
434         ParseURL(&IO->ConnectMe, Cfg->Url, 80);
435         CurlPrepareURL(IO->ConnectMe);
436
437         if (! evcurl_init(IO, 
438 //                        Ctx, 
439                           NULL,
440                           "Citadel RSS Client",
441                           ParseRSSReply, 
442                           RSSAggregatorTerminate))
443         {
444                 syslog(LOG_DEBUG, "Unable to initialize libcurl.\n");
445                 return 0;
446         }
447
448         evcurl_handle_start(IO);
449         return 1;
450 }
451
452
453
454 void DeleteRssCfg(void *vptr)
455 {
456         rss_aggregator *rncptr = (rss_aggregator *)vptr;
457
458         FreeStrBuf(&rncptr->Url);
459         FreeStrBuf(&rncptr->rooms);
460         FreeStrBuf(&rncptr->CData);
461         FreeStrBuf(&rncptr->Key);
462
463         DeleteHash(&rncptr->OtherQRnumbers);
464
465         if (rncptr->Item != NULL)
466         {
467                 FreeStrBuf(&rncptr->Item->guid);
468                 FreeStrBuf(&rncptr->Item->title);
469                 FreeStrBuf(&rncptr->Item->link);
470                 FreeStrBuf(&rncptr->Item->linkTitle);
471                 FreeStrBuf(&rncptr->Item->reLink);
472                 FreeStrBuf(&rncptr->Item->reLinkTitle);
473                 FreeStrBuf(&rncptr->Item->description);
474                 FreeStrBuf(&rncptr->Item->channel_title);
475                 FreeStrBuf(&rncptr->Item->author_or_creator);
476                 FreeStrBuf(&rncptr->Item->author_url);
477                 FreeStrBuf(&rncptr->Item->author_email);
478
479                 free(rncptr->Item);
480         }
481         free(rncptr);
482 }
483
484 eNextState RSSAggregatorTerminate(AsyncIO *IO)
485 {
486         rss_aggregator *rncptr = (rss_aggregator *)IO->Data;
487         HashPos *At;
488         long HKLen;
489         const char *HK;
490         void *vData;
491
492         citthread_mutex_lock(&RSSQueueMutex);
493         rncptr->RefCount --;
494         if (rncptr->RefCount == 0)
495         {
496                 UnlinkRSSAggregator(rncptr);
497
498         }
499         citthread_mutex_unlock(&RSSQueueMutex);
500 /*
501         At = GetNewHashPos(RSSFetchUrls, 0);
502
503         citthread_mutex_lock(&RSSQueueMutex);
504         GetHashPosFromKey(RSSFetchUrls, SKEY(rncptr->Url), At);
505         GetHashPos(RSSFetchUrls, At, &HKLen, &HK, &vData);
506         DeleteEntryFromHash(RSSFetchUrls, At);
507         citthread_mutex_unlock(&RSSQueueMutex);
508
509         DeleteHashPos(&At);
510 */
511         return eAbort;
512 }
513
514 /*
515  * Scan a room's netconfig to determine whether it is requesting any RSS feeds
516  */
517 void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
518 {
519         StrBuf *CfgData;
520         StrBuf *CfgType;
521         StrBuf *Line;
522         rss_room_counter *Count = NULL;
523         struct stat statbuf;
524         char filename[PATH_MAX];
525         int  fd;
526         int Done;
527         rss_aggregator *rncptr = NULL;
528         rss_aggregator *use_this_rncptr = NULL;
529         void *vptr;
530         const char *CfgPtr, *lPtr;
531         const char *Err;
532
533         citthread_mutex_lock(&RSSQueueMutex);
534         if (GetHash(RSSQueueRooms, LKEY(qrbuf->QRnumber), &vptr))
535         {
536                 syslog(LOG_DEBUG, 
537                               "rssclient: [%ld] %s already in progress.\n", 
538                               qrbuf->QRnumber, 
539                               qrbuf->QRname);
540                 citthread_mutex_unlock(&RSSQueueMutex);
541                 return;
542         }
543         citthread_mutex_unlock(&RSSQueueMutex);
544
545         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
546
547         if (CtdlThreadCheckStop())
548                 return;
549                 
550         /* Only do net processing for rooms that have netconfigs */
551         fd = open(filename, 0);
552         if (fd <= 0) {
553                 //syslog(LOG_DEBUG, "rssclient: %s no config.\n", qrbuf->QRname);
554                 return;
555         }
556         if (CtdlThreadCheckStop())
557                 return;
558         if (fstat(fd, &statbuf) == -1) {
559                 syslog(LOG_DEBUG, "ERROR: could not stat configfile '%s' - %s\n",
560                         filename, strerror(errno));
561                 return;
562         }
563         if (CtdlThreadCheckStop())
564                 return;
565         CfgData = NewStrBufPlain(NULL, statbuf.st_size + 1);
566         if (StrBufReadBLOB(CfgData, &fd, 1, statbuf.st_size, &Err) < 0) {
567                 close(fd);
568                 FreeStrBuf(&CfgData);
569                 syslog(LOG_DEBUG, "ERROR: reading config '%s' - %s<br>\n",
570                         filename, strerror(errno));
571                 return;
572         }
573         close(fd);
574         if (CtdlThreadCheckStop())
575                 return;
576         
577         CfgPtr = NULL;
578         CfgType = NewStrBuf();
579         Line = NewStrBufPlain(NULL, StrLength(CfgData));
580         Done = 0;
581         while (!Done)
582         {
583             Done = StrBufSipLine(Line, CfgData, &CfgPtr) == 0;
584             if (StrLength(Line) > 0)
585             {
586                 lPtr = NULL;
587                 StrBufExtract_NextToken(CfgType, Line, &lPtr, '|');
588                 if (!strcasecmp("rssclient", ChrPtr(CfgType)))
589                 {
590                     if (Count == NULL)
591                     {
592                         Count = malloc(sizeof(rss_room_counter));
593                         Count->count = 0;
594                     }
595                     Count->count ++;
596                     rncptr = (rss_aggregator *) malloc(sizeof(rss_aggregator));
597                     memset (rncptr, 0, sizeof(rss_aggregator));
598                     rncptr->roomlist_parts = 1;
599                     rncptr->Url = NewStrBuf();
600                     StrBufExtract_NextToken(rncptr->Url, Line, &lPtr, '|');
601
602                     citthread_mutex_lock(&RSSQueueMutex);
603                     GetHash(RSSFetchUrls, SKEY(rncptr->Url), &vptr);
604                     use_this_rncptr = (rss_aggregator *)vptr;
605                     if (use_this_rncptr != NULL)
606                     {
607                             /* mustn't attach to an active session */
608                             if (use_this_rncptr->RefCount > 0)
609                             {
610                                     DeleteRssCfg(rncptr);
611                                     Count->count--;
612                             }
613                             else 
614                             {
615                                     long *QRnumber;
616                                     StrBufAppendBufPlain(use_this_rncptr->rooms, 
617                                                          qrbuf->QRname, 
618                                                          -1, 0);
619                                     if (use_this_rncptr->roomlist_parts == 1)
620                                     {
621                                             use_this_rncptr->OtherQRnumbers = NewHash(1, lFlathash);
622                                     }
623                                     QRnumber = (long*)malloc(sizeof(long));
624                                     *QRnumber = qrbuf->QRnumber;
625                                     Put(use_this_rncptr->OtherQRnumbers, LKEY(qrbuf->QRnumber), QRnumber, NULL);
626                                     use_this_rncptr->roomlist_parts++;
627                             }
628                             citthread_mutex_unlock(&RSSQueueMutex);
629                             continue;
630                     }
631                     citthread_mutex_unlock(&RSSQueueMutex);
632
633                     rncptr->ItemType = RSS_UNSET;
634                                 
635                     rncptr->rooms = NewStrBufPlain(qrbuf->QRname, -1);
636
637                     citthread_mutex_lock(&RSSQueueMutex);
638                     Put(RSSFetchUrls, SKEY(rncptr->Url), rncptr, DeleteRssCfg);
639                     citthread_mutex_unlock(&RSSQueueMutex);
640                 }
641             }
642         }
643         if (Count != NULL)
644         {
645                 Count->QRnumber = qrbuf->QRnumber;
646                 citthread_mutex_lock(&RSSQueueMutex);
647                 syslog(LOG_DEBUG, "rssclient: [%ld] %s now starting.\n", 
648                               qrbuf->QRnumber, qrbuf->QRname);
649                 Put(RSSQueueRooms, LKEY(qrbuf->QRnumber), Count, NULL);
650                 citthread_mutex_unlock(&RSSQueueMutex);
651         }
652         FreeStrBuf(&CfgData);
653         FreeStrBuf(&CfgType);
654         FreeStrBuf(&Line);
655 }
656
657 /*
658  * Scan for rooms that have RSS client requests configured
659  */
660 void rssclient_scan(void) {
661         static int doing_rssclient = 0;
662         rss_aggregator *rptr = NULL;
663         void *vrptr = NULL;
664         HashPos  *it;
665         long len;
666         const char *Key;
667
668         /*
669          * This is a simple concurrency check to make sure only one rssclient run
670          * is done at a time.  We could do this with a mutex, but since we
671          * don't really require extremely fine granularity here, we'll do it
672          * with a static variable instead.
673          */
674         if (doing_rssclient) return;
675         doing_rssclient = 1;
676
677         syslog(LOG_DEBUG, "rssclient started\n");
678         CtdlForEachRoom(rssclient_scan_room, NULL);
679
680         citthread_mutex_lock(&RSSQueueMutex);
681
682         it = GetNewHashPos(RSSFetchUrls, 0);
683         while (GetNextHashPos(RSSFetchUrls, it, &len, &Key, &vrptr) && 
684                (vrptr != NULL)) {
685                 rptr = (rss_aggregator *)vrptr;
686                 if (rptr->RefCount == 0) 
687                         if (!rss_do_fetching(rptr))
688                                 UnlinkRSSAggregator(rptr);
689         }
690         DeleteHashPos(&it);
691         citthread_mutex_unlock(&RSSQueueMutex);
692
693         syslog(LOG_DEBUG, "rssclient ended\n");
694         doing_rssclient = 0;
695         return;
696 }
697
698 void rss_cleanup(void)
699 {
700         citthread_mutex_destroy(&RSSQueueMutex);
701         DeleteHash(&RSSFetchUrls);
702         DeleteHash(&RSSQueueRooms);
703 }
704
705
706 CTDL_MODULE_INIT(rssclient)
707 {
708         if (threading)
709         {
710                 citthread_mutex_init(&RSSQueueMutex, NULL);
711                 RSSQueueRooms = NewHash(1, lFlathash);
712                 RSSFetchUrls = NewHash(1, NULL);
713                 syslog(LOG_INFO, "%s\n", curl_version());
714                 CtdlRegisterSessionHook(rssclient_scan, EVT_TIMER);
715                 CtdlRegisterCleanupHook(rss_cleanup);
716         }
717         return "rssclient";
718 }