RSSyl: Use g_mkstemp() instead of mkstemp()
[claws.git] / src / plugins / rssyl / rssyl.c
1 /*
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>
5  *
6  * - s-c folderclass callback handler functions
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 /* Global includes */
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <curl/curl.h>
31
32 /* Claws Mail includes */
33 #include <folder.h>
34 #include <procmsg.h>
35 #include <localfolder.h>
36 #include <common/utils.h>
37 #include <main.h>
38 #include <mh.h>
39 #include <xml.h>
40 #include <toolbar.h>
41 #include <prefs_toolbar.h>
42 #include <utils.h>
43
44 /* Local includes */
45 #include "libfeed/feeditem.h"
46 #include "rssyl.h"
47 #include "rssyl_deleted.h"
48 #include "rssyl_gtk.h"
49 #include "rssyl_feed.h"
50 #include "rssyl_prefs.h"
51 #include "rssyl_update_feed.h"
52 #include "rssyl_update_format.h"
53 #include "opml_import.h"
54 #include "opml_export.h"
55 #include "strutils.h"
56
57 FolderClass rssyl_class;
58
59 static gint rssyl_create_tree(Folder *folder);
60 static gint rssyl_scan_tree(Folder *folder);
61
62 static gboolean existing_tree_found = FALSE;
63
64 static void rssyl_init_read_func(FolderItem *item, gpointer data)
65 {
66         RFolderItem *ritem = (RFolderItem *)item;
67         RPrefs *rsprefs = NULL;
68
69         if( !IS_RSSYL_FOLDER_ITEM(item) )
70                 return;
71
72         existing_tree_found = TRUE;
73
74         /* Don't do anything if we're on root of our folder tree or on
75          * a regular folder (no feed) */
76         if( folder_item_parent(item) == NULL || ritem->url == NULL )
77                 return;
78
79         ritem->refresh_id = 0;
80
81         /* Start automatic refresh timer, if necessary */
82         if( ritem->default_refresh_interval ) {
83                 rsprefs = rssyl_prefs_get();
84                 if( !rsprefs->refresh_enabled )
85                         return;
86
87                 ritem->refresh_interval = rsprefs->refresh;
88         }
89
90         /* Start the timer, if determined interval is >0 */
91         if( ritem->refresh_interval > 0 )
92                 rssyl_feed_start_refresh_timeout(ritem);
93 }
94
95 static void rssyl_make_rc_dir(void)
96 {
97         gchar *rssyl_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR,
98                         NULL);
99
100         if( !is_dir_exist(rssyl_dir) ) {
101                 if( make_dir(rssyl_dir) < 0 ) {
102                         g_warning("couldn't create directory %s\n", rssyl_dir);
103                 }
104
105                 debug_print("RSSyl: created directory %s\n", rssyl_dir);
106         }
107
108         g_free(rssyl_dir);
109 }
110
111 static void rssyl_create_default_mailbox(void)
112 {
113         Folder *root = NULL;
114
115         rssyl_make_rc_dir();
116
117         root = folder_new(rssyl_folder_get_class(), RSSYL_DEFAULT_MAILBOX, NULL);
118
119         g_return_if_fail(root != NULL);
120         folder_add(root);
121
122         rssyl_scan_tree(root);
123
124         /* FIXME: subscribe default feed */
125 //      rssyl_subscribe_new_feed(item, RSSYL_DEFAULT_FEED, TRUE);
126 }
127
128 static gboolean rssyl_update_all_feeds_deferred(gpointer data)
129 {
130         rssyl_update_all_feeds();
131         return FALSE;
132 }
133
134 static void rssyl_toolbar_cb_refresh_all_feeds(gpointer parent, const gchar *item_name, gpointer data)
135 {
136         rssyl_update_all_feeds();
137 }
138
139 void rssyl_init(void)
140 {
141         folder_register_class(rssyl_folder_get_class());
142
143         rssyl_gtk_init();
144         rssyl_make_rc_dir();
145
146         rssyl_prefs_init();
147
148         folder_func_to_all_folders((FolderItemFunc)rssyl_init_read_func, NULL);
149
150         if( !existing_tree_found )
151                 rssyl_create_default_mailbox();
152         else
153                 rssyl_update_format();
154
155         prefs_toolbar_register_plugin_item(TOOLBAR_MAIN, "RSSyl", _("Refresh all feeds"), rssyl_toolbar_cb_refresh_all_feeds, NULL);
156
157         if( rssyl_prefs_get()->refresh_on_startup &&
158                         claws_is_starting() )
159                 g_timeout_add(2000, rssyl_update_all_feeds_deferred, NULL);
160 }
161
162 void rssyl_done(void)
163 {
164         rssyl_opml_export();
165
166         prefs_toolbar_unregister_plugin_item(TOOLBAR_MAIN, "RSSyl", _("Refresh all feeds"));
167
168         rssyl_prefs_done();
169         rssyl_gtk_done();
170
171         if( !claws_is_exiting() )
172                 folder_unregister_class(rssyl_folder_get_class());
173
174         debug_print("RSSyl is done\n");
175 }
176
177 static gchar *rssyl_get_new_msg_filename(FolderItem *dest)
178 {
179         gchar *destfile;
180         gchar *destpath;
181
182         destpath = folder_item_get_path(dest);
183         g_return_val_if_fail(destpath != NULL, NULL);
184
185         if( !is_dir_exist(destpath) )
186                 make_dir_hier(destpath);
187
188         for( ; ; ) {
189                 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
190                                 dest->last_num + 1);
191                 if( is_file_entry_exist(destfile) ) {
192                         dest->last_num++;
193                         g_free(destfile);
194                 } else
195                         break;
196         }
197
198         g_free(destpath);
199
200         return destfile;
201 }
202
203 static void rssyl_get_last_num(Folder *folder, FolderItem *item)
204 {
205         gchar *path;
206         const char *f;
207         GDir *dp;
208         GError *error;
209         gint max = 0;
210         gint num;
211
212         g_return_if_fail(item != NULL);
213
214         debug_print("rssyl_get_last_num(): Scanning %s ...\n", item->path);
215         path = folder_item_get_path(item);
216         g_return_if_fail(path != NULL);
217
218         if( (dp = g_dir_open(path, 0, &error)) == NULL ) {
219                 FILE_OP_ERROR(item->path, "g_dir_open");
220                 debug_print("g_dir_open() failed on \"%s\", error %d (%s).\n",
221                                 path, error->code, error->message);
222                 g_free(path);
223                 return;
224         }
225
226         g_free(path);
227
228         while( (f = g_dir_read_name(dp)) != NULL) {
229                 if ((num = to_number(f)) > 0 &&
230                                 g_file_test(f, G_FILE_TEST_IS_REGULAR)) {
231                         if( max < num )
232                                 max = num;
233                 }
234         }
235         g_dir_close(dp);
236
237         debug_print("Last number in dir %s = %d\n", item->path, max);
238         item->last_num = max;
239 }
240
241 static Folder *rssyl_new_folder(const gchar *name, const gchar *path)
242 {
243         Folder *folder;
244
245         debug_print("RSSyl: new_folder: %s (%s)\n", name, path);
246
247         rssyl_make_rc_dir();
248
249         folder = g_new0(Folder, 1);
250         FOLDER(folder)->klass = &rssyl_class;
251         folder_init(FOLDER(folder), name);
252
253         return FOLDER(folder);
254 }
255
256 static void rssyl_destroy_folder(Folder *folder)
257 {
258         folder_local_folder_destroy(LOCAL_FOLDER(folder));
259 }
260
261 static void rssyl_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
262 {
263         GList *cur;
264         RFolderItem *ritem = (RFolderItem *)item;
265
266         folder_item_set_xml(folder, item, tag);
267
268         for( cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
269                 XMLAttr *attr = (XMLAttr *) cur->data;
270
271                 if( !attr || !attr->name || !attr->value)
272                         continue;
273
274                 /* (str) URL */
275                 if( !strcmp(attr->name, "uri")) {
276                         g_free(ritem->url);
277                         ritem->url = g_strdup(attr->value);
278                 }
279                 /* (int) URL auth */
280                 if (!strcmp(attr->name, "auth")) {
281                         ritem->auth->type = atoi(attr->value);
282                 }
283                 /* (str) Auth user */
284                 if (!strcmp(attr->name, "auth_user")) {
285                         g_free(ritem->auth->username);
286                         ritem->auth->username = g_strdup(attr->value);
287                 }
288                 /* (str) Auth pass */
289                 if (!strcmp(attr->name, "auth_pass")) {
290                         gsize len = 0;
291                         guchar *pwd = g_base64_decode(attr->value, &len);
292                         g_free(ritem->auth->password);
293                         ritem->auth->password = (gchar *)pwd;
294                 }
295                 /* (str) Official title */
296                 if( !strcmp(attr->name, "official_title")) {
297                         g_free(ritem->official_title);
298                         ritem->official_title = g_strdup(attr->value);
299                 }
300                 /* (bool) Keep old items */
301                 if( !strcmp(attr->name, "keep_old"))
302                         ritem->keep_old = (atoi(attr->value) == 0 ? FALSE : TRUE );
303                 /* (bool) Use default refresh_interval */
304                 if( !strcmp(attr->name, "default_refresh_interval"))
305                         ritem->default_refresh_interval = (atoi(attr->value) == 0 ? FALSE : TRUE );
306                 /* (int) Refresh interval */
307                 if( !strcmp(attr->name, "refresh_interval"))
308                         ritem->refresh_interval = atoi(attr->value);
309                 /* (bool) Fetch comments */
310                 if( !strcmp(attr->name, "fetch_comments"))
311                         ritem->fetch_comments = (atoi(attr->value) == 0 ? FALSE : TRUE );
312                 /* (int) Max age of posts to fetch comments for */
313                 if( !strcmp(attr->name, "fetch_comments_max_age"))
314                         ritem->fetch_comments_max_age = atoi(attr->value);
315                 /* (bool) Write heading */
316                 if( !strcmp(attr->name, "write_heading"))
317                         ritem->write_heading = (atoi(attr->value) == 0 ? FALSE : TRUE );
318                 /* (int) Silent update */
319                 if( !strcmp(attr->name, "silent_update"))
320                         ritem->silent_update = atoi(attr->value);
321                 /* (bool) Ignore title rename */
322                 if( !strcmp(attr->name, "ignore_title_rename"))
323                         ritem->ignore_title_rename = (atoi(attr->value) == 0 ? FALSE : TRUE );
324                 /* (bool) Verify SSL peer  */
325                 if( !strcmp(attr->name, "ssl_verify_peer"))
326                         ritem->ssl_verify_peer = (atoi(attr->value) == 0 ? FALSE : TRUE );
327         }
328 }
329
330 static XMLTag *rssyl_item_get_xml(Folder *folder, FolderItem *item)
331 {
332         XMLTag *tag;
333         RFolderItem *ri = (RFolderItem *)item;
334         gchar *tmp = NULL;
335
336         tag = folder_item_get_xml(folder, item);
337
338         /* (str) URL */
339         if( ri->url != NULL )
340                 xml_tag_add_attr(tag, xml_attr_new("uri", ri->url));
341         /* (int) Auth */
342         tmp = g_strdup_printf("%d", ri->auth->type);
343         xml_tag_add_attr(tag, xml_attr_new("auth", tmp));
344         g_free(tmp);
345         /* (str) Auth user */
346         if (ri->auth->username != NULL)
347                 xml_tag_add_attr(tag, xml_attr_new("auth_user", ri->auth->username));
348         /* (str) Auth pass */
349         if (ri->auth->password != NULL) {
350                 gchar *pwd = g_base64_encode(ri->auth->password, strlen(ri->auth->password));
351                 xml_tag_add_attr(tag, xml_attr_new("auth_pass", pwd));
352                 g_free(pwd);
353         }
354         /* (str) Official title */
355         if( ri->official_title != NULL )
356                 xml_tag_add_attr(tag, xml_attr_new("official_title", ri->official_title));
357         /* (bool) Keep old items */
358         xml_tag_add_attr(tag, xml_attr_new("keep_old",
359                                 (ri->keep_old ? "1" : "0")) );
360         /* (bool) Use default refresh interval */
361         xml_tag_add_attr(tag, xml_attr_new("default_refresh_interval",
362                                 (ri->default_refresh_interval ? "1" : "0")) );
363         /* (int) Refresh interval */
364         tmp = g_strdup_printf("%d", ri->refresh_interval);
365         xml_tag_add_attr(tag, xml_attr_new("refresh_interval", tmp));
366         g_free(tmp);
367         /* (bool) Fetch comments */
368         xml_tag_add_attr(tag, xml_attr_new("fetch_comments",
369                                 (ri->fetch_comments ? "1" : "0")) );
370         /* (int) Max age of posts to fetch comments for */
371         tmp = g_strdup_printf("%d", ri->fetch_comments_max_age);
372         xml_tag_add_attr(tag, xml_attr_new("fetch_comments_max_age", tmp));
373         g_free(tmp);
374         /* (bool) Write heading */
375         xml_tag_add_attr(tag, xml_attr_new("write_heading",
376                                 (ri->write_heading ? "1" : "0")) );
377         /* (int) Silent update */
378         tmp = g_strdup_printf("%d", ri->silent_update);
379         xml_tag_add_attr(tag, xml_attr_new("silent_update", tmp));
380         g_free(tmp);
381         /* (bool) Ignore title rename */
382         xml_tag_add_attr(tag, xml_attr_new("ignore_title_rename",
383                                 (ri->ignore_title_rename ? "1" : "0")) );
384         /* (bool) Verify SSL peer */
385         xml_tag_add_attr(tag, xml_attr_new("ssl_verify_peer",
386                                 (ri->ssl_verify_peer ? "1" : "0")) );
387
388         return tag;
389 }
390
391 static gint rssyl_scan_tree(Folder *folder)
392 {
393         g_return_val_if_fail(folder != NULL, -1);
394
395         folder->outbox = NULL;
396         folder->draft = NULL;
397         folder->queue = NULL;
398         folder->trash = NULL;
399
400         debug_print("RSSyl: scanning tree\n");
401         rssyl_create_tree(folder);
402
403         return 0;
404 }
405
406 static gint rssyl_create_tree(Folder *folder)
407 {
408         FolderItem *rootitem;
409         GNode *rootnode;
410
411         g_return_val_if_fail(folder != NULL, -1);
412
413         rssyl_make_rc_dir();
414
415         if( !folder->node ) {
416                 rootitem = folder_item_new(folder, folder->name, NULL);
417                 rootitem->folder = folder;
418                 rootnode = g_node_new(rootitem);
419                 folder->node = rootnode;
420                 rootitem->node = rootnode;
421         }
422
423         debug_print("RSSyl: created new rssyl tree\n");
424         return 0;
425 }
426
427 static FolderItem *rssyl_item_new(Folder *folder)
428 {
429         RFolderItem *ritem = g_new0(RFolderItem, 1);
430
431         ritem->url = NULL;
432         ritem->auth = g_new0(FeedAuth, 1);
433         ritem->auth->type = FEED_AUTH_NONE;
434         ritem->auth->username = NULL;
435         ritem->auth->password = NULL;
436         ritem->official_title = NULL;
437         ritem->source_id = NULL;
438         ritem->items = NULL;
439         ritem->keep_old = FALSE;
440         ritem->default_refresh_interval = TRUE;
441         ritem->refresh_interval = atoi(PREF_DEFAULT_REFRESH);
442         ritem->fetch_comments = FALSE;
443         ritem->fetch_comments_max_age = -1;
444         ritem->write_heading = TRUE;
445         ritem->fetching_comments = FALSE;
446         ritem->silent_update = 0;
447         ritem->last_update = 0;
448         ritem->ignore_title_rename = FALSE;
449
450         return (FolderItem *)ritem;
451 }
452
453 static void rssyl_item_destroy(Folder *folder, FolderItem *item)
454 {
455         RFolderItem *ritem = (RFolderItem *)item;
456
457         g_return_if_fail(ritem != NULL);
458
459         g_free(ritem->url);
460         if (ritem->auth->username)
461                 g_free(ritem->auth->username);
462         if (ritem->auth->password)
463                 g_free(ritem->auth->password);
464         g_free(ritem->auth);
465         g_free(ritem->official_title);
466         g_slist_free(ritem->items);
467
468         /* Remove a scheduled refresh, if any */
469         if( ritem->refresh_id != 0)
470                 g_source_remove(ritem->refresh_id);
471
472         g_free(ritem);
473 }
474
475 static FolderItem *rssyl_create_folder(Folder *folder,
476                                                                 FolderItem *parent, const gchar *name)
477 {
478         gchar *path = NULL, *basepath = NULL, *itempath = NULL;
479         FolderItem *newitem = NULL;
480
481         g_return_val_if_fail(folder != NULL, NULL);
482         g_return_val_if_fail(parent != NULL, NULL);
483         g_return_val_if_fail(name != NULL, NULL);
484
485         path = folder_item_get_path(parent);
486         if( !is_dir_exist(path) ) {
487                 if( (make_dir_hier(path) != 0) ) {
488                         debug_print("RSSyl: Couldn't create directory (rec) '%s'\n", path);
489                         return NULL;
490                 }
491         }
492
493         basepath = g_strdelimit(g_strdup(name), G_DIR_SEPARATOR_S, '_');
494         path = g_strconcat(path, G_DIR_SEPARATOR_S, basepath, NULL);
495
496         if( make_dir(path) < 0 ) {
497                 debug_print("RSSyl: Couldn't create directory '%s'\n", path);
498                 g_free(path);
499                 g_free(basepath);
500                 return NULL;
501         }
502         g_free(path);
503
504         itempath = g_strconcat((parent->path ? parent->path : ""),
505                         G_DIR_SEPARATOR_S, basepath, NULL);
506         newitem = folder_item_new(folder, name, itempath);
507         g_free(itempath);
508         g_free(basepath);
509
510         folder_item_append(parent, newitem);
511
512         return newitem;
513 }
514
515 FolderItem *rssyl_get_root_folderitem(FolderItem *item)
516 {
517         FolderItem *i;
518
519         for( i = item; folder_item_parent(i) != NULL; i = folder_item_parent(i) ) { }
520         return i;
521 }
522
523 static gchar *rssyl_item_get_path(Folder *folder, FolderItem *item)
524 {
525         gchar *path, *name;
526
527         g_return_val_if_fail(folder != NULL, NULL);
528         g_return_val_if_fail(item != NULL, NULL);
529
530         debug_print("RSSyl: item_get_path\n");
531
532         name = folder_item_get_name(rssyl_get_root_folderitem(item));
533         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR,
534                         G_DIR_SEPARATOR_S, name, G_DIR_SEPARATOR_S, item->path, NULL);
535         g_free(name);
536
537         return path;
538 }
539
540 static gboolean rssyl_rename_folder_func(GNode *node, gpointer data)
541 {
542         FolderItem *item = node->data;
543         gchar **paths = data;
544         const gchar *oldpath = paths[0];
545         const gchar *newpath = paths[1];
546         gchar *base;
547         gchar *new_itempath;
548         gint oldpathlen;
549
550         oldpathlen = strlen(oldpath);
551         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
552                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
553                 return TRUE;
554         }
555
556         base = item->path + oldpathlen;
557         while (*base == G_DIR_SEPARATOR) base++;
558         if (*base == '\0')
559                 new_itempath = g_strdup(newpath);
560         else
561                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
562                                 NULL);
563         g_free(item->path);
564         item->path = new_itempath;
565
566         return FALSE;
567 }
568
569 static gint rssyl_rename_folder(Folder *folder, FolderItem *item,
570                                 const gchar *name)
571 {
572         gchar *oldpath;
573         gchar *dirname;
574         gchar *newpath, *utf8newpath;
575         gchar *basenewpath;
576         gchar *paths[2];
577
578         g_return_val_if_fail(folder != NULL, -1);
579         g_return_val_if_fail(item != NULL, -1);
580         g_return_val_if_fail(item->path != NULL, -1);
581         g_return_val_if_fail(name != NULL, -1);
582
583         debug_print("RSSyl: rssyl_rename_folder '%s' -> '%s'\n",
584                         item->name, name);
585
586         if (!strcmp(item->name, name))
587                         return 0;
588
589         oldpath = folder_item_get_path(item);
590         if( !is_dir_exist(oldpath) )
591                 make_dir_hier(oldpath);
592
593         dirname = g_path_get_dirname(oldpath);
594         basenewpath = g_strdelimit(g_strdup(name), G_DIR_SEPARATOR_S, '_');
595         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, basenewpath, NULL);
596         g_free(basenewpath);
597
598         if( g_rename(oldpath, newpath) < 0 ) {
599                 FILE_OP_ERROR(oldpath, "rename");
600                 g_free(oldpath);
601                 g_free(newpath);
602                 return -1;
603         }
604
605         g_free(oldpath);
606         g_free(newpath);
607
608         if( strchr(item->path, G_DIR_SEPARATOR) != NULL ) {
609                 dirname = g_path_get_dirname(item->path);
610                 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
611                 g_free(dirname);
612         } else
613                 utf8newpath = g_strdup(name);
614
615         g_free(item->name);
616         item->name = g_strdup(name);
617
618         paths[0] = g_strdup(item->path);
619         paths[1] = utf8newpath;
620         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
621                         rssyl_rename_folder_func, paths);
622
623         g_free(paths[0]);
624         g_free(paths[1]);
625
626         return 0;
627 }
628
629 static gint rssyl_remove_folder(Folder *folder, FolderItem *item)
630 {
631         gchar *path = NULL;
632
633         g_return_val_if_fail(folder != NULL, -1);
634         g_return_val_if_fail(item != NULL, -1);
635         g_return_val_if_fail(item->path != NULL, -1);
636         g_return_val_if_fail(item->stype == F_NORMAL, -1);
637
638         debug_print("RSSyl: removing folder item %s\n", item->path);
639
640         path = folder_item_get_path(item);
641         if( remove_dir_recursive(path) < 0 ) {
642                 g_warning("can't remove directory '%s'\n", path);
643                 g_free(path);
644                 return -1;
645         }
646
647         g_free(path);
648         folder_item_remove(item);
649
650         return 0;
651 }
652
653 static gint rssyl_get_num_list(Folder *folder, FolderItem *item,
654                 MsgNumberList **list, gboolean *old_uids_valid)
655 {
656         gchar *path;
657         GDir *dp;
658         const gchar *d;
659         GError *error;
660         gint num, nummsgs = 0;
661
662         g_return_val_if_fail(item != NULL, -1);
663
664         debug_print("RSSyl: get_num_list: scanning '%s'\n", item->path);
665
666         *old_uids_valid = TRUE;
667         
668         path = folder_item_get_path(item);
669         g_return_val_if_fail(path != NULL, -1);
670
671         if( (dp = g_dir_open(path, 0, &error)) == NULL ) {
672                 FILE_OP_ERROR(item->path, "opendir");
673                 debug_print("g_dir_open() failed on \"%s\", error %d (%s).\n",
674                                 path, error->code, error->message);
675                 g_free(path);
676                 return -1;
677         }
678
679         g_free(path);
680
681         while( (d = g_dir_read_name(dp)) != NULL ) {
682                 if( (num = to_number(d)) > 0 ) {
683                         *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
684                         nummsgs++;
685                 }
686         }
687         g_dir_close(dp);
688
689         debug_print("RSSyl: get_num_list: returning %d\n", nummsgs);
690
691         return nummsgs;
692 }
693
694 static gboolean rssyl_is_msg_changed(Folder *folder, FolderItem *item,
695                 MsgInfo *msginfo)
696 {
697         GStatBuf s;
698         gchar *path = NULL;
699
700         g_return_val_if_fail(folder != NULL, FALSE);
701         g_return_val_if_fail(item != NULL, FALSE);
702         g_return_val_if_fail(msginfo != NULL, FALSE);
703
704         path = g_strconcat(folder_item_get_path(item), G_DIR_SEPARATOR_S,
705                         itos(msginfo->msgnum), NULL);
706
707         if (g_stat(path, &s) < 0 ||
708                 msginfo->size != s.st_size || (
709                                 (msginfo->mtime - s.st_mtime != 0) &&
710                                 (msginfo->mtime - s.st_mtime != 3600) &&
711                                 (msginfo->mtime - s.st_mtime != -3600))) {
712                 g_free(path);
713                 return TRUE;
714         }
715
716         g_free(path);
717         return FALSE;
718 }
719
720 static gchar *rssyl_fetch_msg(Folder *folder, FolderItem *item, gint num)
721 {
722         gchar *path;
723         gchar *file;
724
725         g_return_val_if_fail(item != NULL, NULL);
726         g_return_val_if_fail(num > 0, NULL);
727
728         path = folder_item_get_path(item);
729         file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
730         g_free(path);
731
732         debug_print("RSSyl: fetch_msg '%s'\n", file);
733
734         if( !is_file_exist(file)) {
735                 g_free(file);
736                 return NULL;
737         }
738
739         return file;
740 }
741
742 static MsgInfo *rssyl_get_msginfo(Folder *folder, FolderItem *item, gint num)
743 {
744         MsgInfo *msginfo = NULL;
745         gchar *file;
746         MsgFlags flags;
747
748         g_return_val_if_fail(folder != NULL, NULL);
749         g_return_val_if_fail(item != NULL, NULL);
750         g_return_val_if_fail(num > 0, NULL);
751
752         debug_print("RSSyl: get_msginfo: %d\n", num);
753
754         file = rssyl_fetch_msg(folder, item, num);
755         g_return_val_if_fail(file != NULL, NULL);
756
757         flags.perm_flags = 0;
758         flags.tmp_flags = 0;
759
760         msginfo = rssyl_feed_parse_item_to_msginfo(file, flags, TRUE, TRUE, item);
761         g_free(file);
762
763         if( msginfo )
764                 msginfo->msgnum = num;
765
766         return msginfo;
767 }
768
769 static gint rssyl_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
770                 GHashTable *relation)
771 {
772         gchar *destfile;
773         GSList *cur;
774         MsgFileInfo *fileinfo;
775
776         g_return_val_if_fail(dest != NULL, -1);
777         g_return_val_if_fail(file_list != NULL, -1);
778
779         if( dest->last_num < 0 ) {
780                 rssyl_get_last_num(folder, dest);
781                 if( dest->last_num < 0 ) return -1;
782         }
783
784         for( cur = file_list; cur != NULL; cur = cur->next ) {
785                 fileinfo = (MsgFileInfo *)cur->data;
786
787                 destfile = rssyl_get_new_msg_filename(dest);
788                 g_return_val_if_fail(destfile != NULL, -1);
789                 debug_print("RSSyl: add_msgs: new filename is '%s'\n", destfile);
790
791                 if( copy_file(fileinfo->file, destfile, TRUE) < 0 ) {
792                         g_warning("can't copy message %s to %s\n", fileinfo->file, destfile);
793                         g_free(destfile);
794                         return -1;
795                 }
796
797                 if( relation != NULL )
798                         g_hash_table_insert(relation, fileinfo->msginfo != NULL ?
799                                         (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
800                                         GINT_TO_POINTER(dest->last_num + 1));
801                 g_free(destfile);
802                 dest->last_num++;
803         }
804
805
806         return dest->last_num;
807 }
808
809 static gint rssyl_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
810                 MsgFlags *flags)
811 {
812         GSList file_list;
813         MsgFileInfo fileinfo;
814
815         g_return_val_if_fail(file != NULL, -1);
816
817         fileinfo.msginfo = NULL;
818         fileinfo.file = (gchar *)file;
819         fileinfo.flags = flags;
820         file_list.data = &fileinfo;
821         file_list.next = NULL;
822
823         return rssyl_add_msgs(folder, dest, &file_list, NULL);
824 }
825
826 static gint rssyl_remove_msg(Folder *folder, FolderItem *item, gint num)
827 {
828         gboolean need_scan = FALSE;
829         gchar *file, *tmp;
830
831         g_return_val_if_fail(item != NULL, -1);
832
833         file = rssyl_fetch_msg(folder, item, num);
834         g_return_val_if_fail(file != NULL, -1);
835
836         need_scan = mh_get_class()->scan_required(folder, item);
837
838         /* are we doing a folder move ? */
839         tmp = g_strdup_printf("%s.tmp", file);
840         if (is_file_exist(tmp)) {
841                 g_unlink(tmp);
842                 g_free(tmp);
843                 g_free(file);
844                 return 0;
845         }
846         g_free(tmp);
847
848         rssyl_deleted_add((RFolderItem *)item, file);
849
850         if( g_unlink(file) < 0 ) {
851                 FILE_OP_ERROR(file, "unlink");
852                 g_free(file);
853                 return -1;
854         }
855
856         if( !need_scan )
857                 item->mtime = time(NULL);
858
859         g_free(file);
860         return 0;
861 }
862
863 static gboolean rssyl_subscribe_uri(Folder *folder, const gchar *uri)
864 {
865         if (folder->klass != rssyl_folder_get_class())
866                 return FALSE;
867         return (rssyl_feed_subscribe_new(FOLDER_ITEM(folder->node->data), uri, FALSE) ?
868                         TRUE : FALSE);
869 }
870
871 static void rssyl_copy_private_data(Folder *folder, FolderItem *oldi,
872                 FolderItem *newi)
873 {
874         gchar *dpathold, *dpathnew;
875         RFolderItem *olditem = (RFolderItem *)oldi,
876                                                                         *newitem = (RFolderItem *)newi;
877
878         g_return_if_fail(folder != NULL);
879         g_return_if_fail(olditem != NULL);
880         g_return_if_fail(newitem != NULL);
881
882         if (olditem->url != NULL) {
883                 g_free(newitem->url);
884                 newitem->url = g_strdup(olditem->url);
885         }
886
887         if (olditem->auth != NULL) {
888                 if (newitem->auth != NULL) {
889                         if (newitem->auth->username != NULL) {
890                                 g_free(newitem->auth->username);
891                                 newitem->auth->username = NULL;
892                         }
893                         if (newitem->auth->password != NULL) {
894                                 g_free(newitem->auth->password);
895                                 newitem->auth->password = NULL;
896                         }
897                         g_free(newitem->auth);
898                 }
899                 newitem->auth = g_new0(FeedAuth, 1);
900                 newitem->auth->type = olditem->auth->type;
901                 if (olditem->auth->username != NULL)
902                         newitem->auth->username = g_strdup(olditem->auth->username);
903                 if (olditem->auth->password != NULL)
904                         newitem->auth->password = g_strdup(olditem->auth->password);
905         }
906
907         if (olditem->official_title != NULL) {
908                 g_free(newitem->official_title);
909                 newitem->official_title = g_strdup(olditem->official_title);
910         }
911
912         if (olditem->source_id != NULL) {
913                 g_free(newitem->source_id);
914                 newitem->source_id = g_strdup(olditem->source_id);
915         }
916
917         newitem->keep_old = olditem->keep_old;
918         newitem->default_refresh_interval = olditem->default_refresh_interval;
919         newitem->refresh_interval = olditem->refresh_interval;
920         newitem->fetch_comments = olditem->fetch_comments;
921         newitem->fetch_comments_max_age = olditem->fetch_comments_max_age;
922         newitem->silent_update = olditem->silent_update;
923         newitem->write_heading = olditem->write_heading;
924         newitem->ignore_title_rename = olditem->ignore_title_rename;
925         newitem->ssl_verify_peer = olditem->ssl_verify_peer;
926         newitem->refresh_id = olditem->refresh_id;
927         newitem->fetching_comments = olditem->fetching_comments;
928         newitem->last_update = olditem->last_update;
929
930         dpathold = g_strconcat(rssyl_item_get_path(oldi->folder, oldi),
931                         G_DIR_SEPARATOR_S, RSSYL_DELETED_FILE, NULL);
932         dpathnew = g_strconcat(rssyl_item_get_path(newi->folder, newi),
933                         G_DIR_SEPARATOR_S, RSSYL_DELETED_FILE, NULL);
934         move_file(dpathold, dpathnew, TRUE);
935         g_free(dpathold);
936         g_free(dpathnew);
937
938 }
939
940 /************************************************************************/
941
942 FolderClass *rssyl_folder_get_class()
943 {
944         if( rssyl_class.idstr == NULL ) {
945                 rssyl_class.type = F_UNKNOWN;
946                 rssyl_class.idstr = "rssyl";
947                 rssyl_class.uistr = "RSSyl";
948
949                 /* Folder functions */
950                 rssyl_class.new_folder = rssyl_new_folder;
951                 rssyl_class.destroy_folder = rssyl_destroy_folder;
952                 rssyl_class.set_xml = folder_set_xml;
953                 rssyl_class.get_xml = folder_get_xml;
954                 rssyl_class.scan_tree = rssyl_scan_tree;
955                 rssyl_class.create_tree = rssyl_create_tree;
956
957                 /* FolderItem functions */
958                 rssyl_class.item_new = rssyl_item_new;
959                 rssyl_class.item_destroy = rssyl_item_destroy;
960                 rssyl_class.item_get_path = rssyl_item_get_path;
961                 rssyl_class.create_folder = rssyl_create_folder;
962                 rssyl_class.rename_folder = rssyl_rename_folder;
963                 rssyl_class.remove_folder = rssyl_remove_folder;
964                 rssyl_class.get_num_list = rssyl_get_num_list;
965                 rssyl_class.scan_required = mh_get_class()->scan_required;
966                 rssyl_class.item_set_xml = rssyl_item_set_xml;
967                 rssyl_class.item_get_xml = rssyl_item_get_xml;
968
969                 /* Message functions */
970                 rssyl_class.get_msginfo = rssyl_get_msginfo;
971                 rssyl_class.fetch_msg = rssyl_fetch_msg;
972                 rssyl_class.copy_msg = mh_get_class()->copy_msg;
973                 rssyl_class.copy_msgs = mh_get_class()->copy_msgs;
974                 rssyl_class.add_msg = rssyl_add_msg;
975                 rssyl_class.add_msgs = rssyl_add_msgs;
976                 rssyl_class.remove_msg = rssyl_remove_msg;
977                 rssyl_class.remove_msgs = NULL;
978                 rssyl_class.is_msg_changed = rssyl_is_msg_changed;
979 //              rssyl_class.change_flags = rssyl_change_flags;
980                 rssyl_class.change_flags = NULL;
981                 rssyl_class.subscribe = rssyl_subscribe_uri;
982                 rssyl_class.copy_private_data = rssyl_copy_private_data;
983                 rssyl_class.search_msgs = folder_item_search_msgs_local;
984         }
985
986         return &rssyl_class;
987 }