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.
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.
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
39 #include <sys/types.h>
42 #include <curl/curl.h>
43 #include <libcitadel.h>
46 #include "citserver.h"
50 #include "ctdl_module.h"
52 #include "parsedate.h"
54 #include "citadel_dirs.h"
57 #include "event_client.h"
58 #include "rss_atom_parser.h"
61 #define TMP_MSGDATA 0xFF
62 #define TMP_SHORTER_URL_OFFSET 0xFE
63 #define TMP_SHORTER_URLS 0xFD
67 pthread_mutex_t RSSQueueMutex; /* locks the access to the following vars: */
68 HashList *RSSQueueRooms = NULL; /* rss_room_counter */
69 HashList *RSSFetchUrls = NULL; /*->rss_aggregator;->RefCount access locked*/
71 eNextState RSSAggregator_Terminate(AsyncIO *IO);
72 eNextState RSSAggregator_ShutdownAbort(AsyncIO *IO);
73 struct CitContext rss_CC;
75 struct rssnetcfg *rnclist = NULL;
78 void DeleteRoomReference(long QRnumber)
84 rss_room_counter *pRoomC;
86 At = GetNewHashPos(RSSQueueRooms, 0);
88 if (GetHashPosFromKey(RSSQueueRooms, LKEY(QRnumber), At))
90 GetHashPos(RSSQueueRooms, At, &HKLen, &HK, &vData);
93 pRoomC = (rss_room_counter *) vData;
95 if (pRoomC->count == 0)
96 DeleteEntryFromHash(RSSQueueRooms, At);
102 void UnlinkRooms(rss_aggregator *Cfg)
104 DeleteRoomReference(Cfg->QRnumber);
105 if (Cfg->OtherQRnumbers != NULL)
112 At = GetNewHashPos(Cfg->OtherQRnumbers, 0);
113 while (! server_shutting_down &&
114 GetNextHashPos(Cfg->OtherQRnumbers,
120 long *lData = (long*) vData;
121 DeleteRoomReference(*lData);
128 void UnlinkRSSAggregator(rss_aggregator *Cfg)
134 At = GetNewHashPos(RSSFetchUrls, 0);
135 if (GetHashPosFromKey(RSSFetchUrls, SKEY(Cfg->Url), At))
137 DeleteEntryFromHash(RSSFetchUrls, At);
140 last_run = time(NULL);
143 void DeleteRssCfg(void *vptr)
145 rss_aggregator *RSSAggr = (rss_aggregator *)vptr;
146 AsyncIO *IO = &RSSAggr->IO;
147 EVM_syslog(LOG_DEBUG, "RSS: destroying\n");
149 FreeStrBuf(&RSSAggr->Url);
150 FreeStrBuf(&RSSAggr->rooms);
151 FreeStrBuf(&RSSAggr->CData);
152 FreeStrBuf(&RSSAggr->Key);
153 DeleteHash(&RSSAggr->OtherQRnumbers);
155 DeleteHashPos (&RSSAggr->Pos);
156 DeleteHash (&RSSAggr->Messages);
157 if (RSSAggr->recp.recp_room != NULL)
158 free(RSSAggr->recp.recp_room);
161 if (RSSAggr->Item != NULL)
163 flush_rss_item(RSSAggr->Item);
168 FreeAsyncIOContents(&RSSAggr->IO);
172 eNextState RSSAggregator_Terminate(AsyncIO *IO)
174 rss_aggregator *RSSAggr = (rss_aggregator *)IO->Data;
176 EVM_syslog(LOG_DEBUG, "RSS: Terminating.\n");
179 UnlinkRSSAggregator(RSSAggr);
183 eNextState RSSAggregator_ShutdownAbort(AsyncIO *IO)
186 rss_aggregator *RSSAggr = (rss_aggregator *)IO->Data;
188 pUrl = IO->ConnectMe->PlainUrl;
192 EV_syslog(LOG_DEBUG, "RSS: Aborting by shutdown: %s.\n", pUrl);
195 UnlinkRSSAggregator(RSSAggr);
200 eNextState AbortNetworkSaveMessage (AsyncIO *IO)
202 return eAbort; ///TODO
205 eNextState RSSSaveMessage(AsyncIO *IO)
209 rss_aggregator *RSSAggr = (rss_aggregator *) IO->Data;
211 RSSAggr->ThisMsg->Msg.cm_fields['M'] =
212 SmashStrBuf(&RSSAggr->ThisMsg->Message);
214 CtdlSubmitMsg(&RSSAggr->ThisMsg->Msg, &RSSAggr->recp, NULL, 0);
216 /* write the uidl to the use table so we don't store this item again */
217 cdb_store(CDB_USETABLE,
218 SKEY(RSSAggr->ThisMsg->MsgGUID),
219 &RSSAggr->ThisMsg->ut,
220 sizeof(struct UseTable) );
222 if (GetNextHashPos(RSSAggr->Messages,
225 (void**) &RSSAggr->ThisMsg))
226 return NextDBOperation(IO, RSS_FetchNetworkUsetableEntry);
231 eNextState RSS_FetchNetworkUsetableEntry(AsyncIO *IO)
235 struct cdbdata *cdbut;
236 rss_aggregator *Ctx = (rss_aggregator *) IO->Data;
238 /* Find out if we've already seen this item */
239 strcpy(Ctx->ThisMsg->ut.ut_msgid,
240 ChrPtr(Ctx->ThisMsg->MsgGUID)); /// TODO
241 Ctx->ThisMsg->ut.ut_timestamp = time(NULL);
243 cdbut = cdb_fetch(CDB_USETABLE, SKEY(Ctx->ThisMsg->MsgGUID));
246 /* Item has already been seen */
248 "%s has already been seen\n",
249 ChrPtr(Ctx->ThisMsg->MsgGUID));
252 /* rewrite the record anyway, to update the timestamp */
253 cdb_store(CDB_USETABLE,
254 SKEY(Ctx->ThisMsg->MsgGUID),
255 &Ctx->ThisMsg->ut, sizeof(struct UseTable) );
257 if (GetNextHashPos(Ctx->Messages,
260 (void**) &Ctx->ThisMsg))
261 return NextDBOperation(
263 RSS_FetchNetworkUsetableEntry);
270 NextDBOperation(IO, RSSSaveMessage);
278 int rss_do_fetching(rss_aggregator *Cfg)
285 if ((Cfg->next_poll != 0) && (now < Cfg->next_poll))
288 ri = (rss_item*) malloc(sizeof(rss_item));
289 memset(ri, 0, sizeof(rss_item));
292 if (! InitcURLIOStruct(&Cfg->IO,
294 "Citadel RSS Client",
295 RSSAggregator_ParseReply,
296 RSSAggregator_Terminate,
297 RSSAggregator_ShutdownAbort))
299 syslog(LOG_ALERT, "Unable to initialize libcurl.\n");
303 safestrncpy(((CitContext*)Cfg->IO.CitContext)->cs_host,
305 sizeof(((CitContext*)Cfg->IO.CitContext)->cs_host));
307 syslog(LOG_DEBUG, "Fetching RSS feed <%s>\n", ChrPtr(Cfg->Url));
308 ParseURL(&Cfg->IO.ConnectMe, Cfg->Url, 80);
309 CurlPrepareURL(Cfg->IO.ConnectMe);
311 QueueCurlContext(&Cfg->IO);
316 * Scan a room's netconfig to determine whether it is requesting any RSS feeds
318 void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
320 StrBuf *CfgData=NULL;
323 rss_room_counter *Count = NULL;
325 char filename[PATH_MAX];
328 rss_aggregator *RSSAggr = NULL;
329 rss_aggregator *use_this_RSSAggr = NULL;
331 const char *CfgPtr, *lPtr;
334 pthread_mutex_lock(&RSSQueueMutex);
335 if (GetHash(RSSQueueRooms, LKEY(qrbuf->QRnumber), &vptr))
338 "rssclient: [%ld] %s already in progress.\n",
341 pthread_mutex_unlock(&RSSQueueMutex);
344 pthread_mutex_unlock(&RSSQueueMutex);
346 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
348 if (server_shutting_down)
351 /* Only do net processing for rooms that have netconfigs */
352 fd = open(filename, 0);
355 "rssclient: %s no config.\n",
360 if (server_shutting_down)
363 if (fstat(fd, &statbuf) == -1) {
365 "ERROR: could not stat configfile '%s' - %s\n",
371 if (server_shutting_down)
374 CfgData = NewStrBufPlain(NULL, statbuf.st_size + 1);
376 if (StrBufReadBLOB(CfgData, &fd, 1, statbuf.st_size, &Err) < 0) {
378 FreeStrBuf(&CfgData);
379 syslog(LOG_DEBUG, "ERROR: reading config '%s' - %s<br>\n",
380 filename, strerror(errno));
384 if (server_shutting_down)
388 CfgType = NewStrBuf();
389 Line = NewStrBufPlain(NULL, StrLength(CfgData));
393 Done = StrBufSipLine(Line, CfgData, &CfgPtr) == 0;
394 if (StrLength(Line) > 0)
397 StrBufExtract_NextToken(CfgType, Line, &lPtr, '|');
398 if (!strcasecmp("rssclient", ChrPtr(CfgType)))
403 sizeof(rss_room_counter));
407 RSSAggr = (rss_aggregator *) malloc(
408 sizeof(rss_aggregator));
410 memset (RSSAggr, 0, sizeof(rss_aggregator));
411 RSSAggr->QRnumber = qrbuf->QRnumber;
412 RSSAggr->roomlist_parts = 1;
413 RSSAggr->Url = NewStrBuf();
415 StrBufExtract_NextToken(RSSAggr->Url,
420 pthread_mutex_lock(&RSSQueueMutex);
421 GetHash(RSSFetchUrls,
425 use_this_RSSAggr = (rss_aggregator *)vptr;
426 if (use_this_RSSAggr != NULL)
429 StrBufAppendBufPlain(
430 use_this_RSSAggr->rooms,
433 if (use_this_RSSAggr->roomlist_parts==1)
435 use_this_RSSAggr->OtherQRnumbers
436 = NewHash(1, lFlathash);
438 QRnumber = (long*)malloc(sizeof(long));
439 *QRnumber = qrbuf->QRnumber;
440 Put(use_this_RSSAggr->OtherQRnumbers,
441 LKEY(qrbuf->QRnumber),
444 use_this_RSSAggr->roomlist_parts++;
446 pthread_mutex_unlock(&RSSQueueMutex);
448 FreeStrBuf(&RSSAggr->Url);
453 pthread_mutex_unlock(&RSSQueueMutex);
455 RSSAggr->ItemType = RSS_UNSET;
457 RSSAggr->rooms = NewStrBufPlain(
460 pthread_mutex_lock(&RSSQueueMutex);
467 pthread_mutex_unlock(&RSSQueueMutex);
473 Count->QRnumber = qrbuf->QRnumber;
474 pthread_mutex_lock(&RSSQueueMutex);
475 syslog(LOG_DEBUG, "rssclient: [%ld] %s now starting.\n",
476 qrbuf->QRnumber, qrbuf->QRname);
477 Put(RSSQueueRooms, LKEY(qrbuf->QRnumber), Count, NULL);
478 pthread_mutex_unlock(&RSSQueueMutex);
480 FreeStrBuf(&CfgData);
481 FreeStrBuf(&CfgType);
486 * Scan for rooms that have RSS client requests configured
488 void rssclient_scan(void) {
489 rss_aggregator *rptr = NULL;
494 time_t now = time(NULL);
496 /* Run no more than once every 15 minutes. */
497 if ((now - last_run) < 900) {
502 * This is a simple concurrency check to make sure only one rssclient
503 * run is done at a time.We could do this with a mutex, but since we
504 * don't really require extremely fine granularity here, we'll do it
505 * with a static variable instead.
508 if ((GetCount(RSSQueueRooms) > 0) || (GetCount(RSSFetchUrls) > 0))
511 become_session(&rss_CC);
512 syslog(LOG_DEBUG, "rssclient started\n");
513 CtdlForEachRoom(rssclient_scan_room, NULL);
515 pthread_mutex_lock(&RSSQueueMutex);
517 it = GetNewHashPos(RSSFetchUrls, 0);
518 while (!server_shutting_down &&
519 GetNextHashPos(RSSFetchUrls, it, &len, &Key, &vrptr) &&
521 rptr = (rss_aggregator *)vrptr;
522 if (!rss_do_fetching(rptr))
523 UnlinkRSSAggregator(rptr);
526 pthread_mutex_unlock(&RSSQueueMutex);
528 syslog(LOG_DEBUG, "rssclient ended\n");
532 void rss_cleanup(void)
534 /* citthread_mutex_destroy(&RSSQueueMutex); TODO */
535 DeleteHash(&RSSFetchUrls);
536 DeleteHash(&RSSQueueRooms);
540 CTDL_MODULE_INIT(rssclient)
544 CtdlFillSystemContext(&rss_CC, "rssclient");
545 pthread_mutex_init(&RSSQueueMutex, NULL);
546 RSSQueueRooms = NewHash(1, lFlathash);
547 RSSFetchUrls = NewHash(1, NULL);
548 syslog(LOG_INFO, "%s\n", curl_version());
549 CtdlRegisterSessionHook(rssclient_scan, EVT_TIMER);
550 CtdlRegisterEVCleanupHook(rss_cleanup);