]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/rssclient/serv_rssclient.c
More of the same. I think it's time to reduce complexity here.
[citadel.git] / citadel / modules / rssclient / serv_rssclient.c
index e43b56ee07b63b5865577745e48691b4afc1c4e2..0d8442b41551ba101e2c46196c56594a3dbf26fb 100644 (file)
@@ -305,6 +305,22 @@ handle_unknown_xml_encoding (void *encodingHandleData,
 ///#endif
 //#endif
 
+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);
+       }
+}
 /*
  * Commit a fetched and parsed RSS item to disk
  */
@@ -320,6 +336,7 @@ void rss_save_item(rss_item *ri)
        struct CtdlMessage *msg;
        struct recptypes *recp = NULL;
        int msglen = 0;
+       StrBuf *Message;
 
        recp = (struct recptypes *) malloc(sizeof(struct recptypes));
        if (recp == NULL) return;
@@ -357,7 +374,7 @@ void rss_save_item(rss_item *ri)
        /* Find out if we've already seen this item */
 
        cdbut = cdb_fetch(CDB_USETABLE, utmsgid, strlen(utmsgid));
-#ifdef DEBUG_RSS/////TODO ifndef
+#ifndef DEBUG_RSS
        if (cdbut != NULL) {
                /* Item has already been seen */
                CtdlLogPrintf(CTDL_DEBUG, "%s has already been seen\n", utmsgid);
@@ -475,21 +492,21 @@ void rss_save_item(rss_item *ri)
                if (ri->link == NULL) 
                        ri->link = NewStrBufPlain(HKEY(""));
                msglen += 1024 + StrLength(ri->link) + StrLength(ri->description) ;
-               msg->cm_fields['M'] = malloc(msglen);
-               snprintf(msg->cm_fields['M'], msglen,
+
+               Message = NewStrBufPlain(NULL, StrLength(ri->description));
+
+               StrBufPlain(Message, HKEY(
                         "Content-type: text/html; charset=\"UTF-8\"\r\n\r\n"
-                        "<html><body>\n"
-                        "%s<br><br>\n"
-                        "<a href=\"%s\">%s</a><br>\n"
-                        "<a href=\"%s\">%s</a>\n"
-                        "</body></html>\n"
-                        ,
-                        ChrPtr(ri->description),
-                        ChrPtr(ri->link),
-                        (StrLength(ri->linkTitle)>0)?ChrPtr(ri->linkTitle):ChrPtr(ri->link),
-                        ChrPtr(ri->reLink),
-                        (StrLength(ri->reLinkTitle)>0)?ChrPtr(ri->reLinkTitle):"Reply to this"
-               );
+                        "<html><body>\n"));
+
+               StrBufAppendBuf(Message, ri->description, 0);
+               StrBufAppendBufPlain(Message, HKEY("<br><br>\n"), 0);
+
+               AppendLink(Message, ri->link, ri->linkTitle, NULL);
+               AppendLink(Message, ri->reLink, ri->reLinkTitle, "Reply to this");
+               StrBufAppendBufPlain(Message, HKEY("</body></html>\n"), 0);
+
+               msg->cm_fields['M'] = SmashStrBuf(&Message);
 
                CtdlSubmitMsg(msg, recp, NULL, 0);
                CtdlFreeMessage(msg);
@@ -839,9 +856,19 @@ void RSSATOM_item_title_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const
 
 void ATOM_item_content_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
 {
-       if (StrLength(CData) > 0) {
-               NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
-               StrBufTrim(ri->description);
+       long olen = StrLength (ri->description);
+       long clen = StrLength (CData);
+       if (clen > 0) 
+       {
+               if (olen == 0) {
+                       NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
+                       StrBufTrim(ri->description);
+               }
+               else if (olen < clen) {
+                       FlushStrBuf(ri->description);
+                       NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
+                       StrBufTrim(ri->description);
+               }
        }
 }
 void ATOM_item_summary_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
@@ -856,8 +883,19 @@ void ATOM_item_summary_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const c
 
 void RSS_item_description_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
 {
-       if (StrLength(CData) > 0) {
-               NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
+       long olen = StrLength (ri->description);
+       long clen = StrLength (CData);
+       if (clen > 0) 
+       {
+               if (olen == 0) {
+                       NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
+                       StrBufTrim(ri->description);
+               }
+               else if (olen < clen) {
+                       FlushStrBuf(ri->description);
+                       NewStrBufDupAppendFlush(&ri->description, CData, NULL, 0);
+                       StrBufTrim(ri->description);
+               }
        }
 }
 
@@ -1227,15 +1265,10 @@ void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
 /*
  * Scan for rooms that have RSS client requests configured
  */
-void *rssclient_scan(void *args) {
+void rssclient_scan(void *args) {
        static time_t last_run = 0L;
        static int doing_rssclient = 0;
        rssnetcfg *rptr = NULL;
-       CitContext rssclientCC;
-
-       /* Give this thread its own private CitContext */
-       CtdlFillSystemContext(&rssclientCC, "rssclient");
-       citthread_setspecific(MyConKey, (void *)&rssclientCC );
 
        /*
         * This is a simple concurrency check to make sure only one rssclient run
@@ -1260,11 +1293,6 @@ void *rssclient_scan(void *args) {
        CtdlLogPrintf(CTDL_DEBUG, "rssclient ended\n");
        last_run = time(NULL);
        doing_rssclient = 0;
-       if (!CtdlThreadCheckStop())
-               CtdlThreadSchedule ("RSS Client", CTDLTHREAD_BIGSTACK, rssclient_scan, NULL, last_run + config.c_net_freq);
-       else
-               CtdlLogPrintf(CTDL_DEBUG, "rssclient: Task STOPPED.\n");
-       CtdlClearSystemContext();
        return NULL;
 }
 
@@ -1274,7 +1302,7 @@ CTDL_MODULE_INIT(rssclient)
        if (threading)
        {
                CtdlLogPrintf(CTDL_INFO, "%s\n", curl_version());
-               CtdlThreadSchedule ("RSS Client", CTDLTHREAD_BIGSTACK, rssclient_scan, NULL, 0);
+               CtdlRegisterSessionHook(rssclient_scan, EVT_TIMER);
        }
 
        StartHandlers = NewHash(1, NULL);
@@ -1293,7 +1321,7 @@ CTDL_MODULE_INIT(rssclient)
        AddRSSEndHandler(RSS_item_link_end,        RSS_RSS|RSS_REQUIRE_BUF, HKEY("link"));
        AddRSSEndHandler(RSSATOM_item_title_end,   RSS_ATOM|RSS_RSS|RSS_REQUIRE_BUF, HKEY("title"));
        AddRSSEndHandler(ATOM_item_content_end,    RSS_ATOM|RSS_REQUIRE_BUF, HKEY("content"));
-       AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("encoded"));
+       AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_ATOM|RSS_REQUIRE_BUF, HKEY("encoded"));
        AddRSSEndHandler(ATOM_item_summary_end,    RSS_ATOM|RSS_REQUIRE_BUF, HKEY("summary"));
        AddRSSEndHandler(RSS_item_description_end, RSS_RSS|RSS_REQUIRE_BUF, HKEY("description"));
        AddRSSEndHandler(ATOM_item_published_end,  RSS_ATOM|RSS_REQUIRE_BUF, HKEY("published"));