comparison libpurple/plugins/autoaccept.c @ 15374:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children 32c366eeeb99
comparison
equal deleted inserted replaced
15373:f79e0f4df793 15374:5fe8042783c1
1 /*
2 * Autoaccept - Auto-accept file transfers from selected users
3 * Copyright (C) 2006
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 * 02111-1307, USA.
19 */
20 #include "internal.h"
21
22 #define PLUGIN_ID "core-plugin_pack-autoaccept"
23 #define PLUGIN_NAME N_("Autoaccept")
24 #define PLUGIN_STATIC_NAME "Autoaccept"
25 #define PLUGIN_SUMMARY N_("Auto-accept file transfer requests from selected users.")
26 #define PLUGIN_DESCRIPTION N_("Auto-accept file transfer requests from selected users.")
27 #define PLUGIN_AUTHOR "Sadrul H Chowdhury <sadrul@users.sourceforge.net>"
28
29 /* System headers */
30 #include <glib.h>
31 #if GLIB_CHECK_VERSION(2,6,0)
32 # include <glib/gstdio.h>
33 #else
34 # include <sys/types.h>
35 # include <sys/stat.h>
36 # define g_mkdir mkdir
37 #endif
38
39 /* Gaim headers */
40 #include <plugin.h>
41 #include <version.h>
42
43 #include <blist.h>
44 #include <conversation.h>
45 #include <ft.h>
46 #include <request.h>
47 #include <notify.h>
48 #include <util.h>
49
50 #define PREF_PREFIX "/plugins/core/" PLUGIN_ID
51 #define PREF_PATH PREF_PREFIX "/path"
52 #define PREF_STRANGER PREF_PREFIX "/reject_stranger"
53 #define PREF_NOTIFY PREF_PREFIX "/notify"
54
55 typedef enum
56 {
57 FT_ASK,
58 FT_ACCEPT,
59 FT_REJECT
60 } AutoAcceptSetting;
61
62 static gboolean
63 ensure_path_exists(const char *dir)
64 {
65 if (!g_file_test(dir, G_FILE_TEST_IS_DIR))
66 {
67 if (gaim_build_dir(dir, S_IRUSR | S_IWUSR | S_IXUSR))
68 return FALSE;
69 }
70
71 return TRUE;
72 }
73
74 static void
75 auto_accept_complete_cb(GaimXfer *xfer, GaimXfer *my)
76 {
77 if (xfer == my && gaim_prefs_get_bool(PREF_NOTIFY) &&
78 !gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, xfer->who, xfer->account))
79 {
80 char *message = g_strdup_printf(_("Autoaccepted file transfer of \"%s\" from \"%s\" completed."),
81 xfer->filename, xfer->who);
82 gaim_notify_info(NULL, _("Autoaccept complete"), message, NULL);
83 g_free(message);
84 }
85 }
86
87 static void
88 file_recv_request_cb(GaimXfer *xfer, gpointer handle)
89 {
90 GaimAccount *account;
91 GaimBlistNode *node;
92 const char *pref;
93 char *filename;
94 char *dirname;
95
96 account = xfer->account;
97 node = (GaimBlistNode *)gaim_find_buddy(account, xfer->who);
98
99 if (!node)
100 {
101 if (gaim_prefs_get_bool(PREF_STRANGER))
102 xfer->status = GAIM_XFER_STATUS_CANCEL_LOCAL;
103 return;
104 }
105
106 node = node->parent;
107 g_return_if_fail(GAIM_BLIST_NODE_IS_CONTACT(node));
108
109 pref = gaim_prefs_get_string(PREF_PATH);
110 switch (gaim_blist_node_get_int(node, "autoaccept"))
111 {
112 case FT_ASK:
113 break;
114 case FT_ACCEPT:
115 if (ensure_path_exists(pref))
116 {
117 dirname = g_build_filename(pref, xfer->who, NULL);
118
119 if (!ensure_path_exists(dirname))
120 {
121 g_free(dirname);
122 break;
123 }
124
125 filename = g_build_filename(dirname, xfer->filename, NULL);
126
127 gaim_xfer_request_accepted(xfer, filename);
128
129 g_free(dirname);
130 g_free(filename);
131 }
132
133 gaim_signal_connect(gaim_xfers_get_handle(), "file-recv-complete", handle,
134 GAIM_CALLBACK(auto_accept_complete_cb), xfer);
135 break;
136 case FT_REJECT:
137 xfer->status = GAIM_XFER_STATUS_CANCEL_LOCAL;
138 break;
139 }
140 }
141
142 static void
143 save_cb(GaimBlistNode *node, int choice)
144 {
145 if (GAIM_BLIST_NODE_IS_BUDDY(node))
146 node = node->parent;
147 g_return_if_fail(GAIM_BLIST_NODE_IS_CONTACT(node));
148 gaim_blist_node_set_int(node, "autoaccept", choice);
149 }
150
151 static void
152 set_auto_accept_settings(GaimBlistNode *node, gpointer plugin)
153 {
154 char *message;
155
156 if (GAIM_BLIST_NODE_IS_BUDDY(node))
157 node = node->parent;
158 g_return_if_fail(GAIM_BLIST_NODE_IS_CONTACT(node));
159
160 message = g_strdup_printf(_("When a file-transfer request arrives from %s"),
161 gaim_contact_get_alias((GaimContact *)node));
162 gaim_request_choice(plugin, _("Set Autoaccept Setting"), message,
163 NULL, gaim_blist_node_get_int(node, "autoaccept"),
164 _("_Save"), G_CALLBACK(save_cb),
165 _("_Cancel"), NULL, node,
166 _("Ask"), FT_ASK,
167 _("Auto Accept"), FT_ACCEPT,
168 _("Auto Reject"), FT_REJECT,
169 NULL);
170 g_free(message);
171 }
172
173 static void
174 context_menu(GaimBlistNode *node, GList **menu, gpointer plugin)
175 {
176 GaimMenuAction *action;
177
178 if (!GAIM_BLIST_NODE_IS_BUDDY(node) && !GAIM_BLIST_NODE_IS_CONTACT(node))
179 return;
180
181 action = gaim_menu_action_new(_("Autoaccept File Transfers..."),
182 GAIM_CALLBACK(set_auto_accept_settings), plugin, NULL);
183 (*menu) = g_list_prepend(*menu, action);
184 }
185
186 static gboolean
187 plugin_load(GaimPlugin *plugin)
188 {
189 gaim_signal_connect(gaim_xfers_get_handle(), "file-recv-request", plugin,
190 GAIM_CALLBACK(file_recv_request_cb), plugin);
191 gaim_signal_connect(gaim_blist_get_handle(), "blist-node-extended-menu", plugin,
192 GAIM_CALLBACK(context_menu), plugin);
193 return TRUE;
194 }
195
196 static gboolean
197 plugin_unload(GaimPlugin *plugin)
198 {
199 return TRUE;
200 }
201
202 static GaimPluginPrefFrame *
203 get_plugin_pref_frame(GaimPlugin *plugin)
204 {
205 GaimPluginPrefFrame *frame;
206 GaimPluginPref *pref;
207
208 frame = gaim_plugin_pref_frame_new();
209
210 /* XXX: Is there a better way than this? There really should be. */
211 pref = gaim_plugin_pref_new_with_name_and_label(PREF_PATH, _("Path to save the files in\n"
212 "(Please provide the full path)"));
213 gaim_plugin_pref_frame_add(frame, pref);
214
215 pref = gaim_plugin_pref_new_with_name_and_label(PREF_STRANGER,
216 _("Automatically reject from users not in buddy list"));
217 gaim_plugin_pref_frame_add(frame, pref);
218
219 pref = gaim_plugin_pref_new_with_name_and_label(PREF_NOTIFY,
220 _("Notify with a popup when an autoaccepted file transfer is complete\n"
221 "(only when there's no conversation with the sender)"));
222 gaim_plugin_pref_frame_add(frame, pref);
223
224 return frame;
225 }
226
227 static GaimPluginUiInfo prefs_info = {
228 get_plugin_pref_frame,
229 0,
230 NULL,
231 };
232
233 static GaimPluginInfo info = {
234 GAIM_PLUGIN_MAGIC, /* Magic */
235 GAIM_MAJOR_VERSION, /* Gaim Major Version */
236 GAIM_MINOR_VERSION, /* Gaim Minor Version */
237 GAIM_PLUGIN_STANDARD, /* plugin type */
238 NULL, /* ui requirement */
239 0, /* flags */
240 NULL, /* dependencies */
241 GAIM_PRIORITY_DEFAULT, /* priority */
242
243 PLUGIN_ID, /* plugin id */
244 PLUGIN_NAME, /* name */
245 VERSION, /* version */
246 PLUGIN_SUMMARY, /* summary */
247 PLUGIN_DESCRIPTION, /* description */
248 PLUGIN_AUTHOR, /* author */
249 GAIM_WEBSITE, /* website */
250
251 plugin_load, /* load */
252 plugin_unload, /* unload */
253 NULL, /* destroy */
254
255 NULL, /* ui_info */
256 NULL, /* extra_info */
257 &prefs_info, /* prefs_info */
258 NULL /* actions */
259 };
260
261 static void
262 init_plugin(GaimPlugin *plugin) {
263 char *dirname;
264
265 dirname = g_build_filename(gaim_user_dir(), "autoaccept", NULL);
266 gaim_prefs_add_none(PREF_PREFIX);
267 gaim_prefs_add_string(PREF_PATH, dirname);
268 gaim_prefs_add_bool(PREF_STRANGER, TRUE);
269 gaim_prefs_add_bool(PREF_NOTIFY, TRUE);
270 g_free(dirname);
271 }
272
273 GAIM_INIT_PLUGIN(PLUGIN_STATIC_NAME, init_plugin, info)