2 * Bring external RSS feeds into rooms.
4 * Copyright (c) 2007-2012 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12 * GNU General Public License for more details.
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
33 #include <sys/types.h>
36 #include <curl/curl.h>
37 #include <libcitadel.h>
40 #include "citserver.h"
44 #include "ctdl_module.h"
46 #include "parsedate.h"
48 #include "citadel_dirs.h"
51 #include "event_client.h"
52 #include "rss_atom_parser.h"
55 #define TMP_MSGDATA 0xFF
56 #define TMP_SHORTER_URL_OFFSET 0xFE
57 #define TMP_SHORTER_URLS 0xFD
61 pthread_mutex_t RSSQueueMutex; /* locks the access to the following vars: */
62 HashList *RSSQueueRooms = NULL; /* rss_room_counter */
63 HashList *RSSFetchUrls = NULL; /*->rss_aggregator;->RefCount access locked*/
65 eNextState RSSAggregator_Terminate(AsyncIO *IO);
66 eNextState RSSAggregator_TerminateDB(AsyncIO *IO);
67 eNextState RSSAggregator_ShutdownAbort(AsyncIO *IO);
68 struct CitContext rss_CC;
70 struct rssnetcfg *rnclist = NULL;
73 void DeleteRoomReference(long QRnumber)
79 rss_room_counter *pRoomC;
81 At = GetNewHashPos(RSSQueueRooms, 0);
83 if (GetHashPosFromKey(RSSQueueRooms, LKEY(QRnumber), At))
85 GetHashPos(RSSQueueRooms, At, &HKLen, &HK, &vData);
88 pRoomC = (rss_room_counter *) vData;
90 if (pRoomC->count == 0)
91 DeleteEntryFromHash(RSSQueueRooms, At);
97 void UnlinkRooms(rss_aggregator *Cfg)
99 DeleteRoomReference(Cfg->QRnumber);
100 if (Cfg->OtherQRnumbers != NULL)
107 At = GetNewHashPos(Cfg->OtherQRnumbers, 0);
108 while (! server_shutting_down &&
109 GetNextHashPos(Cfg->OtherQRnumbers,
115 long *lData = (long*) vData;
116 DeleteRoomReference(*lData);
123 void UnlinkRSSAggregator(rss_aggregator *Cfg)
129 At = GetNewHashPos(RSSFetchUrls, 0);
130 if (GetHashPosFromKey(RSSFetchUrls, SKEY(Cfg->Url), At))
132 DeleteEntryFromHash(RSSFetchUrls, At);
135 last_run = time(NULL);
138 void DeleteRssCfg(void *vptr)
140 rss_aggregator *RSSAggr = (rss_aggregator *)vptr;
141 AsyncIO *IO = &RSSAggr->IO;
142 EVM_syslog(LOG_DEBUG, "RSS: destroying\n");
144 FreeStrBuf(&RSSAggr->Url);
145 FreeStrBuf(&RSSAggr->rooms);
146 FreeStrBuf(&RSSAggr->CData);
147 FreeStrBuf(&RSSAggr->Key);
148 DeleteHash(&RSSAggr->OtherQRnumbers);
150 DeleteHashPos (&RSSAggr->Pos);
151 DeleteHash (&RSSAggr->Messages);
152 if (RSSAggr->recp.recp_room != NULL)
153 free(RSSAggr->recp.recp_room);
156 if (RSSAggr->Item != NULL)
158 flush_rss_item(RSSAggr->Item);
163 FreeAsyncIOContents(&RSSAggr->IO);
167 eNextState RSSAggregator_Terminate(AsyncIO *IO)
169 rss_aggregator *RSSAggr = (rss_aggregator *)IO->Data;
171 EVM_syslog(LOG_DEBUG, "RSS: Terminating.\n");
174 UnlinkRSSAggregator(RSSAggr);
178 eNextState RSSAggregator_TerminateDB(AsyncIO *IO)
180 rss_aggregator *RSSAggr = (rss_aggregator *)IO->Data;
182 EVM_syslog(LOG_DEBUG, "RSS: Terminating.\n");
185 UnlinkRSSAggregator(RSSAggr);
189 eNextState RSSAggregator_ShutdownAbort(AsyncIO *IO)
192 rss_aggregator *RSSAggr = (rss_aggregator *)IO->Data;
194 pUrl = IO->ConnectMe->PlainUrl;
198 EV_syslog(LOG_DEBUG, "RSS: Aborting by shutdown: %s.\n", pUrl);
201 UnlinkRSSAggregator(RSSAggr);
206 eNextState AbortNetworkSaveMessage (AsyncIO *IO)
208 return eAbort; ///TODO
211 eNextState RSSSaveMessage(AsyncIO *IO)
215 rss_aggregator *RSSAggr = (rss_aggregator *) IO->Data;
217 RSSAggr->ThisMsg->Msg.cm_fields['M'] =
218 SmashStrBuf(&RSSAggr->ThisMsg->Message);
220 CtdlSubmitMsg(&RSSAggr->ThisMsg->Msg, &RSSAggr->recp, NULL, 0);
222 /* write the uidl to the use table so we don't store this item again */
223 cdb_store(CDB_USETABLE,
224 SKEY(RSSAggr->ThisMsg->MsgGUID),
225 &RSSAggr->ThisMsg->ut,
226 sizeof(struct UseTable) );
228 if (GetNextHashPos(RSSAggr->Messages,
231 (void**) &RSSAggr->ThisMsg))
232 return NextDBOperation(IO, RSS_FetchNetworkUsetableEntry);
237 eNextState RSS_FetchNetworkUsetableEntry(AsyncIO *IO)
241 struct cdbdata *cdbut;
242 rss_aggregator *Ctx = (rss_aggregator *) IO->Data;
244 /* Find out if we've already seen this item */
245 strcpy(Ctx->ThisMsg->ut.ut_msgid,
246 ChrPtr(Ctx->ThisMsg->MsgGUID)); /// TODO
247 Ctx->ThisMsg->ut.ut_timestamp = time(NULL);
249 cdbut = cdb_fetch(CDB_USETABLE, SKEY(Ctx->ThisMsg->MsgGUID));
252 /* Item has already been seen */
254 "%s has already been seen\n",
255 ChrPtr(Ctx->ThisMsg->MsgGUID));
258 /* rewrite the record anyway, to update the timestamp */
259 cdb_store(CDB_USETABLE,
260 SKEY(Ctx->ThisMsg->MsgGUID),
261 &Ctx->ThisMsg->ut, sizeof(struct UseTable) );
263 if (GetNextHashPos(Ctx->Messages,
266 (void**) &Ctx->ThisMsg))
267 return NextDBOperation(
269 RSS_FetchNetworkUsetableEntry);
276 NextDBOperation(IO, RSSSaveMessage);
284 int rss_do_fetching(rss_aggregator *Cfg)
291 if ((Cfg->next_poll != 0) && (now < Cfg->next_poll))
294 ri = (rss_item*) malloc(sizeof(rss_item));
295 memset(ri, 0, sizeof(rss_item));
298 if (! InitcURLIOStruct(&Cfg->IO,
300 "Citadel RSS Client",
301 RSSAggregator_ParseReply,
302 RSSAggregator_Terminate,
303 RSSAggregator_TerminateDB,
304 RSSAggregator_ShutdownAbort))
306 syslog(LOG_ALERT, "Unable to initialize libcurl.\n");
310 safestrncpy(((CitContext*)Cfg->IO.CitContext)->cs_host,
312 sizeof(((CitContext*)Cfg->IO.CitContext)->cs_host));
314 syslog(LOG_DEBUG, "Fetching RSS feed <%s>\n", ChrPtr(Cfg->Url));
315 ParseURL(&Cfg->IO.ConnectMe, Cfg->Url, 80);
316 CurlPrepareURL(Cfg->IO.ConnectMe);
318 QueueCurlContext(&Cfg->IO);
323 * Scan a room's netconfig to determine whether it is requesting any RSS feeds
325 void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
327 StrBuf *CfgData=NULL;
330 rss_room_counter *Count = NULL;
332 char filename[PATH_MAX];
335 rss_aggregator *RSSAggr = NULL;
336 rss_aggregator *use_this_RSSAggr = NULL;
338 const char *CfgPtr, *lPtr;
341 pthread_mutex_lock(&RSSQueueMutex);
342 if (GetHash(RSSQueueRooms, LKEY(qrbuf->QRnumber), &vptr))
345 "rssclient: [%ld] %s already in progress.\n",
348 pthread_mutex_unlock(&RSSQueueMutex);
351 pthread_mutex_unlock(&RSSQueueMutex);
353 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
355 if (server_shutting_down)
358 /* Only do net processing for rooms that have netconfigs */
359 fd = open(filename, 0);
362 "rssclient: %s no config.\n",
367 if (server_shutting_down)
370 if (fstat(fd, &statbuf) == -1) {
372 "ERROR: could not stat configfile '%s' - %s\n",
378 if (server_shutting_down)
381 CfgData = NewStrBufPlain(NULL, statbuf.st_size + 1);
383 if (StrBufReadBLOB(CfgData, &fd, 1, statbuf.st_size, &Err) < 0) {
385 FreeStrBuf(&CfgData);
386 syslog(LOG_DEBUG, "ERROR: reading config '%s' - %s<br>\n",
387 filename, strerror(errno));
391 if (server_shutting_down)
395 CfgType = NewStrBuf();
396 Line = NewStrBufPlain(NULL, StrLength(CfgData));
400 Done = StrBufSipLine(Line, CfgData, &CfgPtr) == 0;
401 if (StrLength(Line) > 0)
404 StrBufExtract_NextToken(CfgType, Line, &lPtr, '|');
405 if (!strcasecmp("rssclient", ChrPtr(CfgType)))
410 sizeof(rss_room_counter));
414 RSSAggr = (rss_aggregator *) malloc(
415 sizeof(rss_aggregator));
417 memset (RSSAggr, 0, sizeof(rss_aggregator));
418 RSSAggr->QRnumber = qrbuf->QRnumber;
419 RSSAggr->roomlist_parts = 1;
420 RSSAggr->Url = NewStrBuf();
422 StrBufExtract_NextToken(RSSAggr->Url,
427 pthread_mutex_lock(&RSSQueueMutex);
428 GetHash(RSSFetchUrls,
432 use_this_RSSAggr = (rss_aggregator *)vptr;
433 if (use_this_RSSAggr != NULL)
436 StrBufAppendBufPlain(
437 use_this_RSSAggr->rooms,
440 if (use_this_RSSAggr->roomlist_parts==1)
442 use_this_RSSAggr->OtherQRnumbers
443 = NewHash(1, lFlathash);
445 QRnumber = (long*)malloc(sizeof(long));
446 *QRnumber = qrbuf->QRnumber;
447 Put(use_this_RSSAggr->OtherQRnumbers,
448 LKEY(qrbuf->QRnumber),
451 use_this_RSSAggr->roomlist_parts++;
453 pthread_mutex_unlock(&RSSQueueMutex);
455 FreeStrBuf(&RSSAggr->Url);
460 pthread_mutex_unlock(&RSSQueueMutex);
462 RSSAggr->ItemType = RSS_UNSET;
464 RSSAggr->rooms = NewStrBufPlain(
467 pthread_mutex_lock(&RSSQueueMutex);
474 pthread_mutex_unlock(&RSSQueueMutex);
480 Count->QRnumber = qrbuf->QRnumber;
481 pthread_mutex_lock(&RSSQueueMutex);
482 syslog(LOG_DEBUG, "rssclient: [%ld] %s now starting.\n",
483 qrbuf->QRnumber, qrbuf->QRname);
484 Put(RSSQueueRooms, LKEY(qrbuf->QRnumber), Count, NULL);
485 pthread_mutex_unlock(&RSSQueueMutex);
487 FreeStrBuf(&CfgData);
488 FreeStrBuf(&CfgType);
493 * Scan for rooms that have RSS client requests configured
495 void rssclient_scan(void) {
496 rss_aggregator *rptr = NULL;
501 time_t now = time(NULL);
503 /* Run no more than once every 15 minutes. */
504 if ((now - last_run) < 900) {
506 "rssclient: polling interval not yet reached; last run was %ldm%lds ago",
507 ((now - last_run) / 60),
508 ((now - last_run) % 60)
514 * This is a simple concurrency check to make sure only one rssclient
515 * run is done at a time.We could do this with a mutex, but since we
516 * don't really require extremely fine granularity here, we'll do it
517 * with a static variable instead.
520 if ((GetCount(RSSQueueRooms) > 0) || (GetCount(RSSFetchUrls) > 0)) {
522 "rssclient: concurrency check failed; %d rooms and %d url's are queued",
523 GetCount(RSSQueueRooms),
524 GetCount(RSSFetchUrls)
529 become_session(&rss_CC);
530 syslog(LOG_DEBUG, "rssclient started\n");
531 CtdlForEachRoom(rssclient_scan_room, NULL);
533 pthread_mutex_lock(&RSSQueueMutex);
535 it = GetNewHashPos(RSSFetchUrls, 0);
536 while (!server_shutting_down &&
537 GetNextHashPos(RSSFetchUrls, it, &len, &Key, &vrptr) &&
539 rptr = (rss_aggregator *)vrptr;
540 if (!rss_do_fetching(rptr))
541 UnlinkRSSAggregator(rptr);
544 pthread_mutex_unlock(&RSSQueueMutex);
546 syslog(LOG_DEBUG, "rssclient ended\n");
550 void rss_cleanup(void)
552 /* citthread_mutex_destroy(&RSSQueueMutex); TODO */
553 DeleteHash(&RSSFetchUrls);
554 DeleteHash(&RSSQueueRooms);
558 CTDL_MODULE_INIT(rssclient)
562 CtdlFillSystemContext(&rss_CC, "rssclient");
563 pthread_mutex_init(&RSSQueueMutex, NULL);
564 RSSQueueRooms = NewHash(1, lFlathash);
565 RSSFetchUrls = NewHash(1, NULL);
566 syslog(LOG_INFO, "%s\n", curl_version());
567 CtdlRegisterSessionHook(rssclient_scan, EVT_TIMER);
568 CtdlRegisterEVCleanupHook(rss_cleanup);