1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
/*
* Autoaccept - Auto-accept file transfers from selected users
* Copyright (C) 2006
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02111-1301, USA.
*/
#include "internal.h"
#define PLUGIN_ID "core-plugin_pack-autoaccept"
#define PLUGIN_NAME N_("Autoaccept")
#define PLUGIN_STATIC_NAME "Autoaccept"
#define PLUGIN_SUMMARY N_("Auto-accept file transfer requests from selected users.")
#define PLUGIN_DESCRIPTION N_("Auto-accept file transfer requests from selected users.")
#define PLUGIN_AUTHOR "Sadrul H Chowdhury <sadrul@users.sourceforge.net>"
/* System headers */
#include <glib.h>
#if GLIB_CHECK_VERSION(2,6,0)
# include <glib/gstdio.h>
#else
# include <sys/types.h>
# include <sys/stat.h>
# define g_mkdir mkdir
#endif
/* Purple headers */
#include <plugin.h>
#include <version.h>
#include <blist.h>
#include <conversation.h>
#include <ft.h>
#include <request.h>
#include <notify.h>
#include <util.h>
#define PREF_PREFIX "/plugins/core/" PLUGIN_ID
#define PREF_PATH PREF_PREFIX "/path"
#define PREF_STRANGER PREF_PREFIX "/reject_stranger"
#define PREF_NOTIFY PREF_PREFIX "/notify"
typedef enum
{
FT_ASK,
FT_ACCEPT,
FT_REJECT
} AutoAcceptSetting;
static gboolean
ensure_path_exists(const char *dir)
{
if (!g_file_test(dir, G_FILE_TEST_IS_DIR))
{
if (purple_build_dir(dir, S_IRUSR | S_IWUSR | S_IXUSR))
return FALSE;
}
return TRUE;
}
static void
auto_accept_complete_cb(PurpleXfer *xfer, PurpleXfer *my)
{
if (xfer == my && purple_prefs_get_bool(PREF_NOTIFY) &&
!purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, xfer->who, xfer->account))
{
char *message = g_strdup_printf(_("Autoaccepted file transfer of \"%s\" from \"%s\" completed."),
xfer->filename, xfer->who);
purple_notify_info(NULL, _("Autoaccept complete"), message, NULL);
g_free(message);
}
}
static void
file_recv_request_cb(PurpleXfer *xfer, gpointer handle)
{
PurpleAccount *account;
PurpleBlistNode *node;
const char *pref;
char *filename;
char *dirname;
account = xfer->account;
node = (PurpleBlistNode *)purple_find_buddy(account, xfer->who);
if (!node)
{
if (purple_prefs_get_bool(PREF_STRANGER))
xfer->status = PURPLE_XFER_STATUS_CANCEL_LOCAL;
return;
}
node = node->parent;
g_return_if_fail(PURPLE_BLIST_NODE_IS_CONTACT(node));
pref = purple_prefs_get_string(PREF_PATH);
switch (purple_blist_node_get_int(node, "autoaccept"))
{
case FT_ASK:
break;
case FT_ACCEPT:
if (ensure_path_exists(pref))
{
int count = 1;
const char *escape;
dirname = g_build_filename(pref, purple_normalize(account, xfer->who), NULL);
if (!ensure_path_exists(dirname))
{
g_free(dirname);
break;
}
escape = purple_escape_filename(xfer->filename);
filename = g_build_filename(dirname, escape, NULL);
/* Make sure the file doesn't exist. Do we want some better checking than this? */
while (g_file_test(filename, G_FILE_TEST_EXISTS)) {
char *file = g_strdup_printf("%s-%d", escape, count++);
g_free(filename);
filename = g_build_filename(dirname, file, NULL);
g_free(file);
}
purple_xfer_request_accepted(xfer, filename);
g_free(dirname);
g_free(filename);
}
purple_signal_connect(purple_xfers_get_handle(), "file-recv-complete", handle,
PURPLE_CALLBACK(auto_accept_complete_cb), xfer);
break;
case FT_REJECT:
xfer->status = PURPLE_XFER_STATUS_CANCEL_LOCAL;
break;
}
}
static void
save_cb(PurpleBlistNode *node, int choice)
{
if (PURPLE_BLIST_NODE_IS_BUDDY(node))
node = node->parent;
g_return_if_fail(PURPLE_BLIST_NODE_IS_CONTACT(node));
purple_blist_node_set_int(node, "autoaccept", choice);
}
static void
set_auto_accept_settings(PurpleBlistNode *node, gpointer plugin)
{
char *message;
if (PURPLE_BLIST_NODE_IS_BUDDY(node))
node = node->parent;
g_return_if_fail(PURPLE_BLIST_NODE_IS_CONTACT(node));
message = g_strdup_printf(_("When a file-transfer request arrives from %s"),
purple_contact_get_alias((PurpleContact *)node));
purple_request_choice(plugin, _("Set Autoaccept Setting"), message,
NULL, purple_blist_node_get_int(node, "autoaccept"),
_("_Save"), G_CALLBACK(save_cb),
_("_Cancel"), NULL,
NULL, NULL, NULL,
node,
_("Ask"), FT_ASK,
_("Auto Accept"), FT_ACCEPT,
_("Auto Reject"), FT_REJECT,
NULL, purple_contact_get_alias((PurpleContact *)node), NULL,
NULL);
g_free(message);
}
static void
context_menu(PurpleBlistNode *node, GList **menu, gpointer plugin)
{
PurpleMenuAction *action;
if (!PURPLE_BLIST_NODE_IS_BUDDY(node) && !PURPLE_BLIST_NODE_IS_CONTACT(node) &&
!(purple_blist_node_get_flags(node) & PURPLE_BLIST_NODE_FLAG_NO_SAVE))
return;
action = purple_menu_action_new(_("Autoaccept File Transfers..."),
PURPLE_CALLBACK(set_auto_accept_settings), plugin, NULL);
(*menu) = g_list_prepend(*menu, action);
}
static gboolean
plugin_load(PurplePlugin *plugin)
{
purple_signal_connect(purple_xfers_get_handle(), "file-recv-request", plugin,
PURPLE_CALLBACK(file_recv_request_cb), plugin);
purple_signal_connect(purple_blist_get_handle(), "blist-node-extended-menu", plugin,
PURPLE_CALLBACK(context_menu), plugin);
return TRUE;
}
static gboolean
plugin_unload(PurplePlugin *plugin)
{
return TRUE;
}
static PurplePluginPrefFrame *
get_plugin_pref_frame(PurplePlugin *plugin)
{
PurplePluginPrefFrame *frame;
PurplePluginPref *pref;
frame = purple_plugin_pref_frame_new();
/* XXX: Is there a better way than this? There really should be. */
pref = purple_plugin_pref_new_with_name_and_label(PREF_PATH, _("Path to save the files in\n"
"(Please provide the full path)"));
purple_plugin_pref_frame_add(frame, pref);
pref = purple_plugin_pref_new_with_name_and_label(PREF_STRANGER,
_("Automatically reject from users not in buddy list"));
purple_plugin_pref_frame_add(frame, pref);
pref = purple_plugin_pref_new_with_name_and_label(PREF_NOTIFY,
_("Notify with a popup when an autoaccepted file transfer is complete\n"
"(only when there's no conversation with the sender)"));
purple_plugin_pref_frame_add(frame, pref);
return frame;
}
static PurplePluginUiInfo prefs_info = {
get_plugin_pref_frame,
0,
NULL,
/* padding */
NULL,
NULL,
NULL,
NULL
};
static PurplePluginInfo info = {
PURPLE_PLUGIN_MAGIC, /* Magic */
PURPLE_MAJOR_VERSION, /* Purple Major Version */
PURPLE_MINOR_VERSION, /* Purple Minor Version */
PURPLE_PLUGIN_STANDARD, /* plugin type */
NULL, /* ui requirement */
0, /* flags */
NULL, /* dependencies */
PURPLE_PRIORITY_DEFAULT, /* priority */
PLUGIN_ID, /* plugin id */
PLUGIN_NAME, /* name */
DISPLAY_VERSION, /* version */
PLUGIN_SUMMARY, /* summary */
PLUGIN_DESCRIPTION, /* description */
PLUGIN_AUTHOR, /* author */
PURPLE_WEBSITE, /* website */
plugin_load, /* load */
plugin_unload, /* unload */
NULL, /* destroy */
NULL, /* ui_info */
NULL, /* extra_info */
&prefs_info, /* prefs_info */
NULL, /* actions */
/* padding */
NULL,
NULL,
NULL,
NULL
};
static void
init_plugin(PurplePlugin *plugin) {
char *dirname;
dirname = g_build_filename(purple_user_dir(), "autoaccept", NULL);
purple_prefs_add_none(PREF_PREFIX);
purple_prefs_add_string(PREF_PATH, dirname);
purple_prefs_add_bool(PREF_STRANGER, TRUE);
purple_prefs_add_bool(PREF_NOTIFY, TRUE);
g_free(dirname);
}
PURPLE_INIT_PLUGIN(PLUGIN_STATIC_NAME, init_plugin, info)
|