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