-typedef struct rssnetcfg rssnetcfg;
-struct rssnetcfg {
- rssnetcfg *next;
- char url[256];
- char *rooms;
- time_t last_error_when;
- int ItemType;
-};
-
-#define RSS_UNSET (1<<0)
-#define RSS_RSS (1<<1)
-#define RSS_ATOM (1<<2)
-#define RSS_REQUIRE_BUF (1<<3)
-
-typedef struct _rss_item {
- char *roomlist;
- int done_parsing;
- StrBuf *guid;
- StrBuf *title;
- StrBuf *link;
- StrBuf *linkTitle;
- StrBuf *reLink;
- StrBuf *reLinkTitle;
- StrBuf *description;
- time_t pubdate;
- StrBuf *channel_title;
- int item_tag_nesting;
- StrBuf *author_or_creator;
- StrBuf *author_url;
- StrBuf *author_email;
-}rss_item;
-
-
-typedef void (*rss_handler_func)(StrBuf *CData,
- rss_item *ri,
- rssnetcfg *Cfg,
- const char** Attr);
-
-typedef struct __rss_xml_handler {
- int Flags;
- rss_handler_func Handler;
-}rss_xml_handler;
-
-
-typedef struct _rsscollection {
- StrBuf *CData;
- StrBuf *Key;
-
- rss_item *Item;
- rssnetcfg *Cfg;
-
- rss_xml_handler *Current;
-} rsscollection;
-
-struct rssnetcfg *rnclist = NULL;
-HashList *StartHandlers;
-HashList *EndHandlers;
-HashList *KnownNameSpaces;
-void AddRSSStartHandler(rss_handler_func Handler, int Flags, const char *key, long len)
-{
- rss_xml_handler *h;
- h = (rss_xml_handler*) malloc(sizeof (rss_xml_handler));
- h->Flags = Flags;
- h->Handler = Handler;
- Put(StartHandlers, key, len, h, NULL);
-}
-void AddRSSEndHandler(rss_handler_func Handler, int Flags, const char *key, long len)
-{
- rss_xml_handler *h;
- h = (rss_xml_handler*) malloc(sizeof (rss_xml_handler));
- h->Flags = Flags;
- h->Handler = Handler;
- Put(EndHandlers, key, len, h, NULL);
-}
-
-///#if 0
-//#ifdef HAVE_ICONV
-#include <iconv.h>
-
-
-/*
- * dug this out of the trashcan of the midgard project, lets see if it works for us.
- * original code by Alexander Bokovoy <bokovoy@avilink.ne> distributed under GPL V2 or later
- */
-
-/* Returns:
- >= 0 - successfull, 0 means conversion doesn't use multibyte sequences
- -1 - error during iconv_open call
- -2 - error during iconv_close call
- ----------------------------------
- This function expects that multibyte encoding in 'charset' wouldn't have
- characters with more than 3 bytes. It is not intended to convert UTF-8 because
- we'll never receive UTF-8 in our handler (it is handled by Exat itself).
-*/
-static int
-fill_encoding_info (const char *charset, XML_Encoding * info)
-{
- iconv_t cd = (iconv_t)(-1);
- int flag;
- CtdlLogPrintf(0, "RSS: fill encoding info ...\n");
-
-#if G_BYTE_ORDER == G_LITTLE_ENDIAN
- cd = iconv_open ("UCS-2LE", charset);
-#else
- cd = iconv_open ("UCS-2BE", charset);
-#endif
-
- if (cd == (iconv_t) (-1))
- {
- return -1;
- }
-
- {
- unsigned short out;
- unsigned char buf[4];
- unsigned int i0, i1, i2;
- int result;
- flag = 0;
- for (i0 = 0; i0 < 0x100; i0++)
- {
- buf[0] = i0;
- info->map[i0] = 0;
- //result = try (cd, buf, 1, &out);
- if (result < 0)
- {
- }
- else if (result > 0)
- {
- info->map[i0] = out;
- }
- else
- {
- for (i1 = 0; i1 < 0x100; i1++)
- {
- buf[1] = i1;
- ///result = try (cd, buf, 2, &out);
- if (result < 0)
- {
- }
- else if (result > 0)
- {
- flag++;
- info->map[i0] = -2;
- }
- else
- {
- for (i2 = 0; i2 < 0x100; i2++)
- {
- buf[2] = i2;
- ////result = try (cd, buf, 3, &out);
- if (result < 0)
- {
- }
- else if (result > 0)
- {
- flag++;
- info->map[i0] = -3;
- }
- }
- }
- }
- }
- }
- }
-
- if (iconv_close (cd) < 0)
- {
- return -2;
- }
- return flag;
-}
-
-static int
-iconv_convertor (void *data, const char *s)
-{
- XML_Encoding *info = data;
- int res;
- CtdlLogPrintf(0, "RSS: Converting ...\n");
-
- if (s == NULL)
- return -1;
-/*
- GByteArray *result;
- result = g_byte_array_new ();
- if (process_block (info->data, (char *) s, strlen (s), result) == 0)
- {
- res = *(result->data);
- g_byte_array_free (result, TRUE);
- return res;
- }
- g_byte_array_free (result, TRUE);
-*/
- return -1;
-}
-
-static void
-my_release (void *data)
-{
- iconv_t cd = (iconv_t) data;
- if (iconv_close (cd) != 0)
- {
-/// TODO: uh no. exit (1);
- }
-}
-int
-handle_unknown_xml_encoding (void *encodingHandleData,
- const XML_Char * name,
- XML_Encoding * info)
-{
- int result;
- CtdlLogPrintf(0, "RSS: unknown encoding ...\n");
- result = fill_encoding_info (name, info);
- if (result >= 0)
- {
- /*
- Special case: client asked for reverse conversion, we'll provide him with
- iconv descriptor which handles it. Client should release it by himself.
- */
- if(encodingHandleData != NULL)
- *((iconv_t *)encodingHandleData) = iconv_open(name, "UTF-8");
- /*
- Optimization: we do not need conversion function if encoding is one-to-one,
- info->map table will be enough
- */
- if (result == 0)
- {
- info->data = NULL;
- info->convert = NULL;
- info->release = NULL;
- return 1;
- }
- /*
- We do need conversion function because this encoding uses multibyte sequences
- */
- info->data = (void *) iconv_open ("UTF-8", name);
- if ((int)info->data == -1)
- return -1;
- info->convert = iconv_convertor;
- info->release = my_release;
- return 1;
- }
- if(encodingHandleData != NULL)
- *(iconv_t *)encodingHandleData = NULL;
- return 0;
-}
-
-///#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
- */
-void rss_save_item(rss_item *ri)
-{
-
- struct MD5Context md5context;
- u_char rawdigest[MD5_DIGEST_LEN];
- int i;
- char utmsgid[SIZ];
- struct cdbdata *cdbut;
- struct UseTable ut;
- struct CtdlMessage *msg;
- struct recptypes *recp = NULL;
- int msglen = 0;
- StrBuf *Message;
-
- recp = (struct recptypes *) malloc(sizeof(struct recptypes));
- if (recp == NULL) return;
- memset(recp, 0, sizeof(struct recptypes));
- memset(&ut, 0, sizeof(struct UseTable));
- recp->recp_room = strdup(ri->roomlist);
- recp->num_room = num_tokens(ri->roomlist, '|');
- recp->recptypes_magic = RECPTYPES_MAGIC;
-
- /* 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);
- snprintf(utmsgid, sizeof utmsgid, "rss/%s", ChrPtr(ri->guid));
- }
- else {
- MD5Init(&md5context);
- if (ri->title != NULL) {
- MD5Update(&md5context, (const unsigned char*)ChrPtr(ri->title), StrLength(ri->title));
- }
- if (ri->link != NULL) {
- MD5Update(&md5context, (const unsigned char*)ChrPtr(ri->link), StrLength(ri->link));
- }
- MD5Final(rawdigest, &md5context);
- for (i=0; i<MD5_DIGEST_LEN; i++) {
- sprintf(&utmsgid[i*2], "%02X", (unsigned char) (rawdigest[i] & 0xff));
- utmsgid[i*2] = tolower(utmsgid[i*2]);
- utmsgid[(i*2)+1] = tolower(utmsgid[(i*2)+1]);
- }
- strcat(utmsgid, "_rss2ctdl");
- }
-
- /* Find out if we've already seen this item */
-
- cdbut = cdb_fetch(CDB_USETABLE, utmsgid, strlen(utmsgid));
-#ifndef DEBUG_RSS
- if (cdbut != NULL) {
- /* Item has already been seen */
- CtdlLogPrintf(CTDL_DEBUG, "%s has already been seen\n", utmsgid);
- cdb_free(cdbut);
-
- /* rewrite the record anyway, to update the timestamp */
- strcpy(ut.ut_msgid, utmsgid);
- ut.ut_timestamp = time(NULL);
- cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid), &ut, sizeof(struct UseTable) );
- }
- else
-#endif
-{
- /* Item has not been seen, so save it. */
- CtdlLogPrintf(CTDL_DEBUG, "RSS: saving item...\n");
- if (ri->description == NULL) ri->description = NewStrBufPlain(HKEY(""));
- StrBufSpaceToBlank(ri->description);
- msg = malloc(sizeof(struct CtdlMessage));
- memset(msg, 0, sizeof(struct CtdlMessage));
- msg->cm_magic = CTDLMESSAGE_MAGIC;
- msg->cm_anon_type = MES_NORMAL;
- msg->cm_format_type = FMT_RFC822;
-
- if (ri->guid != NULL) {
- msg->cm_fields['E'] = strdup(ChrPtr(ri->guid));
- }
-
- if (ri->author_or_creator != NULL) {
- char *From;
- StrBuf *Encoded, *QPEncoded;
- 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);
- 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);
- msg->cm_fields['A'] = SmashStrBuf(&QPEncoded);
-
- FreeStrBuf(&Encoded);
- FreeStrBuf(&UserName);
- FreeStrBuf(&EmailAddress);
- FreeStrBuf(&EncBuf);