RSSyl: zero out password variables after migration to password store
[claws.git] / src / plugins / rssyl / rssyl.c
index 0f9ac06379e9d3b8238013148cfe1cc9914a7f38..139f5af0fd06349d9b8941d069f92f831e837c07 100644 (file)
@@ -99,7 +99,7 @@ static void rssyl_make_rc_dir(void)
 
        if( !is_dir_exist(rssyl_dir) ) {
                if( make_dir(rssyl_dir) < 0 ) {
-                       g_warning("couldn't create directory %s\n", rssyl_dir);
+                       g_warning("couldn't create directory %s", rssyl_dir);
                }
 
                debug_print("RSSyl: created directory %s\n", rssyl_dir);
@@ -152,7 +152,7 @@ void rssyl_init(void)
        else
                rssyl_update_format();
 
-       prefs_toolbar_register_plugin_item(TOOLBAR_MAIN, "RSSyl", _("Refresh all feeds"), rssyl_toolbar_cb_refresh_all_feeds, NULL);
+       prefs_toolbar_register_plugin_item(TOOLBAR_MAIN, PLUGIN_NAME, _("Refresh all feeds"), rssyl_toolbar_cb_refresh_all_feeds, NULL);
 
        if( rssyl_prefs_get()->refresh_on_startup &&
                        claws_is_starting() )
@@ -163,7 +163,7 @@ void rssyl_done(void)
 {
        rssyl_opml_export();
 
-       prefs_toolbar_unregister_plugin_item(TOOLBAR_MAIN, "RSSyl", _("Refresh all feeds"));
+       prefs_toolbar_unregister_plugin_item(TOOLBAR_MAIN, PLUGIN_NAME, _("Refresh all feeds"));
 
        rssyl_prefs_done();
        rssyl_gtk_done();
@@ -203,8 +203,9 @@ static gchar *rssyl_get_new_msg_filename(FolderItem *dest)
 static void rssyl_get_last_num(Folder *folder, FolderItem *item)
 {
        gchar *path;
-       DIR *dp;
-       struct dirent *d;
+       const char *f;
+       GDir *dp;
+       GError *error = NULL;
        gint max = 0;
        gint num;
 
@@ -214,21 +215,25 @@ static void rssyl_get_last_num(Folder *folder, FolderItem *item)
        path = folder_item_get_path(item);
        g_return_if_fail(path != NULL);
 
-       if( (dp = opendir(path)) == NULL ) {
-               FILE_OP_ERROR(item->path, "opendir");
+       if( (dp = g_dir_open(path, 0, &error)) == NULL ) {
+               FILE_OP_ERROR(item->path, "g_dir_open");
+               debug_print("g_dir_open() failed on \"%s\", error %d (%s).\n",
+                               path, error->code, error->message);
+               g_error_free(error);
                g_free(path);
                return;
        }
 
        g_free(path);
 
-       while( (d = readdir(dp)) != NULL ) {
-               if( (num = to_number(d->d_name)) > 0 && dirent_is_regular_file(d) ) {
+       while( (f = g_dir_read_name(dp)) != NULL) {
+               if ((num = to_number(f)) > 0 &&
+                               g_file_test(f, G_FILE_TEST_IS_REGULAR)) {
                        if( max < num )
                                max = num;
                }
        }
-       closedir(dp);
+       g_dir_close(dp);
 
        debug_print("Last number in dir %s = %d\n", item->path, max);
        item->last_num = max;
@@ -272,6 +277,24 @@ static void rssyl_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
                        g_free(ritem->url);
                        ritem->url = g_strdup(attr->value);
                }
+               /* (int) URL auth */
+               if (!strcmp(attr->name, "auth")) {
+                       ritem->auth->type = atoi(attr->value);
+               }
+               /* (str) Auth user */
+               if (!strcmp(attr->name, "auth_user")) {
+                       g_free(ritem->auth->username);
+                       ritem->auth->username = g_strdup(attr->value);
+               }
+               /* (str) Auth pass - save directly to password store */
+               if (!strcmp(attr->name, "auth_pass")) {
+                       gsize len = 0;
+                       guchar *pwd = g_base64_decode(attr->value, &len);
+                       memset(attr->value, 0, strlen(attr->value));
+                       rssyl_passwd_set(ritem, (gchar *)pwd);
+                       memset(pwd, 0, strlen(pwd));
+                       g_free(pwd);
+               }
                /* (str) Official title */
                if( !strcmp(attr->name, "official_title")) {
                        g_free(ritem->official_title);
@@ -318,6 +341,13 @@ static XMLTag *rssyl_item_get_xml(Folder *folder, FolderItem *item)
        /* (str) URL */
        if( ri->url != NULL )
                xml_tag_add_attr(tag, xml_attr_new("uri", ri->url));
+       /* (int) Auth */
+       tmp = g_strdup_printf("%d", ri->auth->type);
+       xml_tag_add_attr(tag, xml_attr_new("auth", tmp));
+       g_free(tmp);
+       /* (str) Auth user */
+       if (ri->auth->username != NULL)
+               xml_tag_add_attr(tag, xml_attr_new("auth_user", ri->auth->username));
        /* (str) Official title */
        if( ri->official_title != NULL )
                xml_tag_add_attr(tag, xml_attr_new("official_title", ri->official_title));
@@ -396,6 +426,10 @@ static FolderItem *rssyl_item_new(Folder *folder)
        RFolderItem *ritem = g_new0(RFolderItem, 1);
 
        ritem->url = NULL;
+       ritem->auth = g_new0(FeedAuth, 1);
+       ritem->auth->type = FEED_AUTH_NONE;
+       ritem->auth->username = NULL;
+       ritem->auth->password = NULL;
        ritem->official_title = NULL;
        ritem->source_id = NULL;
        ritem->items = NULL;
@@ -420,6 +454,11 @@ static void rssyl_item_destroy(Folder *folder, FolderItem *item)
        g_return_if_fail(ritem != NULL);
 
        g_free(ritem->url);
+       if (ritem->auth->username)
+               g_free(ritem->auth->username);
+       if (ritem->auth->password)
+               g_free(ritem->auth->password);
+       g_free(ritem->auth);
        g_free(ritem->official_title);
        g_slist_free(ritem->items);
 
@@ -485,11 +524,9 @@ static gchar *rssyl_item_get_path(Folder *folder, FolderItem *item)
        g_return_val_if_fail(folder != NULL, NULL);
        g_return_val_if_fail(item != NULL, NULL);
 
-       debug_print("RSSyl: item_get_path\n");
-
        name = folder_item_get_name(rssyl_get_root_folderitem(item));
        path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RSSYL_DIR,
-                       G_DIR_SEPARATOR_S, name, G_DIR_SEPARATOR_S, item->path, NULL);
+                       G_DIR_SEPARATOR_S, name, item->path, NULL);
        g_free(name);
 
        return path;
@@ -507,7 +544,7 @@ static gboolean rssyl_rename_folder_func(GNode *node, gpointer data)
 
        oldpathlen = strlen(oldpath);
        if (strncmp(oldpath, item->path, oldpathlen) != 0) {
-               g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
+               g_warning("path doesn't match: %s, %s", oldpath, item->path);
                return TRUE;
        }
 
@@ -597,7 +634,7 @@ static gint rssyl_remove_folder(Folder *folder, FolderItem *item)
 
        path = folder_item_get_path(item);
        if( remove_dir_recursive(path) < 0 ) {
-               g_warning("can't remove directory '%s'\n", path);
+               g_warning("can't remove directory '%s'", path);
                g_free(path);
                return -1;
        }
@@ -612,8 +649,9 @@ static gint rssyl_get_num_list(Folder *folder, FolderItem *item,
                MsgNumberList **list, gboolean *old_uids_valid)
 {
        gchar *path;
-       DIR *dp;
-       struct dirent *d;
+       GDir *dp;
+       const gchar *d;
+       GError *error = NULL;
        gint num, nummsgs = 0;
 
        g_return_val_if_fail(item != NULL, -1);
@@ -625,22 +663,23 @@ static gint rssyl_get_num_list(Folder *folder, FolderItem *item,
        path = folder_item_get_path(item);
        g_return_val_if_fail(path != NULL, -1);
 
-       if( (dp = opendir(path)) == NULL ) {
-               FILE_OP_ERROR(item->path, "opendir");
+       if( (dp = g_dir_open(path, 0, &error)) == NULL ) {
+               debug_print("g_dir_open() failed on \"%s\", error %d (%s).\n",
+                               path, error->code, error->message);
+               g_error_free(error);
                g_free(path);
                return -1;
        }
 
        g_free(path);
 
-       while( (d = readdir(dp)) != NULL ) {
-               if( (num = to_number(d->d_name)) > 0 ) {
+       while( (d = g_dir_read_name(dp)) != NULL ) {
+               if( (num = to_number(d)) > 0 ) {
                        *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
                        nummsgs++;
                }
        }
-
-       closedir(dp);
+       g_dir_close(dp);
 
        debug_print("RSSyl: get_num_list: returning %d\n", nummsgs);
 
@@ -650,15 +689,17 @@ static gint rssyl_get_num_list(Folder *folder, FolderItem *item,
 static gboolean rssyl_is_msg_changed(Folder *folder, FolderItem *item,
                MsgInfo *msginfo)
 {
-       struct stat s;
+       GStatBuf s;
        gchar *path = NULL;
+       gchar *itempath = NULL;
 
        g_return_val_if_fail(folder != NULL, FALSE);
        g_return_val_if_fail(item != NULL, FALSE);
        g_return_val_if_fail(msginfo != NULL, FALSE);
 
-       path = g_strconcat(folder_item_get_path(item), G_DIR_SEPARATOR_S,
-                       itos(msginfo->msgnum), NULL);
+       itempath = folder_item_get_path(item);
+       path = g_strconcat(itempath, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
+       g_free(itempath);
 
        if (g_stat(path, &s) < 0 ||
                msginfo->size != s.st_size || (
@@ -744,12 +785,10 @@ static gint rssyl_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                g_return_val_if_fail(destfile != NULL, -1);
                debug_print("RSSyl: add_msgs: new filename is '%s'\n", destfile);
 
-               if( link(fileinfo->file, destfile) < 0 ) {
-                       if( copy_file(fileinfo->file, destfile, TRUE) < 0 ) {
-                               g_warning("can't copy message %s to %s\n", fileinfo->file, destfile);
-                               g_free(destfile);
-                               return -1;
-                       }
+               if( copy_file(fileinfo->file, destfile, TRUE) < 0 ) {
+                       g_warning("can't copy message %s to %s", fileinfo->file, destfile);
+                       g_free(destfile);
+                       return -1;
                }
 
                if( relation != NULL )
@@ -842,6 +881,26 @@ static void rssyl_copy_private_data(Folder *folder, FolderItem *oldi,
                newitem->url = g_strdup(olditem->url);
        }
 
+       if (olditem->auth != NULL) {
+               if (newitem->auth != NULL) {
+                       if (newitem->auth->username != NULL) {
+                               g_free(newitem->auth->username);
+                               newitem->auth->username = NULL;
+                       }
+                       if (newitem->auth->password != NULL) {
+                               g_free(newitem->auth->password);
+                               newitem->auth->password = NULL;
+                       }
+                       g_free(newitem->auth);
+               }
+               newitem->auth = g_new0(FeedAuth, 1);
+               newitem->auth->type = olditem->auth->type;
+               if (olditem->auth->username != NULL)
+                       newitem->auth->username = g_strdup(olditem->auth->username);
+               if (olditem->auth->password != NULL)
+                       newitem->auth->password = g_strdup(olditem->auth->password);
+       }
+
        if (olditem->official_title != NULL) {
                g_free(newitem->official_title);
                newitem->official_title = g_strdup(olditem->official_title);
@@ -882,7 +941,7 @@ FolderClass *rssyl_folder_get_class()
        if( rssyl_class.idstr == NULL ) {
                rssyl_class.type = F_UNKNOWN;
                rssyl_class.idstr = "rssyl";
-               rssyl_class.uistr = "RSSyl";
+               rssyl_class.uistr = PLUGIN_NAME;
 
                /* Folder functions */
                rssyl_class.new_folder = rssyl_new_folder;