[796da03] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * libpurple module - Main file * |
---|
| 5 | * * |
---|
[0e788f5] | 6 | * Copyright 2009-2012 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
[796da03] | 7 | * * |
---|
| 8 | * This program is free software; you can redistribute it and/or modify * |
---|
| 9 | * it under the terms of the GNU General Public License as published by * |
---|
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
---|
| 11 | * (at your option) any later version. * |
---|
| 12 | * * |
---|
| 13 | * This program is distributed in the hope that it will be useful, * |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
| 16 | * GNU General Public License for more details. * |
---|
| 17 | * * |
---|
| 18 | * You should have received a copy of the GNU General Public License along * |
---|
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., * |
---|
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
---|
| 21 | * * |
---|
| 22 | \***************************************************************************/ |
---|
| 23 | |
---|
[b3117f2] | 24 | #include "bitlbee.h" |
---|
[d93c8beb] | 25 | #include "bpurple.h" |
---|
[e5d8d21] | 26 | #include "help.h" |
---|
[b3117f2] | 27 | |
---|
[0ac1a375] | 28 | #include <stdarg.h> |
---|
| 29 | |
---|
[796da03] | 30 | #include <glib.h> |
---|
| 31 | #include <purple.h> |
---|
| 32 | |
---|
| 33 | GSList *purple_connections; |
---|
| 34 | |
---|
[0ac1a375] | 35 | /* This makes me VERY sad... :-( But some libpurple callbacks come in without |
---|
| 36 | any context so this is the only way to get that. Don't want to support |
---|
| 37 | libpurple in daemon mode anyway. */ |
---|
[4aa0f6b] | 38 | static bee_t *local_bee; |
---|
[0ac1a375] | 39 | |
---|
[5ebff60] | 40 | static char *set_eval_display_name(set_t *set, char *value); |
---|
[f85e9d6] | 41 | |
---|
[6a48992] | 42 | void purple_request_input_callback(guint id, struct im_connection *ic, |
---|
| 43 | const char *message, const char *who); |
---|
| 44 | |
---|
| 45 | /* purple_request_input specific stuff */ |
---|
| 46 | typedef void (*ri_callback_t)(gpointer, const gchar *); |
---|
| 47 | |
---|
| 48 | struct request_input_data { |
---|
| 49 | ri_callback_t data_callback; |
---|
| 50 | void *user_data; |
---|
[2c5ab49] | 51 | struct im_connection *ic; |
---|
| 52 | char *buddy; |
---|
| 53 | guint id; |
---|
[6a48992] | 54 | }; |
---|
| 55 | |
---|
[5ebff60] | 56 | struct im_connection *purple_ic_by_pa(PurpleAccount *pa) |
---|
[860ba6a] | 57 | { |
---|
| 58 | GSList *i; |
---|
[d93c8beb] | 59 | struct purple_data *pd; |
---|
[5ebff60] | 60 | |
---|
| 61 | for (i = purple_connections; i; i = i->next) { |
---|
[d93c8beb] | 62 | pd = ((struct im_connection *) i->data)->proto_data; |
---|
| 63 | if (pd->account == pa) { |
---|
[860ba6a] | 64 | return i->data; |
---|
[5ebff60] | 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
[860ba6a] | 68 | return NULL; |
---|
| 69 | } |
---|
| 70 | |
---|
[5ebff60] | 71 | static struct im_connection *purple_ic_by_gc(PurpleConnection *gc) |
---|
[7da726b] | 72 | { |
---|
[5ebff60] | 73 | return purple_ic_by_pa(purple_connection_get_account(gc)); |
---|
[7da726b] | 74 | } |
---|
| 75 | |
---|
[5ebff60] | 76 | static gboolean purple_menu_cmp(const char *a, const char *b) |
---|
[8ad5c34] | 77 | { |
---|
[5ebff60] | 78 | while (*a && *b) { |
---|
| 79 | while (*a == '_') { |
---|
| 80 | a++; |
---|
| 81 | } |
---|
| 82 | while (*b == '_') { |
---|
| 83 | b++; |
---|
| 84 | } |
---|
| 85 | if (g_ascii_tolower(*a) != g_ascii_tolower(*b)) { |
---|
[8ad5c34] | 86 | return FALSE; |
---|
[5ebff60] | 87 | } |
---|
| 88 | |
---|
| 89 | a++; |
---|
| 90 | b++; |
---|
[8ad5c34] | 91 | } |
---|
[5ebff60] | 92 | |
---|
| 93 | return (*a == '\0' && *b == '\0'); |
---|
[8ad5c34] | 94 | } |
---|
| 95 | |
---|
[5ebff60] | 96 | static void purple_init(account_t *acc) |
---|
[796da03] | 97 | { |
---|
[5ebff60] | 98 | PurplePlugin *prpl = purple_plugins_find_with_id((char *) acc->prpl->data); |
---|
[0f7ee7e5] | 99 | PurplePluginProtocolInfo *pi = prpl->info->extra_info; |
---|
[52cae01] | 100 | PurpleAccount *pa; |
---|
| 101 | GList *i, *st; |
---|
[bab1c86] | 102 | set_t *s; |
---|
[7c5affca] | 103 | char help_title[64]; |
---|
| 104 | GString *help; |
---|
[3c9b095] | 105 | static gboolean dir_fixed = FALSE; |
---|
[5ebff60] | 106 | |
---|
[3c9b095] | 107 | /* Layer violation coming up: Making an exception for libpurple here. |
---|
| 108 | Dig in the IRC state a bit to get a username. Ideally we should |
---|
| 109 | check if s/he identified but this info doesn't seem *that* important. |
---|
| 110 | It's just that fecking libpurple can't *not* store this shit. |
---|
[5ebff60] | 111 | |
---|
[3c9b095] | 112 | Remember that libpurple is not really meant to be used on public |
---|
| 113 | servers anyway! */ |
---|
[5ebff60] | 114 | if (!dir_fixed) { |
---|
[e4c3041] | 115 | PurpleCertificatePool *pool; |
---|
[3c9b095] | 116 | irc_t *irc = acc->bee->ui_data; |
---|
| 117 | char *dir; |
---|
[5ebff60] | 118 | |
---|
| 119 | dir = g_strdup_printf("%s/purple/%s", global.conf->configdir, irc->user->nick); |
---|
| 120 | purple_util_set_user_dir(dir); |
---|
| 121 | g_free(dir); |
---|
| 122 | |
---|
[3c9b095] | 123 | purple_blist_load(); |
---|
| 124 | purple_prefs_load(); |
---|
[12f041d] | 125 | |
---|
| 126 | if (proxytype == PROXY_SOCKS4A) { |
---|
| 127 | /* do this here after loading prefs. yes, i know, it sucks */ |
---|
| 128 | purple_prefs_set_bool("/purple/proxy/socks4_remotedns", TRUE); |
---|
| 129 | } |
---|
| 130 | |
---|
[e4c3041] | 131 | /* re-create the certificate cache directory */ |
---|
| 132 | pool = purple_certificate_find_pool("x509", "tls_peers"); |
---|
| 133 | dir = purple_certificate_pool_mkpath(pool, NULL); |
---|
| 134 | purple_build_dir(dir, 0700); |
---|
| 135 | |
---|
[3c9b095] | 136 | dir_fixed = TRUE; |
---|
| 137 | } |
---|
[5ebff60] | 138 | |
---|
| 139 | help = g_string_new(""); |
---|
| 140 | g_string_printf(help, "BitlBee libpurple module %s (%s).\n\nSupported settings:", |
---|
| 141 | (char *) acc->prpl->name, prpl->info->name); |
---|
| 142 | |
---|
| 143 | if (pi->user_splits) { |
---|
[eb4c81a] | 144 | GList *l; |
---|
[5ebff60] | 145 | g_string_append_printf(help, "\n* username: Username"); |
---|
| 146 | for (l = pi->user_splits; l; l = l->next) { |
---|
| 147 | g_string_append_printf(help, "%c%s", |
---|
| 148 | purple_account_user_split_get_separator(l->data), |
---|
| 149 | purple_account_user_split_get_text(l->data)); |
---|
| 150 | } |
---|
[eb4c81a] | 151 | } |
---|
[5ebff60] | 152 | |
---|
[52cae01] | 153 | /* Convert all protocol_options into per-account setting variables. */ |
---|
[5ebff60] | 154 | for (i = pi->protocol_options; i; i = i->next) { |
---|
[0f7ee7e5] | 155 | PurpleAccountOption *o = i->data; |
---|
| 156 | const char *name; |
---|
| 157 | char *def = NULL; |
---|
| 158 | set_eval eval = NULL; |
---|
[4dc6b8d] | 159 | void *eval_data = NULL; |
---|
| 160 | GList *io = NULL; |
---|
| 161 | GSList *opts = NULL; |
---|
[5ebff60] | 162 | |
---|
| 163 | name = purple_account_option_get_setting(o); |
---|
| 164 | |
---|
| 165 | switch (purple_account_option_get_type(o)) { |
---|
[0f7ee7e5] | 166 | case PURPLE_PREF_STRING: |
---|
[5ebff60] | 167 | def = g_strdup(purple_account_option_get_default_string(o)); |
---|
| 168 | |
---|
| 169 | g_string_append_printf(help, "\n* %s (%s), %s, default: %s", |
---|
| 170 | name, purple_account_option_get_text(o), |
---|
| 171 | "string", def); |
---|
| 172 | |
---|
[0f7ee7e5] | 173 | break; |
---|
[5ebff60] | 174 | |
---|
[0f7ee7e5] | 175 | case PURPLE_PREF_INT: |
---|
[5ebff60] | 176 | def = g_strdup_printf("%d", purple_account_option_get_default_int(o)); |
---|
[0f7ee7e5] | 177 | eval = set_eval_int; |
---|
[5ebff60] | 178 | |
---|
| 179 | g_string_append_printf(help, "\n* %s (%s), %s, default: %s", |
---|
| 180 | name, purple_account_option_get_text(o), |
---|
| 181 | "integer", def); |
---|
| 182 | |
---|
[0f7ee7e5] | 183 | break; |
---|
[5ebff60] | 184 | |
---|
[0f7ee7e5] | 185 | case PURPLE_PREF_BOOLEAN: |
---|
[5ebff60] | 186 | if (purple_account_option_get_default_bool(o)) { |
---|
| 187 | def = g_strdup("true"); |
---|
| 188 | } else { |
---|
| 189 | def = g_strdup("false"); |
---|
| 190 | } |
---|
[0f7ee7e5] | 191 | eval = set_eval_bool; |
---|
[5ebff60] | 192 | |
---|
| 193 | g_string_append_printf(help, "\n* %s (%s), %s, default: %s", |
---|
| 194 | name, purple_account_option_get_text(o), |
---|
| 195 | "boolean", def); |
---|
| 196 | |
---|
[0f7ee7e5] | 197 | break; |
---|
[5ebff60] | 198 | |
---|
[4dc6b8d] | 199 | case PURPLE_PREF_STRING_LIST: |
---|
[5ebff60] | 200 | def = g_strdup(purple_account_option_get_default_list_value(o)); |
---|
| 201 | |
---|
| 202 | g_string_append_printf(help, "\n* %s (%s), %s, default: %s", |
---|
| 203 | name, purple_account_option_get_text(o), |
---|
| 204 | "list", def); |
---|
| 205 | g_string_append(help, "\n Possible values: "); |
---|
| 206 | |
---|
| 207 | for (io = purple_account_option_get_list(o); io; io = io->next) { |
---|
[4dc6b8d] | 208 | PurpleKeyValuePair *kv = io->data; |
---|
[5ebff60] | 209 | opts = g_slist_append(opts, kv->value); |
---|
[c96c72f] | 210 | /* TODO: kv->value is not a char*, WTF? */ |
---|
[5ebff60] | 211 | if (strcmp(kv->value, kv->key) != 0) { |
---|
| 212 | g_string_append_printf(help, "%s (%s), ", (char *) kv->value, kv->key); |
---|
| 213 | } else { |
---|
| 214 | g_string_append_printf(help, "%s, ", (char *) kv->value); |
---|
| 215 | } |
---|
[4dc6b8d] | 216 | } |
---|
[5ebff60] | 217 | g_string_truncate(help, help->len - 2); |
---|
[4dc6b8d] | 218 | eval = set_eval_list; |
---|
| 219 | eval_data = opts; |
---|
[5ebff60] | 220 | |
---|
[4dc6b8d] | 221 | break; |
---|
[5ebff60] | 222 | |
---|
[0f7ee7e5] | 223 | default: |
---|
[4aa0f6b] | 224 | /** No way to talk to the user right now, invent one when |
---|
| 225 | this becomes important. |
---|
[e67e513] | 226 | irc_rootmsg( acc->irc, "Setting with unknown type: %s (%d) Expect stuff to break..\n", |
---|
[4dc6b8d] | 227 | name, purple_account_option_get_type( o ) ); |
---|
[4aa0f6b] | 228 | */ |
---|
[5ebff60] | 229 | g_string_append_printf(help, "\n* [%s] UNSUPPORTED (type %d)", |
---|
| 230 | name, purple_account_option_get_type(o)); |
---|
[b3117f2] | 231 | name = NULL; |
---|
[0f7ee7e5] | 232 | } |
---|
[5ebff60] | 233 | |
---|
| 234 | if (name != NULL) { |
---|
| 235 | s = set_add(&acc->set, name, def, eval, acc); |
---|
[0f7ee7e5] | 236 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[4dc6b8d] | 237 | s->eval_data = eval_data; |
---|
[5ebff60] | 238 | g_free(def); |
---|
[0f7ee7e5] | 239 | } |
---|
| 240 | } |
---|
[5ebff60] | 241 | |
---|
| 242 | g_snprintf(help_title, sizeof(help_title), "purple %s", (char *) acc->prpl->name); |
---|
| 243 | help_add_mem(&global.help, help_title, help->str); |
---|
| 244 | g_string_free(help, TRUE); |
---|
| 245 | |
---|
| 246 | s = set_add(&acc->set, "display_name", NULL, set_eval_display_name, acc); |
---|
[f85e9d6] | 247 | s->flags |= ACC_SET_ONLINE_ONLY; |
---|
[5ebff60] | 248 | |
---|
| 249 | if (pi->options & OPT_PROTO_MAIL_CHECK) { |
---|
| 250 | s = set_add(&acc->set, "mail_notifications", "false", set_eval_bool, acc); |
---|
[bab1c86] | 251 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[dd43c62] | 252 | |
---|
[b38f655] | 253 | s = set_add(&acc->set, "mail_notifications_handle", NULL, NULL, acc); |
---|
[dd43c62] | 254 | s->flags |= ACC_SET_OFFLINE_ONLY | SET_NULL_OK; |
---|
[bab1c86] | 255 | } |
---|
[5ebff60] | 256 | |
---|
| 257 | if (strcmp(prpl->info->name, "Gadu-Gadu") == 0) { |
---|
| 258 | s = set_add(&acc->set, "gg_sync_contacts", "true", set_eval_bool, acc); |
---|
| 259 | } |
---|
| 260 | |
---|
[52cae01] | 261 | /* Go through all away states to figure out if away/status messages |
---|
| 262 | are possible. */ |
---|
[5ebff60] | 263 | pa = purple_account_new(acc->user, (char *) acc->prpl->data); |
---|
| 264 | for (st = purple_account_get_status_types(pa); st; st = st->next) { |
---|
| 265 | PurpleStatusPrimitive prim = purple_status_type_get_primitive(st->data); |
---|
| 266 | |
---|
| 267 | if (prim == PURPLE_STATUS_AVAILABLE) { |
---|
| 268 | if (purple_status_type_get_attr(st->data, "message")) { |
---|
[52cae01] | 269 | acc->flags |= ACC_FLAG_STATUS_MESSAGE; |
---|
[5ebff60] | 270 | } |
---|
| 271 | } else if (prim != PURPLE_STATUS_OFFLINE) { |
---|
| 272 | if (purple_status_type_get_attr(st->data, "message")) { |
---|
[52cae01] | 273 | acc->flags |= ACC_FLAG_AWAY_MESSAGE; |
---|
[5ebff60] | 274 | } |
---|
[52cae01] | 275 | } |
---|
| 276 | } |
---|
[5ebff60] | 277 | purple_accounts_remove(pa); |
---|
[796da03] | 278 | } |
---|
| 279 | |
---|
[5ebff60] | 280 | static void purple_sync_settings(account_t *acc, PurpleAccount *pa) |
---|
[b74b287] | 281 | { |
---|
[5ebff60] | 282 | PurplePlugin *prpl = purple_plugins_find_with_id(pa->protocol_id); |
---|
[b74b287] | 283 | PurplePluginProtocolInfo *pi = prpl->info->extra_info; |
---|
| 284 | GList *i; |
---|
[5ebff60] | 285 | |
---|
| 286 | for (i = pi->protocol_options; i; i = i->next) { |
---|
[b74b287] | 287 | PurpleAccountOption *o = i->data; |
---|
| 288 | const char *name; |
---|
| 289 | set_t *s; |
---|
[5ebff60] | 290 | |
---|
| 291 | name = purple_account_option_get_setting(o); |
---|
| 292 | s = set_find(&acc->set, name); |
---|
| 293 | if (s->value == NULL) { |
---|
[b74b287] | 294 | continue; |
---|
[5ebff60] | 295 | } |
---|
| 296 | |
---|
| 297 | switch (purple_account_option_get_type(o)) { |
---|
[b74b287] | 298 | case PURPLE_PREF_STRING: |
---|
[4dc6b8d] | 299 | case PURPLE_PREF_STRING_LIST: |
---|
[5ebff60] | 300 | purple_account_set_string(pa, name, set_getstr(&acc->set, name)); |
---|
[b74b287] | 301 | break; |
---|
[5ebff60] | 302 | |
---|
[b74b287] | 303 | case PURPLE_PREF_INT: |
---|
[5ebff60] | 304 | purple_account_set_int(pa, name, set_getint(&acc->set, name)); |
---|
[b74b287] | 305 | break; |
---|
[5ebff60] | 306 | |
---|
[b74b287] | 307 | case PURPLE_PREF_BOOLEAN: |
---|
[5ebff60] | 308 | purple_account_set_bool(pa, name, set_getbool(&acc->set, name)); |
---|
[b74b287] | 309 | break; |
---|
[5ebff60] | 310 | |
---|
[b74b287] | 311 | default: |
---|
| 312 | break; |
---|
| 313 | } |
---|
| 314 | } |
---|
[5ebff60] | 315 | |
---|
| 316 | if (pi->options & OPT_PROTO_MAIL_CHECK) { |
---|
| 317 | purple_account_set_check_mail(pa, set_getbool(&acc->set, "mail_notifications")); |
---|
| 318 | } |
---|
[b74b287] | 319 | } |
---|
| 320 | |
---|
[5ebff60] | 321 | static void purple_login(account_t *acc) |
---|
[796da03] | 322 | { |
---|
[5ebff60] | 323 | struct im_connection *ic = imcb_new(acc); |
---|
[d93c8beb] | 324 | struct purple_data *pd; |
---|
[5ebff60] | 325 | |
---|
| 326 | if ((local_bee != NULL && local_bee != acc->bee) || |
---|
| 327 | (global.conf->runmode == RUNMODE_DAEMON && !getenv("BITLBEE_DEBUG"))) { |
---|
| 328 | imcb_error(ic, "Daemon mode detected. Do *not* try to use libpurple in daemon mode! " |
---|
| 329 | "Please use inetd or ForkDaemon mode instead."); |
---|
| 330 | imc_logout(ic, FALSE); |
---|
[6967d01] | 331 | return; |
---|
| 332 | } |
---|
[4aa0f6b] | 333 | local_bee = acc->bee; |
---|
[5ebff60] | 334 | |
---|
[796da03] | 335 | /* For now this is needed in the _connected() handlers if using |
---|
| 336 | GLib event handling, to make sure we're not handling events |
---|
| 337 | on dead connections. */ |
---|
[5ebff60] | 338 | purple_connections = g_slist_prepend(purple_connections, ic); |
---|
| 339 | |
---|
[d93c8beb] | 340 | ic->proto_data = pd = g_new0(struct purple_data, 1); |
---|
| 341 | pd->account = purple_account_new(acc->user, (char *) acc->prpl->data); |
---|
[6a48992] | 342 | pd->input_requests = g_hash_table_new_full(g_direct_hash, g_direct_equal, |
---|
| 343 | NULL, g_free); |
---|
| 344 | pd->next_request_id = 0; |
---|
[d93c8beb] | 345 | purple_account_set_password(pd->account, acc->pass); |
---|
| 346 | purple_sync_settings(acc, pd->account); |
---|
[5ebff60] | 347 | |
---|
[d93c8beb] | 348 | purple_account_set_enabled(pd->account, "BitlBee", TRUE); |
---|
[dd43c62] | 349 | |
---|
[b38f655] | 350 | if (set_getbool(&acc->set, "mail_notifications") && set_getstr(&acc->set, "mail_notifications_handle")) { |
---|
| 351 | imcb_add_buddy(ic, set_getstr(&acc->set, "mail_notifications_handle"), NULL); |
---|
[dd43c62] | 352 | } |
---|
[796da03] | 353 | } |
---|
| 354 | |
---|
[5ebff60] | 355 | static void purple_logout(struct im_connection *ic) |
---|
[796da03] | 356 | { |
---|
[d93c8beb] | 357 | struct purple_data *pd = ic->proto_data; |
---|
[5ebff60] | 358 | |
---|
[2dd23da] | 359 | if (!pd) { |
---|
| 360 | return; |
---|
| 361 | } |
---|
| 362 | |
---|
[d93c8beb] | 363 | purple_account_set_enabled(pd->account, "BitlBee", FALSE); |
---|
[5ebff60] | 364 | purple_connections = g_slist_remove(purple_connections, ic); |
---|
[d93c8beb] | 365 | purple_accounts_remove(pd->account); |
---|
[6a48992] | 366 | g_hash_table_destroy(pd->input_requests); |
---|
[d93c8beb] | 367 | g_free(pd); |
---|
[796da03] | 368 | } |
---|
| 369 | |
---|
[5ebff60] | 370 | static int purple_buddy_msg(struct im_connection *ic, char *who, char *message, int flags) |
---|
[796da03] | 371 | { |
---|
[389f7be] | 372 | PurpleConversation *conv; |
---|
[d93c8beb] | 373 | struct purple_data *pd = ic->proto_data; |
---|
[5ebff60] | 374 | |
---|
[6a48992] | 375 | if (!strncmp(who, PURPLE_REQUEST_HANDLE, sizeof(PURPLE_REQUEST_HANDLE) - 1)) { |
---|
| 376 | guint request_id = atoi(who + sizeof(PURPLE_REQUEST_HANDLE)); |
---|
| 377 | purple_request_input_callback(request_id, ic, message, who); |
---|
| 378 | return 1; |
---|
| 379 | } |
---|
| 380 | |
---|
[5ebff60] | 381 | if ((conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, |
---|
[d93c8beb] | 382 | who, pd->account)) == NULL) { |
---|
[5ebff60] | 383 | conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, |
---|
[d93c8beb] | 384 | pd->account, who); |
---|
[389f7be] | 385 | } |
---|
[5ebff60] | 386 | |
---|
| 387 | purple_conv_im_send(purple_conversation_get_im_data(conv), message); |
---|
| 388 | |
---|
[0cbef26] | 389 | return 1; |
---|
[796da03] | 390 | } |
---|
| 391 | |
---|
[5ebff60] | 392 | static GList *purple_away_states(struct im_connection *ic) |
---|
[796da03] | 393 | { |
---|
[d93c8beb] | 394 | struct purple_data *pd = ic->proto_data; |
---|
[ec5e57d] | 395 | GList *st, *ret = NULL; |
---|
[5ebff60] | 396 | |
---|
[d93c8beb] | 397 | for (st = purple_account_get_status_types(pd->account); st; st = st->next) { |
---|
[5ebff60] | 398 | PurpleStatusPrimitive prim = purple_status_type_get_primitive(st->data); |
---|
| 399 | if (prim != PURPLE_STATUS_AVAILABLE && prim != PURPLE_STATUS_OFFLINE) { |
---|
| 400 | ret = g_list_append(ret, (void *) purple_status_type_get_name(st->data)); |
---|
| 401 | } |
---|
[279607e] | 402 | } |
---|
[5ebff60] | 403 | |
---|
[ec5e57d] | 404 | return ret; |
---|
[796da03] | 405 | } |
---|
| 406 | |
---|
[5ebff60] | 407 | static void purple_set_away(struct im_connection *ic, char *state_txt, char *message) |
---|
[796da03] | 408 | { |
---|
[d93c8beb] | 409 | struct purple_data *pd = ic->proto_data; |
---|
| 410 | GList *status_types = purple_account_get_status_types(pd->account), *st; |
---|
[ec5e57d] | 411 | PurpleStatusType *pst = NULL; |
---|
[279607e] | 412 | GList *args = NULL; |
---|
[5ebff60] | 413 | |
---|
| 414 | for (st = status_types; st; st = st->next) { |
---|
[ec5e57d] | 415 | pst = st->data; |
---|
[5ebff60] | 416 | |
---|
| 417 | if (state_txt == NULL && |
---|
| 418 | purple_status_type_get_primitive(pst) == PURPLE_STATUS_AVAILABLE) { |
---|
[279607e] | 419 | break; |
---|
[5ebff60] | 420 | } |
---|
[279607e] | 421 | |
---|
[5ebff60] | 422 | if (state_txt != NULL && |
---|
| 423 | g_strcasecmp(state_txt, purple_status_type_get_name(pst)) == 0) { |
---|
[ec5e57d] | 424 | break; |
---|
[5ebff60] | 425 | } |
---|
[ec5e57d] | 426 | } |
---|
[5ebff60] | 427 | |
---|
| 428 | if (message && purple_status_type_get_attr(pst, "message")) { |
---|
| 429 | args = g_list_append(args, "message"); |
---|
| 430 | args = g_list_append(args, message); |
---|
[279607e] | 431 | } |
---|
| 432 | |
---|
[d93c8beb] | 433 | purple_account_set_status_list(pd->account, |
---|
| 434 | st ? purple_status_type_get_id(pst) : "away", |
---|
[5ebff60] | 435 | TRUE, args); |
---|
| 436 | |
---|
| 437 | g_list_free(args); |
---|
[796da03] | 438 | } |
---|
| 439 | |
---|
[5ebff60] | 440 | static char *set_eval_display_name(set_t *set, char *value) |
---|
[f85e9d6] | 441 | { |
---|
| 442 | account_t *acc = set->data; |
---|
| 443 | struct im_connection *ic = acc->ic; |
---|
[5ebff60] | 444 | |
---|
| 445 | if (ic) { |
---|
| 446 | imcb_log(ic, "Changing display_name not currently supported with libpurple!"); |
---|
| 447 | } |
---|
| 448 | |
---|
[f85e9d6] | 449 | return NULL; |
---|
| 450 | } |
---|
| 451 | |
---|
[694be84] | 452 | /* Bad bad gadu-gadu, not saving buddy list by itself */ |
---|
[5ebff60] | 453 | static void purple_gg_buddylist_export(PurpleConnection *gc) |
---|
[694be84] | 454 | { |
---|
[5ebff60] | 455 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
| 456 | |
---|
| 457 | if (set_getstr(&ic->acc->set, "gg_sync_contacts")) { |
---|
| 458 | GList *actions = gc->prpl->info->actions(gc->prpl, gc); |
---|
[694be84] | 459 | GList *p; |
---|
[5ebff60] | 460 | for (p = g_list_first(actions); p; p = p->next) { |
---|
| 461 | if (((PurplePluginAction *) p->data) && |
---|
| 462 | purple_menu_cmp(((PurplePluginAction *) p->data)->label, |
---|
| 463 | "Upload buddylist to Server") == 0) { |
---|
[694be84] | 464 | PurplePluginAction action; |
---|
| 465 | action.plugin = gc->prpl; |
---|
| 466 | action.context = gc; |
---|
| 467 | action.user_data = NULL; |
---|
[5ebff60] | 468 | ((PurplePluginAction *) p->data)->callback(&action); |
---|
[694be84] | 469 | break; |
---|
| 470 | } |
---|
| 471 | } |
---|
[5ebff60] | 472 | g_list_free(actions); |
---|
[694be84] | 473 | } |
---|
| 474 | } |
---|
| 475 | |
---|
[5ebff60] | 476 | static void purple_gg_buddylist_import(PurpleConnection *gc) |
---|
[694be84] | 477 | { |
---|
[5ebff60] | 478 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
| 479 | |
---|
| 480 | if (set_getstr(&ic->acc->set, "gg_sync_contacts")) { |
---|
| 481 | GList *actions = gc->prpl->info->actions(gc->prpl, gc); |
---|
[694be84] | 482 | GList *p; |
---|
[5ebff60] | 483 | for (p = g_list_first(actions); p; p = p->next) { |
---|
| 484 | if (((PurplePluginAction *) p->data) && |
---|
| 485 | purple_menu_cmp(((PurplePluginAction *) p->data)->label, |
---|
| 486 | "Download buddylist from Server") == 0) { |
---|
[694be84] | 487 | PurplePluginAction action; |
---|
| 488 | action.plugin = gc->prpl; |
---|
| 489 | action.context = gc; |
---|
| 490 | action.user_data = NULL; |
---|
[5ebff60] | 491 | ((PurplePluginAction *) p->data)->callback(&action); |
---|
[694be84] | 492 | break; |
---|
| 493 | } |
---|
| 494 | } |
---|
[5ebff60] | 495 | g_list_free(actions); |
---|
[694be84] | 496 | } |
---|
| 497 | } |
---|
| 498 | |
---|
[5ebff60] | 499 | static void purple_add_buddy(struct im_connection *ic, char *who, char *group) |
---|
[796da03] | 500 | { |
---|
[b3117f2] | 501 | PurpleBuddy *pb; |
---|
[3e59c8d] | 502 | PurpleGroup *pg = NULL; |
---|
[d93c8beb] | 503 | struct purple_data *pd = ic->proto_data; |
---|
[5ebff60] | 504 | |
---|
| 505 | if (group && !(pg = purple_find_group(group))) { |
---|
| 506 | pg = purple_group_new(group); |
---|
| 507 | purple_blist_add_group(pg, NULL); |
---|
[3e59c8d] | 508 | } |
---|
[694be84] | 509 | |
---|
[d93c8beb] | 510 | pb = purple_buddy_new(pd->account, who, NULL); |
---|
[5ebff60] | 511 | purple_blist_add_buddy(pb, NULL, pg, NULL); |
---|
[d93c8beb] | 512 | purple_account_add_buddy(pd->account, pb); |
---|
[5ebff60] | 513 | |
---|
[d93c8beb] | 514 | purple_gg_buddylist_export(pd->account->gc); |
---|
[796da03] | 515 | } |
---|
| 516 | |
---|
[5ebff60] | 517 | static void purple_remove_buddy(struct im_connection *ic, char *who, char *group) |
---|
[796da03] | 518 | { |
---|
[b3117f2] | 519 | PurpleBuddy *pb; |
---|
[d93c8beb] | 520 | struct purple_data *pd = ic->proto_data; |
---|
[5ebff60] | 521 | |
---|
[d93c8beb] | 522 | pb = purple_find_buddy(pd->account, who); |
---|
[5ebff60] | 523 | if (pb != NULL) { |
---|
[a91550c] | 524 | PurpleGroup *group; |
---|
[5ebff60] | 525 | |
---|
| 526 | group = purple_buddy_get_group(pb); |
---|
[d93c8beb] | 527 | purple_account_remove_buddy(pd->account, pb, group); |
---|
[5ebff60] | 528 | |
---|
| 529 | purple_blist_remove_buddy(pb); |
---|
[b3117f2] | 530 | } |
---|
[694be84] | 531 | |
---|
[d93c8beb] | 532 | purple_gg_buddylist_export(pd->account->gc); |
---|
[796da03] | 533 | } |
---|
| 534 | |
---|
[5ebff60] | 535 | static void purple_add_permit(struct im_connection *ic, char *who) |
---|
[05a8932] | 536 | { |
---|
[d93c8beb] | 537 | struct purple_data *pd = ic->proto_data; |
---|
[5ebff60] | 538 | |
---|
[d93c8beb] | 539 | purple_privacy_permit_add(pd->account, who, FALSE); |
---|
[05a8932] | 540 | } |
---|
| 541 | |
---|
[5ebff60] | 542 | static void purple_add_deny(struct im_connection *ic, char *who) |
---|
[05a8932] | 543 | { |
---|
[d93c8beb] | 544 | struct purple_data *pd = ic->proto_data; |
---|
[5ebff60] | 545 | |
---|
[d93c8beb] | 546 | purple_privacy_deny_add(pd->account, who, FALSE); |
---|
[05a8932] | 547 | } |
---|
| 548 | |
---|
[5ebff60] | 549 | static void purple_rem_permit(struct im_connection *ic, char *who) |
---|
[05a8932] | 550 | { |
---|
[d93c8beb] | 551 | struct purple_data *pd = ic->proto_data; |
---|
[5ebff60] | 552 | |
---|
[d93c8beb] | 553 | purple_privacy_permit_remove(pd->account, who, FALSE); |
---|
[05a8932] | 554 | } |
---|
| 555 | |
---|
[5ebff60] | 556 | static void purple_rem_deny(struct im_connection *ic, char *who) |
---|
[05a8932] | 557 | { |
---|
[d93c8beb] | 558 | struct purple_data *pd = ic->proto_data; |
---|
[5ebff60] | 559 | |
---|
[d93c8beb] | 560 | purple_privacy_deny_remove(pd->account, who, FALSE); |
---|
[05a8932] | 561 | } |
---|
| 562 | |
---|
[5ebff60] | 563 | static void purple_get_info(struct im_connection *ic, char *who) |
---|
[e77c264] | 564 | { |
---|
[d93c8beb] | 565 | struct purple_data *pd = ic->proto_data; |
---|
| 566 | |
---|
| 567 | serv_get_info(purple_account_get_connection(pd->account), who); |
---|
[e77c264] | 568 | } |
---|
| 569 | |
---|
[5ebff60] | 570 | static void purple_keepalive(struct im_connection *ic) |
---|
[796da03] | 571 | { |
---|
| 572 | } |
---|
| 573 | |
---|
[5ebff60] | 574 | static int purple_send_typing(struct im_connection *ic, char *who, int flags) |
---|
[796da03] | 575 | { |
---|
[487f555] | 576 | PurpleTypingState state = PURPLE_NOT_TYPING; |
---|
[d93c8beb] | 577 | struct purple_data *pd = ic->proto_data; |
---|
[5ebff60] | 578 | |
---|
| 579 | if (flags & OPT_TYPING) { |
---|
[487f555] | 580 | state = PURPLE_TYPING; |
---|
[5ebff60] | 581 | } else if (flags & OPT_THINKING) { |
---|
[487f555] | 582 | state = PURPLE_TYPED; |
---|
[5ebff60] | 583 | } |
---|
| 584 | |
---|
[d93c8beb] | 585 | serv_send_typing(purple_account_get_connection(pd->account), who, state); |
---|
[5ebff60] | 586 | |
---|
[bad41f56] | 587 | return 1; |
---|
[796da03] | 588 | } |
---|
| 589 | |
---|
[5ebff60] | 590 | static void purple_chat_msg(struct groupchat *gc, char *message, int flags) |
---|
[f485008] | 591 | { |
---|
| 592 | PurpleConversation *pc = gc->data; |
---|
[5ebff60] | 593 | |
---|
| 594 | purple_conv_chat_send(purple_conversation_get_chat_data(pc), message); |
---|
[f485008] | 595 | } |
---|
| 596 | |
---|
[5ebff60] | 597 | struct groupchat *purple_chat_with(struct im_connection *ic, char *who) |
---|
[8ad5c34] | 598 | { |
---|
| 599 | /* No, "of course" this won't work this way. Or in fact, it almost |
---|
| 600 | does, but it only lets you send msgs to it, you won't receive |
---|
| 601 | any. Instead, we have to click the virtual menu item. |
---|
| 602 | PurpleAccount *pa = ic->proto_data; |
---|
| 603 | PurpleConversation *pc; |
---|
| 604 | PurpleConvChat *pcc; |
---|
| 605 | struct groupchat *gc; |
---|
[5ebff60] | 606 | |
---|
[8ad5c34] | 607 | gc = imcb_chat_new( ic, "BitlBee-libpurple groupchat" ); |
---|
| 608 | gc->data = pc = purple_conversation_new( PURPLE_CONV_TYPE_CHAT, pa, "BitlBee-libpurple groupchat" ); |
---|
| 609 | pc->ui_data = gc; |
---|
[5ebff60] | 610 | |
---|
[8ad5c34] | 611 | pcc = PURPLE_CONV_CHAT( pc ); |
---|
| 612 | purple_conv_chat_add_user( pcc, ic->acc->user, "", 0, TRUE ); |
---|
| 613 | purple_conv_chat_invite_user( pcc, who, "Please join my chat", FALSE ); |
---|
| 614 | //purple_conv_chat_add_user( pcc, who, "", 0, TRUE ); |
---|
| 615 | */ |
---|
[5ebff60] | 616 | |
---|
[8ad5c34] | 617 | /* There went my nice afternoon. :-( */ |
---|
[5ebff60] | 618 | |
---|
[d93c8beb] | 619 | struct purple_data *pd = ic->proto_data; |
---|
| 620 | PurplePlugin *prpl = purple_plugins_find_with_id(pd->account->protocol_id); |
---|
[8ad5c34] | 621 | PurplePluginProtocolInfo *pi = prpl->info->extra_info; |
---|
[d93c8beb] | 622 | PurpleBuddy *pb = purple_find_buddy(pd->account, who); |
---|
[8ad5c34] | 623 | PurpleMenuAction *mi; |
---|
| 624 | GList *menu; |
---|
[5ebff60] | 625 | |
---|
[8ad5c34] | 626 | void (*callback)(PurpleBlistNode *, gpointer); /* FFFFFFFFFFFFFUUUUUUUUUUUUUU */ |
---|
[5ebff60] | 627 | |
---|
| 628 | if (!pb || !pi || !pi->blist_node_menu) { |
---|
[8ad5c34] | 629 | return NULL; |
---|
[5ebff60] | 630 | } |
---|
| 631 | |
---|
| 632 | menu = pi->blist_node_menu(&pb->node); |
---|
| 633 | while (menu) { |
---|
[8ad5c34] | 634 | mi = menu->data; |
---|
[5ebff60] | 635 | if (purple_menu_cmp(mi->label, "initiate chat") || |
---|
| 636 | purple_menu_cmp(mi->label, "initiate conference")) { |
---|
[8ad5c34] | 637 | break; |
---|
[5ebff60] | 638 | } |
---|
[8ad5c34] | 639 | menu = menu->next; |
---|
| 640 | } |
---|
[5ebff60] | 641 | |
---|
| 642 | if (menu == NULL) { |
---|
[8ad5c34] | 643 | return NULL; |
---|
[5ebff60] | 644 | } |
---|
| 645 | |
---|
[8ad5c34] | 646 | /* Call the fucker. */ |
---|
[5ebff60] | 647 | callback = (void *) mi->callback; |
---|
| 648 | callback(&pb->node, menu->data); |
---|
| 649 | |
---|
[8ad5c34] | 650 | return NULL; |
---|
| 651 | } |
---|
| 652 | |
---|
[5ebff60] | 653 | void purple_chat_invite(struct groupchat *gc, char *who, char *message) |
---|
[8ad5c34] | 654 | { |
---|
| 655 | PurpleConversation *pc = gc->data; |
---|
[5ebff60] | 656 | PurpleConvChat *pcc = PURPLE_CONV_CHAT(pc); |
---|
[d93c8beb] | 657 | struct purple_data *pd = gc->ic->proto_data; |
---|
[5ebff60] | 658 | |
---|
[d93c8beb] | 659 | serv_chat_invite(purple_account_get_connection(pd->account), |
---|
[5ebff60] | 660 | purple_conv_chat_get_id(pcc), |
---|
| 661 | message && *message ? message : "Please join my chat", |
---|
| 662 | who); |
---|
[8ad5c34] | 663 | } |
---|
| 664 | |
---|
[5ebff60] | 665 | void purple_chat_kick(struct groupchat *gc, char *who, const char *message) |
---|
[e41cc40] | 666 | { |
---|
| 667 | PurpleConversation *pc = gc->data; |
---|
[5ebff60] | 668 | char *str = g_strdup_printf("kick %s %s", who, message); |
---|
| 669 | |
---|
| 670 | purple_conversation_do_command(pc, str, NULL, NULL); |
---|
| 671 | g_free(str); |
---|
[e41cc40] | 672 | } |
---|
| 673 | |
---|
[5ebff60] | 674 | void purple_chat_leave(struct groupchat *gc) |
---|
[15794dc] | 675 | { |
---|
| 676 | PurpleConversation *pc = gc->data; |
---|
[5ebff60] | 677 | |
---|
| 678 | purple_conversation_destroy(pc); |
---|
[15794dc] | 679 | } |
---|
| 680 | |
---|
[5ebff60] | 681 | struct groupchat *purple_chat_join(struct im_connection *ic, const char *room, const char *nick, const char *password, |
---|
| 682 | set_t **sets) |
---|
[c3caa46] | 683 | { |
---|
[d93c8beb] | 684 | struct purple_data *pd = ic->proto_data; |
---|
| 685 | PurplePlugin *prpl = purple_plugins_find_with_id(pd->account->protocol_id); |
---|
[c3caa46] | 686 | PurplePluginProtocolInfo *pi = prpl->info->extra_info; |
---|
| 687 | GHashTable *chat_hash; |
---|
| 688 | PurpleConversation *conv; |
---|
| 689 | GList *info, *l; |
---|
[5ebff60] | 690 | |
---|
| 691 | if (!pi->chat_info || !pi->chat_info_defaults || |
---|
[d93c8beb] | 692 | !(info = pi->chat_info(purple_account_get_connection(pd->account)))) { |
---|
[5ebff60] | 693 | imcb_error(ic, "Joining chatrooms not supported by this protocol"); |
---|
[c3caa46] | 694 | return NULL; |
---|
| 695 | } |
---|
[5ebff60] | 696 | |
---|
[d93c8beb] | 697 | if ((conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT, |
---|
| 698 | room, pd->account))) { |
---|
[5ebff60] | 699 | purple_conversation_destroy(conv); |
---|
| 700 | } |
---|
| 701 | |
---|
[d93c8beb] | 702 | chat_hash = pi->chat_info_defaults( |
---|
| 703 | purple_account_get_connection(pd->account), room |
---|
| 704 | ); |
---|
[5ebff60] | 705 | |
---|
| 706 | for (l = info; l; l = l->next) { |
---|
[c3caa46] | 707 | struct proto_chat_entry *pce = l->data; |
---|
[5ebff60] | 708 | |
---|
| 709 | if (strcmp(pce->identifier, "handle") == 0) { |
---|
| 710 | g_hash_table_replace(chat_hash, "handle", g_strdup(nick)); |
---|
| 711 | } else if (strcmp(pce->identifier, "password") == 0) { |
---|
| 712 | g_hash_table_replace(chat_hash, "password", g_strdup(password)); |
---|
| 713 | } else if (strcmp(pce->identifier, "passwd") == 0) { |
---|
| 714 | g_hash_table_replace(chat_hash, "passwd", g_strdup(password)); |
---|
| 715 | } |
---|
[c3caa46] | 716 | } |
---|
[5ebff60] | 717 | |
---|
[d93c8beb] | 718 | serv_join_chat(purple_account_get_connection(pd->account), chat_hash); |
---|
[5ebff60] | 719 | |
---|
[c3caa46] | 720 | return NULL; |
---|
| 721 | } |
---|
| 722 | |
---|
[5ebff60] | 723 | void purple_transfer_request(struct im_connection *ic, file_transfer_t *ft, char *handle); |
---|
[edfc6db] | 724 | |
---|
[860ba6a] | 725 | static void purple_ui_init(); |
---|
| 726 | |
---|
[dca8eff] | 727 | GHashTable *prplcb_ui_info() |
---|
| 728 | { |
---|
| 729 | static GHashTable *ret; |
---|
[5ebff60] | 730 | |
---|
| 731 | if (ret == NULL) { |
---|
[dca8eff] | 732 | ret = g_hash_table_new(g_str_hash, g_str_equal); |
---|
[5ebff60] | 733 | g_hash_table_insert(ret, "name", "BitlBee"); |
---|
| 734 | g_hash_table_insert(ret, "version", BITLBEE_VERSION); |
---|
[dca8eff] | 735 | } |
---|
[5ebff60] | 736 | |
---|
[dca8eff] | 737 | return ret; |
---|
| 738 | } |
---|
| 739 | |
---|
[5ebff60] | 740 | static PurpleCoreUiOps bee_core_uiops = |
---|
[860ba6a] | 741 | { |
---|
| 742 | NULL, |
---|
| 743 | NULL, |
---|
| 744 | purple_ui_init, |
---|
| 745 | NULL, |
---|
[dca8eff] | 746 | prplcb_ui_info, |
---|
[860ba6a] | 747 | }; |
---|
| 748 | |
---|
[5ebff60] | 749 | static void prplcb_conn_progress(PurpleConnection *gc, const char *text, size_t step, size_t step_count) |
---|
[860ba6a] | 750 | { |
---|
[5ebff60] | 751 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
| 752 | |
---|
| 753 | imcb_log(ic, "%s", text); |
---|
[860ba6a] | 754 | } |
---|
| 755 | |
---|
[5ebff60] | 756 | static void prplcb_conn_connected(PurpleConnection *gc) |
---|
[860ba6a] | 757 | { |
---|
[5ebff60] | 758 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
[f85e9d6] | 759 | const char *dn; |
---|
| 760 | set_t *s; |
---|
[5ebff60] | 761 | |
---|
| 762 | imcb_connected(ic); |
---|
| 763 | |
---|
| 764 | if ((dn = purple_connection_get_display_name(gc)) && |
---|
| 765 | (s = set_find(&ic->acc->set, "display_name"))) { |
---|
| 766 | g_free(s->value); |
---|
| 767 | s->value = g_strdup(dn); |
---|
[f85e9d6] | 768 | } |
---|
[694be84] | 769 | |
---|
| 770 | // user list needs to be requested for Gadu-Gadu |
---|
[5ebff60] | 771 | purple_gg_buddylist_import(gc); |
---|
| 772 | |
---|
| 773 | if (gc->flags & PURPLE_CONNECTION_HTML) { |
---|
[d250b2a] | 774 | ic->flags |= OPT_DOES_HTML; |
---|
[5ebff60] | 775 | } |
---|
[860ba6a] | 776 | } |
---|
| 777 | |
---|
[5ebff60] | 778 | static void prplcb_conn_disconnected(PurpleConnection *gc) |
---|
[860ba6a] | 779 | { |
---|
[5ebff60] | 780 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
| 781 | |
---|
| 782 | if (ic != NULL) { |
---|
| 783 | imc_logout(ic, !gc->wants_to_die); |
---|
[b74b287] | 784 | } |
---|
[860ba6a] | 785 | } |
---|
| 786 | |
---|
[5ebff60] | 787 | static void prplcb_conn_notice(PurpleConnection *gc, const char *text) |
---|
[860ba6a] | 788 | { |
---|
[5ebff60] | 789 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
| 790 | |
---|
| 791 | if (ic != NULL) { |
---|
| 792 | imcb_log(ic, "%s", text); |
---|
| 793 | } |
---|
[860ba6a] | 794 | } |
---|
| 795 | |
---|
[5ebff60] | 796 | static void prplcb_conn_report_disconnect_reason(PurpleConnection *gc, PurpleConnectionError reason, const char *text) |
---|
[860ba6a] | 797 | { |
---|
[5ebff60] | 798 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
| 799 | |
---|
[860ba6a] | 800 | /* PURPLE_CONNECTION_ERROR_NAME_IN_USE means concurrent login, |
---|
| 801 | should probably handle that. */ |
---|
[5ebff60] | 802 | if (ic != NULL) { |
---|
| 803 | imcb_error(ic, "%s", text); |
---|
| 804 | } |
---|
[860ba6a] | 805 | } |
---|
| 806 | |
---|
| 807 | static PurpleConnectionUiOps bee_conn_uiops = |
---|
| 808 | { |
---|
| 809 | prplcb_conn_progress, |
---|
| 810 | prplcb_conn_connected, |
---|
| 811 | prplcb_conn_disconnected, |
---|
| 812 | prplcb_conn_notice, |
---|
| 813 | NULL, |
---|
| 814 | NULL, |
---|
| 815 | NULL, |
---|
| 816 | prplcb_conn_report_disconnect_reason, |
---|
| 817 | }; |
---|
| 818 | |
---|
[5ebff60] | 819 | static void prplcb_blist_update(PurpleBuddyList *list, PurpleBlistNode *node) |
---|
[7da726b] | 820 | { |
---|
[5ebff60] | 821 | if (node->type == PURPLE_BLIST_BUDDY_NODE) { |
---|
| 822 | PurpleBuddy *bud = (PurpleBuddy *) node; |
---|
| 823 | PurpleGroup *group = purple_buddy_get_group(bud); |
---|
| 824 | struct im_connection *ic = purple_ic_by_pa(bud->account); |
---|
[4f103ea] | 825 | PurpleStatus *as; |
---|
| 826 | int flags = 0; |
---|
[5ebff60] | 827 | |
---|
| 828 | if (ic == NULL) { |
---|
[db4cd40] | 829 | return; |
---|
[5ebff60] | 830 | } |
---|
| 831 | |
---|
| 832 | if (bud->server_alias) { |
---|
| 833 | imcb_rename_buddy(ic, bud->name, bud->server_alias); |
---|
| 834 | } else if (bud->alias) { |
---|
| 835 | imcb_rename_buddy(ic, bud->name, bud->alias); |
---|
| 836 | } |
---|
| 837 | |
---|
| 838 | if (group) { |
---|
| 839 | imcb_add_buddy(ic, bud->name, purple_group_get_name(group)); |
---|
| 840 | } |
---|
| 841 | |
---|
| 842 | flags |= purple_presence_is_online(bud->presence) ? OPT_LOGGED_IN : 0; |
---|
| 843 | flags |= purple_presence_is_available(bud->presence) ? 0 : OPT_AWAY; |
---|
| 844 | |
---|
| 845 | as = purple_presence_get_active_status(bud->presence); |
---|
| 846 | |
---|
| 847 | imcb_buddy_status(ic, bud->name, flags, purple_status_get_name(as), |
---|
| 848 | purple_status_get_attr_string(as, "message")); |
---|
| 849 | |
---|
| 850 | imcb_buddy_times(ic, bud->name, |
---|
| 851 | purple_presence_get_login_time(bud->presence), |
---|
| 852 | purple_presence_get_idle_time(bud->presence)); |
---|
[7da726b] | 853 | } |
---|
| 854 | } |
---|
| 855 | |
---|
[5ebff60] | 856 | static void prplcb_blist_new(PurpleBlistNode *node) |
---|
[a08e875] | 857 | { |
---|
[5ebff60] | 858 | if (node->type == PURPLE_BLIST_BUDDY_NODE) { |
---|
| 859 | PurpleBuddy *bud = (PurpleBuddy *) node; |
---|
| 860 | struct im_connection *ic = purple_ic_by_pa(bud->account); |
---|
| 861 | |
---|
| 862 | if (ic == NULL) { |
---|
[a08e875] | 863 | return; |
---|
[5ebff60] | 864 | } |
---|
| 865 | |
---|
| 866 | imcb_add_buddy(ic, bud->name, NULL); |
---|
| 867 | |
---|
| 868 | prplcb_blist_update(NULL, node); |
---|
[a08e875] | 869 | } |
---|
| 870 | } |
---|
| 871 | |
---|
[5ebff60] | 872 | static void prplcb_blist_remove(PurpleBuddyList *list, PurpleBlistNode *node) |
---|
[7da726b] | 873 | { |
---|
[a91550c] | 874 | /* |
---|
[5ebff60] | 875 | PurpleBuddy *bud = (PurpleBuddy*) node; |
---|
| 876 | |
---|
| 877 | if( node->type == PURPLE_BLIST_BUDDY_NODE ) |
---|
| 878 | { |
---|
| 879 | struct im_connection *ic = purple_ic_by_pa( bud->account ); |
---|
| 880 | |
---|
| 881 | if( ic == NULL ) |
---|
| 882 | return; |
---|
| 883 | |
---|
| 884 | imcb_remove_buddy( ic, bud->name, NULL ); |
---|
| 885 | } |
---|
[a91550c] | 886 | */ |
---|
[7da726b] | 887 | } |
---|
| 888 | |
---|
| 889 | static PurpleBlistUiOps bee_blist_uiops = |
---|
| 890 | { |
---|
| 891 | NULL, |
---|
| 892 | prplcb_blist_new, |
---|
| 893 | NULL, |
---|
| 894 | prplcb_blist_update, |
---|
| 895 | prplcb_blist_remove, |
---|
| 896 | }; |
---|
| 897 | |
---|
[5ebff60] | 898 | void prplcb_conv_new(PurpleConversation *conv) |
---|
[f485008] | 899 | { |
---|
[5ebff60] | 900 | if (conv->type == PURPLE_CONV_TYPE_CHAT) { |
---|
| 901 | struct im_connection *ic = purple_ic_by_pa(conv->account); |
---|
[f485008] | 902 | struct groupchat *gc; |
---|
[5ebff60] | 903 | |
---|
| 904 | gc = imcb_chat_new(ic, conv->name); |
---|
| 905 | if (conv->title != NULL) { |
---|
| 906 | imcb_chat_name_hint(gc, conv->title); |
---|
| 907 | imcb_chat_topic(gc, NULL, conv->title, 0); |
---|
[fd213fe] | 908 | } |
---|
[36ee8c6] | 909 | |
---|
[f485008] | 910 | conv->ui_data = gc; |
---|
| 911 | gc->data = conv; |
---|
[5ebff60] | 912 | |
---|
[c3caa46] | 913 | /* libpurple brokenness: Whatever. Show that we join right away, |
---|
| 914 | there's no clear "This is you!" signaling in _add_users so |
---|
| 915 | don't even try. */ |
---|
[5ebff60] | 916 | imcb_chat_add_buddy(gc, gc->ic->acc->user); |
---|
[f485008] | 917 | } |
---|
| 918 | } |
---|
| 919 | |
---|
[5ebff60] | 920 | void prplcb_conv_free(PurpleConversation *conv) |
---|
[f485008] | 921 | { |
---|
| 922 | struct groupchat *gc = conv->ui_data; |
---|
[5ebff60] | 923 | |
---|
| 924 | imcb_chat_free(gc); |
---|
[f485008] | 925 | } |
---|
| 926 | |
---|
[5ebff60] | 927 | void prplcb_conv_add_users(PurpleConversation *conv, GList *cbuddies, gboolean new_arrivals) |
---|
[f485008] | 928 | { |
---|
| 929 | struct groupchat *gc = conv->ui_data; |
---|
| 930 | GList *b; |
---|
[5ebff60] | 931 | |
---|
| 932 | for (b = cbuddies; b; b = b->next) { |
---|
[f485008] | 933 | PurpleConvChatBuddy *pcb = b->data; |
---|
[5ebff60] | 934 | |
---|
| 935 | imcb_chat_add_buddy(gc, pcb->name); |
---|
[f485008] | 936 | } |
---|
| 937 | } |
---|
| 938 | |
---|
[5ebff60] | 939 | void prplcb_conv_del_users(PurpleConversation *conv, GList *cbuddies) |
---|
[f485008] | 940 | { |
---|
| 941 | struct groupchat *gc = conv->ui_data; |
---|
| 942 | GList *b; |
---|
[5ebff60] | 943 | |
---|
| 944 | for (b = cbuddies; b; b = b->next) { |
---|
| 945 | imcb_chat_remove_buddy(gc, b->data, ""); |
---|
| 946 | } |
---|
[f485008] | 947 | } |
---|
| 948 | |
---|
[5ebff60] | 949 | void prplcb_conv_chat_msg(PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, |
---|
| 950 | time_t mtime) |
---|
[f485008] | 951 | { |
---|
| 952 | struct groupchat *gc = conv->ui_data; |
---|
| 953 | PurpleBuddy *buddy; |
---|
[5ebff60] | 954 | |
---|
[f485008] | 955 | /* ..._SEND means it's an outgoing message, no need to echo those. */ |
---|
[5ebff60] | 956 | if (flags & PURPLE_MESSAGE_SEND) { |
---|
[f485008] | 957 | return; |
---|
[5ebff60] | 958 | } |
---|
| 959 | |
---|
| 960 | buddy = purple_find_buddy(conv->account, who); |
---|
| 961 | if (buddy != NULL) { |
---|
| 962 | who = purple_buddy_get_name(buddy); |
---|
| 963 | } |
---|
| 964 | |
---|
| 965 | imcb_chat_msg(gc, who, (char *) message, 0, mtime); |
---|
[f485008] | 966 | } |
---|
| 967 | |
---|
[5ebff60] | 968 | static void prplcb_conv_im(PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, |
---|
| 969 | time_t mtime) |
---|
[d250b2a] | 970 | { |
---|
[5ebff60] | 971 | struct im_connection *ic = purple_ic_by_pa(conv->account); |
---|
[3e7b640] | 972 | PurpleBuddy *buddy; |
---|
[5ebff60] | 973 | |
---|
[389f7be] | 974 | /* ..._SEND means it's an outgoing message, no need to echo those. */ |
---|
[5ebff60] | 975 | if (flags & PURPLE_MESSAGE_SEND) { |
---|
[3e7b640] | 976 | return; |
---|
[5ebff60] | 977 | } |
---|
| 978 | |
---|
| 979 | buddy = purple_find_buddy(conv->account, who); |
---|
| 980 | if (buddy != NULL) { |
---|
| 981 | who = purple_buddy_get_name(buddy); |
---|
| 982 | } |
---|
| 983 | |
---|
| 984 | imcb_buddy_msg(ic, (char *) who, (char *) message, 0, mtime); |
---|
[d250b2a] | 985 | } |
---|
| 986 | |
---|
[bad41f56] | 987 | /* No, this is not a ui_op but a signal. */ |
---|
[5ebff60] | 988 | static void prplcb_buddy_typing(PurpleAccount *account, const char *who, gpointer null) |
---|
[bad41f56] | 989 | { |
---|
| 990 | PurpleConversation *conv; |
---|
| 991 | PurpleConvIm *im; |
---|
| 992 | int state; |
---|
[5ebff60] | 993 | |
---|
| 994 | if ((conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, account)) == NULL) { |
---|
[bad41f56] | 995 | return; |
---|
[5ebff60] | 996 | } |
---|
| 997 | |
---|
[bad41f56] | 998 | im = PURPLE_CONV_IM(conv); |
---|
[5ebff60] | 999 | switch (purple_conv_im_get_typing_state(im)) { |
---|
[bad41f56] | 1000 | case PURPLE_TYPING: |
---|
| 1001 | state = OPT_TYPING; |
---|
| 1002 | break; |
---|
| 1003 | case PURPLE_TYPED: |
---|
| 1004 | state = OPT_THINKING; |
---|
| 1005 | break; |
---|
| 1006 | default: |
---|
| 1007 | state = 0; |
---|
| 1008 | } |
---|
[5ebff60] | 1009 | |
---|
| 1010 | imcb_buddy_typing(purple_ic_by_pa(account), who, state); |
---|
[bad41f56] | 1011 | } |
---|
| 1012 | |
---|
[5ebff60] | 1013 | static PurpleConversationUiOps bee_conv_uiops = |
---|
[860ba6a] | 1014 | { |
---|
[f485008] | 1015 | prplcb_conv_new, /* create_conversation */ |
---|
| 1016 | prplcb_conv_free, /* destroy_conversation */ |
---|
| 1017 | prplcb_conv_chat_msg, /* write_chat */ |
---|
[d250b2a] | 1018 | prplcb_conv_im, /* write_im */ |
---|
[e046390] | 1019 | NULL, /* write_conv */ |
---|
[f485008] | 1020 | prplcb_conv_add_users, /* chat_add_users */ |
---|
[860ba6a] | 1021 | NULL, /* chat_rename_user */ |
---|
[f485008] | 1022 | prplcb_conv_del_users, /* chat_remove_users */ |
---|
[860ba6a] | 1023 | NULL, /* chat_update_user */ |
---|
| 1024 | NULL, /* present */ |
---|
| 1025 | NULL, /* has_focus */ |
---|
| 1026 | NULL, /* custom_smiley_add */ |
---|
| 1027 | NULL, /* custom_smiley_write */ |
---|
| 1028 | NULL, /* custom_smiley_close */ |
---|
| 1029 | NULL, /* send_confirm */ |
---|
| 1030 | }; |
---|
| 1031 | |
---|
[5ebff60] | 1032 | struct prplcb_request_action_data { |
---|
[0ac1a375] | 1033 | void *user_data, *bee_data; |
---|
| 1034 | PurpleRequestActionCb yes, no; |
---|
| 1035 | int yes_i, no_i; |
---|
| 1036 | }; |
---|
| 1037 | |
---|
[5ebff60] | 1038 | static void prplcb_request_action_yes(void *data) |
---|
[0ac1a375] | 1039 | { |
---|
| 1040 | struct prplcb_request_action_data *pqad = data; |
---|
[5ebff60] | 1041 | |
---|
| 1042 | if (pqad->yes) { |
---|
| 1043 | pqad->yes(pqad->user_data, pqad->yes_i); |
---|
| 1044 | } |
---|
[0ac1a375] | 1045 | } |
---|
| 1046 | |
---|
[5ebff60] | 1047 | static void prplcb_request_action_no(void *data) |
---|
[0ac1a375] | 1048 | { |
---|
| 1049 | struct prplcb_request_action_data *pqad = data; |
---|
[5ebff60] | 1050 | |
---|
| 1051 | if (pqad->no) { |
---|
| 1052 | pqad->no(pqad->user_data, pqad->no_i); |
---|
| 1053 | } |
---|
[2c5ab49] | 1054 | } |
---|
| 1055 | |
---|
| 1056 | /* q->free() callback from query_del()*/ |
---|
| 1057 | static void prplcb_request_action_free(void *data) |
---|
| 1058 | { |
---|
| 1059 | struct prplcb_request_action_data *pqad = data; |
---|
| 1060 | |
---|
| 1061 | pqad->bee_data = NULL; |
---|
| 1062 | purple_request_close(PURPLE_REQUEST_ACTION, pqad); |
---|
[0ac1a375] | 1063 | } |
---|
| 1064 | |
---|
[5ebff60] | 1065 | static void *prplcb_request_action(const char *title, const char *primary, const char *secondary, |
---|
| 1066 | int default_action, PurpleAccount *account, const char *who, |
---|
| 1067 | PurpleConversation *conv, void *user_data, size_t action_count, |
---|
| 1068 | va_list actions) |
---|
[0ac1a375] | 1069 | { |
---|
[5ebff60] | 1070 | struct prplcb_request_action_data *pqad; |
---|
[0ac1a375] | 1071 | int i; |
---|
| 1072 | char *q; |
---|
[5ebff60] | 1073 | |
---|
| 1074 | pqad = g_new0(struct prplcb_request_action_data, 1); |
---|
| 1075 | |
---|
| 1076 | for (i = 0; i < action_count; i++) { |
---|
[0ac1a375] | 1077 | char *caption; |
---|
| 1078 | void *fn; |
---|
[5ebff60] | 1079 | |
---|
| 1080 | caption = va_arg(actions, char*); |
---|
| 1081 | fn = va_arg(actions, void*); |
---|
| 1082 | |
---|
| 1083 | if (strstr(caption, "Accept") || strstr(caption, "OK")) { |
---|
[0ac1a375] | 1084 | pqad->yes = fn; |
---|
| 1085 | pqad->yes_i = i; |
---|
[5ebff60] | 1086 | } else if (strstr(caption, "Reject") || strstr(caption, "Cancel")) { |
---|
[0ac1a375] | 1087 | pqad->no = fn; |
---|
| 1088 | pqad->no_i = i; |
---|
| 1089 | } |
---|
| 1090 | } |
---|
[5ebff60] | 1091 | |
---|
[0ac1a375] | 1092 | pqad->user_data = user_data; |
---|
[5ebff60] | 1093 | |
---|
[4aa0f6b] | 1094 | /* TODO: IRC stuff here :-( */ |
---|
[5ebff60] | 1095 | q = g_strdup_printf("Request: %s\n\n%s\n\n%s", title, primary, secondary); |
---|
[56985aa] | 1096 | pqad->bee_data = query_add(local_bee->ui_data, purple_ic_by_pa(account), q, |
---|
[2c5ab49] | 1097 | prplcb_request_action_yes, prplcb_request_action_no, |
---|
| 1098 | prplcb_request_action_free, pqad); |
---|
[5ebff60] | 1099 | |
---|
| 1100 | g_free(q); |
---|
| 1101 | |
---|
[e5d8d21] | 1102 | return pqad; |
---|
[0ac1a375] | 1103 | } |
---|
| 1104 | |
---|
[3bb333c] | 1105 | /* So it turns out some requests have no account context at all, because |
---|
| 1106 | * libpurple hates us. This means that query_del_by_conn() won't remove those |
---|
| 1107 | * on logout, and will segfault if the user replies. That's why this exists. |
---|
| 1108 | */ |
---|
| 1109 | static void prplcb_close_request(PurpleRequestType type, void *data) |
---|
| 1110 | { |
---|
[2c5ab49] | 1111 | struct prplcb_request_action_data *pqad; |
---|
| 1112 | struct request_input_data *ri; |
---|
| 1113 | struct purple_data *pd; |
---|
| 1114 | |
---|
| 1115 | if (!data) { |
---|
| 1116 | return; |
---|
| 1117 | } |
---|
| 1118 | |
---|
| 1119 | switch (type) { |
---|
| 1120 | case PURPLE_REQUEST_ACTION: |
---|
| 1121 | pqad = data; |
---|
| 1122 | /* if this is null, it's because query_del was run already */ |
---|
| 1123 | if (pqad->bee_data) { |
---|
| 1124 | query_del(local_bee->ui_data, pqad->bee_data); |
---|
| 1125 | } |
---|
| 1126 | g_free(pqad); |
---|
| 1127 | break; |
---|
| 1128 | case PURPLE_REQUEST_INPUT: |
---|
| 1129 | ri = data; |
---|
| 1130 | pd = ri->ic->proto_data; |
---|
| 1131 | imcb_remove_buddy(ri->ic, ri->buddy, NULL); |
---|
| 1132 | g_free(ri->buddy); |
---|
| 1133 | g_hash_table_remove(pd->input_requests, GUINT_TO_POINTER(ri->id)); |
---|
| 1134 | break; |
---|
| 1135 | default: |
---|
| 1136 | g_free(data); |
---|
| 1137 | break; |
---|
[3bb333c] | 1138 | } |
---|
| 1139 | |
---|
[177ffd7] | 1140 | } |
---|
| 1141 | |
---|
[6a48992] | 1142 | void* prplcb_request_input(const char *title, const char *primary, |
---|
| 1143 | const char *secondary, const char *default_value, gboolean multiline, |
---|
| 1144 | gboolean masked, gchar *hint, const char *ok_text, GCallback ok_cb, |
---|
| 1145 | const char *cancel_text, GCallback cancel_cb, PurpleAccount *account, |
---|
| 1146 | const char *who, PurpleConversation *conv, void *user_data) |
---|
| 1147 | { |
---|
| 1148 | struct im_connection *ic = purple_ic_by_pa(account); |
---|
| 1149 | struct purple_data *pd = ic->proto_data; |
---|
| 1150 | struct request_input_data *ri = g_new0(struct request_input_data, 1); |
---|
| 1151 | guint id = pd->next_request_id++; |
---|
| 1152 | |
---|
[2c5ab49] | 1153 | ri->id = id; |
---|
| 1154 | ri->ic = ic; |
---|
| 1155 | ri->buddy = g_strdup_printf("%s_%u", PURPLE_REQUEST_HANDLE, id); |
---|
[6a48992] | 1156 | ri->data_callback = (ri_callback_t) ok_cb; |
---|
| 1157 | ri->user_data = user_data; |
---|
| 1158 | g_hash_table_insert(pd->input_requests, GUINT_TO_POINTER(id), ri); |
---|
| 1159 | |
---|
[2c5ab49] | 1160 | imcb_add_buddy(ic, ri->buddy, NULL); |
---|
| 1161 | imcb_buddy_msg(ic, ri->buddy, secondary, 0, 0); |
---|
[6a48992] | 1162 | |
---|
[2c5ab49] | 1163 | return ri; |
---|
[6a48992] | 1164 | } |
---|
| 1165 | |
---|
| 1166 | void purple_request_input_callback(guint id, struct im_connection *ic, |
---|
| 1167 | const char *message, const char *who) |
---|
| 1168 | { |
---|
| 1169 | struct purple_data *pd = ic->proto_data; |
---|
[2c5ab49] | 1170 | struct request_input_data *ri; |
---|
[6a48992] | 1171 | |
---|
[2c5ab49] | 1172 | if (!(ri = g_hash_table_lookup(pd->input_requests, GUINT_TO_POINTER(id)))) { |
---|
| 1173 | return; |
---|
[6a48992] | 1174 | } |
---|
| 1175 | |
---|
[2c5ab49] | 1176 | ri->data_callback(ri->user_data, message); |
---|
| 1177 | |
---|
| 1178 | purple_request_close(PURPLE_REQUEST_INPUT, ri); |
---|
[6a48992] | 1179 | } |
---|
| 1180 | |
---|
| 1181 | |
---|
[0ac1a375] | 1182 | static PurpleRequestUiOps bee_request_uiops = |
---|
| 1183 | { |
---|
[6a48992] | 1184 | prplcb_request_input, |
---|
[d0527c1] | 1185 | NULL, |
---|
[0ac1a375] | 1186 | prplcb_request_action, |
---|
[d0527c1] | 1187 | NULL, |
---|
| 1188 | NULL, |
---|
[3bb333c] | 1189 | prplcb_close_request, |
---|
[d0527c1] | 1190 | NULL, |
---|
[0ac1a375] | 1191 | }; |
---|
| 1192 | |
---|
[5ebff60] | 1193 | static void prplcb_privacy_permit_added(PurpleAccount *account, const char *name) |
---|
[05a8932] | 1194 | { |
---|
[5ebff60] | 1195 | struct im_connection *ic = purple_ic_by_pa(account); |
---|
| 1196 | |
---|
| 1197 | if (!g_slist_find_custom(ic->permit, name, (GCompareFunc) ic->acc->prpl->handle_cmp)) { |
---|
| 1198 | ic->permit = g_slist_prepend(ic->permit, g_strdup(name)); |
---|
| 1199 | } |
---|
[05a8932] | 1200 | } |
---|
| 1201 | |
---|
[5ebff60] | 1202 | static void prplcb_privacy_permit_removed(PurpleAccount *account, const char *name) |
---|
[05a8932] | 1203 | { |
---|
[5ebff60] | 1204 | struct im_connection *ic = purple_ic_by_pa(account); |
---|
[05a8932] | 1205 | void *n; |
---|
[5ebff60] | 1206 | |
---|
| 1207 | n = g_slist_find_custom(ic->permit, name, (GCompareFunc) ic->acc->prpl->handle_cmp); |
---|
| 1208 | ic->permit = g_slist_remove(ic->permit, n); |
---|
[05a8932] | 1209 | } |
---|
| 1210 | |
---|
[5ebff60] | 1211 | static void prplcb_privacy_deny_added(PurpleAccount *account, const char *name) |
---|
[05a8932] | 1212 | { |
---|
[5ebff60] | 1213 | struct im_connection *ic = purple_ic_by_pa(account); |
---|
| 1214 | |
---|
| 1215 | if (!g_slist_find_custom(ic->deny, name, (GCompareFunc) ic->acc->prpl->handle_cmp)) { |
---|
| 1216 | ic->deny = g_slist_prepend(ic->deny, g_strdup(name)); |
---|
| 1217 | } |
---|
[05a8932] | 1218 | } |
---|
| 1219 | |
---|
[5ebff60] | 1220 | static void prplcb_privacy_deny_removed(PurpleAccount *account, const char *name) |
---|
[05a8932] | 1221 | { |
---|
[5ebff60] | 1222 | struct im_connection *ic = purple_ic_by_pa(account); |
---|
[05a8932] | 1223 | void *n; |
---|
[5ebff60] | 1224 | |
---|
| 1225 | n = g_slist_find_custom(ic->deny, name, (GCompareFunc) ic->acc->prpl->handle_cmp); |
---|
| 1226 | ic->deny = g_slist_remove(ic->deny, n); |
---|
[05a8932] | 1227 | } |
---|
| 1228 | |
---|
| 1229 | static PurplePrivacyUiOps bee_privacy_uiops = |
---|
| 1230 | { |
---|
| 1231 | prplcb_privacy_permit_added, |
---|
| 1232 | prplcb_privacy_permit_removed, |
---|
| 1233 | prplcb_privacy_deny_added, |
---|
| 1234 | prplcb_privacy_deny_removed, |
---|
| 1235 | }; |
---|
| 1236 | |
---|
[5ebff60] | 1237 | static void prplcb_debug_print(PurpleDebugLevel level, const char *category, const char *arg_s) |
---|
[0cbef26] | 1238 | { |
---|
[5ebff60] | 1239 | fprintf(stderr, "DEBUG %s: %s", category, arg_s); |
---|
[0cbef26] | 1240 | } |
---|
| 1241 | |
---|
| 1242 | static PurpleDebugUiOps bee_debug_uiops = |
---|
| 1243 | { |
---|
| 1244 | prplcb_debug_print, |
---|
| 1245 | }; |
---|
| 1246 | |
---|
[5ebff60] | 1247 | static guint prplcb_ev_timeout_add(guint interval, GSourceFunc func, gpointer udata) |
---|
[4164e62] | 1248 | { |
---|
[5ebff60] | 1249 | return b_timeout_add(interval, (b_event_handler) func, udata); |
---|
[4164e62] | 1250 | } |
---|
| 1251 | |
---|
[5ebff60] | 1252 | static guint prplcb_ev_input_add(int fd, PurpleInputCondition cond, PurpleInputFunction func, gpointer udata) |
---|
[4164e62] | 1253 | { |
---|
[5ebff60] | 1254 | return b_input_add(fd, cond | B_EV_FLAG_FORCE_REPEAT, (b_event_handler) func, udata); |
---|
[4164e62] | 1255 | } |
---|
| 1256 | |
---|
[5ebff60] | 1257 | static gboolean prplcb_ev_remove(guint id) |
---|
[4164e62] | 1258 | { |
---|
[5ebff60] | 1259 | b_event_remove((gint) id); |
---|
[4164e62] | 1260 | return TRUE; |
---|
| 1261 | } |
---|
| 1262 | |
---|
[5ebff60] | 1263 | static PurpleEventLoopUiOps glib_eventloops = |
---|
[4164e62] | 1264 | { |
---|
| 1265 | prplcb_ev_timeout_add, |
---|
| 1266 | prplcb_ev_remove, |
---|
| 1267 | prplcb_ev_input_add, |
---|
| 1268 | prplcb_ev_remove, |
---|
| 1269 | }; |
---|
| 1270 | |
---|
[5ebff60] | 1271 | static void *prplcb_notify_email(PurpleConnection *gc, const char *subject, const char *from, |
---|
| 1272 | const char *to, const char *url) |
---|
[bab1c86] | 1273 | { |
---|
[5ebff60] | 1274 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
| 1275 | |
---|
[0864a52] | 1276 | imcb_notify_email(ic, "Received e-mail from %s for %s: %s <%s>", from, to, subject, url); |
---|
[5ebff60] | 1277 | |
---|
[bab1c86] | 1278 | return NULL; |
---|
| 1279 | } |
---|
| 1280 | |
---|
[5ebff60] | 1281 | static void *prplcb_notify_userinfo(PurpleConnection *gc, const char *who, PurpleNotifyUserInfo *user_info) |
---|
[e77c264] | 1282 | { |
---|
[5ebff60] | 1283 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
| 1284 | GString *info = g_string_new(""); |
---|
| 1285 | GList *l = purple_notify_user_info_get_entries(user_info); |
---|
[e77c264] | 1286 | char *key; |
---|
| 1287 | const char *value; |
---|
| 1288 | int n; |
---|
[5ebff60] | 1289 | |
---|
| 1290 | while (l) { |
---|
[e77c264] | 1291 | PurpleNotifyUserInfoEntry *e = l->data; |
---|
[5ebff60] | 1292 | |
---|
| 1293 | switch (purple_notify_user_info_entry_get_type(e)) { |
---|
[e77c264] | 1294 | case PURPLE_NOTIFY_USER_INFO_ENTRY_PAIR: |
---|
| 1295 | case PURPLE_NOTIFY_USER_INFO_ENTRY_SECTION_HEADER: |
---|
[5ebff60] | 1296 | key = g_strdup(purple_notify_user_info_entry_get_label(e)); |
---|
| 1297 | value = purple_notify_user_info_entry_get_value(e); |
---|
| 1298 | |
---|
| 1299 | if (key) { |
---|
| 1300 | strip_html(key); |
---|
| 1301 | g_string_append_printf(info, "%s: ", key); |
---|
| 1302 | |
---|
| 1303 | if (value) { |
---|
| 1304 | n = strlen(value) - 1; |
---|
| 1305 | while (g_ascii_isspace(value[n])) { |
---|
| 1306 | n--; |
---|
| 1307 | } |
---|
| 1308 | g_string_append_len(info, value, n + 1); |
---|
[e77c264] | 1309 | } |
---|
[5ebff60] | 1310 | g_string_append_c(info, '\n'); |
---|
| 1311 | g_free(key); |
---|
[e77c264] | 1312 | } |
---|
[5ebff60] | 1313 | |
---|
[e77c264] | 1314 | break; |
---|
| 1315 | case PURPLE_NOTIFY_USER_INFO_ENTRY_SECTION_BREAK: |
---|
[5ebff60] | 1316 | g_string_append(info, "------------------------\n"); |
---|
[e77c264] | 1317 | break; |
---|
| 1318 | } |
---|
[5ebff60] | 1319 | |
---|
[e77c264] | 1320 | l = l->next; |
---|
| 1321 | } |
---|
[5ebff60] | 1322 | |
---|
| 1323 | imcb_log(ic, "User %s info:\n%s", who, info->str); |
---|
| 1324 | g_string_free(info, TRUE); |
---|
| 1325 | |
---|
[e77c264] | 1326 | return NULL; |
---|
| 1327 | } |
---|
| 1328 | |
---|
[437bd9b] | 1329 | static PurpleNotifyUiOps bee_notify_uiops = |
---|
[bab1c86] | 1330 | { |
---|
[5ebff60] | 1331 | NULL, |
---|
| 1332 | prplcb_notify_email, |
---|
| 1333 | NULL, |
---|
| 1334 | NULL, |
---|
| 1335 | NULL, |
---|
| 1336 | NULL, |
---|
| 1337 | prplcb_notify_userinfo, |
---|
[bab1c86] | 1338 | }; |
---|
| 1339 | |
---|
[5ebff60] | 1340 | static void *prplcb_account_request_authorize(PurpleAccount *account, const char *remote_user, |
---|
| 1341 | const char *id, const char *alias, const char *message, gboolean on_list, |
---|
| 1342 | PurpleAccountRequestAuthorizationCb authorize_cb, |
---|
| 1343 | PurpleAccountRequestAuthorizationCb deny_cb, void *user_data) |
---|
[d0527c1] | 1344 | { |
---|
[5ebff60] | 1345 | struct im_connection *ic = purple_ic_by_pa(account); |
---|
[d0527c1] | 1346 | char *q; |
---|
[5ebff60] | 1347 | |
---|
| 1348 | if (alias) { |
---|
| 1349 | q = g_strdup_printf("%s (%s) wants to add you to his/her contact " |
---|
| 1350 | "list. (%s)", alias, remote_user, message); |
---|
| 1351 | } else { |
---|
| 1352 | q = g_strdup_printf("%s wants to add you to his/her contact " |
---|
| 1353 | "list. (%s)", remote_user, message); |
---|
| 1354 | } |
---|
| 1355 | |
---|
| 1356 | imcb_ask_with_free(ic, q, user_data, authorize_cb, deny_cb, NULL); |
---|
| 1357 | g_free(q); |
---|
| 1358 | |
---|
[3e59c8d] | 1359 | return NULL; |
---|
[d0527c1] | 1360 | } |
---|
| 1361 | |
---|
| 1362 | static PurpleAccountUiOps bee_account_uiops = |
---|
| 1363 | { |
---|
| 1364 | NULL, |
---|
| 1365 | NULL, |
---|
| 1366 | NULL, |
---|
| 1367 | prplcb_account_request_authorize, |
---|
| 1368 | NULL, |
---|
| 1369 | }; |
---|
| 1370 | |
---|
[2309152] | 1371 | extern PurpleXferUiOps bee_xfer_uiops; |
---|
[edfc6db] | 1372 | |
---|
[860ba6a] | 1373 | static void purple_ui_init() |
---|
| 1374 | { |
---|
[5ebff60] | 1375 | purple_connections_set_ui_ops(&bee_conn_uiops); |
---|
| 1376 | purple_blist_set_ui_ops(&bee_blist_uiops); |
---|
| 1377 | purple_conversations_set_ui_ops(&bee_conv_uiops); |
---|
| 1378 | purple_request_set_ui_ops(&bee_request_uiops); |
---|
| 1379 | purple_privacy_set_ui_ops(&bee_privacy_uiops); |
---|
| 1380 | purple_notify_set_ui_ops(&bee_notify_uiops); |
---|
| 1381 | purple_accounts_set_ui_ops(&bee_account_uiops); |
---|
| 1382 | purple_xfers_set_ui_ops(&bee_xfer_uiops); |
---|
| 1383 | |
---|
| 1384 | if (getenv("BITLBEE_DEBUG")) { |
---|
| 1385 | purple_debug_set_ui_ops(&bee_debug_uiops); |
---|
| 1386 | } |
---|
[860ba6a] | 1387 | } |
---|
| 1388 | |
---|
[796da03] | 1389 | void purple_initmodule() |
---|
| 1390 | { |
---|
[cd741d8] | 1391 | struct prpl funcs; |
---|
[796da03] | 1392 | GList *prots; |
---|
[e5d8d21] | 1393 | GString *help; |
---|
[3c9b095] | 1394 | char *dir; |
---|
[5ebff60] | 1395 | |
---|
| 1396 | if (B_EV_IO_READ != PURPLE_INPUT_READ || |
---|
| 1397 | B_EV_IO_WRITE != PURPLE_INPUT_WRITE) { |
---|
[e046390] | 1398 | /* FIXME FIXME FIXME FIXME FIXME :-) */ |
---|
[5ebff60] | 1399 | exit(1); |
---|
[e046390] | 1400 | } |
---|
[5ebff60] | 1401 | |
---|
| 1402 | dir = g_strdup_printf("%s/purple", global.conf->configdir); |
---|
| 1403 | purple_util_set_user_dir(dir); |
---|
| 1404 | g_free(dir); |
---|
| 1405 | |
---|
| 1406 | purple_debug_set_enabled(FALSE); |
---|
| 1407 | purple_core_set_ui_ops(&bee_core_uiops); |
---|
| 1408 | purple_eventloop_set_ui_ops(&glib_eventloops); |
---|
| 1409 | if (!purple_core_init("BitlBee")) { |
---|
[796da03] | 1410 | /* Initializing the core failed. Terminate. */ |
---|
[5ebff60] | 1411 | fprintf(stderr, "libpurple initialization failed.\n"); |
---|
[796da03] | 1412 | abort(); |
---|
| 1413 | } |
---|
[5ebff60] | 1414 | |
---|
| 1415 | if (proxytype != PROXY_NONE) { |
---|
[ff94563] | 1416 | PurpleProxyInfo *pi = purple_global_proxy_get_info(); |
---|
[5ebff60] | 1417 | switch (proxytype) { |
---|
[12f041d] | 1418 | case PROXY_SOCKS4A: |
---|
[7add7ec] | 1419 | case PROXY_SOCKS4: |
---|
[5ebff60] | 1420 | purple_proxy_info_set_type(pi, PURPLE_PROXY_SOCKS4); |
---|
[7add7ec] | 1421 | break; |
---|
| 1422 | case PROXY_SOCKS5: |
---|
[5ebff60] | 1423 | purple_proxy_info_set_type(pi, PURPLE_PROXY_SOCKS5); |
---|
[7add7ec] | 1424 | break; |
---|
| 1425 | case PROXY_HTTP: |
---|
[5ebff60] | 1426 | purple_proxy_info_set_type(pi, PURPLE_PROXY_HTTP); |
---|
[7add7ec] | 1427 | break; |
---|
[5ebff60] | 1428 | } |
---|
| 1429 | purple_proxy_info_set_host(pi, proxyhost); |
---|
| 1430 | purple_proxy_info_set_port(pi, proxyport); |
---|
| 1431 | purple_proxy_info_set_username(pi, proxyuser); |
---|
| 1432 | purple_proxy_info_set_password(pi, proxypass); |
---|
[7add7ec] | 1433 | } |
---|
[5ebff60] | 1434 | |
---|
| 1435 | purple_set_blist(purple_blist_new()); |
---|
| 1436 | |
---|
[bad41f56] | 1437 | /* No, really. So far there were ui_ops for everything, but now suddenly |
---|
| 1438 | one needs to use signals for typing notification stuff. :-( */ |
---|
[5ebff60] | 1439 | purple_signal_connect(purple_conversations_get_handle(), "buddy-typing", |
---|
| 1440 | &funcs, PURPLE_CALLBACK(prplcb_buddy_typing), NULL); |
---|
| 1441 | purple_signal_connect(purple_conversations_get_handle(), "buddy-typed", |
---|
| 1442 | &funcs, PURPLE_CALLBACK(prplcb_buddy_typing), NULL); |
---|
| 1443 | purple_signal_connect(purple_conversations_get_handle(), "buddy-typing-stopped", |
---|
| 1444 | &funcs, PURPLE_CALLBACK(prplcb_buddy_typing), NULL); |
---|
| 1445 | |
---|
| 1446 | memset(&funcs, 0, sizeof(funcs)); |
---|
[cd741d8] | 1447 | funcs.login = purple_login; |
---|
| 1448 | funcs.init = purple_init; |
---|
| 1449 | funcs.logout = purple_logout; |
---|
| 1450 | funcs.buddy_msg = purple_buddy_msg; |
---|
| 1451 | funcs.away_states = purple_away_states; |
---|
| 1452 | funcs.set_away = purple_set_away; |
---|
| 1453 | funcs.add_buddy = purple_add_buddy; |
---|
| 1454 | funcs.remove_buddy = purple_remove_buddy; |
---|
[05a8932] | 1455 | funcs.add_permit = purple_add_permit; |
---|
| 1456 | funcs.add_deny = purple_add_deny; |
---|
| 1457 | funcs.rem_permit = purple_rem_permit; |
---|
| 1458 | funcs.rem_deny = purple_rem_deny; |
---|
[e77c264] | 1459 | funcs.get_info = purple_get_info; |
---|
[cd741d8] | 1460 | funcs.keepalive = purple_keepalive; |
---|
| 1461 | funcs.send_typing = purple_send_typing; |
---|
| 1462 | funcs.handle_cmp = g_strcasecmp; |
---|
[f485008] | 1463 | /* TODO(wilmer): Set these only for protocols that support them? */ |
---|
| 1464 | funcs.chat_msg = purple_chat_msg; |
---|
[8ad5c34] | 1465 | funcs.chat_with = purple_chat_with; |
---|
| 1466 | funcs.chat_invite = purple_chat_invite; |
---|
[e41cc40] | 1467 | funcs.chat_kick = purple_chat_kick; |
---|
[15794dc] | 1468 | funcs.chat_leave = purple_chat_leave; |
---|
[c3caa46] | 1469 | funcs.chat_join = purple_chat_join; |
---|
[edfc6db] | 1470 | funcs.transfer_request = purple_transfer_request; |
---|
[5ebff60] | 1471 | |
---|
| 1472 | help = g_string_new("BitlBee libpurple module supports the following IM protocols:\n"); |
---|
| 1473 | |
---|
[bab1c86] | 1474 | /* Add a protocol entry to BitlBee's structures for every protocol |
---|
[5ebff60] | 1475 | supported by this libpurple instance. */ |
---|
| 1476 | for (prots = purple_plugins_get_protocols(); prots; prots = prots->next) { |
---|
[796da03] | 1477 | PurplePlugin *prot = prots->data; |
---|
[cd741d8] | 1478 | struct prpl *ret; |
---|
[5ebff60] | 1479 | |
---|
[c775a58] | 1480 | /* If we already have this one (as a native module), don't |
---|
| 1481 | add a libpurple duplicate. */ |
---|
[5ebff60] | 1482 | if (find_protocol(prot->info->id)) { |
---|
[c775a58] | 1483 | continue; |
---|
[5ebff60] | 1484 | } |
---|
| 1485 | |
---|
| 1486 | ret = g_memdup(&funcs, sizeof(funcs)); |
---|
[cd741d8] | 1487 | ret->name = ret->data = prot->info->id; |
---|
[5ebff60] | 1488 | if (strncmp(ret->name, "prpl-", 5) == 0) { |
---|
[cd741d8] | 1489 | ret->name += 5; |
---|
[5ebff60] | 1490 | } |
---|
| 1491 | register_protocol(ret); |
---|
| 1492 | |
---|
| 1493 | g_string_append_printf(help, "\n* %s (%s)", ret->name, prot->info->name); |
---|
| 1494 | |
---|
[bab1c86] | 1495 | /* libpurple doesn't define a protocol called OSCAR, but we |
---|
| 1496 | need it to be compatible with normal BitlBee. */ |
---|
[5ebff60] | 1497 | if (g_strcasecmp(prot->info->id, "prpl-aim") == 0) { |
---|
| 1498 | ret = g_memdup(&funcs, sizeof(funcs)); |
---|
[cd741d8] | 1499 | ret->name = "oscar"; |
---|
| 1500 | ret->data = prot->info->id; |
---|
[5ebff60] | 1501 | register_protocol(ret); |
---|
[cd741d8] | 1502 | } |
---|
[796da03] | 1503 | } |
---|
[5ebff60] | 1504 | |
---|
| 1505 | g_string_append(help, "\n\nFor used protocols, more information about available " |
---|
| 1506 | "settings can be found using \x02help purple <protocol name>\x02 " |
---|
| 1507 | "(create an account using that protocol first!)"); |
---|
| 1508 | |
---|
[bab1c86] | 1509 | /* Add a simple dynamically-generated help item listing all |
---|
| 1510 | the supported protocols. */ |
---|
[5ebff60] | 1511 | help_add_mem(&global.help, "purple", help->str); |
---|
| 1512 | g_string_free(help, TRUE); |
---|
[796da03] | 1513 | } |
---|