2 * $Id: serv_rssclient.c 5652 2007-10-29 20:14:48Z ajc $
4 * Bring external RSS feeds into rooms.
12 #if TIME_WITH_SYS_TIME
13 # include <sys/time.h>
17 # include <sys/time.h>
26 #include <sys/types.h>
30 #include "citserver.h"
35 #include "ctdl_module.h"
36 #include "clientsocket.h"
39 #include "citadel_dirs.h"
47 struct rssnetcfg *next;
64 struct rssnetcfg *rnclist = NULL;
68 * Commit a fetched and parsed RSS item to disk
70 void rss_save_item(struct rss_item *ri) {
72 struct MD5Context md5context;
73 u_char rawdigest[MD5_DIGEST_LEN];
76 struct cdbdata *cdbut;
78 struct CtdlMessage *msg;
79 struct recptypes *recp = NULL;
82 recp = (struct recptypes *) malloc(sizeof(struct recptypes));
83 if (recp == NULL) return;
84 memset(recp, 0, sizeof(struct recptypes));
85 recp->recp_room = strdup(ri->roomlist);
86 recp->num_room = num_tokens(ri->roomlist, '|');
87 recp->recptypes_magic = RECPTYPES_MAGIC;
89 /* Construct a GUID to use in the S_USETABLE table.
90 * If one is not present in the item itself, make one up.
92 if (ri->guid != NULL) {
93 snprintf(utmsgid, sizeof utmsgid, "rss/%s", ri->guid);
97 if (ri->title != NULL) {
98 MD5Update(&md5context, ri->title, strlen(ri->title));
100 if (ri->link != NULL) {
101 MD5Update(&md5context, ri->link, strlen(ri->link));
103 MD5Final(rawdigest, &md5context);
104 for (i=0; i<MD5_DIGEST_LEN; i++) {
105 sprintf(&utmsgid[i*2], "%02X", (unsigned char) (rawdigest[i] & 0xff));
106 utmsgid[i*2] = tolower(utmsgid[i*2]);
107 utmsgid[(i*2)+1] = tolower(utmsgid[(i*2)+1]);
109 strcat(utmsgid, "_rss2ctdl");
112 /* Find out if we've already seen this item */
113 cdbut = cdb_fetch(CDB_USETABLE, utmsgid, strlen(utmsgid));
115 /* Item has already been seen */
116 lprintf(CTDL_DEBUG, "%s has already been seen\n", utmsgid);
119 /* rewrite the record anyway, to update the timestamp */
120 strcpy(ut.ut_msgid, utmsgid);
121 ut.ut_timestamp = time(NULL);
122 cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid), &ut, sizeof(struct UseTable) );
125 /* Item has not been seen, so save it. */
127 for (i=strlen(ri->description); i>=0; --i) {
128 if (isspace(ri->description[i])) {
129 ri->description[i] = ' ';
133 msg = malloc(sizeof(struct CtdlMessage));
134 memset(msg, 0, sizeof(struct CtdlMessage));
135 msg->cm_magic = CTDLMESSAGE_MAGIC;
136 msg->cm_anon_type = MES_NORMAL;
137 msg->cm_format_type = FMT_RFC822;
138 msg->cm_fields['A'] = strdup("rss");
139 msg->cm_fields['N'] = strdup(NODENAME);
140 msg->cm_fields['U'] = strdup(ri->title);
141 msg->cm_fields['T'] = malloc(64);
142 snprintf(msg->cm_fields['T'], 64, "%ld", ri->pubdate);
144 msglen = 1024 + strlen(ri->link) + strlen(ri->description) ;
145 msg->cm_fields['M'] = malloc(msglen);
146 snprintf(msg->cm_fields['M'], msglen,
147 "Content-type: text/html\r\n\r\n"
150 "<a href=\"%s\">%s</a>\n"
157 CtdlSubmitMsg(msg, recp, NULL);
158 CtdlFreeMessage(msg);
159 free_recipients(recp);
161 /* write the uidl to the use table so we don't store this item again */
162 strcpy(ut.ut_msgid, utmsgid);
163 ut.ut_timestamp = time(NULL);
164 cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid), &ut, sizeof(struct UseTable) );
171 * Convert an RDF/RSS datestamp into a time_t
173 time_t rdf_parsedate(char *p)
178 if (strlen(p) < 10) return 0L;
180 memset(&tm, 0, sizeof tm);
182 /* YYYY-MM-DDTHH:MM format...
184 if ( (p[4] == '-') && (p[7] == '-') ) {
185 tm.tm_year = atoi(&p[0]) - 1900;
186 tm.tm_mon = atoi(&p[5]) - 1;
187 tm.tm_mday = atoi(&p[8]);
188 if ( (p[10] == 'T') && (p[13] == ':') ) {
189 tm.tm_hour = atoi(&p[11]);
190 tm.tm_min = atoi(&p[14]);
195 /* FIXME try an imap timestamp conversion */
203 void rss_xml_start(void *data, const char *el, const char **attr) {
204 struct rss_item *ri = (struct rss_item *) data;
206 if (!strcasecmp(el, "item")) {
208 /* Initialize the feed item data structure */
209 if (ri->guid != NULL) free(ri->guid);
211 if (ri->title != NULL) free(ri->title);
213 if (ri->link != NULL) free(ri->link);
215 if (ri->description != NULL) free(ri->description);
216 ri->description = NULL;
218 /* Throw away any existing character data */
219 if (ri->chardata_len > 0) {
222 ri->chardata_len = 0;
230 void rss_xml_end(void *data, const char *supplied_el) {
231 struct rss_item *ri = (struct rss_item *) data;
236 /* Axe the namespace, we don't care about it */
238 safestrncpy(el, supplied_el, sizeof el);
239 while (sep = strchr(el, ':'), sep) {
243 if (!strcasecmp(el, "guid")) {
244 if (ri->guid != NULL) free(ri->guid);
245 striplt(ri->chardata);
246 ri->guid = strdup(ri->chardata);
249 if (!strcasecmp(el, "title")) {
250 if (ri->title != NULL) free(ri->title);
251 striplt(ri->chardata);
252 ri->title = strdup(ri->chardata);
255 if (!strcasecmp(el, "link")) {
256 if (ri->link != NULL) free(ri->link);
257 striplt(ri->chardata);
258 ri->link = strdup(ri->chardata);
261 if (!strcasecmp(el, "description")) {
262 if (ri->description != NULL) free(ri->description);
263 ri->description = strdup(ri->chardata);
266 if ( (!strcasecmp(el, "pubdate")) || (!strcasecmp(el, "date")) ) {
267 striplt(ri->chardata);
268 ri->pubdate = rdf_parsedate(ri->chardata);
271 if (!strcasecmp(el, "item")) {
275 if ( (!strcasecmp(el, "rss")) || (!strcasecmp(el, "rdf")) ) {
276 lprintf(CTDL_DEBUG, "KILL THE PARSER, KILL THE PARSER, KILL THE PARSER!\n");
277 ri->done_parsing = 1;
280 if (ri->chardata_len > 0) {
283 ri->chardata_len = 0;
290 * This callback stores up the data which appears in between tags.
292 void rss_xml_chardata(void *data, const XML_Char *s, int len) {
293 struct rss_item *ri = (struct rss_item *) data;
298 old_len = ri->chardata_len;
299 new_len = old_len + len;
300 new_buffer = realloc(ri->chardata, new_len + 1);
301 if (new_buffer != NULL) {
302 memcpy(&new_buffer[old_len], s, len);
303 new_buffer[new_len] = 0;
304 ri->chardata = new_buffer;
305 ri->chardata_len = new_len;
312 * Parses an url into hostname, port number and resource identifier.
314 int parse_url(char *url, char *hostname, int *port, char *identifier)
316 char protocol[1024], scratch[1024];
320 strcpy(scratch, url);
321 ptr = (char *)strchr(scratch, ':');
323 return(1); /* no protocol specified */
326 strcpy(protocol, scratch);
327 if (strcmp(protocol, "http")) {
328 return(2); /* not HTTP */
331 strcpy(scratch, url);
332 ptr = (char *) strstr(scratch, "//");
334 return(3); /* no server specified */
338 strcpy(hostname, ptr);
339 nptr = (char *)strchr(ptr, ':');
341 *port = 80; /* default */
342 nptr = (char *)strchr(hostname, '/');
345 sscanf(nptr, ":%d", port);
346 nptr = (char *)strchr(hostname, ':');
353 nptr = (char *)strchr(ptr, '/');
356 return(4); /* no url specified */
359 strcpy(identifier, nptr);
371 void rss_do_fetching(char *url, char *rooms) {
379 int got_bytes = (-1);
382 xp = XML_ParserCreateNS("UTF-8", ':');
384 lprintf(CTDL_ALERT, "Cannot create XML parser!\n");
388 memset(&ri, 0, sizeof(struct rss_item));
390 XML_SetElementHandler(xp, rss_xml_start, rss_xml_end);
391 XML_SetCharacterDataHandler(xp, rss_xml_chardata);
392 XML_SetUserData(xp, &ri);
397 if (parse_url(url, rsshost, &rssport, rssurl) != 0) {
398 lprintf(CTDL_ALERT, "Invalid URL: %s\n", url);
401 lprintf(CTDL_NOTICE, "Connecting to <%s>\n", rsshost);
402 sprintf(buf, "%d", rssport);
403 sock = sock_connect(rsshost, buf, "tcp");
407 lprintf(CTDL_DEBUG, "Connected!\n");
409 snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r", rssurl);
410 lprintf(CTDL_DEBUG, "<%s\n", buf);
411 sock_puts(sock, buf);
413 snprintf(buf, sizeof buf, "Server: %s\r", rsshost);
414 lprintf(CTDL_DEBUG, "<%s\n", buf);
415 sock_puts(sock, buf);
417 sock_puts(sock, "\r");
419 if (sock_getln(sock, buf, sizeof buf) >= 0) {
420 lprintf(CTDL_DEBUG, ">%s\n", buf);
421 remove_token(buf, 0, ' ');
424 while (got_bytes = sock_getln(sock, buf, sizeof buf),
425 (got_bytes >= 0 && (strcmp(buf, "")) && (strcmp(buf, "\r"))) ) {
426 /* FIXME handle 302 redirects!! */
430 while (got_bytes = sock_read(sock, buf, sizeof buf, 0),
431 ((got_bytes>=0) && (ri.done_parsing == 0)) ) {
432 XML_Parse(xp, buf, got_bytes, 0);
434 if (ri.done_parsing == 0) XML_Parse(xp, "", 0, 1);
440 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
445 /* Free the feed item data structure */
446 if (ri.guid != NULL) free(ri.guid);
448 if (ri.title != NULL) free(ri.title);
450 if (ri.link != NULL) free(ri.link);
452 if (ri.description != NULL) free(ri.description);
453 ri.description = NULL;
454 if (ri.chardata_len > 0) {
463 * Scan a room's netconfig to determine whether it is requesting any RSS feeds
465 void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
467 char filename[PATH_MAX];
472 struct rssnetcfg *rncptr = NULL;
473 struct rssnetcfg *use_this_rncptr = NULL;
477 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
479 /* Only do net processing for rooms that have netconfigs */
480 fp = fopen(filename, "r");
485 while (fgets(buf, sizeof buf, fp) != NULL) {
486 buf[strlen(buf)-1] = 0;
488 extract_token(instr, buf, 0, '|', sizeof instr);
489 if (!strcasecmp(instr, "rssclient")) {
490 extract_token(feedurl, buf, 1, '|', sizeof feedurl);
492 /* If any other rooms have requested the same feed, then we will just add this
493 * room to the target list for that client request.
495 for (rncptr=rnclist; rncptr!=NULL; rncptr=rncptr->next) {
496 if (!strcmp(rncptr->url, feedurl)) {
497 use_this_rncptr = rncptr;
501 /* Otherwise create a new client request */
502 if (use_this_rncptr == NULL) {
503 rncptr = (struct rssnetcfg *) malloc(sizeof(struct rssnetcfg));
504 if (rncptr != NULL) {
505 rncptr->next = rnclist;
506 safestrncpy(rncptr->url, feedurl, sizeof rncptr->url);
507 rncptr->rooms = NULL;
509 use_this_rncptr = rncptr;
513 /* Add the room name to the request */
514 if (use_this_rncptr != NULL) {
515 if (use_this_rncptr->rooms == NULL) {
516 rncptr->rooms = strdup(qrbuf->QRname);
519 len = strlen(use_this_rncptr->rooms) + strlen(qrbuf->QRname) + 5;
520 ptr = realloc(use_this_rncptr->rooms, len);
523 strcat(ptr, qrbuf->QRname);
524 use_this_rncptr->rooms = ptr;
537 * Scan for rooms that have RSS client requests configured
539 void rssclient_scan(void) {
540 static time_t last_run = 0L;
541 static int doing_rssclient = 0;
542 struct rssnetcfg *rptr = NULL;
545 * Run RSS client no more frequently than once every n seconds
547 if ( (time(NULL) - last_run) < config.c_net_freq ) {
552 * This is a simple concurrency check to make sure only one rssclient run
553 * is done at a time. We could do this with a mutex, but since we
554 * don't really require extremely fine granularity here, we'll do it
555 * with a static variable instead.
557 if (doing_rssclient) return;
560 lprintf(CTDL_DEBUG, "rssclient started\n");
561 ForEachRoom(rssclient_scan_room, NULL);
563 while (rnclist != NULL) {
564 rss_do_fetching(rnclist->url, rnclist->rooms);
566 rnclist = rnclist->next;
567 if (rptr->rooms != NULL) free(rptr->rooms);
571 lprintf(CTDL_DEBUG, "rssclient ended\n");
572 last_run = time(NULL);
577 #endif /* HAVE_EXPAT */
579 CTDL_MODULE_INIT(rssclient)
582 CtdlRegisterSessionHook(rssclient_scan, EVT_TIMER);
584 lprintf(CTDL_INFO, "This server is missing the Expat XML parser. RSS client will be disabled.\n");
586 /* return our Subversion id for the Log */
587 return "$Id: serv_rssclient.c 5652 2007-10-29 20:14:48Z ajc $";