/*
* Bring external RSS feeds into rooms.
*
- * Copyright (c) 2007-2012 by the citadel.org team
+ * Copyright (c) 2007-2017 by the citadel.org team
*
* This program is open source software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3.
#include "citadel_dirs.h"
#include "md5.h"
#include "context.h"
-#include "event_client.h"
-#include "rss_atom_parser.h"
+#include "internet_addressing.h"
+struct rssroom {
+ struct rssroom *next;
+ char *room;
+};
-#define TMP_MSGDATA 0xFF
-#define TMP_SHORTER_URL_OFFSET 0xFE
-#define TMP_SHORTER_URLS 0xFD
-
-time_t last_run = 0L;
+struct rssurl {
+ struct rssurl *next;
+ char *url;
+ struct rssroom *rooms;
+};
-pthread_mutex_t RSSQueueMutex; /* locks the access to the following vars: */
-HashList *RSSQueueRooms = NULL; /* rss_room_counter */
-HashList *RSSFetchUrls = NULL; /*->rss_aggregator;->RefCount access locked*/
+struct rssparser {
+ StrBuf *CData;
+ struct CtdlMessage *msg;
+ char *link;
+ char *description;
+ char *item_id;
+ struct rssroom *rooms;
+};
-eNextState RSSAggregator_Terminate(AsyncIO *IO);
-eNextState RSSAggregator_TerminateDB(AsyncIO *IO);
-eNextState RSSAggregator_ShutdownAbort(AsyncIO *IO);
+time_t last_run = 0L;
struct CitContext rss_CC;
+struct rssurl *rsstodo = NULL;
-struct rssnetcfg *rnclist = NULL;
-int RSSClientDebugEnabled = 0;
-#define N ((rss_aggregator*)IO->Data)->Cfg.QRnumber
-
-#define DBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (RSSClientDebugEnabled != 0))
-
-#define EVRSSC_syslog(LEVEL, FORMAT, ...) \
- DBGLOG(LEVEL) syslog(LEVEL, \
- "%s[%ld]CC[%d][%ld]RSS" FORMAT, \
- IOSTR, IO->ID, CCID, N, __VA_ARGS__)
-
-#define EVRSSCM_syslog(LEVEL, FORMAT) \
- DBGLOG(LEVEL) syslog(LEVEL, \
- "%s[%ld]CC[%d][%ld]RSS" FORMAT, \
- IOSTR, IO->ID, CCID, N)
-
-#define EVRSSQ_syslog(LEVEL, FORMAT, ...) \
- DBGLOG(LEVEL) syslog(LEVEL, "RSS" FORMAT, \
- __VA_ARGS__)
-#define EVRSSQM_syslog(LEVEL, FORMAT) \
- DBGLOG(LEVEL) syslog(LEVEL, "RSS" FORMAT)
-
-#define EVRSSCSM_syslog(LEVEL, FORMAT) \
- DBGLOG(LEVEL) syslog(LEVEL, "%s[%ld][%ld]RSS" FORMAT, \
- IOSTR, IO->ID, N)
-
-typedef enum _RSSState {
- eRSSCreated,
- eRSSFetching,
- eRSSFailure,
- eRSSParsing,
- eRSSUT
-} RSSState;
-ConstStr RSSStates[] = {
- {HKEY("Aggregator created")},
- {HKEY("Fetching content")},
- {HKEY("Failed")},
- {HKEY("parsing content")},
- {HKEY("checking usetable")}
-};
-static void SetRSSState(AsyncIO *IO, RSSState State)
+// This handler is called whenever an XML tag opens.
+//
+void rss_start_element(void *data, const char *el, const char **attribute)
{
- CitContext* CCC = IO->CitContext;
- if (CCC != NULL)
- memcpy(CCC->cs_clientname, RSSStates[State].Key, RSSStates[State].len + 1);
-}
-
-void DeleteRoomReference(long QRnumber)
-{
- HashPos *At;
- long HKLen;
- const char *HK;
- void *vData = NULL;
- rss_room_counter *pRoomC;
-
- At = GetNewHashPos(RSSQueueRooms, 0);
-
- if (GetHashPosFromKey(RSSQueueRooms, LKEY(QRnumber), At))
- {
- GetHashPos(RSSQueueRooms, At, &HKLen, &HK, &vData);
- if (vData != NULL)
- {
- pRoomC = (rss_room_counter *) vData;
- pRoomC->count --;
- if (pRoomC->count == 0)
- DeleteEntryFromHash(RSSQueueRooms, At);
+ struct rssparser *r = (struct rssparser *)data;
+ int i;
+
+ if (
+ (!strcasecmp(el, "entry"))
+ || (!strcasecmp(el, "item"))
+ ) {
+ // this is the start of a new item(rss) or entry(atom)
+ if (r->msg != NULL) {
+ CM_Free(r->msg);
+ r->msg = NULL;
}
+ r->msg = malloc(sizeof(struct CtdlMessage));
+ memset(r->msg, 0, sizeof(struct CtdlMessage));
+ r->msg->cm_magic = CTDLMESSAGE_MAGIC;
+ r->msg->cm_anon_type = MES_NORMAL;
+ r->msg->cm_format_type = FMT_RFC822;
}
- DeleteHashPos(&At);
-}
-void UnlinkRooms(rss_aggregator *RSSAggr)
-{
- DeleteRoomReference(RSSAggr->Cfg.QRnumber);
- if (RSSAggr->OtherQRnumbers != NULL)
- {
- long HKLen;
- const char *HK;
- HashPos *At;
- void *vData;
-
- At = GetNewHashPos(RSSAggr->OtherQRnumbers, 0);
- while (! server_shutting_down &&
- GetNextHashPos(RSSAggr->OtherQRnumbers,
- At,
- &HKLen, &HK,
- &vData) &&
- (vData != NULL))
- {
- pRSSConfig *Data = (pRSSConfig*) vData;
- DeleteRoomReference(Data->QRnumber);
+ else if (!strcasecmp(el, "link")) { // atom feeds have the link as an attribute
+ for(i = 0; attribute[i]; i += 2) {
+ if (!strcasecmp(attribute[i], "href")) {
+ if (r->link != NULL) {
+ free(r->link);
+ r->link = NULL;
+ }
+ r->link = strdup(attribute[i+1]);
+ striplt(r->link);
+ }
}
-
- DeleteHashPos(&At);
}
}
-void UnlinkRSSAggregator(rss_aggregator *RSSAggr)
-{
- HashPos *At;
-
- pthread_mutex_lock(&RSSQueueMutex);
- UnlinkRooms(RSSAggr);
- At = GetNewHashPos(RSSFetchUrls, 0);
- if (GetHashPosFromKey(RSSFetchUrls, SKEY(RSSAggr->Url), At))
- {
- DeleteEntryFromHash(RSSFetchUrls, At);
- }
- DeleteHashPos(&At);
- last_run = time(NULL);
- pthread_mutex_unlock(&RSSQueueMutex);
-}
-
-void DeleteRssCfg(void *vptr)
+// This handler is called whenever an XML tag closes.
+//
+void rss_end_element(void *data, const char *el)
{
- rss_aggregator *RSSAggr = (rss_aggregator *)vptr;
- AsyncIO *IO = &RSSAggr->IO;
-
- if (IO->CitContext != NULL)
- EVRSSCM_syslog(LOG_DEBUG, "RSS: destroying\n");
+ struct rssparser *r = (struct rssparser *)data;
- FreeStrBuf(&RSSAggr->Url);
- FreeStrBuf(&RSSAggr->rooms);
- FreeStrBuf(&RSSAggr->CData);
- FreeStrBuf(&RSSAggr->Key);
- DeleteHash(&RSSAggr->OtherQRnumbers);
+ if ( // end of a new item(rss) or entry(atom)
+ (!strcasecmp(el, "entry"))
+ || (!strcasecmp(el, "item"))
+ ) {
- DeleteHashPos (&RSSAggr->Pos);
- DeleteHash (&RSSAggr->Messages);
- if (RSSAggr->recp.recp_room != NULL)
- free(RSSAggr->recp.recp_room);
+ if (r->msg != NULL) { // Save the message to the rooms
+ // use the link as an item id if nothing else is available
+ if ((r->item_id == NULL) && (r->link != NULL)) {
+ r->item_id = strdup(r->link);
+ }
- if (RSSAggr->Item != NULL)
- {
- flush_rss_item(RSSAggr->Item);
+ // check the use table
+ StrBuf *u = NewStrBuf();
+ StrBufAppendPrintf(u, "rss/%s", r->item_id);
+ time_t already_seen = CheckIfAlreadySeen(u, time(NULL), 0, eUpdate);
+ FreeStrBuf(&u);
+
+ if (already_seen == 0) {
+
+ // Compose the message text
+ StrBuf *TheMessage = NewStrBuf();
+ StrBufAppendPrintf(TheMessage,
+ "Content-type: text/html\n\n"
+ "\n\n"
+ "<html><head></head><body>"
+ );
+
+ if (r->description != NULL) {
+ StrBufAppendPrintf(TheMessage, "%s<br><br>\r\n", r->description);
+ free(r->description);
+ r->description = NULL;
+ }
+
+ if (r->link != NULL) {
+ StrBufAppendPrintf(TheMessage, "<a href=\"%s\">%s</a>\r\n", r->link, r->link);
+ free(r->link);
+ r->link = NULL;
+ }
+
+ StrBufAppendPrintf(TheMessage, "</body></html>\r\n");
+ CM_SetField(r->msg, eMesageText, ChrPtr(TheMessage), StrLength(TheMessage));
+ FreeStrBuf(&TheMessage);
+
+ if (CM_IsEmpty(r->msg, eAuthor)) {
+ CM_SetField(r->msg, eAuthor, HKEY("rss"));
+ }
+
+ if (CM_IsEmpty(r->msg, eTimestamp)) {
+ CM_SetFieldLONG(r->msg, eTimestamp, time(NULL));
+ }
+
+ // Save it to the room(s)
+ struct rssroom *rr = NULL;
+ long msgnum = (-1);
+ for (rr=r->rooms; rr!=NULL; rr=rr->next) {
+ if (rr == r->rooms) {
+ msgnum = CtdlSubmitMsg(r->msg, NULL, rr->room, 0);
+ }
+ else {
+ CtdlSaveMsgPointerInRoom(rr->room, msgnum, 0, NULL);
+ }
+ syslog(LOG_DEBUG, "Saved message %ld to %s", msgnum, rr->room);
+ }
+ }
+ else {
+ syslog(LOG_DEBUG, "%s was already seen %ld seconds ago", r->item_id, already_seen);
+ }
+
+ CM_Free(r->msg);
+ r->msg = NULL;
+ }
- free(RSSAggr->Item);
+ if (r->item_id != NULL) {
+ free(r->item_id);
+ r->item_id = NULL;
+ }
}
- FreeAsyncIOContents(&RSSAggr->IO);
- memset(RSSAggr, 0, sizeof(rss_aggregator));
- free(RSSAggr);
-}
-
-eNextState RSSAggregator_Terminate(AsyncIO *IO)
-{
- rss_aggregator *RSSAggr = (rss_aggregator *)IO->Data;
-
- EVRSSCM_syslog(LOG_DEBUG, "RSS: Terminating.\n");
-
- StopCurlWatchers(IO);
- UnlinkRSSAggregator(RSSAggr);
- return eAbort;
-}
-
-eNextState RSSAggregator_TerminateDB(AsyncIO *IO)
-{
- rss_aggregator *RSSAggr = (rss_aggregator *)IO->Data;
-
- EVRSSCM_syslog(LOG_DEBUG, "RSS: Terminating.\n");
-
-
- StopDBWatchers(&RSSAggr->IO);
- UnlinkRSSAggregator(RSSAggr);
- return eAbort;
-}
-
-eNextState RSSAggregator_ShutdownAbort(AsyncIO *IO)
-{
- const char *pUrl;
- rss_aggregator *RSSAggr = (rss_aggregator *)IO->Data;
-
- pUrl = IO->ConnectMe->PlainUrl;
- if (pUrl == NULL)
- pUrl = "";
-
- EVRSSC_syslog(LOG_DEBUG, "RSS: Aborting by shutdown: %s.\n", pUrl);
-
- StopCurlWatchers(IO);
- UnlinkRSSAggregator(RSSAggr);
- return eAbort;
-}
-
-void AppendLink(StrBuf *Message,
- StrBuf *link,
- StrBuf *LinkTitle,
- const char *Title)
-{
- if (StrLength(link) > 0)
- {
- StrBufAppendBufPlain(Message, HKEY("<a href=\""), 0);
- StrBufAppendBuf(Message, link, 0);
- StrBufAppendBufPlain(Message, HKEY("\">"), 0);
- if (StrLength(LinkTitle) > 0)
- StrBufAppendBuf(Message, LinkTitle, 0);
- else if ((Title != NULL) && !IsEmptyStr(Title))
- StrBufAppendBufPlain(Message, Title, -1, 0);
- else
- StrBufAppendBuf(Message, link, 0);
- StrBufAppendBufPlain(Message, HKEY("</a><br>\n"), 0);
+ else if (!strcasecmp(el, "title")) { // item subject (rss and atom)
+ if ((r->msg != NULL) && (CM_IsEmpty(r->msg, eMsgSubject))) {
+ CM_SetField(r->msg, eMsgSubject, ChrPtr(r->CData), StrLength(r->CData));
+ striplt(r->msg->cm_fields[eMsgSubject]);
+ }
}
-}
-
-int rss_format_item(AsyncIO *IO, networker_save_message *SaveMsg)
-{
- StrBuf *Message;
- int msglen = 0;
-
- if (StrLength(SaveMsg->description) +
- StrLength(SaveMsg->link) +
- StrLength(SaveMsg->linkTitle) +
- StrLength(SaveMsg->reLink) +
- StrLength(SaveMsg->reLinkTitle) +
- StrLength(SaveMsg->title) == 0)
- {
- EVRSSCM_syslog(LOG_INFO, "Refusing to save empty message.");
- return 0;
+ else if (!strcasecmp(el, "author")) { // author of item (rss and maybe atom)
+ if ((r->msg != NULL) && (CM_IsEmpty(r->msg, eAuthor))) {
+ CM_SetField(r->msg, eAuthor, ChrPtr(r->CData), StrLength(r->CData));
+ striplt(r->msg->cm_fields[eAuthor]);
+ }
}
- CM_Flush(&SaveMsg->Msg);
-
- if (SaveMsg->author_or_creator != NULL) {
-
- char *From;
- StrBuf *Encoded = NULL;
- int FromAt;
-
- From = html_to_ascii(ChrPtr(SaveMsg->author_or_creator),
- StrLength(SaveMsg->author_or_creator),
- 512, 0);
- StrBufPlain(SaveMsg->author_or_creator, From, -1);
- StrBufTrim(SaveMsg->author_or_creator);
- free(From);
-
- FromAt = strchr(ChrPtr(SaveMsg->author_or_creator), '@') != NULL;
- if (!FromAt && StrLength (SaveMsg->author_email) > 0)
- {
- StrBufRFC2047encode(&Encoded, SaveMsg->author_or_creator);
- CM_SetAsFieldSB(&SaveMsg->Msg, eAuthor, &Encoded);
- CM_SetAsFieldSB(&SaveMsg->Msg, eMessagePath, &SaveMsg->author_email);
+ else if (!strcasecmp(el, "pubdate")) { // date/time stamp (rss) Sat, 25 Feb 2017 14:28:01 EST
+ if ((r->msg)&&(r->msg->cm_fields[eTimestamp]==NULL)) {
+ CM_SetFieldLONG(r->msg, eTimestamp, parsedate(ChrPtr(r->CData)));
}
- else
- {
- if (FromAt)
- {
- CM_SetAsFieldSB(&SaveMsg->Msg, eAuthor, &SaveMsg->author_or_creator);
- CM_CopyField(&SaveMsg->Msg, eMessagePath, eAuthor);
- }
- else
- {
- StrBufRFC2047encode(&Encoded,
- SaveMsg->author_or_creator);
- CM_SetAsFieldSB(&SaveMsg->Msg, eAuthor, &Encoded);
- CM_SetField(&SaveMsg->Msg, eMessagePath, HKEY("rss@localhost"));
+ }
- }
+ else if (!strcasecmp(el, "updated")) { // date/time stamp (atom) 2003-12-13T18:30:02Z
+ if ((r->msg)&&(r->msg->cm_fields[eTimestamp]==NULL)) {
+ struct tm t;
+ char zulu;
+ memset(&t, 0, sizeof t);
+ sscanf(ChrPtr(r->CData), "%d-%d-%dT%d:%d:%d%c", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec, &zulu);
+ t.tm_year -= 1900;
+ t.tm_mon -= 1;
+ CM_SetFieldLONG(r->msg, eTimestamp, mktime(&t));
}
}
- else {
- CM_SetField(&SaveMsg->Msg, eAuthor, HKEY("rss"));
- }
- CM_SetField(&SaveMsg->Msg, eNodeName, CFG_KEY(c_nodename));
- if (SaveMsg->title != NULL) {
- long len;
- char *Sbj;
- StrBuf *Encoded, *QPEncoded;
-
- QPEncoded = NULL;
- StrBufSpaceToBlank(SaveMsg->title);
- len = StrLength(SaveMsg->title);
- Sbj = html_to_ascii(ChrPtr(SaveMsg->title), len, 512, 0);
- len = strlen(Sbj);
- if ((len > 0) && (Sbj[len - 1] == '\n'))
- {
- len --;
- Sbj[len] = '\0';
+ else if (!strcasecmp(el, "link")) { // link to story (rss)
+ if (r->link != NULL) {
+ free(r->link);
+ r->link = NULL;
}
- Encoded = NewStrBufPlain(Sbj, len);
- free(Sbj);
-
- StrBufTrim(Encoded);
- StrBufRFC2047encode(&QPEncoded, Encoded);
-
- CM_SetAsFieldSB(&SaveMsg->Msg, eMsgSubject, &QPEncoded);
- FreeStrBuf(&Encoded);
+ r->link = strdup(ChrPtr(r->CData));
+ striplt(r->link);
}
- if (SaveMsg->link == NULL)
- SaveMsg->link = NewStrBufPlain(HKEY(""));
-
-#if 0 /* temporarily disable shorter urls. */
- SaveMsg->Msg.cm_fields[TMP_SHORTER_URLS] =
- GetShorterUrls(SaveMsg->description);
-#endif
- msglen += 1024 + StrLength(SaveMsg->link) + StrLength(SaveMsg->description) ;
-
- Message = NewStrBufPlain(NULL, msglen);
-
- StrBufPlain(Message, HKEY(
- "Content-type: text/html; charset=\"UTF-8\"\r\n\r\n"
- "<html><body>\n"));
-#if 0 /* disable shorter url for now. */
- SaveMsg->Msg.cm_fields[TMP_SHORTER_URL_OFFSET] = StrLength(Message);
-#endif
- StrBufAppendBuf(Message, SaveMsg->description, 0);
- StrBufAppendBufPlain(Message, HKEY("<br><br>\n"), 0);
+ else if (
+ (!strcasecmp(el, "guid")) // unique item id (rss)
+ || (!strcasecmp(el, "id")) // unique item id (atom)
+ ) {
+ if (r->item_id != NULL) {
+ free(r->item_id);
+ r->item_id = NULL;
+ }
+ r->item_id = strdup(ChrPtr(r->CData));
+ striplt(r->item_id);
+ }
- AppendLink(Message, SaveMsg->link, SaveMsg->linkTitle, NULL);
- AppendLink(Message, SaveMsg->reLink, SaveMsg->reLinkTitle, "Reply to this");
- StrBufAppendBufPlain(Message, HKEY("</body></html>\n"), 0);
+ else if (
+ (!strcasecmp(el, "description")) // message text (rss)
+ || (!strcasecmp(el, "summary")) // message text (atom)
+ || (!strcasecmp(el, "content")) // message text (atom)
+ ) {
+ if (r->description != NULL) {
+ free(r->description);
+ r->description = NULL;
+ }
+ r->description = strdup(ChrPtr(r->CData));
+ striplt(r->description);
+ }
- SaveMsg->Message = Message;
- return 1;
+ if (r->CData != NULL) {
+ FreeStrBuf(&r->CData);
+ r->CData = NULL;
+ }
}
-eNextState RSSSaveMessage(AsyncIO *IO)
-{
- long len;
- const char *Key;
- rss_aggregator *RSSAggr = (rss_aggregator *) IO->Data;
- if (rss_format_item(IO, RSSAggr->ThisMsg))
- {
- CM_SetAsFieldSB(&RSSAggr->ThisMsg->Msg, eMesageText,
- &RSSAggr->ThisMsg->Message);
+// This handler is called whenever data appears between opening and closing tags.
+//
+void rss_handle_data(void *data, const char *content, int length)
+{
+ struct rssparser *r = (struct rssparser *)data;
- CtdlSubmitMsg(&RSSAggr->ThisMsg->Msg, &RSSAggr->recp, NULL, 0);
-
- /* write the uidl to the use table so we don't store this item again */
-
- CheckIfAlreadySeen("RSS Item Insert", RSSAggr->ThisMsg->MsgGUID, EvGetNow(IO), 0, eWrite, CCID, IO->ID);
+ if (r->CData == NULL) {
+ r->CData = NewStrBuf();
}
- if (GetNextHashPos(RSSAggr->Messages,
- RSSAggr->Pos,
- &len, &Key,
- (void**) &RSSAggr->ThisMsg))
- return NextDBOperation(IO, RSS_FetchNetworkUsetableEntry);
- else
- return eAbort;
+ StrBufAppendBufPlain(r->CData, content, length, 0);
}
-eNextState RSS_FetchNetworkUsetableEntry(AsyncIO *IO)
+
+// Feed has been downloaded, now parse it.
+//
+void rss_parse_feed(StrBuf *Feed, struct rssroom *rooms)
{
- static const time_t antiExpire = USETABLE_ANTIEXPIRE_HIRES;
- time_t seenstamp = 0;
-#ifndef DEBUG_RSS
- const char *Key;
- long len;
- rss_aggregator *Ctx = (rss_aggregator *) IO->Data;
-
- /* Find out if we've already seen this item */
-// todo: expiry?
- SetRSSState(IO, eRSSUT);
- seenstamp = CheckIfAlreadySeen("RSS Item Seen",
- Ctx->ThisMsg->MsgGUID,
- EvGetNow(IO),
- antiExpire,
- eCheckUpdate,
- CCID, IO->ID);
- if (seenstamp < antiExpire)
- {
- /* Item has already been seen */
- EVRSSC_syslog(LOG_DEBUG,
- "%s has already been seen - %ld < %ld",
- ChrPtr(Ctx->ThisMsg->MsgGUID),
- seenstamp, antiExpire);
-
- SetRSSState(IO, eRSSParsing);
-
- if (GetNextHashPos(Ctx->Messages,
- Ctx->Pos,
- &len, &Key,
- (void**) &Ctx->ThisMsg))
- return NextDBOperation(
- IO,
- RSS_FetchNetworkUsetableEntry);
- else
- return eAbort;
- }
- else
-#endif
- {
- /* Item has already been seen */
- EVRSSC_syslog(LOG_DEBUG,
- "%s Parsing - %ld >= %ld",
- ChrPtr(Ctx->ThisMsg->MsgGUID),
- seenstamp, antiExpire);
- SetRSSState(IO, eRSSParsing);
-
- NextDBOperation(IO, RSSSaveMessage);
- return eSendMore;
- }
- return eSendMore;
+ struct rssparser r;
+
+ memset(&r, 0, sizeof r);
+ r.rooms = rooms;
+ XML_Parser p = XML_ParserCreate("UTF-8");
+ XML_SetElementHandler(p, rss_start_element, rss_end_element);
+ XML_SetCharacterDataHandler(p, rss_handle_data);
+ XML_SetUserData(p, (void *)&r);
+ XML_Parse(p, ChrPtr(Feed), StrLength(Feed), XML_TRUE);
+ XML_ParserFree(p);
}
-void UpdateLastKnownGood(pRSSConfig *pCfg, time_t now)
+
+// Add a feed/room pair into the todo list
+//
+void rssclient_push_todo(char *rssurl, char *roomname)
{
- OneRoomNetCfg* pRNCfg;
- begin_critical_section(S_NETCONFIGS);
- pRNCfg = CtdlGetNetCfgForRoom (pCfg->QRnumber);
- if (pRNCfg != NULL)
- {
- RSSCfgLine *RSSCfg = (RSSCfgLine *)pRNCfg->NetConfigs[rssclient];
+ struct rssurl *r = NULL;
+ struct rssurl *thisone = NULL;
+ struct rssroom *newroom = NULL;
- while (RSSCfg != NULL)
- {
- if (RSSCfg == pCfg->pCfg)
- break;
+ syslog(LOG_DEBUG, "rssclient_push_todo(%s, %s)", rssurl, roomname);
- RSSCfg = RSSCfg->next;
- }
- if (RSSCfg != NULL)
- {
- pRNCfg->changed = 1;
- RSSCfg->last_known_good = now;
+ for (r=rsstodo; r!=NULL; r=r->next) {
+ if (!strcasecmp(r->url, rssurl)) {
+ thisone = r;
}
}
-
- end_critical_section(S_NETCONFIGS);
-}
-
-eNextState RSSAggregator_AnalyseReply(AsyncIO *IO)
-{
- HashPos *it = NULL;
- long len;
- const char *Key;
- pRSSConfig *pCfg;
- u_char rawdigest[MD5_DIGEST_LEN];
- struct MD5Context md5context;
- StrBuf *guid;
- rss_aggregator *Ctx = (rss_aggregator *) IO->Data;
-
- if (IO->HttpReq.httpcode != 200)
- {
- StrBuf *ErrMsg;
- long lens[2];
- const char *strs[2];
-
- SetRSSState(IO, eRSSFailure);
- ErrMsg = NewStrBuf();
- if (IO) EVRSSC_syslog(LOG_ALERT, "need a 200, got a %ld !\n",
- IO->HttpReq.httpcode);
-
- strs[0] = ChrPtr(Ctx->Url);
- lens[0] = StrLength(Ctx->Url);
-
- strs[1] = ChrPtr(Ctx->rooms);
- lens[1] = StrLength(Ctx->rooms);
-
- if (IO->HttpReq.CurlError == NULL)
- IO->HttpReq.CurlError = "";
-
- StrBufPrintf(ErrMsg,
- "Error while RSS-Aggregation Run of %s\n"
- " need a 200, got a %ld !\n"
- " Curl Error message: \n%s / %s\n"
- " Response text was: \n"
- " \n %s\n",
- ChrPtr(Ctx->Url),
- IO->HttpReq.httpcode,
- IO->HttpReq.errdesc,
- IO->HttpReq.CurlError,
- ChrPtr(IO->HttpReq.ReplyData)
- );
-
- CtdlAideFPMessage(
- ChrPtr(ErrMsg),
- "RSS Aggregation run failure",
- 2, strs, (long*) &lens,
- CCID,
- IO->ID,
- EvGetNow(IO));
-
- FreeStrBuf(&ErrMsg);
- EVRSSC_syslog(LOG_DEBUG,
- "RSS feed returned an invalid http status code. <%s><HTTP %ld>\n",
- ChrPtr(Ctx->Url),
- IO->HttpReq.httpcode);
- return eAbort;
+ if (thisone == NULL) {
+ thisone = malloc(sizeof(struct rssurl));
+ thisone->url = strdup(rssurl);
+ thisone->rooms = NULL;
+ thisone->next = rsstodo;
+ rsstodo = thisone;
}
- pCfg = &Ctx->Cfg;
-
- while (pCfg != NULL)
- {
- UpdateLastKnownGood (pCfg, EvGetNow(IO));
- if ((Ctx->roomlist_parts > 1) &&
- (it == NULL))
- {
- it = GetNewHashPos(RSSFetchUrls, 0);
- }
- if (it != NULL)
- {
- void *vptr;
- if (GetNextHashPos(Ctx->OtherQRnumbers, it, &len, &Key, &vptr))
- pCfg = vptr;
- else
- pCfg = NULL;
- }
- else
- pCfg = NULL;
- }
- DeleteHashPos (&it);
+ newroom = malloc(sizeof(struct rssroom));
+ newroom->room = strdup(roomname);
+ newroom->next = thisone->rooms;
+ thisone->rooms = newroom;
+}
- SetRSSState(IO, eRSSUT);
- MD5Init(&md5context);
+// Callback function for curl
+//
+size_t rss_pof_write_data(void *buffer, size_t size, size_t nmemb, void *userp)
+{
+ StrBuf *Downloaded = (StrBuf *)userp;
+ size_t bytes = size * nmemb;
+ StrBufAppendBufPlain(Downloaded, buffer, bytes, 0);
+ return(bytes);
+}
- MD5Update(&md5context,
- (const unsigned char*)SKEY(IO->HttpReq.ReplyData));
- MD5Update(&md5context,
- (const unsigned char*)SKEY(Ctx->Url));
+// pull one feed (possibly multiple rooms)
+//
+void rss_pull_one_feed(struct rssurl *url)
+{
+ CURL *curl;
+ CURLcode res;
+ StrBuf *Downloaded = NULL;
- MD5Final(rawdigest, &md5context);
- guid = NewStrBufPlain(NULL,
- MD5_DIGEST_LEN * 2 + 12 /* _rss2ctdl*/);
- StrBufHexEscAppend(guid, NULL, rawdigest, MD5_DIGEST_LEN);
- StrBufAppendBufPlain(guid, HKEY("_rssFM"), 0);
- if (StrLength(guid) > 40)
- StrBufCutAt(guid, 40, NULL);
- /* Find out if we've already seen this item */
+ syslog(LOG_DEBUG, "rss_pull_one_feed(%s)", url->url);
-#ifndef DEBUG_RSS
+ curl = curl_easy_init();
+ if (!curl) {
+ return;
+ }
- if (CheckIfAlreadySeen("RSS Whole",
- guid,
- EvGetNow(IO),
- EvGetNow(IO) - USETABLE_ANTIEXPIRE,
- eCheckUpdate,
- CCID, IO->ID)
- != 0)
- {
- FreeStrBuf(&guid);
+ Downloaded = NewStrBuf();
- EVRSSC_syslog(LOG_DEBUG, "RSS feed already seen. <%s>\n", ChrPtr(Ctx->Url));
- return eAbort;
+ curl_easy_setopt(curl, CURLOPT_URL, url->url);
+ curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Follow redirects
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rss_pof_write_data); // What to do with downloaded data
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, Downloaded); // Give it our StrBuf to work with
+ curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L); // Time out after 20 seconds
+ res = curl_easy_perform(curl); // Perform the request
+ if (res != CURLE_OK) {
+ syslog(LOG_WARNING, "Failed to load feed: %s", curl_easy_strerror(res));
}
- FreeStrBuf(&guid);
-#endif
- SetRSSState(IO, eRSSParsing);
- return RSSAggregator_ParseReply(IO);
-}
+ curl_easy_cleanup(curl);
-eNextState RSSAggregator_FinishHttp(AsyncIO *IO)
-{
- return CurlQueueDBOperation(IO, RSSAggregator_AnalyseReply);
+ rss_parse_feed(Downloaded, url->rooms); // parse the feed
+ FreeStrBuf(&Downloaded); // free the downloaded feed data
}
-/*
- * Begin a feed parse
- */
-int rss_do_fetching(rss_aggregator *RSSAggr)
+
+// We have a list, now download the feeds
+//
+void rss_pull_feeds(void)
{
- AsyncIO *IO = &RSSAggr->IO;
- rss_item *ri;
- time_t now;
-
- now = time(NULL);
-
- if ((RSSAggr->next_poll != 0) && (now < RSSAggr->next_poll))
- return 0;
-
- ri = (rss_item*) malloc(sizeof(rss_item));
- memset(ri, 0, sizeof(rss_item));
- RSSAggr->Item = ri;
-
- if (! InitcURLIOStruct(&RSSAggr->IO,
- RSSAggr,
- "Citadel RSS Client",
- RSSAggregator_FinishHttp,
- RSSAggregator_Terminate,
- RSSAggregator_TerminateDB,
- RSSAggregator_ShutdownAbort))
- {
- EVRSSCM_syslog(LOG_ALERT, "Unable to initialize libcurl.\n");
- return 0;
+ struct rssurl *r;
+ struct rssroom *rr;
+
+ while (rsstodo != NULL) {
+ rss_pull_one_feed(rsstodo);
+ r = rsstodo;
+ rsstodo = rsstodo->next;
+ while (r->rooms != NULL) {
+ rr = r->rooms;
+ r->rooms = r->rooms->next;
+ free(rr->room);
+ free(rr);
+ }
+ free(r->url);
+ free(r);
}
- SetRSSState(IO, eRSSCreated);
-
- safestrncpy(((CitContext*)RSSAggr->IO.CitContext)->cs_host,
- ChrPtr(RSSAggr->Url),
- sizeof(((CitContext*)RSSAggr->IO.CitContext)->cs_host));
-
- EVRSSC_syslog(LOG_DEBUG, "Fetching RSS feed <%s>\n", ChrPtr(RSSAggr->Url));
- ParseURL(&RSSAggr->IO.ConnectMe, RSSAggr->Url, 80);
- CurlPrepareURL(RSSAggr->IO.ConnectMe);
-
- SetRSSState(IO, eRSSFetching);
- QueueCurlContext(&RSSAggr->IO);
- return 1;
}
-/*
- * Scan a room's netconfig to determine whether it is requesting any RSS feeds
- */
-void rssclient_scan_room(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCFG)
+
+// Scan a room's netconfig looking for RSS feed parsing requests
+//
+void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
{
- const RSSCfgLine *RSSCfg = (RSSCfgLine *)OneRNCFG->NetConfigs[rssclient];
- rss_aggregator *RSSAggr = NULL;
- rss_aggregator *use_this_RSSAggr = NULL;
- void *vptr;
+ char *serialized_config = NULL;
+ int num_configs = 0;
+ char cfgline[SIZ];
+ int i = 0;
- pthread_mutex_lock(&RSSQueueMutex);
- if (GetHash(RSSQueueRooms, LKEY(qrbuf->QRnumber), &vptr))
- {
- EVRSSQ_syslog(LOG_DEBUG,
- "rssclient: [%ld] %s already in progress.\n",
- qrbuf->QRnumber,
- qrbuf->QRname);
- pthread_mutex_unlock(&RSSQueueMutex);
+ serialized_config = LoadRoomNetConfigFile(qrbuf->QRnumber);
+ if (!serialized_config) {
return;
}
- pthread_mutex_unlock(&RSSQueueMutex);
- if (server_shutting_down) return;
-
- while (RSSCfg != NULL)
- {
- pthread_mutex_lock(&RSSQueueMutex);
- GetHash(RSSFetchUrls,
- SKEY(RSSCfg->Url),
- &vptr);
-
- use_this_RSSAggr = (rss_aggregator *)vptr;
- if (use_this_RSSAggr != NULL)
- {
- pRSSConfig *pRSSCfg;
-
- StrBufAppendBufPlain(
- use_this_RSSAggr->rooms,
- qrbuf->QRname,
- -1, 0);
- if (use_this_RSSAggr->roomlist_parts==1)
- {
- use_this_RSSAggr->OtherQRnumbers
- = NewHash(1, lFlathash);
+ num_configs = num_tokens(serialized_config, '\n');
+ for (i=0; i<num_configs; ++i) {
+ extract_token(cfgline, serialized_config, i, '\n', sizeof cfgline);
+ if (!strncasecmp(cfgline, HKEY("rssclient|"))) {
+ strcpy(cfgline, &cfgline[10]);
+ char *vbar = strchr(cfgline, '|');
+ if (vbar != NULL) {
+ *vbar = 0;
}
-
- pRSSCfg = (pRSSConfig *) malloc(sizeof(pRSSConfig));
-
- pRSSCfg->QRnumber = qrbuf->QRnumber;
- pRSSCfg->pCfg = RSSCfg;
-
- Put(use_this_RSSAggr->OtherQRnumbers,
- LKEY(qrbuf->QRnumber),
- pRSSCfg,
- NULL);
- use_this_RSSAggr->roomlist_parts++;
-
- pthread_mutex_unlock(&RSSQueueMutex);
-
- RSSCfg = RSSCfg->next;
- continue;
+ rssclient_push_todo(cfgline, qrbuf->QRname);
}
- pthread_mutex_unlock(&RSSQueueMutex);
-
- RSSAggr = (rss_aggregator *) malloc(
- sizeof(rss_aggregator));
-
- memset (RSSAggr, 0, sizeof(rss_aggregator));
- RSSAggr->Cfg.QRnumber = qrbuf->QRnumber;
- RSSAggr->Cfg.pCfg = RSSCfg;
- RSSAggr->roomlist_parts = 1;
- RSSAggr->Url = NewStrBufDup(RSSCfg->Url);
-
- RSSAggr->ItemType = RSS_UNSET;
-
- RSSAggr->rooms = NewStrBufPlain(
- qrbuf->QRname, -1);
-
- pthread_mutex_lock(&RSSQueueMutex);
-
- Put(RSSFetchUrls,
- SKEY(RSSAggr->Url),
- RSSAggr,
- DeleteRssCfg);
-
- pthread_mutex_unlock(&RSSQueueMutex);
- RSSCfg = RSSCfg->next;
}
+
+ free(serialized_config);
}
+
/*
* Scan for rooms that have RSS client requests configured
*/
void rssclient_scan(void) {
- int RSSRoomCount, RSSCount;
- rss_aggregator *rptr = NULL;
- void *vrptr = NULL;
- HashPos *it;
- long len;
- const char *Key;
time_t now = time(NULL);
/* Run no more than once every 15 minutes. */
if ((now - last_run) < 900) {
- EVRSSQ_syslog(LOG_DEBUG,
- "Client: polling interval not yet reached; last run was %ldm%lds ago",
- ((now - last_run) / 60),
- ((now - last_run) % 60)
+ syslog(LOG_DEBUG,
+ "Client: polling interval not yet reached; last run was %ldm%lds ago",
+ ((now - last_run) / 60),
+ ((now - last_run) % 60)
);
return;
}
- /*
- * This is a simple concurrency check to make sure only one rssclient
- * run is done at a time.
- */
- pthread_mutex_lock(&RSSQueueMutex);
- RSSCount = GetCount(RSSFetchUrls);
- RSSRoomCount = GetCount(RSSQueueRooms);
- pthread_mutex_unlock(&RSSQueueMutex);
-
- if ((RSSRoomCount > 0) || (RSSCount > 0)) {
- EVRSSQ_syslog(LOG_DEBUG,
- "rssclient: concurrency check failed; %d rooms and %d url's are queued",
- RSSRoomCount, RSSCount
- );
- return;
- }
-
become_session(&rss_CC);
- EVRSSQM_syslog(LOG_DEBUG, "rssclient started");
- CtdlForEachNetCfgRoom(rssclient_scan_room, NULL, rssclient);
-
- if (GetCount(RSSFetchUrls) > 0)
- {
- pthread_mutex_lock(&RSSQueueMutex);
- EVRSSQ_syslog(LOG_DEBUG,
- "rssclient starting %d Clients",
- GetCount(RSSFetchUrls));
-
- it = GetNewHashPos(RSSFetchUrls, 0);
- while (!server_shutting_down &&
- GetNextHashPos(RSSFetchUrls, it, &len, &Key, &vrptr) &&
- (vrptr != NULL)) {
- rptr = (rss_aggregator *)vrptr;
- if (!rss_do_fetching(rptr))
- UnlinkRSSAggregator(rptr);
- }
- DeleteHashPos(&it);
- pthread_mutex_unlock(&RSSQueueMutex);
- }
- else
- EVRSSQM_syslog(LOG_DEBUG, "Nothing to do.");
-
- EVRSSQM_syslog(LOG_DEBUG, "rssclient ended\n");
+ syslog(LOG_DEBUG, "rssclient started");
+ CtdlForEachRoom(rssclient_scan_room, NULL);
+ rss_pull_feeds();
+ syslog(LOG_DEBUG, "rssclient ended");
+ last_run = time(NULL);
return;
}
-void rss_cleanup(void)
-{
- /* citthread_mutex_destroy(&RSSQueueMutex); TODO */
- DeleteHash(&RSSFetchUrls);
- DeleteHash(&RSSQueueRooms);
-}
-
-void LogDebugEnableRSSClient(const int n)
-{
- RSSClientDebugEnabled = n;
-}
-
-
-typedef struct __RSSVetoInfo {
- StrBuf *ErrMsg;
- time_t Now;
- int Veto;
-}RSSVetoInfo;
-
-void rssclient_veto_scan_room(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCFG)
-{
- RSSVetoInfo *Info = (RSSVetoInfo *) data;
- const RSSCfgLine *RSSCfg = (RSSCfgLine *)OneRNCFG->NetConfigs[rssclient];
-
- while (RSSCfg != NULL)
- {
- if ((RSSCfg->last_known_good != 0) &&
- (RSSCfg->last_known_good + USETABLE_ANTIEXPIRE < Info->Now))
- {
- StrBufAppendPrintf(Info->ErrMsg,
- "RSS feed not seen for a %d days:: <",
- (Info->Now - RSSCfg->last_known_good) / (24 * 60 * 60));
-
- StrBufAppendBuf(Info->ErrMsg, RSSCfg->Url, 0);
- StrBufAppendBufPlain(Info->ErrMsg, HKEY(">\n"), 0);
- }
- RSSCfg = RSSCfg->next;
- }
-}
-
-int RSSCheckUsetableVeto(StrBuf *ErrMsg)
-{
- RSSVetoInfo Info;
-
- Info.ErrMsg = ErrMsg;
- Info.Now = time (NULL);
- Info.Veto = 0;
-
- CtdlForEachNetCfgRoom(rssclient_veto_scan_room, &Info, rssclient);
-
- return Info.Veto;;
-}
-
-
-
-
-void ParseRSSClientCfgLine(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCFG)
-{
- RSSCfgLine *RSSCfg;
-
- RSSCfg = (RSSCfgLine *) malloc (sizeof(RSSCfgLine));
- RSSCfg->Url = NewStrBufPlain (NULL, StrLength (Line));
-
-
- StrBufExtract_NextToken(RSSCfg->Url, Line, &LinePos, '|');
- RSSCfg->last_known_good = StrBufExtractNext_long(Line, &LinePos, '|');
-
-
- RSSCfg->next = (RSSCfgLine *)OneRNCFG->NetConfigs[ThisOne->C];
- OneRNCFG->NetConfigs[ThisOne->C] = (RoomNetCfgLine*) RSSCfg;
-}
-
-void SerializeRSSClientCfgLine(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *RNCfg, RoomNetCfgLine *data)
-{
- RSSCfgLine *RSSCfg = (RSSCfgLine*) data;
-
- StrBufAppendBufPlain(OutputBuffer, CKEY(ThisOne->Str), 0);
- StrBufAppendBufPlain(OutputBuffer, HKEY("|"), 0);
- StrBufAppendBufPlain(OutputBuffer, SKEY(RSSCfg->Url), 0);
- StrBufAppendPrintf(OutputBuffer, "|%ld\n", RSSCfg->last_known_good);
-}
-
-void DeleteRSSClientCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data)
-{
- RSSCfgLine *RSSCfg = (RSSCfgLine*) *data;
-
- FreeStrBuf(&RSSCfg->Url);
- free(*data);
- *data = NULL;
-}
-
CTDL_MODULE_INIT(rssclient)
{
if (!threading)
{
- CtdlRegisterTDAPVetoHook (RSSCheckUsetableVeto, CDB_USETABLE, 0);
-
- CtdlREGISTERRoomCfgType(rssclient, ParseRSSClientCfgLine, 0, 1, SerializeRSSClientCfgLine, DeleteRSSClientCfgLine);
- pthread_mutex_init(&RSSQueueMutex, NULL);
- RSSQueueRooms = NewHash(1, lFlathash);
- RSSFetchUrls = NewHash(1, NULL);
- syslog(LOG_INFO, "%s\n", curl_version());
+ syslog(LOG_INFO, "%s", curl_version());
CtdlRegisterSessionHook(rssclient_scan, EVT_TIMER, PRIO_AGGR + 300);
- CtdlRegisterEVCleanupHook(rss_cleanup);
- CtdlRegisterDebugFlagHook(HKEY("rssclient"), LogDebugEnableRSSClient, &RSSClientDebugEnabled);
}
else
{
}
return "rssclient";
}
+