Incoming statuses with characters 0x80-0xFF were double-escaped.
[thrasher.git] / thrasher.c
blob0b1396f18e76595ef8bff06077903111fdf8d1ea
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 void thrasher_signing_off_cb(PurpleConnection *gc);
42 static PurpleEventLoopUiOps glib_eventloops =
44 (guint(*)(guint, GSourceFunc, gpointer)) thrasher_wrapper_set_timeout_add,
45 (gboolean(*)(guint)) thrasher_wrapper_call_source_remove,
46 ((guint(*)(int, PurpleInputCondition, PurpleInputFunction, gpointer))
47 thrasher_wrapper_set_input_add),
48 (gboolean(*)(guint))thrasher_wrapper_call_source_remove,
49 NULL,
50 NULL,
52 /* padding */
53 NULL,
54 NULL,
55 NULL
57 static PurpleCoreUiOps thrasher_core_uiops =
59 NULL,
60 NULL,
61 NULL,
62 NULL,
64 /* padding */
65 NULL,
66 NULL,
67 NULL,
68 NULL
72 static PurpleConnectionUiOps thrasher_conn_uiops =
74 // connect progress
75 thrasher_connection_update_progress,
76 // connected,
77 thrasher_connection,
78 // disconnected
79 NULL,
80 // connection-specific notices, effectively unused
81 NULL,
82 // report disconnect
83 NULL,
84 // computer network connected
85 NULL,
86 // computer network disconnected
87 NULL,
88 // report disconnect reason
89 NULL,
90 // reserved 1
91 NULL,
92 // reserved 2
93 NULL,
94 // reserved 3
95 NULL
98 void thrasher_init()
100 purple_core_set_ui_ops(&thrasher_core_uiops);
101 purple_eventloop_set_ui_ops(&glib_eventloops);
102 purple_connections_set_ui_ops(&thrasher_conn_uiops);
104 if (!purple_core_init(UI_ID))
105 error(EXIT_FAILURE, 0, "libpurple init failed");
107 thrasher_blist_init();
108 thrasher_connection_init();
109 thrasher_xfer_init();
110 thrasher_perl_init();
111 thrasher_request_init();
112 thrasher_conversations_init();
114 // This hash table has no ownership on the pointers stored in it
115 account_to_connection = g_hash_table_new(g_direct_hash,
116 g_direct_equal);
117 // Automatically cleans up keys, does not own stored pointers
118 jid_to_account = g_hash_table_new_full(g_str_hash, g_str_equal,
119 g_free, NULL);
121 purple_set_blist(purple_blist_new());
123 purple_signal_connect(purple_connections_get_handle(),
124 "signing-off",
125 thrasher_connection_get_handle(),
126 PURPLE_CALLBACK(thrasher_signing_off_cb),
127 NULL);
130 void thrasher_whoami (PurpleAccount * pa)
132 if (pa)
133 purple_debug_info("thrasher",
134 "You are user [%s]\n",
135 pa->username);
138 static void
139 thrasher_signing_off_cb(PurpleConnection *gc) {
140 g_return_if_fail(gc);
142 PurpleAccount* pa = purple_connection_get_account(gc);
143 g_return_if_fail(pa);
145 if (g_hash_table_lookup(account_to_connection, pa)) {
146 char *jid = thrasher_account_get_jid(pa);
147 g_hash_table_remove(jid_to_account, jid);
148 g_hash_table_remove(account_to_connection, pa);
152 void
153 thrasher_logout (PurpleAccount *pa)
155 g_return_if_fail(pa);
157 purple_account_set_enabled(pa, UI_ID, FALSE);
159 // Only disconnect if we haven't already
160 if (g_hash_table_lookup(account_to_connection, pa)) {
161 purple_account_disconnect(pa);
162 } else {
163 purple_debug_info("thrasher",
164 "Declined to log out unconnected account\n");
167 if (pa->ui_data) {
168 Thrasher_PurpleAccount_UI_Data* ui_data = pa->ui_data;
169 pa->ui_data = NULL;
170 g_hash_table_destroy(ui_data->pending_send_file);
171 g_free(ui_data);
175 PurpleAccount * thrasher_get_account_by_jid(const char* jid) {
176 g_return_val_if_fail(jid, NULL);
177 return g_hash_table_lookup(jid_to_account, jid);
180 PurpleConnection * thrasher_get_connection_by_account
181 (const PurpleAccount* account) {
182 g_return_val_if_fail(account, NULL);
183 return g_hash_table_lookup(account_to_connection, account);
186 // An error can occur during the login, when the protocol decides
187 // there's going to be a connection error before it even returns the
188 // connection to us. In that case, our user data won't exist on the
189 // connection, but the connection error still needs to propagate back
190 // to the user. In that case, we use this value. This rather ties
191 // us to a single thread.
193 static char *current_login_jid = NULL;
194 static uint got_error = 0;
196 char * get_current_login_jid () {
197 return current_login_jid;
200 void set_got_error (uint new_val) {
201 got_error = new_val;
205 static void
206 thrasher_prefs_init() {
207 purple_prefs_load();
209 /* Disable all logging to disk because it might leave files under
210 * USER_ROOT_DIR open.
212 purple_prefs_set_bool("/purple/logging/log_ims", FALSE);
213 purple_prefs_set_bool("/purple/logging/log_chats", FALSE);
214 purple_prefs_set_bool("/purple/logging/log_system", FALSE);
217 PurpleAccount *
218 thrasher_login (char *service, char *username, char *password, char *jid,
219 GHashTable *other_args)
221 got_error = 0;
222 current_login_jid = jid;
223 gchar *user_dir;
225 if (purple_get_core() == 0)
226 error(EXIT_FAILURE, 0, "Purple core is uninitialized");
228 purple_debug_info("thrasher",
229 "Thrasher login initiated\n");
231 /* Test service for validity */
233 /* Test user for validity */
234 /* We need to verify there is NO path redirection in the user name! */
235 /* We also need to verify username is NOT longer than MAX_NAME_LEN */
237 /* Test password for validity */
240 /* Validate the root_dir and build us a filename */
241 user_dir = g_build_filename(USER_ROOT_DIR, service, username, NULL);
243 purple_util_set_user_dir(user_dir);
245 g_free(user_dir);
247 PurpleAccount *account;
248 PurpleSavedStatus *status;
250 /* Setup the account and throw it back */
251 account = purple_account_new(username, service);
252 g_hash_table_insert(jid_to_account, g_strdup(jid), account);
253 purple_account_set_password(account, password);
255 Thrasher_PurpleAccount_UI_Data* ui_data
256 = g_new(Thrasher_PurpleAccount_UI_Data, 1);
257 ui_data->jid = g_strdup(jid); /* Store the JID with the account */
258 ui_data->pending_send_file = g_hash_table_new(g_str_hash,
259 g_str_equal);
260 account->ui_data = ui_data;
262 if (other_args) {
263 GHashTableIter iter;
264 gpointer key, value;
265 char *char_key, *char_value;
267 g_hash_table_iter_init(&iter, other_args);
268 while (g_hash_table_iter_next(&iter, &key, &value)) {
269 char_value = (char *)value;
270 char_key = (char *)key;
271 if (!strncmp(key, "int_", 4))
273 char_key += 4;
274 int int_value = atoi(char_value);
275 purple_debug_info("thrasher",
276 "Setting account #: %s -> %d\n",
277 char_key, int_value);
278 purple_account_set_int(account, char_key, atoi(char_value));
279 } else if (!strncmp(key, "bool_", 5)) {
280 char_key += 5;
281 int value = (*char_value == '0' ||
282 *char_value == 0) ? FALSE: TRUE;
283 purple_debug_info("thrasher",
284 "Setting account bool: %s -> %d\n",
285 char_key, value);
286 purple_account_set_bool(account, char_key, value);
287 } else {
288 purple_debug_info("thrasher",
289 "Setting account string: %s -> %s\n",
290 char_key, char_value);
291 purple_account_set_string(account, char_key,
292 char_value);
297 purple_account_set_enabled(account, UI_ID, TRUE);
298 status = purple_savedstatus_new(NULL, PURPLE_STATUS_AVAILABLE);
299 purple_savedstatus_activate(status);
301 current_login_jid = NULL;
303 if (got_error) {
304 purple_debug_info("thrasher",
305 "got an error during initial login\n");
306 return NULL;
309 #ifdef TH_DEBUG
310 purple_debug_info("thrasher",
311 "Using libpurple v%s\n",
312 purple_core_get_version());
313 purple_debug_info("thrasher",
314 "Supported protocols\n");
315 GList *iter; int i;
316 iter = purple_plugins_get_protocols();
317 for (i = 0; iter; iter = iter->next) {
318 PurplePlugin *plugin = iter->data;
319 PurplePluginInfo *info = plugin->info;
320 if (info && info->name) {
321 purple_debug_info("thrasher",
322 "\t%d: %s\n",
323 i++,
324 info->name);
327 #endif
330 /* Not sure how I got in this state, but write a catch nonetheless */
331 if (!purple_plugins_get_protocols())
332 error(EXIT_FAILURE, 0, "libpurple reports NO supported protocols, this is a BAD thing");
334 return account;
338 * How to send message to remote user
340 * pa = PurpleAccount ptr
341 * name = recipient name char*
342 * message = message char*
344 * If the conversation data can be created this returns TRUE, else it returns FALSE
346 * Get the conversation if it already exists
347 * (Per the code flow, we may be able to set type to NULL and let it figure it out.)
350 gboolean
351 thrasher_send_msg (PurpleAccount *pa, const char *name, const char *message)
353 PurpleConversation *conv = thrasher_find_conversation_with_account(pa,
354 name);
355 PurpleConvIm *im;
357 /* Get the IM specific data */
358 im = purple_conversation_get_im_data(conv);
360 if (im) {
361 purple_conv_im_send(im, message);
362 return TRUE;
365 purple_debug_info("thrasher",
366 "Failed in purple_conv_im_send\n");
368 return FALSE;
371 void thrasher_connection (PurpleConnection *conn)
373 PurpleAccount *account = purple_connection_get_account(conn);
375 g_hash_table_insert(account_to_connection, account, conn);
377 thrasher_wrapper_connection(thrasher_account_get_jid(account));
380 PurpleConnection* thrasher_connection_for_account(PurpleAccount
381 *account) {
382 g_return_val_if_fail(account, NULL);
384 return g_hash_table_lookup(account_to_connection, account);
388 * thrasher_account_get_jid
390 gchar*
391 thrasher_account_get_jid(PurpleAccount *account)
393 g_return_val_if_fail(account, NULL);
395 Thrasher_PurpleAccount_UI_Data* ui_data = account->ui_data;
396 return ui_data->jid;
400 * thrasher_account_get_pending_send_file
402 GHashTable*
403 thrasher_account_get_pending_send_file(PurpleAccount *account) {
404 g_return_val_if_fail(account, NULL);
406 Thrasher_PurpleAccount_UI_Data* ui_data = account->ui_data;
407 g_return_val_if_fail(ui_data, NULL);
408 return ui_data->pending_send_file;
411 gboolean
412 thrasher_account_buddy_is_authorized(PurpleBuddy *buddy) {
413 /* FIXME: There's no cross-prpl call for this? Get tricky. */
414 g_return_val_if_fail(buddy, 1);
415 PurpleAccount* account = purple_buddy_get_account(buddy);
416 g_return_val_if_fail(account, 1);
418 const char *prpl_id = purple_account_get_protocol_id(account);
419 g_return_val_if_fail(prpl_id, 1);
420 PurplePlugin *prpl = purple_find_prpl(prpl_id);
421 g_return_val_if_fail(prpl, 1);
423 PurplePluginProtocolInfo *prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);
424 g_return_val_if_fail(prpl_info, 1);
426 if (prpl_info->list_emblem) {
427 const char *emblem_name = prpl_info->list_emblem(buddy);
428 if (emblem_name && 0 == strcmp(emblem_name, "not-authorized")) {
429 return 0;
433 return 1;