+void rss_save_item(rss_item *ri, rss_aggregator *Cfg)
+{
+ networker_save_message *SaveMsg;
+ struct MD5Context md5context;
+ u_char rawdigest[MD5_DIGEST_LEN];
+ int msglen = 0;
+ StrBuf *Message;
+ StrBuf *guid;
+ AsyncIO *IO = &Cfg->IO;
+ int n;
+
+
+ SaveMsg = (networker_save_message *) malloc(
+ sizeof(networker_save_message));
+ memset(SaveMsg, 0, sizeof(networker_save_message));
+
+ /* Construct a GUID to use in the S_USETABLE table.
+ * If one is not present in the item itself, make one up.
+ */
+ if (ri->guid != NULL) {
+ StrBufSpaceToBlank(ri->guid);
+ StrBufTrim(ri->guid);
+ guid = NewStrBufPlain(HKEY("rss/"));
+ StrBufAppendBuf(guid, ri->guid, 0);
+ }
+ else {
+ MD5Init(&md5context);
+ if (ri->title != NULL) {
+ MD5Update(&md5context,
+ (const unsigned char*)SKEY(ri->title));
+ }
+ if (ri->link != NULL) {
+ MD5Update(&md5context,
+ (const unsigned char*)SKEY(ri->link));
+ }
+ MD5Final(rawdigest, &md5context);
+ guid = NewStrBufPlain(NULL,
+ MD5_DIGEST_LEN * 2 + 12 /* _rss2ctdl*/);
+ StrBufHexEscAppend(guid, NULL, rawdigest, MD5_DIGEST_LEN);
+ StrBufAppendBufPlain(guid, HKEY("_rss2ctdl"), 0);
+ }
+
+ /* translate Item into message. */
+ EVM_syslog(LOG_DEBUG, "RSS: translating item...\n");
+ if (ri->description == NULL) ri->description = NewStrBufPlain(HKEY(""));
+ StrBufSpaceToBlank(ri->description);
+ SaveMsg->Msg.cm_magic = CTDLMESSAGE_MAGIC;
+ SaveMsg->Msg.cm_anon_type = MES_NORMAL;
+ SaveMsg->Msg.cm_format_type = FMT_RFC822;
+
+ if (ri->guid != NULL) {
+ SaveMsg->Msg.cm_fields['E'] = strdup(ChrPtr(ri->guid));
+ }
+
+ if (ri->author_or_creator != NULL) {
+ char *From;
+ StrBuf *Encoded = NULL;
+ int FromAt;
+
+ From = html_to_ascii(ChrPtr(ri->author_or_creator),
+ StrLength(ri->author_or_creator),
+ 512, 0);
+ StrBufPlain(ri->author_or_creator, From, -1);
+ StrBufTrim(ri->author_or_creator);
+ free(From);
+
+ FromAt = strchr(ChrPtr(ri->author_or_creator), '@') != NULL;
+ if (!FromAt && StrLength (ri->author_email) > 0)
+ {
+ StrBufRFC2047encode(&Encoded, ri->author_or_creator);
+ SaveMsg->Msg.cm_fields['A'] = SmashStrBuf(&Encoded);
+ SaveMsg->Msg.cm_fields['P'] =
+ SmashStrBuf(&ri->author_email);
+ }
+ else
+ {
+ if (FromAt)
+ {
+ SaveMsg->Msg.cm_fields['A'] =
+ SmashStrBuf(&ri->author_or_creator);
+ SaveMsg->Msg.cm_fields['P'] =
+ strdup(SaveMsg->Msg.cm_fields['A']);
+ }
+ else
+ {
+ StrBufRFC2047encode(&Encoded,
+ ri->author_or_creator);
+ SaveMsg->Msg.cm_fields['A'] =
+ SmashStrBuf(&Encoded);
+ SaveMsg->Msg.cm_fields['P'] =
+ strdup("rss@localhost");
+
+ }
+ if (ri->pubdate <= 0) {
+ ri->pubdate = time(NULL);
+ }
+ }
+ }
+ else {
+ SaveMsg->Msg.cm_fields['A'] = strdup("rss");
+ }
+
+ SaveMsg->Msg.cm_fields['N'] = strdup(NODENAME);
+ if (ri->title != NULL) {
+ long len;
+ char *Sbj;
+ StrBuf *Encoded, *QPEncoded;
+
+ QPEncoded = NULL;
+ StrBufSpaceToBlank(ri->title);
+ len = StrLength(ri->title);
+ Sbj = html_to_ascii(ChrPtr(ri->title), len, 512, 0);
+ len = strlen(Sbj);
+ if (Sbj[len - 1] == '\n')
+ {
+ len --;
+ Sbj[len] = '\0';
+ }
+ Encoded = NewStrBufPlain(Sbj, len);
+ free(Sbj);
+
+ StrBufTrim(Encoded);
+ StrBufRFC2047encode(&QPEncoded, Encoded);
+
+ SaveMsg->Msg.cm_fields['U'] = SmashStrBuf(&QPEncoded);
+ FreeStrBuf(&Encoded);
+ }
+ SaveMsg->Msg.cm_fields['T'] = malloc(64);
+ snprintf(SaveMsg->Msg.cm_fields['T'], 64, "%ld", ri->pubdate);
+ if (ri->channel_title != NULL) {
+ if (StrLength(ri->channel_title) > 0) {
+ SaveMsg->Msg.cm_fields['O'] =
+ strdup(ChrPtr(ri->channel_title));
+ }
+ }
+ if (ri->link == NULL)
+ ri->link = NewStrBufPlain(HKEY(""));
+
+#if 0 /* temporarily disable shorter urls. */
+ SaveMsg->Msg.cm_fields[TMP_SHORTER_URLS] =
+ GetShorterUrls(ri->description);
+#endif
+
+ msglen += 1024 + StrLength(ri->link) + StrLength(ri->description) ;
+
+ Message = NewStrBufPlain(NULL, StrLength(ri->description));
+
+ 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, 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);
+
+ SaveMsg->MsgGUID = guid;
+ SaveMsg->Message = Message;
+
+ n = GetCount(Cfg->Messages) + 1;
+ Put(Cfg->Messages, IKEY(n), SaveMsg, FreeNetworkSaveMessage);
+}
+
+
+void rss_xml_start(void *data, const char *supplied_el, const char **attr)
+{
+ rss_xml_handler *h;
+ rss_aggregator *RSSAggr = (rss_aggregator*) data;
+ rss_item *ri = RSSAggr->Item;
+ void *pv;
+ const char *pel;
+ char *sep = NULL;
+
+ /* Axe the namespace, we don't care about it */
+/// syslog(LOG_DEBUG, "RSS: supplied el %d: %s...\n", RSSAggr->Cfg->ItemType, supplied_el);
+ pel = supplied_el;
+ while (sep = strchr(pel, ':'), sep) {
+ pel = sep + 1;
+ }
+
+ if (pel != supplied_el)
+ {
+ void *v;
+
+ if (!GetHash(KnownNameSpaces,
+ supplied_el,
+ pel - supplied_el - 1,
+ &v))
+ {
+#ifdef DEBUG_RSS
+ syslog(LOG_DEBUG, "RSS: START ignoring because of wrong namespace [%s]\n",
+ supplied_el);
+#endif
+ return;
+ }
+ }
+
+ StrBufPlain(RSSAggr->Key, pel, -1);
+ StrBufLowerCase(RSSAggr->Key);
+ if (GetHash(StartHandlers, SKEY(RSSAggr->Key), &pv))
+ {
+ h = (rss_xml_handler*) pv;
+
+ if (((h->Flags & RSS_UNSET) != 0) &&
+ (RSSAggr->ItemType == RSS_UNSET))
+ {
+ h->Handler(RSSAggr->CData, ri, RSSAggr, attr);
+ }
+ else if (((h->Flags & RSS_RSS) != 0) &&
+ (RSSAggr->ItemType == RSS_RSS))
+ {
+ h->Handler(RSSAggr->CData, ri, RSSAggr, attr);
+ }
+ else if (((h->Flags & RSS_ATOM) != 0) &&
+ (RSSAggr->ItemType == RSS_ATOM))
+ {
+ h->Handler(RSSAggr->CData, ri, RSSAggr, attr);
+ }
+#ifdef DEBUG_RSS
+ else
+ syslog(LOG_DEBUG, "RSS: START unhandled: [%s] [%s]...\n", pel, supplied_el);
+#endif
+ }
+#ifdef DEBUG_RSS
+ else
+ syslog(LOG_DEBUG, "RSS: START unhandled: [%s] [%s]...\n", pel, supplied_el);
+#endif
+}
+
+void rss_xml_end(void *data, const char *supplied_el)
+{
+ rss_xml_handler *h;
+ rss_aggregator *RSSAggr = (rss_aggregator*) data;
+ rss_item *ri = RSSAggr->Item;
+ const char *pel;
+ char *sep = NULL;
+ void *pv;
+
+ /* Axe the namespace, we don't care about it */
+ pel = supplied_el;
+ while (sep = strchr(pel, ':'), sep) {
+ pel = sep + 1;
+ }
+// syslog(LOG_DEBUG, "RSS: END %s...\n", el);
+ if (pel != supplied_el)
+ {
+ void *v;
+
+ if (!GetHash(KnownNameSpaces,
+ supplied_el,
+ pel - supplied_el - 1,
+ &v))
+ {
+#ifdef DEBUG_RSS
+ syslog(LOG_DEBUG, "RSS: END ignoring because of wrong namespace [%s] = [%s]\n",
+ supplied_el, ChrPtr(RSSAggr->CData));
+#endif
+ FlushStrBuf(RSSAggr->CData);
+ return;
+ }
+ }
+
+ StrBufPlain(RSSAggr->Key, pel, -1);
+ StrBufLowerCase(RSSAggr->Key);
+ if (GetHash(EndHandlers, SKEY(RSSAggr->Key), &pv))
+ {
+ h = (rss_xml_handler*) pv;
+
+ if (((h->Flags & RSS_UNSET) != 0) &&
+ (RSSAggr->ItemType == RSS_UNSET))
+ {
+ h->Handler(RSSAggr->CData, ri, RSSAggr, NULL);
+ }
+ else if (((h->Flags & RSS_RSS) != 0) &&
+ (RSSAggr->ItemType == RSS_RSS))
+ {
+ h->Handler(RSSAggr->CData, ri, RSSAggr, NULL);
+ }
+ else if (((h->Flags & RSS_ATOM) != 0) &&
+ (RSSAggr->ItemType == RSS_ATOM))
+ {
+ h->Handler(RSSAggr->CData, ri, RSSAggr, NULL);
+ }
+#ifdef DEBUG_RSS
+ else
+ syslog(LOG_DEBUG, "RSS: END unhandled: [%s] [%s] = [%s]...\n", pel, supplied_el, ChrPtr(RSSAggr->CData));
+#endif
+ }
+#ifdef DEBUG_RSS
+ else
+ syslog(LOG_DEBUG, "RSS: END unhandled: [%s] [%s] = [%s]...\n", pel, supplied_el, ChrPtr(RSSAggr->CData));
+#endif
+ FlushStrBuf(RSSAggr->CData);
+}
+
+/*
+ * Callback function for passing libcurl's output to expat for parsing
+ * we don't do streamed parsing so expat can handle non-utf8 documents