From 4d191ad36e95a12d7de2ac865c42b1ecc4439597 Mon Sep 17 00:00:00 2001
From: Andrew Potter <agpotter@gmail.com>
Date: Sun, 3 Mar 2013 14:38:44 -0800
Subject: [PATCH] xfer: Add new config option auto_accept_nicks

New option xfer.file.auto_accept_nicks allows automatic accept of
xfers from the comma-separated nick list. Server names can be
optionally prefixed to the nickname with a period,
e.g. "freenode.FlashCode".
---
 src/plugins/xfer/xfer-config.c |  7 ++++++
 src/plugins/xfer/xfer-config.h |  1 +
 src/plugins/xfer/xfer.c        | 49 +++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/src/plugins/xfer/xfer-config.c b/src/plugins/xfer/xfer-config.c
index ab9171e..6f1e8dd 100644
--- a/src/plugins/xfer/xfer-config.c
+++ b/src/plugins/xfer/xfer-config.c
@@ -61,6 +61,7 @@ struct t_config_option *xfer_config_file_auto_rename;
 struct t_config_option *xfer_config_file_auto_resume;
 struct t_config_option *xfer_config_file_auto_accept_files;
 struct t_config_option *xfer_config_file_auto_accept_chats;
+struct t_config_option *xfer_config_file_auto_accept_nicks;
 
 
 
@@ -304,6 +305,12 @@ xfer_config_init ()
         "auto_accept_chats", "boolean",
         N_("automatically accept chat requests (use carefully!)"),
         NULL, 0, 0, "off", NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL);
+    xfer_config_file_auto_accept_nicks = weechat_config_new_option (
+        xfer_config_file, ptr_section,
+        "auto_accept_nicks", "string",
+        N_("automatically accept incoming files from specific comma-separated "
+           "nicknames (use carefully!)"),
+        NULL, 0, 0, "", NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL);
 
     return 1;
 }
diff --git a/src/plugins/xfer/xfer-config.h b/src/plugins/xfer/xfer-config.h
index 051e4d8..e437278 100644
--- a/src/plugins/xfer/xfer-config.h
+++ b/src/plugins/xfer/xfer-config.h
@@ -49,6 +49,7 @@ extern struct t_config_option *xfer_config_file_auto_rename;
 extern struct t_config_option *xfer_config_file_auto_resume;
 extern struct t_config_option *xfer_config_file_auto_accept_files;
 extern struct t_config_option *xfer_config_file_auto_accept_chats;
+extern struct t_config_option *xfer_config_file_auto_accept_nicks;
 
 extern int xfer_config_init ();
 extern int xfer_config_read ();
diff --git a/src/plugins/xfer/xfer.c b/src/plugins/xfer/xfer.c
index 194fce0..07994df 100644
--- a/src/plugins/xfer/xfer.c
+++ b/src/plugins/xfer/xfer.c
@@ -520,6 +520,51 @@ xfer_alloc ()
 }
 
 /*
+ * Tests if the given nick is an auto_accept_nick
+ *
+ * Returns:
+ *  0: False
+ *  1: True
+ */
+
+static int
+xfer_auto_accept_nick (const char *remote_nick, const char *server)
+{
+    int size, i;
+    int ret = 0;
+    const char *config_nicks = weechat_config_string (xfer_config_file_auto_accept_nicks);
+    char **nicks = weechat_string_split (config_nicks, ",", 0, 0, &size);
+
+    if ( size > 0 )
+    {
+        for ( i = 0; i < size; ++i )
+        {
+            char *server_sep = strchr (nicks[i], '.');
+            if ( !server_sep )
+            {
+	            if ( weechat_strcasecmp (remote_nick, nicks[i]) == 0 )
+                {
+                    ret = 1;
+                    break;
+                }
+            }
+            else
+            {
+	            if ( weechat_strncasecmp (server, nicks[i], server_sep - nicks[i]) == 0 
+	                 && weechat_strcasecmp (remote_nick, server_sep + 1) == 0 )
+                {
+                    ret = 1;
+                    break;
+                }
+            }
+        }
+        weechat_string_free_split (nicks);
+    }
+
+    return ret;
+}
+
+/*
  * Adds a xfer to list.
  *
  * Returns pointer to new xfer, NULL if error.
@@ -681,7 +726,9 @@ xfer_new (const char *plugin_name, const char *plugin_id,
     if ( ( (type == XFER_TYPE_FILE_RECV)
                 && (weechat_config_boolean (xfer_config_file_auto_accept_files)) )
          || ( (type == XFER_TYPE_CHAT_RECV)
-              && (weechat_config_boolean (xfer_config_file_auto_accept_chats)) ) )
+              && (weechat_config_boolean (xfer_config_file_auto_accept_chats)) )
+         || ( (type == XFER_TYPE_FILE_RECV)
+              && (xfer_auto_accept_nick (new_xfer->remote_nick, new_xfer->plugin_id)) ) )
         xfer_network_accept (new_xfer);
     else
         xfer_buffer_refresh (WEECHAT_HOTLIST_PRIVATE);
-- 
1.8.1.4

