Push public IM aliases as XMPP roster names and vCard full names.
[thrasher.git] / thrasher.c
blob186719b1ac7649316b740fc18c50912c8b1d34fa
1 /*
2 * Thrasher Bird - XMPP transport via libpurple
3 * Copyright (C) 2008 Barracuda Networks, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Thrasher Bird; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
19 #include <glib.h>
20 #include <signal.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/socket.h>
25 #include "purple.h"
26 #include "thrasher.h"
27 #include "thblist.h"
28 #include "thconnection.h"
29 #include "thperl.h"
30 #include "thft.h"
31 #include "threquest.h"
32 #include "thconversations.h"
34 GHashTable *account_to_connection = NULL;
35 GHashTable *jid_to_account = NULL;
37 void debug_print(PurpleDebugLevel level, const char *category, const char *arg_s);
38 gboolean is_enabled(PurpleDebugLevel level, const char *category);
40 static PurpleEventLoopUiOps glib_eventloops =
42 (guint(*)(guint, GSourceFunc, gpointer)) thrasher_wrapper_set_timeout_add,
43 (gboolean(*)(guint)) thrasher_wrapper_call_source_remove,
44 ((guint(*)(int, PurpleInputCondition, PurpleInputFunction, gpointer))
45 thrasher_wrapper_set_input_add),
46 (gboolean(*)(guint))thrasher_wrapper_call_source_remove,
47 NULL,
48 NULL,
50 /* padding */
51 NULL,
52 NULL,
53 NULL
55 static PurpleCoreUiOps thrasher_core_uiops =
57 NULL,
58 NULL,
59 NULL,
60 NULL,
62 /* padding */
63 NULL,
64 NULL,
65 NULL,
66 NULL
70 static PurpleConnectionUiOps thrasher_conn_uiops =
72 // connect progress
73 thrasher_connection_update_progress,
74 // connected,
75 thrasher_connection,
76 // disconnected
77 NULL,
78 // connection-specific notices, effectively unused
79 NULL,
80 // report disconnect
81 NULL,
82 // computer network connected
83 NULL,
84 // computer network disconnected
85 NULL,
86 // report disconnect reason
87 NULL,
88 // reserved 1
89 NULL,
90 // reserved 2
91 NULL,
92 // reserved 3
93 NULL
96 void thrasher_init()
98 purple_core_set_ui_ops(&thrasher_core_uiops);
99 purple_eventloop_set_ui_ops(&glib_eventloops);
100 purple_connections_set_ui_ops(&thrasher_conn_uiops);
102 if (!purple_core_init(UI_ID))
103 error(EXIT_FAILURE, 0, "libpurple init failed");
105 thrasher_blist_init();
106 thrasher_connection_init();
107 thrasher_xfer_init();
108 thrasher_perl_init();
109 thrasher_request_init();
110 thrasher_conversations_init();
112 // This hash table has no ownership on the pointers stored in it
113 account_to_connection = g_hash_table_new(g_direct_hash,
114 g_direct_equal);
115 // Automatically cleans up keys, does not own stored pointers
116 jid_to_account = g_hash_table_new_full(g_str_hash, g_str_equal,
117 g_free, NULL);
119 purple_set_blist(purple_blist_new());
121 purple_signal_connect(purple_connections_get_handle(),
122 "signing-off",
123 thrasher_connection_get_handle(),
124 PURPLE_CALLBACK(thrasher_signing_off_cb),
125 NULL);
128 void thrasher_whoami (PurpleAccount * pa)
130 if (pa)
131 purple_debug_info("thrasher",
132 "You are user [%s]\n",
133 pa->username);
136 void thrasher_signing_off_cb(PurpleConnection *gc) {
137 g_return_if_fail(gc);
139 PurpleAccount* pa = purple_connection_get_account(gc);
140 g_return_if_fail(pa);
142 if (g_hash_table_lookup(account_to_connection, pa)) {
143 char *jid = thrasher_account_get_jid(pa);
144 g_hash_table_remove(jid_to_account, jid);
145 g_hash_table_remove(account_to_connection, pa);
149 void
150 thrasher_logout (PurpleAccount *pa)
152 g_return_if_fail(pa);
154 purple_account_set_enabled(pa, UI_ID, FALSE);
156 // Only disconnect if we haven't already
157 if (g_hash_table_lookup(account_to_connection, pa)) {
158 purple_account_disconnect(pa);
159 } else {
160 purple_debug_info("thrasher",
161 "Declined to log out unconnected account\n");
164 if (pa->ui_data) {
165 Thrasher_PurpleAccount_UI_Data* ui_data = pa->ui_data;
166 pa->ui_data = NULL;
167 g_hash_table_destroy(ui_data->pending_send_file);
168 g_free(ui_data);
172 PurpleAccount * thrasher_get_account_by_jid(const char* jid) {
173 g_return_val_if_fail(jid, NULL);
174 return g_hash_table_lookup(jid_to_account, jid);
177 PurpleConnection * thrasher_get_connection_by_account
178 (const PurpleAccount* account) {
179 g_return_val_if_fail(account, NULL);
180 return g_hash_table_lookup(account_to_connection, account);
183 // An error can occur during the login, when the protocol decides
184 // there's going to be a connection error before it even returns the
185 // connection to us. In that case, our user data won't exist on the
186 // connection, but the connection error still needs to propagate back
187 // to the user. In that case, we use this value. This rather ties
188 // us to a single thread.
190 static char *current_login_jid = NULL;
191 static uint got_error = 0;
193 char * get_current_login_jid () {
194 return current_login_jid;
197 void set_got_error (uint new_val) {
198 got_error = new_val;
202 static void
203 thrasher_prefs_init() {
204 purple_prefs_load();
206 /* Disable all logging to disk because it might leave files under
207 * USER_ROOT_DIR open.
209 purple_prefs_set_bool("/purple/logging/log_ims", FALSE);
210 purple_prefs_set_bool("/purple/logging/log_chats", FALSE);
211 purple_prefs_set_bool("/purple/logging/log_system", FALSE);
214 PurpleAccount *
215 thrasher_login (char *service, char *username, char *password, char *jid,
216 GHashTable *other_args)
218 got_error = 0;
219 current_login_jid = jid;
220 gchar *user_dir;
222 if (purple_get_core() == 0)
223 error(EXIT_FAILURE, 0, "Purple core is uninitialized");
225 purple_debug_info("thrasher",
226 "Thrasher login initiated\n");
228 /* Test service for validity */
230 /* Test user for validity */
231 /* We need to verify there is NO path redirection in the user name! */
232 /* We also need to verify username is NOT longer than MAX_NAME_LEN */
234 /* Test password for validity */
237 /* Validate the root_dir and build us a filename */
238 user_dir = g_build_filename(USER_ROOT_DIR, service, username, NULL);
240 purple_util_set_user_dir(user_dir);
242 g_free(user_dir);
244 PurpleAccount *account;
245 PurpleSavedStatus *status;
247 /* Need to research these... */
248 purple_blist_load();
249 thrasher_prefs_init();
250 purple_pounces_load(); /* Don't believe we need this */
252 /* Setup the account and throw it back */
253 account = purple_account_new(username, service);
254 g_hash_table_insert(jid_to_account, g_strdup(jid), account);
255 purple_account_set_password(account, password);
257 Thrasher_PurpleAccount_UI_Data* ui_data
258 = g_new(Thrasher_PurpleAccount_UI_Data, 1);
259 ui_data->jid = g_strdup(jid); /* Store the JID with the account */
260 ui_data->pending_send_file = g_hash_table_new(g_str_hash,
261 g_str_equal);
262 account->ui_data = ui_data;
264 if (other_args) {
265 GHashTableIter iter;
266 gpointer key, value;
267 char *char_key, *char_value;
269 g_hash_table_iter_init(&iter, other_args);
270 while (g_hash_table_iter_next(&iter, &key, &value)) {
271 char_value = (char *)value;
272 char_key = (char *)key;
273 if (!strncmp(key, "int_", 4))
275 char_key += 4;
276 int int_value = atoi(char_value);
277 purple_debug_info("thrasher",
278 "Setting account #: %s -> %d\n",
279 char_key, int_value);
280 purple_account_set_int(account, char_key, atoi(char_value));
281 } else if (!strncmp(key, "bool_", 5)) {
282 char_key += 5;
283 int value = (*char_value == '0' ||
284 *char_value == 0) ? FALSE: TRUE;
285 purple_debug_info("thrasher",
286 "Setting account bool: %s -> %d\n",
287 char_key, value);
288 purple_account_set_bool(account, char_key, value);
289 } else {
290 purple_debug_info("thrasher",
291 "Setting account string: %s -> %s\n",
292 char_key, char_value);
293 purple_account_set_string(account, char_key,
294 char_value);
299 purple_account_set_enabled(account, UI_ID, TRUE);
300 status = purple_savedstatus_new(NULL, PURPLE_STATUS_AVAILABLE);
301 purple_savedstatus_activate(status);
303 current_login_jid = NULL;
305 if (got_error) {
306 purple_debug_info("thrasher",
307 "got an error during initial login\n");
308 return NULL;
311 #ifdef TH_DEBUG
312 purple_debug_info("thrasher",
313 "Using libpurple v%s\n",
314 purple_core_get_version());
315 purple_debug_info("thrasher",
316 "Supported protocols\n");
317 GList *iter; int i;
318 iter = purple_plugins_get_protocols();
319 for (i = 0; iter; iter = iter->next) {
320 PurplePlugin *plugin = iter->data;
321 PurplePluginInfo *info = plugin->info;
322 if (info && info->name) {
323 purple_debug_info("thrasher",
324 "\t%d: %s\n",
325 i++,
326 info->name);
329 #endif
332 /* Not sure how I got in this state, but write a catch nonetheless */
333 if (!purple_plugins_get_protocols())
334 error(EXIT_FAILURE, 0, "libpurple reports NO supported protocols, this is a BAD thing");
336 return account;
340 * How to send message to remote user
342 * pa = PurpleAccount ptr
343 * name = recipient name char*
344 * message = message char*
346 * If the conversation data can be created this returns TRUE, else it returns FALSE
348 * Get the conversation if it already exists
349 * (Per the code flow, we may be able to set type to NULL and let it figure it out.)
352 gboolean
353 thrasher_send_msg (PurpleAccount *pa, const char *name, const char *message)
355 PurpleConversation *conv = thrasher_find_conversation_with_account(pa,
356 name);
357 PurpleConvIm *im;
359 /* Get the IM specific data */
360 im = purple_conversation_get_im_data(conv);
362 if (im) {
363 purple_conv_im_send(im, message);
364 return TRUE;
367 purple_debug_info("thrasher",
368 "Failed in purple_conv_im_send\n");
370 return FALSE;
373 void thrasher_connection (PurpleConnection *conn)
375 PurpleAccount *account = purple_connection_get_account(conn);
377 g_hash_table_insert(account_to_connection, account, conn);
379 thrasher_wrapper_connection(thrasher_account_get_jid(account));
382 PurpleConnection* thrasher_connection_for_account(PurpleAccount
383 *account) {
384 g_return_val_if_fail(account, NULL);
386 return g_hash_table_lookup(account_to_connection, account);
390 * thrasher_account_get_jid
392 gchar*
393 thrasher_account_get_jid(PurpleAccount *account)
395 g_return_val_if_fail(account, NULL);
397 Thrasher_PurpleAccount_UI_Data* ui_data = account->ui_data;
398 return ui_data->jid;
402 * thrasher_account_get_pending_send_file
404 GHashTable*
405 thrasher_account_get_pending_send_file(PurpleAccount *account) {
406 g_return_val_if_fail(account, NULL);
408 Thrasher_PurpleAccount_UI_Data* ui_data = account->ui_data;
409 g_return_val_if_fail(ui_data, NULL);
410 return ui_data->pending_send_file;
413 gboolean
414 thrasher_account_buddy_is_authorized(PurpleBuddy *buddy) {
415 /* FIXME: There's no cross-prpl call for this? Get tricky. */
416 g_return_val_if_fail(buddy, 1);
417 PurpleAccount* account = purple_buddy_get_account(buddy);
418 g_return_val_if_fail(account, 1);
420 const char *prpl_id = purple_account_get_protocol_id(account);
421 g_return_val_if_fail(prpl_id, 1);
422 PurplePlugin *prpl = purple_find_prpl(prpl_id);
423 g_return_val_if_fail(prpl, 1);
425 PurplePluginProtocolInfo *prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);
426 g_return_val_if_fail(prpl_info, 1);
428 if (prpl_info->list_emblem) {
429 const char *emblem_name = prpl_info->list_emblem(buddy);
430 if (emblem_name && 0 == strcmp(emblem_name, "not-authorized")) {
431 return 0;
435 return 1;