rss.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <mrss.h>
00004 #include <libxml/xmlmemory.h>
00005 #include <libxml/parser.h>
00006 
00007 #include "lpbot.h"
00008 
00018 int parseRss(xmlDoc *doc, xmlNode *cur)
00019 {
00020         xmlChar *key;
00021         cur = cur->xmlChildrenNode;
00022         lp_rss *rss = g_new0(lp_rss, 1);
00023 
00024 
00025         while (cur)
00026         {
00027                 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00028                 if ((!xmlStrcmp(cur->name, (const xmlChar *)"name")))
00029                         rss->name = strdup((char*)key);
00030                 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"url")))
00031                         rss->url = strdup((char*)key);
00032                 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"server")))
00033                         rss->server = lp_find_server((char*)key);
00034                 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"channel")))
00035                         rss->channel = strdup((char*)key);
00036                 xmlFree(key);
00037                 cur = cur->next;
00038         }
00039         config->rsslist = g_list_append(config->rsslist, rss);
00040         return 0;
00041 }
00042 
00043 /*
00044  * Chesk an RSS feed for new entries.
00045  * @param rss the feed
00046  * @return -1 on error, 0 on success
00047  */
00048 int check_rss(lp_rss *rss)
00049 {
00050         mrss_t *data;
00051         mrss_error_t ret;
00052         mrss_item_t *item;
00053 
00054         if ((ret=mrss_parse_url(rss->url, &data)))
00055         {
00056                 fprintf (stderr, "MRSS return error: %s\n", mrss_strerror (ret));
00057                 return -1;
00058         }
00059 
00060         item = data->item;
00061         while (item)
00062         {
00063                 // don't print any item for the first time
00064                 if(rss->lastupd==0)
00065                         break;
00066                 if(get_date(item->pubDate, NULL)>rss->lastupd)
00067                 {
00068                         lp_send(rss->server, "privmsg %s :14%s7 %s3 %s\n",
00069                                         rss->channel, rss->name, item->title, item->link);
00070                 }
00071                 item = item->next;
00072         }
00073         rss->lastupd=get_date(data->item->pubDate, NULL);
00074         mrss_free (data);
00075         return 0;
00076 }
00077 
00081 void lp_rss_free(lp_rss *rss)
00082 {
00083         free(rss->name);
00084         free(rss->url);
00085         free(rss->channel);
00086 }
00087 /* @} */

Generated on Mon May 19 15:36:55 2008 for lpbot by  doxygen 1.5.4