2 * Claws-Mail-- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 Hiroyuki Yamamoto
4 * This file (C) 2005 Andrej Kacian <andrej@kacian.sk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #include <glib/gi18n.h>
33 /* Claws Mail includes */
36 #include <common/utils.h>
39 #include "libfeed/date.h"
40 #include "libfeed/feeditem.h"
43 #include "rssyl_deleted.h"
44 #include "rssyl_feed.h"
45 #include "rssyl_parse_feed.h"
48 /* rssyl_cb_feed_compare()
50 * GCompareFunc function called by glib2's g_slist_find_custom().
53 static gint rssyl_cb_feed_compare(const FeedItem *a, const FeedItem *b)
55 gboolean date_eq = FALSE, url_eq = FALSE, title_eq = FALSE;
56 gboolean pubdate_eq = FALSE, moddate_eq = FALSE;
57 gboolean no_url = FALSE, no_date = FALSE, no_title = FALSE;
58 gboolean no_pubdate = FALSE, no_moddate = FALSE;
59 gchar *atit = NULL, *btit = NULL;
61 g_return_val_if_fail(a != NULL && b != NULL, 1);
63 /* ID should be unique. If it matches, we've found what we came for. */
64 if( (a->id != NULL) && (b->id != NULL) ) {
65 if( !strcmp(a->id, b->id) ) {
69 /* If both IDs are present, but they do not match, these are not the
70 * droids we're looking for. */
74 /* Ok, we have no ID to aid us. Let's have a look at item timestamps
75 * and item title & url. */
76 if( (a->url != NULL) && (b->url != NULL) ) {
77 if( !strcmp(a->url, b->url) )
82 /* Now we prepare some boolean flags to help us express comparing choices
86 if( (a->title != NULL) && (b->title != NULL) ) {
87 atit = conv_unmime_header(a->title, CS_UTF_8, FALSE);
88 btit = conv_unmime_header(b->title, CS_UTF_8, FALSE);
89 if( !strcmp(atit, btit) )
97 if (b->date_published <= 0) {
100 if (a->date_published == b->date_published)
105 if (b->date_modified <= 0) {
108 if (a->date_modified == b->date_modified)
112 if (no_pubdate && no_moddate)
115 if (pubdate_eq || (no_pubdate && moddate_eq))
118 /* If timestamp and url match, it is reasonable to assume
119 * we found our item. */
120 if (url_eq && date_eq)
123 /* Likewise if timestamp and title match. */
124 if (title_eq && date_eq)
127 /* There is no timestamp and the url matches (or there is none),
128 * we need to compare titles, ... */
129 if( (no_url || url_eq) && no_date ) {
136 /* ... and as a last resort, if there is no title, item texts. */
137 if( no_title && a->text && b->text ) {
138 if( !strcmp(a->text, b->text) )
144 /* We don't know this item. */
150 ITEM_CHANGED_TEXTONLY,
154 static gint rssyl_feed_item_changed(FeedItem *new_item, FeedItem *old_item )
156 debug_print("RSSyl: comparing '%s' and '%s'\n",
157 new_item->title, old_item->title);
159 /* if both have title ... */
160 if( old_item->title && new_item->title ) {
161 gchar *old = conv_unmime_header(old_item->title, CS_UTF_8, FALSE);
162 gchar *new = conv_unmime_header(new_item->title, CS_UTF_8, FALSE);
163 if( strcmp(old, new) != 0 ) { /* ... compare "unmimed" titles */
164 debug_print("RSSyl:\t\titem titles differ:\nOLD: '%s'\nNEW: '%s'\n",
173 /* if atleast one has a title, they differ */
174 if( old_item->title || new_item->title ) {
175 debug_print("RSSyl:\t\t+/- title\n");
180 if( old_item->author && new_item->author ) {
181 gchar *old = conv_unmime_header(old_item->author, CS_UTF_8, TRUE);
182 gchar *new = conv_unmime_header(new_item->author, CS_UTF_8, TRUE);
183 if( strcmp(old, new) ) { /* ... compare "unmimed" authors */
186 debug_print("RSSyl:\t\titem authors differ\n");
192 /* if atleast one has author, they differ */
193 if( old_item->author || new_item->author ) {
194 debug_print("RSSyl:\t\t+/- author\n");
199 /* if both have text ... */
200 if( old_item->text && new_item->text ) {
201 if( strcmp(old_item->text, new_item->text) ) { /* ... compare them */
202 debug_print("RSSyl:\t\titem texts differ\n");
203 debug_print("\nOLD: '%s'\n", old_item->text);
204 debug_print("\nNEW: '%s'\n", new_item->text);
206 return ITEM_CHANGED_TEXTONLY;
209 /* if at least one has some text, they differ */
210 if( old_item->text || new_item->text ) {
211 debug_print("RSSyl:\t\t+/- text\n");
212 return ITEM_CHANGED_TEXTONLY;
216 /* they don't seem to differ */
217 return ITEM_UNCHANGED;
224 EXISTS_CHANGED_TEXTONLY
227 /* rssyl_feed_item_exists()
229 * Returns 1 if a feed item already exists locally, 2 if there's a changed
230 * item with link that already belongs to existing item, 3 if only item's
231 * text has changed, 0 if item is new.
234 static guint rssyl_feed_item_exists(RFolderItem *ritem, FeedItem *fitem,
238 FeedItem *efitem = NULL;
241 g_return_val_if_fail(ritem != NULL, FALSE);
242 g_return_val_if_fail(fitem != NULL, FALSE);
244 if( ritem->items == NULL || g_slist_length(ritem->items) == 0 )
247 if( (item = g_slist_find_custom(ritem->items,
248 (gconstpointer)fitem, (GCompareFunc)rssyl_cb_feed_compare)) ) {
249 efitem = (FeedItem *)item->data;
250 if( (changed = rssyl_feed_item_changed(fitem, efitem)) > ITEM_UNCHANGED ) {
252 if (changed == ITEM_CHANGED_TEXTONLY)
253 return EXISTS_CHANGED_TEXTONLY;
255 return EXISTS_CHANGED;
258 return EXISTS_UNCHANGED;
264 /* =============================================================== */
266 void rssyl_add_item(RFolderItem *ritem, FeedItem *feed_item)
268 FeedItem *old_item = NULL;
270 MsgPermFlags oldperm_flags = 0;
275 gchar *meta_charset = NULL;
276 gchar *baseurl = NULL;
277 gchar *template = NULL;
278 gchar *tmp = NULL, *tmpurl = NULL, *tmpid = NULL;
279 gchar *dirname = NULL;
281 gchar *heading = NULL;
282 gchar *pathbasename = NULL;
284 FeedItemEnclosure *enc = NULL;
287 g_return_if_fail(ritem != NULL);
289 /* If item title is empty, try to fill it from source title (Atom only). */
290 tmp = feed_item_get_sourcetitle(feed_item);
291 if( feed_item_get_title(feed_item) == NULL ||
292 strlen(feed_item->title) == 0 ) {
293 if( tmp != NULL && strlen(tmp) > 0 )
294 feed_item_set_title(feed_item, tmp);
296 feed_item_set_title(feed_item, C_("Empty RSS feed title placeholder", "(empty)"));
300 if (feed_item_get_id(feed_item) == NULL) {
301 debug_print("RSSyl: item ID empty, using its URL as ID.\n");
302 feed_item_set_id(feed_item, feed_item_get_url(feed_item));
306 /* If neither item date is set, use date from source (Atom only). */
307 if( feed_item_get_date_modified(feed_item) == -1 &&
308 feed_item_get_date_published(feed_item) == -1 )
309 feed_item_set_date_published(feed_item,
310 feed_item_get_sourcedate(feed_item));
312 /* Fix up subject, url and ID (rssyl_format_string()) so that
313 * comparing doesn't break. */
314 debug_print("RSSyl: fixing up subject '%s'\n", feed_item_get_title(feed_item));
315 tmp = rssyl_format_string(feed_item_get_title(feed_item), TRUE, TRUE);
316 feed_item_set_title(feed_item, tmp);
318 debug_print("RSSyl: fixing up URL\n");
319 tmp = rssyl_format_string(feed_item_get_url(feed_item), FALSE, TRUE);
320 feed_item_set_url(feed_item, tmp);
322 if( feed_item_get_id(feed_item) != NULL ) {
323 debug_print("RSSyl: fixing up ID\n");
324 tmp = rssyl_format_string(feed_item_get_id(feed_item), FALSE, TRUE);
325 feed_item_set_id(feed_item, tmp);
329 /* If there's a summary, but no text, use summary as text. */
330 if( feed_item_get_text(feed_item) == NULL &&
331 (tmp = feed_item_get_summary(feed_item)) != NULL ) {
332 feed_item_set_text(feed_item, tmp);
333 g_free(feed_item->summary); /* We do not need summary in rssyl now. */
334 feed_item->summary = NULL;
337 /* Do not add if the item already exists, update if it does exist, but
339 dif = rssyl_feed_item_exists(ritem, feed_item, &old_item);
340 debug_print("RSSyl: rssyl_feed_item_exists returned %d\n", dif);
342 if( dif == EXISTS_UNCHANGED ) {
343 debug_print("RSSyl: This item already exists, skipping...\n");
347 /* Item is already in the list, but has changed */
348 if( dif >= EXISTS_CHANGED && old_item != NULL ) {
349 debug_print("RSSyl: Item changed, removing old one and adding new.\n");
351 /* Store permflags of the old item. */
352 ctx = (RFeedCtx *)old_item->data;
353 pathbasename = g_path_get_basename(ctx->path);
354 msginfo = folder_item_get_msginfo((FolderItem *)ritem,
356 g_free(pathbasename);
357 oldperm_flags = msginfo->flags.perm_flags;
359 ritem->items = g_slist_remove(ritem->items, old_item);
360 if (g_unlink(ctx->path) != 0) {
361 debug_print("RSSyl: Error, could not delete file '%s': %s\n",
362 ctx->path, g_strerror(errno));
366 feed_item_free(old_item);
370 /* Check against list of deleted items. */
371 if (rssyl_deleted_check(ritem->deleted_items, feed_item)) {
372 debug_print("RSSyl: Item '%s' found among deleted items, NOT adding it.\n",
373 feed_item_get_title(feed_item));
377 /* Add a new item, formatting its title along the way */
378 debug_print("RSSyl: Adding item '%s'\n", feed_item_get_title(feed_item));
379 ritem->items = g_slist_prepend(ritem->items, feed_item_copy(feed_item));
381 dirname = folder_item_get_path(&ritem->item);
382 template = g_strconcat(dirname, G_DIR_SEPARATOR_S,
383 RSSYL_TMP_TEMPLATE, NULL);
384 if ((fd = g_mkstemp(template)) < 0) {
385 g_warning("Couldn't g_mkstemp('%s'), not adding message!\n", template);
393 g_warning("Couldn't open file '%s', not adding message!\n", template);
400 if( (tmp = feed_item_get_author(feed_item)) != NULL ) {
401 if( g_utf8_validate(tmp, -1, NULL)) {
402 conv_encode_header_full(hdr, 1023, tmp, strlen("From: "),
404 fprintf(f, "From: %s\n", hdr);
406 fprintf(f, "From: %s\n", tmp);
410 if( (tmpd = feed_item_get_date_modified(feed_item)) != -1 ) {
411 tmp = createRFC822Date(&tmpd);
412 debug_print("RSSyl: using date_modified: '%s'\n", tmp);
413 } else if( (tmpd = feed_item_get_date_published(feed_item)) != -1 ) {
414 tmp = createRFC822Date(&tmpd);
415 debug_print("RSSyl: using date_published: '%s'\n", tmp);
418 tmp = createRFC822Date(&tmpd);
422 fprintf(f, "Date: %s\n", tmp);
426 if( (tmp = feed_item_get_title(feed_item)) != NULL ) {
428 /* (Atom only) Strip HTML markup from title for the Subject line. */
429 if( feed_item_get_title_format(feed_item) == FEED_ITEM_TITLE_HTML
430 || feed_item_get_title_format(feed_item) == FEED_ITEM_TITLE_XHTML) {
431 debug_print("RSSyl: item title is HTML/XHTML, stripping tags for Subject line\n");
436 if( g_utf8_validate(tmp, -1, NULL) ) {
437 conv_encode_header_full(hdr, 1023, tmp, strlen("Subject: "),
439 debug_print("RSSyl: Subject: %s\n", hdr);
440 fprintf(f, "Subject: %s\n", hdr);
442 fprintf(f, "Subject: %s\n", tmp);
444 if( feed_item_get_title_format(feed_item) == FEED_ITEM_TITLE_HTML
445 || feed_item_get_title_format(feed_item) == FEED_ITEM_TITLE_XHTML) {
447 fprintf(f, "X-RSSyl-OrigTitle: %s\n", feed_item_get_title(feed_item));
450 debug_print("RSSyl: No feed title, it seems\n");
451 fprintf(f, "Subject: (empty)\n");
455 if( (tmpurl = feed_item_get_url(feed_item)) == NULL ) {
456 if( feed_item_get_id(feed_item) != NULL &&
457 feed_item_id_is_permalink(feed_item) ) {
458 tmpurl = feed_item_get_id(feed_item);
463 fprintf(f, "X-RSSyl-URL: %s\n", tmpurl);
465 if( ritem->last_update > 0) {
466 fprintf(f, "X-RSSyl-Last-Seen: %ld\n", ritem->last_update);
470 if( (tmpid = feed_item_get_id(feed_item)) == NULL )
471 tmpid = feed_item_get_url(feed_item);
473 fprintf(f, "Message-ID: <%s>\n", tmpid);
475 /* X-RSSyl-Comments */
476 if( (text = feed_item_get_comments_url(feed_item)) != NULL )
477 fprintf(f, "X-RSSyl-Comments: %s\n", text);
480 if( (text = feed_item_get_parent_id(feed_item)) != NULL )
481 fprintf(f, "References: <%s>\n", text);
484 text = feed_item_get_text(feed_item);
485 if( text && g_utf8_validate(text, -1, NULL) ) {
486 fprintf(f, "Content-Type: text/html; charset=UTF-8\n\n");
487 meta_charset = g_strdup("<meta http-equiv=\"Content-Type\" "
488 "content=\"text/html; charset=UTF-8\">");
490 fprintf(f, "Content-Type: text/html\n\n");
493 /* construct base href */
494 if( feed_item_get_url(feed_item) != NULL )
495 baseurl = g_strdup_printf("<base href=\"%s\">\n",
496 feed_item_get_url(feed_item) );
498 if( ritem->write_heading )
499 heading = g_strdup_printf("<h2>%s</h2>\n<br><br>\n",
500 feed_item_get_title(feed_item));
503 fprintf(f, "<html><head>"
508 "URL: <a href=\"%s\">%s</a>\n\n<br><br>\n"
511 RSSYL_TEXT_END"\n\n",
512 (meta_charset ? meta_charset : ""),
513 (baseurl ? baseurl : ""),
514 (heading ? heading : ""),
515 (tmpurl ? tmpurl : ""),
516 (tmpurl ? tmpurl : "n/a"),
517 (text ? text : ""), (text ? "\n" : "") );
519 g_free(meta_charset);
523 if( (enc = feed_item_get_enclosure(feed_item)) != NULL )
524 fprintf(f, "<p><a href=\"%s\">Attached media file</a> [%s] (%ld bytes)</p>\n",
525 feed_item_enclosure_get_url(enc),
526 feed_item_enclosure_get_type(enc),
527 feed_item_enclosure_get_size(enc) );
529 fprintf(f, "</body></html>\n");
532 g_return_if_fail(template != NULL);
534 flags = g_new(MsgFlags, 1);
535 flags->perm_flags = MSG_NEW | MSG_UNREAD;
536 flags->tmp_flags = 0;
538 d = folder_item_add_msg(&ritem->item, template, flags, TRUE);
542 ctx = g_new0(RFeedCtx, 1);
543 ctx->path = (gpointer)g_strdup_printf("%s%c%d", dirname,
545 ctx->last_seen = ritem->last_update;
546 ((FeedItem *)ritem->items->data)->data = (gpointer)ctx;
550 /* Unset unread+new if the changed item wasn't set unread and user
551 * doesn't want to see it unread because of the change. */
552 if (dif != EXISTS_NEW) {
553 if (!(oldperm_flags & MSG_UNREAD) && (ritem->silent_update == 2
554 || (ritem->silent_update == 1 && dif == EXISTS_CHANGED_TEXTONLY)))
555 procmsg_msginfo_unset_flags(
556 folder_item_get_msginfo((FolderItem *)ritem, d), MSG_NEW | MSG_UNREAD, 0);
559 debug_print("RSSyl: folder_item_add_msg(): %d\n", d);