]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/rssclient/serv_rssclient.c
Merge branch 'master' of ssh://git.citadel.org/appl/gitroot/citadel
[citadel.git] / citadel / modules / rssclient / serv_rssclient.c
index 17ec61ace1d02f24422832d27695bcc76bf3df0a..b2cd3747f92ccfad92bbe97544b0fdbdd507882b 100644 (file)
@@ -85,6 +85,7 @@ typedef struct _rss_item {
        int item_tag_nesting;
        StrBuf *author_or_creator;
        StrBuf *author_url;
+       StrBuf *author_email;
 }rss_item;
 
 
@@ -304,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
  */
@@ -319,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;
@@ -390,16 +408,53 @@ void rss_save_item(rss_item *ri)
                        StrBuf *UserName;
                        StrBuf *EmailAddress;
                        StrBuf *EncBuf;
+                       int FromAt;
+                       int FromLen;
                        
                        UserName = NewStrBuf();
                        EmailAddress = NewStrBuf();
                        EncBuf = NewStrBuf();
 ////TODO!
+                       StrBufTrim(ri->author_or_creator);
                        From = html_to_ascii(ChrPtr(ri->author_or_creator),
                                             StrLength(ri->author_or_creator), 
                                             512, 0);
-
-                       Encoded = NewStrBufPlain(From, -1);
+                       FromLen = strlen(From);
+                       if (From[FromLen - 1] == '\n')
+                       {
+                               From[FromLen - 1] = '\0';
+                       }
+                       FromAt = strchr(From, '@') != NULL;
+                       if (!FromAt && StrLength (ri->author_email) > 0)
+                       {
+                               Encoded = NewStrBuf();
+                               if (!IsEmptyStr(From))
+                               {
+                                       StrBufPrintf(Encoded,
+                                                    "%s<%s>", 
+                                                    From, 
+                                                    ChrPtr(ri->author_email));
+                               }
+                               else
+                               {
+                                       StrBufPrintf(Encoded,
+                                                    "<%s>", 
+                                                    ChrPtr(ri->author_email));
+                               }
+                       }
+                       else
+                       {
+                               if (FromAt)
+                                       Encoded = NewStrBufPlain(From, -1);
+                               else 
+                               {
+                                       Encoded = NewStrBuf();
+                                       StrBufPrintf(Encoded,
+                                                    "%s<%s>", 
+                                                    From, 
+                                                    "rss@localhost"); /// TODO: get hostname?
+                               }
+                       }
                        free(From);
                        StrBufTrim(Encoded);
                        QPEncoded = StrBufSanitizeEmailRecipientVector(Encoded, UserName, EmailAddress, EncBuf);
@@ -425,7 +480,13 @@ void rss_save_item(rss_item *ri)
                        StrBufSpaceToBlank(ri->title);
                        len = StrLength(ri->title);
                        Sbj = html_to_ascii(ChrPtr(ri->title), len, 512, 0);
-                       Encoded = NewStrBufPlain(Sbj, -1);
+                       len = strlen(Sbj);
+                       if (Sbj[len - 1] == '\n')
+                       {
+                               len --;
+                               Sbj[len] = '\0';
+                       }
+                       Encoded = NewStrBufPlain(Sbj, len);
                        free(Sbj);
 
                        StrBufTrim(Encoded);
@@ -444,21 +505,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);
@@ -520,6 +581,7 @@ void flush_rss_item(rss_item *ri)
        FreeStrBuf(&ri->title);
        FreeStrBuf(&ri->link);
        FreeStrBuf(&ri->author_or_creator);
+       FreeStrBuf(&ri->author_email);
        FreeStrBuf(&ri->author_url);
        FreeStrBuf(&ri->description);
 }
@@ -796,6 +858,13 @@ void RSS_item_link_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char*
                StrBufTrim(ri->link);
        }
 }
+void RSS_item_relink_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
+{
+       if (StrLength(CData) > 0) {
+               NewStrBufDupAppendFlush(&ri->reLink, CData, NULL, 0);
+               StrBufTrim(ri->reLink);
+       }
+}
 
 void RSSATOM_item_title_end (StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
 {
@@ -807,9 +876,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)
@@ -824,8 +903,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);
+               }
        }
 }
 
@@ -881,6 +971,14 @@ void ATOM_item_name_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char*
        }
 }
 
+void ATOM_item_email_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
+{
+       if (StrLength(CData) > 0) {
+               NewStrBufDupAppendFlush(&ri->author_email, CData, NULL, 0);
+               StrBufTrim(ri->author_email);
+       }
+}
+
 void RSS_item_creator_end(StrBuf *CData, rss_item *ri, rssnetcfg *Cfg, const char** Attr)
 {
        if ((StrLength(CData) > 0) && 
@@ -1187,15 +1285,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
@@ -1220,11 +1313,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;
 }
 
@@ -1234,7 +1322,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);
@@ -1251,9 +1339,15 @@ CTDL_MODULE_INIT(rssclient)
        AddRSSEndHandler(RSS_item_guid_end,        RSS_RSS|RSS_REQUIRE_BUF, HKEY("guid"));
        AddRSSEndHandler(ATOM_item_id_end,         RSS_ATOM|RSS_REQUIRE_BUF, HKEY("id"));
        AddRSSEndHandler(RSS_item_link_end,        RSS_RSS|RSS_REQUIRE_BUF, HKEY("link"));
+#if 0 
+// hm, rss to the comments of that blog, might be interesting in future, but... 
+       AddRSSEndHandler(RSS_item_relink_end,      RSS_RSS|RSS_REQUIRE_BUF, HKEY("commentrss"));
+// comment count...
+       AddRSSEndHandler(RSS_item_relink_end,      RSS_RSS|RSS_REQUIRE_BUF, HKEY("comments"));
+#endif
        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"));
@@ -1262,9 +1356,11 @@ CTDL_MODULE_INIT(rssclient)
        AddRSSEndHandler(RSS_item_date_end,        RSS_RSS|RSS_REQUIRE_BUF, HKEY("date"));
        AddRSSEndHandler(RSS_item_author_end,      RSS_RSS|RSS_REQUIRE_BUF, HKEY("author"));
        AddRSSEndHandler(RSS_item_creator_end,     RSS_RSS|RSS_REQUIRE_BUF, HKEY("creator"));
+/* <author> */
+       AddRSSEndHandler(ATOM_item_email_end,      RSS_ATOM|RSS_REQUIRE_BUF, HKEY("email"));
        AddRSSEndHandler(ATOM_item_name_end,       RSS_ATOM|RSS_REQUIRE_BUF, HKEY("name"));
        AddRSSEndHandler(ATOM_item_uri_end,        RSS_ATOM|RSS_REQUIRE_BUF, HKEY("uri"));
-
+/* </author> */
        AddRSSEndHandler(RSS_item_item_end,        RSS_RSS, HKEY("item"));
        AddRSSEndHandler(RSS_item_rss_end,         RSS_RSS, HKEY("rss"));
        AddRSSEndHandler(RSS_item_rdf_end,         RSS_RSS, HKEY("rdf"));