[796da03] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * libpurple module - Main file * |
---|
| 5 | * * |
---|
[bab1c86] | 6 | * Copyright 2009-2010 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" |
---|
[e5d8d21] | 25 | #include "help.h" |
---|
[b3117f2] | 26 | |
---|
[0ac1a375] | 27 | #include <stdarg.h> |
---|
| 28 | |
---|
[796da03] | 29 | #include <glib.h> |
---|
| 30 | #include <purple.h> |
---|
| 31 | |
---|
| 32 | GSList *purple_connections; |
---|
| 33 | |
---|
[0ac1a375] | 34 | /* This makes me VERY sad... :-( But some libpurple callbacks come in without |
---|
| 35 | any context so this is the only way to get that. Don't want to support |
---|
| 36 | libpurple in daemon mode anyway. */ |
---|
| 37 | static irc_t *local_irc; |
---|
| 38 | |
---|
[f85e9d6] | 39 | static char *set_eval_display_name( set_t *set, char *value ); |
---|
| 40 | |
---|
[2309152] | 41 | struct im_connection *purple_ic_by_pa( PurpleAccount *pa ) |
---|
[860ba6a] | 42 | { |
---|
| 43 | GSList *i; |
---|
| 44 | |
---|
| 45 | for( i = purple_connections; i; i = i->next ) |
---|
| 46 | if( ((struct im_connection *)i->data)->proto_data == pa ) |
---|
| 47 | return i->data; |
---|
| 48 | |
---|
| 49 | return NULL; |
---|
| 50 | } |
---|
| 51 | |
---|
[7da726b] | 52 | static struct im_connection *purple_ic_by_gc( PurpleConnection *gc ) |
---|
| 53 | { |
---|
| 54 | return purple_ic_by_pa( purple_connection_get_account( gc ) ); |
---|
| 55 | } |
---|
| 56 | |
---|
[8ad5c34] | 57 | static gboolean purple_menu_cmp( const char *a, const char *b ) |
---|
| 58 | { |
---|
| 59 | while( *a && *b ) |
---|
| 60 | { |
---|
| 61 | while( *a == '_' ) a ++; |
---|
| 62 | while( *b == '_' ) b ++; |
---|
| 63 | if( tolower( *a ) != tolower( *b ) ) |
---|
| 64 | return FALSE; |
---|
| 65 | |
---|
| 66 | a ++; |
---|
| 67 | b ++; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | return ( *a == '\0' && *b == '\0' ); |
---|
| 71 | } |
---|
| 72 | |
---|
[796da03] | 73 | static void purple_init( account_t *acc ) |
---|
| 74 | { |
---|
[45a19e5] | 75 | PurplePlugin *prpl = purple_plugins_find_with_id( (char*) acc->prpl->data ); |
---|
[0f7ee7e5] | 76 | PurplePluginProtocolInfo *pi = prpl->info->extra_info; |
---|
[52cae01] | 77 | PurpleAccount *pa; |
---|
| 78 | GList *i, *st; |
---|
[bab1c86] | 79 | set_t *s; |
---|
[7c5affca] | 80 | char help_title[64]; |
---|
| 81 | GString *help; |
---|
| 82 | |
---|
| 83 | help = g_string_new( "" ); |
---|
| 84 | g_string_printf( help, "BitlBee libpurple module %s (%s).\n\nSupported settings:", |
---|
| 85 | (char*) acc->prpl->name, prpl->info->name ); |
---|
[0cbef26] | 86 | |
---|
[52cae01] | 87 | /* Convert all protocol_options into per-account setting variables. */ |
---|
[0f7ee7e5] | 88 | for( i = pi->protocol_options; i; i = i->next ) |
---|
| 89 | { |
---|
| 90 | PurpleAccountOption *o = i->data; |
---|
| 91 | const char *name; |
---|
| 92 | char *def = NULL; |
---|
| 93 | set_eval eval = NULL; |
---|
[4dc6b8d] | 94 | void *eval_data = NULL; |
---|
| 95 | GList *io = NULL; |
---|
| 96 | GSList *opts = NULL; |
---|
[0f7ee7e5] | 97 | |
---|
| 98 | name = purple_account_option_get_setting( o ); |
---|
| 99 | |
---|
| 100 | switch( purple_account_option_get_type( o ) ) |
---|
| 101 | { |
---|
| 102 | case PURPLE_PREF_STRING: |
---|
| 103 | def = g_strdup( purple_account_option_get_default_string( o ) ); |
---|
[7c5affca] | 104 | |
---|
| 105 | g_string_append_printf( help, "\n* %s (%s), %s, default: %s", |
---|
| 106 | name, purple_account_option_get_text( o ), |
---|
| 107 | "string", def ); |
---|
| 108 | |
---|
[0f7ee7e5] | 109 | break; |
---|
| 110 | |
---|
| 111 | case PURPLE_PREF_INT: |
---|
| 112 | def = g_strdup_printf( "%d", purple_account_option_get_default_int( o ) ); |
---|
| 113 | eval = set_eval_int; |
---|
[7c5affca] | 114 | |
---|
| 115 | g_string_append_printf( help, "\n* %s (%s), %s, default: %s", |
---|
| 116 | name, purple_account_option_get_text( o ), |
---|
| 117 | "integer", def ); |
---|
| 118 | |
---|
[0f7ee7e5] | 119 | break; |
---|
| 120 | |
---|
| 121 | case PURPLE_PREF_BOOLEAN: |
---|
| 122 | if( purple_account_option_get_default_bool( o ) ) |
---|
| 123 | def = g_strdup( "true" ); |
---|
| 124 | else |
---|
| 125 | def = g_strdup( "false" ); |
---|
| 126 | eval = set_eval_bool; |
---|
[7c5affca] | 127 | |
---|
| 128 | g_string_append_printf( help, "\n* %s (%s), %s, default: %s", |
---|
| 129 | name, purple_account_option_get_text( o ), |
---|
| 130 | "boolean", def ); |
---|
| 131 | |
---|
[0f7ee7e5] | 132 | break; |
---|
| 133 | |
---|
[4dc6b8d] | 134 | case PURPLE_PREF_STRING_LIST: |
---|
| 135 | def = g_strdup( purple_account_option_get_default_list_value( o ) ); |
---|
[7c5affca] | 136 | |
---|
| 137 | g_string_append_printf( help, "\n* %s (%s), %s, default: %s", |
---|
| 138 | name, purple_account_option_get_text( o ), |
---|
| 139 | "list", def ); |
---|
| 140 | g_string_append( help, "\n Possible values: " ); |
---|
| 141 | |
---|
[4dc6b8d] | 142 | for( io = purple_account_option_get_list( o ); io; io = io->next ) |
---|
| 143 | { |
---|
| 144 | PurpleKeyValuePair *kv = io->data; |
---|
[d8acfd3] | 145 | opts = g_slist_append( opts, kv->value ); |
---|
[c96c72f] | 146 | /* TODO: kv->value is not a char*, WTF? */ |
---|
[d8acfd3] | 147 | if( strcmp( kv->value, kv->key ) != 0 ) |
---|
[c96c72f] | 148 | g_string_append_printf( help, "%s (%s), ", (char*) kv->value, kv->key ); |
---|
[d8acfd3] | 149 | else |
---|
[c96c72f] | 150 | g_string_append_printf( help, "%s, ", (char*) kv->value ); |
---|
[4dc6b8d] | 151 | } |
---|
[7c5affca] | 152 | g_string_truncate( help, help->len - 2 ); |
---|
[4dc6b8d] | 153 | eval = set_eval_list; |
---|
| 154 | eval_data = opts; |
---|
[7c5affca] | 155 | |
---|
[4dc6b8d] | 156 | break; |
---|
| 157 | |
---|
[0f7ee7e5] | 158 | default: |
---|
[4dc6b8d] | 159 | irc_usermsg( acc->irc, "Setting with unknown type: %s (%d) Expect stuff to break..\n", |
---|
| 160 | name, purple_account_option_get_type( o ) ); |
---|
[b3117f2] | 161 | name = NULL; |
---|
[0f7ee7e5] | 162 | } |
---|
| 163 | |
---|
[b3117f2] | 164 | if( name != NULL ) |
---|
[0f7ee7e5] | 165 | { |
---|
| 166 | s = set_add( &acc->set, name, def, eval, acc ); |
---|
| 167 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[4dc6b8d] | 168 | s->eval_data = eval_data; |
---|
[0f7ee7e5] | 169 | g_free( def ); |
---|
| 170 | } |
---|
| 171 | } |
---|
[52cae01] | 172 | |
---|
[7c5affca] | 173 | g_snprintf( help_title, sizeof( help_title ), "purple %s", (char*) acc->prpl->name ); |
---|
| 174 | help_add_mem( &global.help, help_title, help->str ); |
---|
| 175 | g_string_free( help, TRUE ); |
---|
| 176 | |
---|
[f85e9d6] | 177 | s = set_add( &acc->set, "display_name", NULL, set_eval_display_name, acc ); |
---|
| 178 | s->flags |= ACC_SET_ONLINE_ONLY; |
---|
| 179 | |
---|
[bab1c86] | 180 | if( pi->options & OPT_PROTO_MAIL_CHECK ) |
---|
| 181 | { |
---|
| 182 | s = set_add( &acc->set, "mail_notifications", "false", set_eval_bool, acc ); |
---|
| 183 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 184 | } |
---|
| 185 | |
---|
[52cae01] | 186 | /* Go through all away states to figure out if away/status messages |
---|
| 187 | are possible. */ |
---|
| 188 | pa = purple_account_new( acc->user, (char*) acc->prpl->data ); |
---|
| 189 | for( st = purple_account_get_status_types( pa ); st; st = st->next ) |
---|
| 190 | { |
---|
| 191 | PurpleStatusPrimitive prim = purple_status_type_get_primitive( st->data ); |
---|
| 192 | |
---|
| 193 | if( prim == PURPLE_STATUS_AVAILABLE ) |
---|
| 194 | { |
---|
| 195 | if( purple_status_type_get_attr( st->data, "message" ) ) |
---|
| 196 | acc->flags |= ACC_FLAG_STATUS_MESSAGE; |
---|
| 197 | } |
---|
| 198 | else if( prim != PURPLE_STATUS_OFFLINE ) |
---|
| 199 | { |
---|
| 200 | if( purple_status_type_get_attr( st->data, "message" ) ) |
---|
| 201 | acc->flags |= ACC_FLAG_AWAY_MESSAGE; |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | purple_accounts_remove( pa ); |
---|
[796da03] | 205 | } |
---|
| 206 | |
---|
[b74b287] | 207 | static void purple_sync_settings( account_t *acc, PurpleAccount *pa ) |
---|
| 208 | { |
---|
| 209 | PurplePlugin *prpl = purple_plugins_find_with_id( pa->protocol_id ); |
---|
| 210 | PurplePluginProtocolInfo *pi = prpl->info->extra_info; |
---|
| 211 | GList *i; |
---|
| 212 | |
---|
| 213 | for( i = pi->protocol_options; i; i = i->next ) |
---|
| 214 | { |
---|
| 215 | PurpleAccountOption *o = i->data; |
---|
| 216 | const char *name; |
---|
| 217 | set_t *s; |
---|
| 218 | |
---|
| 219 | name = purple_account_option_get_setting( o ); |
---|
| 220 | s = set_find( &acc->set, name ); |
---|
| 221 | if( s->value == NULL ) |
---|
| 222 | continue; |
---|
| 223 | |
---|
| 224 | switch( purple_account_option_get_type( o ) ) |
---|
| 225 | { |
---|
| 226 | case PURPLE_PREF_STRING: |
---|
[4dc6b8d] | 227 | case PURPLE_PREF_STRING_LIST: |
---|
[b74b287] | 228 | purple_account_set_string( pa, name, set_getstr( &acc->set, name ) ); |
---|
| 229 | break; |
---|
| 230 | |
---|
| 231 | case PURPLE_PREF_INT: |
---|
| 232 | purple_account_set_int( pa, name, set_getint( &acc->set, name ) ); |
---|
| 233 | break; |
---|
| 234 | |
---|
| 235 | case PURPLE_PREF_BOOLEAN: |
---|
| 236 | purple_account_set_bool( pa, name, set_getbool( &acc->set, name ) ); |
---|
| 237 | break; |
---|
| 238 | |
---|
| 239 | default: |
---|
| 240 | break; |
---|
| 241 | } |
---|
| 242 | } |
---|
[bab1c86] | 243 | |
---|
| 244 | if( pi->options & OPT_PROTO_MAIL_CHECK ) |
---|
| 245 | purple_account_set_check_mail( pa, set_getbool( &acc->set, "mail_notifications" ) ); |
---|
[b74b287] | 246 | } |
---|
| 247 | |
---|
[796da03] | 248 | static void purple_login( account_t *acc ) |
---|
| 249 | { |
---|
| 250 | struct im_connection *ic = imcb_new( acc ); |
---|
[860ba6a] | 251 | PurpleAccount *pa; |
---|
[796da03] | 252 | |
---|
[0ac1a375] | 253 | if( local_irc != NULL && local_irc != acc->irc ) |
---|
[6967d01] | 254 | { |
---|
[0ac1a375] | 255 | irc_usermsg( acc->irc, "Daemon mode detected. Do *not* try to use libpurple in daemon mode! " |
---|
| 256 | "Please use inetd or ForkDaemon mode instead." ); |
---|
[6967d01] | 257 | return; |
---|
| 258 | } |
---|
[0ac1a375] | 259 | local_irc = acc->irc; |
---|
[6967d01] | 260 | |
---|
[796da03] | 261 | /* For now this is needed in the _connected() handlers if using |
---|
| 262 | GLib event handling, to make sure we're not handling events |
---|
| 263 | on dead connections. */ |
---|
| 264 | purple_connections = g_slist_prepend( purple_connections, ic ); |
---|
| 265 | |
---|
[cd741d8] | 266 | ic->proto_data = pa = purple_account_new( acc->user, (char*) acc->prpl->data ); |
---|
[860ba6a] | 267 | purple_account_set_password( pa, acc->pass ); |
---|
[b74b287] | 268 | purple_sync_settings( acc, pa ); |
---|
[860ba6a] | 269 | |
---|
| 270 | purple_account_set_enabled( pa, "BitlBee", TRUE ); |
---|
[796da03] | 271 | } |
---|
| 272 | |
---|
| 273 | static void purple_logout( struct im_connection *ic ) |
---|
| 274 | { |
---|
[db4cd40] | 275 | PurpleAccount *pa = ic->proto_data; |
---|
| 276 | |
---|
| 277 | purple_account_set_enabled( pa, "BitlBee", FALSE ); |
---|
[796da03] | 278 | purple_connections = g_slist_remove( purple_connections, ic ); |
---|
[b74b287] | 279 | purple_accounts_remove( pa ); |
---|
[796da03] | 280 | } |
---|
| 281 | |
---|
| 282 | static int purple_buddy_msg( struct im_connection *ic, char *who, char *message, int flags ) |
---|
| 283 | { |
---|
[389f7be] | 284 | PurpleConversation *conv; |
---|
| 285 | |
---|
| 286 | if( ( conv = purple_find_conversation_with_account( PURPLE_CONV_TYPE_IM, |
---|
| 287 | who, ic->proto_data ) ) == NULL ) |
---|
| 288 | { |
---|
| 289 | conv = purple_conversation_new( PURPLE_CONV_TYPE_IM, |
---|
| 290 | ic->proto_data, who ); |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | purple_conv_im_send( purple_conversation_get_im_data( conv ), message ); |
---|
[0cbef26] | 294 | |
---|
| 295 | return 1; |
---|
[796da03] | 296 | } |
---|
| 297 | |
---|
| 298 | static GList *purple_away_states( struct im_connection *ic ) |
---|
| 299 | { |
---|
[ec5e57d] | 300 | PurpleAccount *pa = ic->proto_data; |
---|
| 301 | GList *st, *ret = NULL; |
---|
| 302 | |
---|
| 303 | for( st = purple_account_get_status_types( pa ); st; st = st->next ) |
---|
[279607e] | 304 | { |
---|
| 305 | PurpleStatusPrimitive prim = purple_status_type_get_primitive( st->data ); |
---|
| 306 | if( prim != PURPLE_STATUS_AVAILABLE && prim != PURPLE_STATUS_OFFLINE ) |
---|
| 307 | ret = g_list_append( ret, (void*) purple_status_type_get_name( st->data ) ); |
---|
| 308 | } |
---|
[ec5e57d] | 309 | |
---|
| 310 | return ret; |
---|
[796da03] | 311 | } |
---|
| 312 | |
---|
| 313 | static void purple_set_away( struct im_connection *ic, char *state_txt, char *message ) |
---|
| 314 | { |
---|
[ec5e57d] | 315 | PurpleAccount *pa = ic->proto_data; |
---|
| 316 | GList *status_types = purple_account_get_status_types( pa ), *st; |
---|
| 317 | PurpleStatusType *pst = NULL; |
---|
[279607e] | 318 | GList *args = NULL; |
---|
[ec5e57d] | 319 | |
---|
| 320 | for( st = status_types; st; st = st->next ) |
---|
| 321 | { |
---|
| 322 | pst = st->data; |
---|
| 323 | |
---|
[279607e] | 324 | if( state_txt == NULL && |
---|
[bab1c86] | 325 | purple_status_type_get_primitive( pst ) == PURPLE_STATUS_AVAILABLE ) |
---|
[279607e] | 326 | break; |
---|
| 327 | |
---|
| 328 | if( state_txt != NULL && |
---|
| 329 | g_strcasecmp( state_txt, purple_status_type_get_name( pst ) ) == 0 ) |
---|
[ec5e57d] | 330 | break; |
---|
| 331 | } |
---|
| 332 | |
---|
[bab1c86] | 333 | if( message && purple_status_type_get_attr( pst, "message" ) ) |
---|
[279607e] | 334 | { |
---|
| 335 | args = g_list_append( args, "message" ); |
---|
| 336 | args = g_list_append( args, message ); |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | purple_account_set_status_list( pa, st ? purple_status_type_get_id( pst ) : "away", |
---|
| 340 | TRUE, args ); |
---|
| 341 | |
---|
| 342 | g_list_free( args ); |
---|
[796da03] | 343 | } |
---|
| 344 | |
---|
[f85e9d6] | 345 | static char *set_eval_display_name( set_t *set, char *value ) |
---|
| 346 | { |
---|
| 347 | account_t *acc = set->data; |
---|
| 348 | struct im_connection *ic = acc->ic; |
---|
| 349 | |
---|
| 350 | return NULL; |
---|
| 351 | } |
---|
| 352 | |
---|
[796da03] | 353 | static void purple_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 354 | { |
---|
[b3117f2] | 355 | PurpleBuddy *pb; |
---|
| 356 | |
---|
| 357 | pb = purple_buddy_new( (PurpleAccount*) ic->proto_data, who, NULL ); |
---|
| 358 | purple_blist_add_buddy( pb, NULL, NULL, NULL ); |
---|
| 359 | purple_account_add_buddy( (PurpleAccount*) ic->proto_data, pb ); |
---|
[796da03] | 360 | } |
---|
| 361 | |
---|
| 362 | static void purple_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 363 | { |
---|
[b3117f2] | 364 | PurpleBuddy *pb; |
---|
| 365 | |
---|
| 366 | pb = purple_find_buddy( (PurpleAccount*) ic->proto_data, who ); |
---|
| 367 | if( pb != NULL ) |
---|
| 368 | { |
---|
| 369 | purple_account_remove_buddy( (PurpleAccount*) ic->proto_data, pb, NULL ); |
---|
| 370 | purple_blist_remove_buddy( pb ); |
---|
| 371 | } |
---|
[796da03] | 372 | } |
---|
| 373 | |
---|
[05a8932] | 374 | static void purple_add_permit( struct im_connection *ic, char *who ) |
---|
| 375 | { |
---|
| 376 | PurpleAccount *pa = ic->proto_data; |
---|
| 377 | |
---|
| 378 | purple_privacy_permit_add( pa, who, FALSE ); |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | static void purple_add_deny( struct im_connection *ic, char *who ) |
---|
| 382 | { |
---|
| 383 | PurpleAccount *pa = ic->proto_data; |
---|
| 384 | |
---|
| 385 | purple_privacy_deny_add( pa, who, FALSE ); |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | static void purple_rem_permit( struct im_connection *ic, char *who ) |
---|
| 389 | { |
---|
| 390 | PurpleAccount *pa = ic->proto_data; |
---|
| 391 | |
---|
| 392 | purple_privacy_permit_remove( pa, who, FALSE ); |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | static void purple_rem_deny( struct im_connection *ic, char *who ) |
---|
| 396 | { |
---|
| 397 | PurpleAccount *pa = ic->proto_data; |
---|
| 398 | |
---|
| 399 | purple_privacy_deny_remove( pa, who, FALSE ); |
---|
| 400 | } |
---|
| 401 | |
---|
[e77c264] | 402 | static void purple_get_info( struct im_connection *ic, char *who ) |
---|
| 403 | { |
---|
| 404 | serv_get_info( purple_account_get_connection( ic->proto_data ), who ); |
---|
| 405 | } |
---|
| 406 | |
---|
[796da03] | 407 | static void purple_keepalive( struct im_connection *ic ) |
---|
| 408 | { |
---|
| 409 | } |
---|
| 410 | |
---|
[487f555] | 411 | static int purple_send_typing( struct im_connection *ic, char *who, int flags ) |
---|
[796da03] | 412 | { |
---|
[487f555] | 413 | PurpleTypingState state = PURPLE_NOT_TYPING; |
---|
| 414 | PurpleConversation *conv; |
---|
| 415 | |
---|
| 416 | if( flags & OPT_TYPING ) |
---|
| 417 | state = PURPLE_TYPING; |
---|
| 418 | else if( flags & OPT_THINKING ) |
---|
| 419 | state = PURPLE_TYPED; |
---|
| 420 | |
---|
| 421 | if( ( conv = purple_find_conversation_with_account( PURPLE_CONV_TYPE_IM, |
---|
| 422 | who, ic->proto_data ) ) == NULL ) |
---|
| 423 | { |
---|
| 424 | purple_conv_im_set_typing_state( purple_conversation_get_im_data( conv ), state ); |
---|
| 425 | return 1; |
---|
| 426 | } |
---|
| 427 | else |
---|
| 428 | { |
---|
| 429 | return 0; |
---|
| 430 | } |
---|
[796da03] | 431 | } |
---|
| 432 | |
---|
[f485008] | 433 | static void purple_chat_msg( struct groupchat *gc, char *message, int flags ) |
---|
| 434 | { |
---|
| 435 | PurpleConversation *pc = gc->data; |
---|
| 436 | |
---|
| 437 | purple_conv_chat_send( purple_conversation_get_chat_data( pc ), message ); |
---|
| 438 | } |
---|
| 439 | |
---|
[8ad5c34] | 440 | struct groupchat *purple_chat_with( struct im_connection *ic, char *who ) |
---|
| 441 | { |
---|
| 442 | /* No, "of course" this won't work this way. Or in fact, it almost |
---|
| 443 | does, but it only lets you send msgs to it, you won't receive |
---|
| 444 | any. Instead, we have to click the virtual menu item. |
---|
| 445 | PurpleAccount *pa = ic->proto_data; |
---|
| 446 | PurpleConversation *pc; |
---|
| 447 | PurpleConvChat *pcc; |
---|
| 448 | struct groupchat *gc; |
---|
| 449 | |
---|
| 450 | gc = imcb_chat_new( ic, "BitlBee-libpurple groupchat" ); |
---|
| 451 | gc->data = pc = purple_conversation_new( PURPLE_CONV_TYPE_CHAT, pa, "BitlBee-libpurple groupchat" ); |
---|
| 452 | pc->ui_data = gc; |
---|
| 453 | |
---|
| 454 | pcc = PURPLE_CONV_CHAT( pc ); |
---|
| 455 | purple_conv_chat_add_user( pcc, ic->acc->user, "", 0, TRUE ); |
---|
| 456 | purple_conv_chat_invite_user( pcc, who, "Please join my chat", FALSE ); |
---|
| 457 | //purple_conv_chat_add_user( pcc, who, "", 0, TRUE ); |
---|
| 458 | */ |
---|
| 459 | |
---|
| 460 | /* There went my nice afternoon. :-( */ |
---|
| 461 | |
---|
| 462 | PurpleAccount *pa = ic->proto_data; |
---|
| 463 | PurplePlugin *prpl = purple_plugins_find_with_id( pa->protocol_id ); |
---|
| 464 | PurplePluginProtocolInfo *pi = prpl->info->extra_info; |
---|
| 465 | PurpleBuddy *pb = purple_find_buddy( (PurpleAccount*) ic->proto_data, who ); |
---|
| 466 | PurpleMenuAction *mi; |
---|
| 467 | GList *menu; |
---|
| 468 | void (*callback)(PurpleBlistNode *, gpointer); /* FFFFFFFFFFFFFUUUUUUUUUUUUUU */ |
---|
| 469 | |
---|
| 470 | if( !pb || !pi || !pi->blist_node_menu ) |
---|
| 471 | return NULL; |
---|
| 472 | |
---|
| 473 | menu = pi->blist_node_menu( &pb->node ); |
---|
| 474 | while( menu ) |
---|
| 475 | { |
---|
| 476 | mi = menu->data; |
---|
| 477 | if( purple_menu_cmp( mi->label, "initiate chat" ) || |
---|
| 478 | purple_menu_cmp( mi->label, "initiate conference" ) ) |
---|
| 479 | break; |
---|
| 480 | menu = menu->next; |
---|
| 481 | } |
---|
| 482 | |
---|
| 483 | if( menu == NULL ) |
---|
| 484 | return NULL; |
---|
| 485 | |
---|
| 486 | /* Call the fucker. */ |
---|
| 487 | callback = (void*) mi->callback; |
---|
| 488 | callback( &pb->node, menu->data ); |
---|
| 489 | |
---|
| 490 | return NULL; |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | void purple_chat_invite( struct groupchat *gc, char *who, char *message ) |
---|
| 494 | { |
---|
| 495 | PurpleConversation *pc = gc->data; |
---|
| 496 | PurpleConvChat *pcc = PURPLE_CONV_CHAT( pc ); |
---|
| 497 | |
---|
[5d1b3a95] | 498 | serv_chat_invite( purple_account_get_connection( gc->ic->proto_data ), |
---|
| 499 | purple_conv_chat_get_id( pcc ), |
---|
| 500 | message && *message ? message : "Please join my chat", |
---|
| 501 | who ); |
---|
[8ad5c34] | 502 | } |
---|
| 503 | |
---|
[c96c72f] | 504 | void purple_chat_leave( struct groupchat *gc ) |
---|
[15794dc] | 505 | { |
---|
| 506 | PurpleConversation *pc = gc->data; |
---|
| 507 | |
---|
| 508 | purple_conversation_destroy( pc ); |
---|
| 509 | } |
---|
| 510 | |
---|
[c3caa46] | 511 | struct groupchat *purple_chat_join( struct im_connection *ic, const char *room, const char *nick, const char *password ) |
---|
| 512 | { |
---|
| 513 | PurpleAccount *pa = ic->proto_data; |
---|
| 514 | PurplePlugin *prpl = purple_plugins_find_with_id( pa->protocol_id ); |
---|
| 515 | PurplePluginProtocolInfo *pi = prpl->info->extra_info; |
---|
| 516 | GHashTable *chat_hash; |
---|
| 517 | PurpleConversation *conv; |
---|
| 518 | GList *info, *l; |
---|
| 519 | |
---|
| 520 | if( !pi->chat_info || !pi->chat_info_defaults || |
---|
| 521 | !( info = pi->chat_info( purple_account_get_connection( pa ) ) ) ) |
---|
| 522 | { |
---|
| 523 | imcb_error( ic, "Joining chatrooms not supported by this protocol" ); |
---|
| 524 | return NULL; |
---|
| 525 | } |
---|
| 526 | |
---|
| 527 | if( ( conv = purple_find_conversation_with_account( PURPLE_CONV_TYPE_CHAT, room, pa ) ) ) |
---|
| 528 | purple_conversation_destroy( conv ); |
---|
| 529 | |
---|
| 530 | chat_hash = pi->chat_info_defaults( purple_account_get_connection( pa ), room ); |
---|
| 531 | |
---|
| 532 | for( l = info; l; l = l->next ) |
---|
| 533 | { |
---|
| 534 | struct proto_chat_entry *pce = l->data; |
---|
| 535 | |
---|
| 536 | if( strcmp( pce->identifier, "handle" ) == 0 ) |
---|
| 537 | g_hash_table_replace( chat_hash, "handle", g_strdup( nick ) ); |
---|
| 538 | else if( strcmp( pce->identifier, "password" ) == 0 ) |
---|
| 539 | g_hash_table_replace( chat_hash, "password", g_strdup( password ) ); |
---|
| 540 | else if( strcmp( pce->identifier, "passwd" ) == 0 ) |
---|
| 541 | g_hash_table_replace( chat_hash, "passwd", g_strdup( password ) ); |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | serv_join_chat( purple_account_get_connection( pa ), chat_hash ); |
---|
| 545 | |
---|
| 546 | return NULL; |
---|
| 547 | } |
---|
| 548 | |
---|
[edfc6db] | 549 | void purple_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *handle ); |
---|
| 550 | |
---|
[860ba6a] | 551 | static void purple_ui_init(); |
---|
| 552 | |
---|
[dca8eff] | 553 | GHashTable *prplcb_ui_info() |
---|
| 554 | { |
---|
| 555 | static GHashTable *ret; |
---|
| 556 | |
---|
| 557 | if( ret == NULL ) |
---|
| 558 | { |
---|
| 559 | ret = g_hash_table_new(g_str_hash, g_str_equal); |
---|
| 560 | g_hash_table_insert( ret, "name", "BitlBee" ); |
---|
| 561 | g_hash_table_insert( ret, "version", BITLBEE_VERSION ); |
---|
| 562 | } |
---|
| 563 | |
---|
| 564 | return ret; |
---|
| 565 | } |
---|
| 566 | |
---|
[860ba6a] | 567 | static PurpleCoreUiOps bee_core_uiops = |
---|
| 568 | { |
---|
| 569 | NULL, |
---|
| 570 | NULL, |
---|
| 571 | purple_ui_init, |
---|
| 572 | NULL, |
---|
[dca8eff] | 573 | prplcb_ui_info, |
---|
[860ba6a] | 574 | }; |
---|
| 575 | |
---|
| 576 | static void prplcb_conn_progress( PurpleConnection *gc, const char *text, size_t step, size_t step_count ) |
---|
| 577 | { |
---|
[0cbef26] | 578 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
| 579 | |
---|
| 580 | imcb_log( ic, "%s", text ); |
---|
[860ba6a] | 581 | } |
---|
| 582 | |
---|
| 583 | static void prplcb_conn_connected( PurpleConnection *gc ) |
---|
| 584 | { |
---|
[d250b2a] | 585 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
[f85e9d6] | 586 | const char *dn; |
---|
| 587 | set_t *s; |
---|
[d250b2a] | 588 | |
---|
| 589 | imcb_connected( ic ); |
---|
| 590 | |
---|
[f85e9d6] | 591 | if( ( dn = purple_connection_get_display_name( gc ) ) && |
---|
| 592 | ( s = set_find( &ic->acc->set, "display_name" ) ) ) |
---|
| 593 | { |
---|
| 594 | g_free( s->value ); |
---|
| 595 | s->value = g_strdup( dn ); |
---|
| 596 | } |
---|
| 597 | |
---|
[d250b2a] | 598 | if( gc->flags & PURPLE_CONNECTION_HTML ) |
---|
| 599 | ic->flags |= OPT_DOES_HTML; |
---|
[860ba6a] | 600 | } |
---|
| 601 | |
---|
| 602 | static void prplcb_conn_disconnected( PurpleConnection *gc ) |
---|
| 603 | { |
---|
[0cbef26] | 604 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
| 605 | |
---|
| 606 | if( ic != NULL ) |
---|
[b74b287] | 607 | { |
---|
[31fc06fb] | 608 | imc_logout( ic, !gc->wants_to_die ); |
---|
[b74b287] | 609 | } |
---|
[860ba6a] | 610 | } |
---|
| 611 | |
---|
| 612 | static void prplcb_conn_notice( PurpleConnection *gc, const char *text ) |
---|
| 613 | { |
---|
[0cbef26] | 614 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
| 615 | |
---|
| 616 | if( ic != NULL ) |
---|
| 617 | imcb_log( ic, "%s", text ); |
---|
[860ba6a] | 618 | } |
---|
| 619 | |
---|
| 620 | static void prplcb_conn_report_disconnect_reason( PurpleConnection *gc, PurpleConnectionError reason, const char *text ) |
---|
| 621 | { |
---|
[0cbef26] | 622 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
| 623 | |
---|
[860ba6a] | 624 | /* PURPLE_CONNECTION_ERROR_NAME_IN_USE means concurrent login, |
---|
| 625 | should probably handle that. */ |
---|
[0cbef26] | 626 | if( ic != NULL ) |
---|
| 627 | imcb_error( ic, "%s", text ); |
---|
[860ba6a] | 628 | } |
---|
| 629 | |
---|
| 630 | static PurpleConnectionUiOps bee_conn_uiops = |
---|
| 631 | { |
---|
| 632 | prplcb_conn_progress, |
---|
| 633 | prplcb_conn_connected, |
---|
| 634 | prplcb_conn_disconnected, |
---|
| 635 | prplcb_conn_notice, |
---|
| 636 | NULL, |
---|
| 637 | NULL, |
---|
| 638 | NULL, |
---|
| 639 | prplcb_conn_report_disconnect_reason, |
---|
| 640 | }; |
---|
| 641 | |
---|
[7da726b] | 642 | static void prplcb_blist_new( PurpleBlistNode *node ) |
---|
| 643 | { |
---|
| 644 | PurpleBuddy *bud = (PurpleBuddy*) node; |
---|
| 645 | |
---|
[db4cd40] | 646 | if( node->type == PURPLE_BLIST_BUDDY_NODE ) |
---|
[7da726b] | 647 | { |
---|
[db4cd40] | 648 | struct im_connection *ic = purple_ic_by_pa( bud->account ); |
---|
| 649 | |
---|
| 650 | if( ic == NULL ) |
---|
| 651 | return; |
---|
| 652 | |
---|
[7da726b] | 653 | imcb_add_buddy( ic, bud->name, NULL ); |
---|
| 654 | if( bud->server_alias ) |
---|
[4524f66] | 655 | { |
---|
| 656 | imcb_rename_buddy( ic, bud->name, bud->server_alias ); |
---|
[7da726b] | 657 | imcb_buddy_nick_hint( ic, bud->name, bud->server_alias ); |
---|
[4524f66] | 658 | } |
---|
[7da726b] | 659 | } |
---|
| 660 | } |
---|
| 661 | |
---|
| 662 | static void prplcb_blist_update( PurpleBuddyList *list, PurpleBlistNode *node ) |
---|
| 663 | { |
---|
| 664 | PurpleBuddy *bud = (PurpleBuddy*) node; |
---|
| 665 | |
---|
[db4cd40] | 666 | if( node->type == PURPLE_BLIST_BUDDY_NODE ) |
---|
[7da726b] | 667 | { |
---|
[db4cd40] | 668 | struct im_connection *ic = purple_ic_by_pa( bud->account ); |
---|
[4f103ea] | 669 | PurpleStatus *as; |
---|
| 670 | int flags = 0; |
---|
| 671 | |
---|
[db4cd40] | 672 | if( ic == NULL ) |
---|
| 673 | return; |
---|
| 674 | |
---|
[4524f66] | 675 | if( bud->server_alias ) |
---|
| 676 | imcb_rename_buddy( ic, bud->name, bud->server_alias ); |
---|
| 677 | |
---|
[4f103ea] | 678 | flags |= purple_presence_is_online( bud->presence ) ? OPT_LOGGED_IN : 0; |
---|
| 679 | flags |= purple_presence_is_available( bud->presence ) ? 0 : OPT_AWAY; |
---|
| 680 | |
---|
| 681 | as = purple_presence_get_active_status( bud->presence ); |
---|
| 682 | |
---|
| 683 | imcb_buddy_status( ic, bud->name, flags, purple_status_get_name( as ), |
---|
| 684 | purple_status_get_attr_string( as, "message" ) ); |
---|
[7da726b] | 685 | } |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | static void prplcb_blist_remove( PurpleBuddyList *list, PurpleBlistNode *node ) |
---|
| 689 | { |
---|
[3e7b640] | 690 | /* |
---|
[7da726b] | 691 | PurpleBuddy *bud = (PurpleBuddy*) node; |
---|
| 692 | |
---|
[db4cd40] | 693 | if( node->type == PURPLE_BLIST_BUDDY_NODE ) |
---|
[0cbef26] | 694 | { |
---|
[db4cd40] | 695 | struct im_connection *ic = purple_ic_by_pa( bud->account ); |
---|
| 696 | |
---|
| 697 | if( ic == NULL ) |
---|
| 698 | return; |
---|
| 699 | |
---|
[0cbef26] | 700 | imcb_remove_buddy( ic, bud->name, NULL ); |
---|
| 701 | } |
---|
[b3117f2] | 702 | */ |
---|
[7da726b] | 703 | } |
---|
| 704 | |
---|
| 705 | static PurpleBlistUiOps bee_blist_uiops = |
---|
| 706 | { |
---|
| 707 | NULL, |
---|
| 708 | prplcb_blist_new, |
---|
| 709 | NULL, |
---|
| 710 | prplcb_blist_update, |
---|
| 711 | prplcb_blist_remove, |
---|
| 712 | }; |
---|
| 713 | |
---|
[f485008] | 714 | void prplcb_conv_new( PurpleConversation *conv ) |
---|
| 715 | { |
---|
| 716 | if( conv->type == PURPLE_CONV_TYPE_CHAT ) |
---|
| 717 | { |
---|
| 718 | struct im_connection *ic = purple_ic_by_pa( conv->account ); |
---|
| 719 | struct groupchat *gc; |
---|
| 720 | |
---|
| 721 | gc = imcb_chat_new( ic, conv->name ); |
---|
| 722 | conv->ui_data = gc; |
---|
| 723 | gc->data = conv; |
---|
[c3caa46] | 724 | |
---|
| 725 | /* libpurple brokenness: Whatever. Show that we join right away, |
---|
| 726 | there's no clear "This is you!" signaling in _add_users so |
---|
| 727 | don't even try. */ |
---|
| 728 | imcb_chat_add_buddy( gc, gc->ic->acc->user ); |
---|
[f485008] | 729 | } |
---|
| 730 | } |
---|
| 731 | |
---|
| 732 | void prplcb_conv_free( PurpleConversation *conv ) |
---|
| 733 | { |
---|
| 734 | struct groupchat *gc = conv->ui_data; |
---|
| 735 | |
---|
| 736 | imcb_chat_free( gc ); |
---|
| 737 | } |
---|
| 738 | |
---|
| 739 | void prplcb_conv_add_users( PurpleConversation *conv, GList *cbuddies, gboolean new_arrivals ) |
---|
| 740 | { |
---|
| 741 | struct groupchat *gc = conv->ui_data; |
---|
| 742 | GList *b; |
---|
| 743 | |
---|
| 744 | for( b = cbuddies; b; b = b->next ) |
---|
| 745 | { |
---|
| 746 | PurpleConvChatBuddy *pcb = b->data; |
---|
| 747 | |
---|
| 748 | imcb_chat_add_buddy( gc, pcb->name ); |
---|
| 749 | } |
---|
| 750 | } |
---|
| 751 | |
---|
| 752 | void prplcb_conv_del_users( PurpleConversation *conv, GList *cbuddies ) |
---|
| 753 | { |
---|
| 754 | struct groupchat *gc = conv->ui_data; |
---|
| 755 | GList *b; |
---|
| 756 | |
---|
| 757 | for( b = cbuddies; b; b = b->next ) |
---|
| 758 | imcb_chat_remove_buddy( gc, b->data, "" ); |
---|
| 759 | } |
---|
| 760 | |
---|
| 761 | void prplcb_conv_chat_msg( PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, time_t mtime ) |
---|
| 762 | { |
---|
| 763 | struct groupchat *gc = conv->ui_data; |
---|
| 764 | PurpleBuddy *buddy; |
---|
| 765 | |
---|
| 766 | /* ..._SEND means it's an outgoing message, no need to echo those. */ |
---|
| 767 | if( flags & PURPLE_MESSAGE_SEND ) |
---|
| 768 | return; |
---|
| 769 | |
---|
| 770 | buddy = purple_find_buddy( conv->account, who ); |
---|
| 771 | if( buddy != NULL ) |
---|
| 772 | who = purple_buddy_get_name( buddy ); |
---|
| 773 | |
---|
| 774 | imcb_chat_msg( gc, who, (char*) message, 0, mtime ); |
---|
| 775 | } |
---|
| 776 | |
---|
[d250b2a] | 777 | static void prplcb_conv_im( PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, time_t mtime ) |
---|
| 778 | { |
---|
| 779 | struct im_connection *ic = purple_ic_by_pa( conv->account ); |
---|
[3e7b640] | 780 | PurpleBuddy *buddy; |
---|
[d250b2a] | 781 | |
---|
[389f7be] | 782 | /* ..._SEND means it's an outgoing message, no need to echo those. */ |
---|
[3e7b640] | 783 | if( flags & PURPLE_MESSAGE_SEND ) |
---|
| 784 | return; |
---|
| 785 | |
---|
| 786 | buddy = purple_find_buddy( conv->account, who ); |
---|
| 787 | if( buddy != NULL ) |
---|
[a19ea7a] | 788 | who = purple_buddy_get_name( buddy ); |
---|
[3e7b640] | 789 | |
---|
| 790 | imcb_buddy_msg( ic, (char*) who, (char*) message, 0, mtime ); |
---|
[d250b2a] | 791 | } |
---|
| 792 | |
---|
[860ba6a] | 793 | static PurpleConversationUiOps bee_conv_uiops = |
---|
| 794 | { |
---|
[f485008] | 795 | prplcb_conv_new, /* create_conversation */ |
---|
| 796 | prplcb_conv_free, /* destroy_conversation */ |
---|
| 797 | prplcb_conv_chat_msg, /* write_chat */ |
---|
[d250b2a] | 798 | prplcb_conv_im, /* write_im */ |
---|
[e046390] | 799 | NULL, /* write_conv */ |
---|
[f485008] | 800 | prplcb_conv_add_users, /* chat_add_users */ |
---|
[860ba6a] | 801 | NULL, /* chat_rename_user */ |
---|
[f485008] | 802 | prplcb_conv_del_users, /* chat_remove_users */ |
---|
[860ba6a] | 803 | NULL, /* chat_update_user */ |
---|
| 804 | NULL, /* present */ |
---|
| 805 | NULL, /* has_focus */ |
---|
| 806 | NULL, /* custom_smiley_add */ |
---|
| 807 | NULL, /* custom_smiley_write */ |
---|
| 808 | NULL, /* custom_smiley_close */ |
---|
| 809 | NULL, /* send_confirm */ |
---|
| 810 | }; |
---|
| 811 | |
---|
[0ac1a375] | 812 | struct prplcb_request_action_data |
---|
| 813 | { |
---|
| 814 | void *user_data, *bee_data; |
---|
| 815 | PurpleRequestActionCb yes, no; |
---|
| 816 | int yes_i, no_i; |
---|
| 817 | }; |
---|
| 818 | |
---|
| 819 | static void prplcb_request_action_yes( void *data ) |
---|
| 820 | { |
---|
| 821 | struct prplcb_request_action_data *pqad = data; |
---|
| 822 | |
---|
| 823 | pqad->yes( pqad->user_data, pqad->yes_i ); |
---|
| 824 | g_free( pqad ); |
---|
| 825 | } |
---|
| 826 | |
---|
| 827 | static void prplcb_request_action_no( void *data ) |
---|
| 828 | { |
---|
| 829 | struct prplcb_request_action_data *pqad = data; |
---|
| 830 | |
---|
| 831 | pqad->no( pqad->user_data, pqad->no_i ); |
---|
| 832 | g_free( pqad ); |
---|
| 833 | } |
---|
| 834 | |
---|
| 835 | static void *prplcb_request_action( const char *title, const char *primary, const char *secondary, |
---|
| 836 | int default_action, PurpleAccount *account, const char *who, |
---|
| 837 | PurpleConversation *conv, void *user_data, size_t action_count, |
---|
| 838 | va_list actions ) |
---|
| 839 | { |
---|
| 840 | struct prplcb_request_action_data *pqad; |
---|
| 841 | int i; |
---|
| 842 | char *q; |
---|
| 843 | |
---|
| 844 | pqad = g_new0( struct prplcb_request_action_data, 1 ); |
---|
| 845 | |
---|
| 846 | for( i = 0; i < action_count; i ++ ) |
---|
| 847 | { |
---|
| 848 | char *caption; |
---|
| 849 | void *fn; |
---|
| 850 | |
---|
| 851 | caption = va_arg( actions, char* ); |
---|
| 852 | fn = va_arg( actions, void* ); |
---|
| 853 | |
---|
[437bd9b] | 854 | if( strstr( caption, "Accept" ) ) |
---|
[0ac1a375] | 855 | { |
---|
| 856 | pqad->yes = fn; |
---|
| 857 | pqad->yes_i = i; |
---|
| 858 | } |
---|
[437bd9b] | 859 | else if( strstr( caption, "Reject" ) || strstr( caption, "Cancel" ) ) |
---|
[0ac1a375] | 860 | { |
---|
| 861 | pqad->no = fn; |
---|
| 862 | pqad->no_i = i; |
---|
| 863 | } |
---|
| 864 | } |
---|
| 865 | |
---|
| 866 | pqad->user_data = user_data; |
---|
| 867 | |
---|
| 868 | q = g_strdup_printf( "Request: %s\n\n%s\n\n%s", title, primary, secondary ); |
---|
| 869 | pqad->bee_data = query_add( local_irc, purple_ic_by_pa( account ), q, |
---|
| 870 | prplcb_request_action_yes, prplcb_request_action_no, pqad ); |
---|
| 871 | |
---|
| 872 | g_free( q ); |
---|
[e5d8d21] | 873 | |
---|
| 874 | return pqad; |
---|
[0ac1a375] | 875 | } |
---|
| 876 | |
---|
| 877 | static PurpleRequestUiOps bee_request_uiops = |
---|
| 878 | { |
---|
| 879 | NULL, |
---|
| 880 | NULL, |
---|
| 881 | prplcb_request_action, |
---|
| 882 | NULL, |
---|
| 883 | NULL, |
---|
| 884 | NULL, |
---|
| 885 | NULL, |
---|
| 886 | }; |
---|
| 887 | |
---|
[05a8932] | 888 | static void prplcb_privacy_permit_added( PurpleAccount *account, const char *name ) |
---|
| 889 | { |
---|
| 890 | struct im_connection *ic = purple_ic_by_pa( account ); |
---|
| 891 | |
---|
| 892 | if( !g_slist_find_custom( ic->permit, name, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) |
---|
| 893 | ic->permit = g_slist_prepend( ic->permit, g_strdup( name ) ); |
---|
| 894 | } |
---|
| 895 | |
---|
| 896 | static void prplcb_privacy_permit_removed( PurpleAccount *account, const char *name ) |
---|
| 897 | { |
---|
| 898 | struct im_connection *ic = purple_ic_by_pa( account ); |
---|
| 899 | void *n; |
---|
| 900 | |
---|
| 901 | n = g_slist_find_custom( ic->permit, name, (GCompareFunc) ic->acc->prpl->handle_cmp ); |
---|
| 902 | ic->permit = g_slist_remove( ic->permit, n ); |
---|
| 903 | } |
---|
| 904 | |
---|
| 905 | static void prplcb_privacy_deny_added( PurpleAccount *account, const char *name ) |
---|
| 906 | { |
---|
| 907 | struct im_connection *ic = purple_ic_by_pa( account ); |
---|
| 908 | |
---|
| 909 | if( !g_slist_find_custom( ic->deny, name, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) |
---|
| 910 | ic->deny = g_slist_prepend( ic->deny, g_strdup( name ) ); |
---|
| 911 | } |
---|
| 912 | |
---|
| 913 | static void prplcb_privacy_deny_removed( PurpleAccount *account, const char *name ) |
---|
| 914 | { |
---|
| 915 | struct im_connection *ic = purple_ic_by_pa( account ); |
---|
| 916 | void *n; |
---|
| 917 | |
---|
| 918 | n = g_slist_find_custom( ic->deny, name, (GCompareFunc) ic->acc->prpl->handle_cmp ); |
---|
| 919 | ic->deny = g_slist_remove( ic->deny, n ); |
---|
| 920 | } |
---|
| 921 | |
---|
| 922 | static PurplePrivacyUiOps bee_privacy_uiops = |
---|
| 923 | { |
---|
| 924 | prplcb_privacy_permit_added, |
---|
| 925 | prplcb_privacy_permit_removed, |
---|
| 926 | prplcb_privacy_deny_added, |
---|
| 927 | prplcb_privacy_deny_removed, |
---|
| 928 | }; |
---|
| 929 | |
---|
[0cbef26] | 930 | static void prplcb_debug_print( PurpleDebugLevel level, const char *category, const char *arg_s ) |
---|
| 931 | { |
---|
[6967d01] | 932 | fprintf( stderr, "DEBUG %s: %s", category, arg_s ); |
---|
[0cbef26] | 933 | } |
---|
| 934 | |
---|
| 935 | static PurpleDebugUiOps bee_debug_uiops = |
---|
| 936 | { |
---|
| 937 | prplcb_debug_print, |
---|
| 938 | }; |
---|
| 939 | |
---|
[4164e62] | 940 | static guint prplcb_ev_timeout_add( guint interval, GSourceFunc func, gpointer udata ) |
---|
| 941 | { |
---|
| 942 | return b_timeout_add( interval, (b_event_handler) func, udata ); |
---|
| 943 | } |
---|
| 944 | |
---|
| 945 | static guint prplcb_ev_input_add( int fd, PurpleInputCondition cond, PurpleInputFunction func, gpointer udata ) |
---|
| 946 | { |
---|
| 947 | return b_input_add( fd, cond | B_EV_FLAG_FORCE_REPEAT, (b_event_handler) func, udata ); |
---|
| 948 | } |
---|
| 949 | |
---|
| 950 | static gboolean prplcb_ev_remove( guint id ) |
---|
| 951 | { |
---|
| 952 | b_event_remove( (gint) id ); |
---|
| 953 | return TRUE; |
---|
| 954 | } |
---|
| 955 | |
---|
| 956 | static PurpleEventLoopUiOps glib_eventloops = |
---|
| 957 | { |
---|
| 958 | prplcb_ev_timeout_add, |
---|
| 959 | prplcb_ev_remove, |
---|
| 960 | prplcb_ev_input_add, |
---|
| 961 | prplcb_ev_remove, |
---|
| 962 | }; |
---|
| 963 | |
---|
[bab1c86] | 964 | static void *prplcb_notify_email( PurpleConnection *gc, const char *subject, const char *from, |
---|
| 965 | const char *to, const char *url ) |
---|
| 966 | { |
---|
| 967 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
| 968 | |
---|
| 969 | imcb_log( ic, "Received e-mail from %s for %s: %s <%s>", from, to, subject, url ); |
---|
| 970 | |
---|
| 971 | return NULL; |
---|
| 972 | } |
---|
| 973 | |
---|
[e77c264] | 974 | static void *prplcb_notify_userinfo( PurpleConnection *gc, const char *who, PurpleNotifyUserInfo *user_info ) |
---|
| 975 | { |
---|
| 976 | struct im_connection *ic = purple_ic_by_gc( gc ); |
---|
| 977 | GString *info = g_string_new( "" ); |
---|
| 978 | GList *l = purple_notify_user_info_get_entries( user_info ); |
---|
| 979 | char *key; |
---|
| 980 | const char *value; |
---|
| 981 | int n; |
---|
| 982 | |
---|
| 983 | while( l ) |
---|
| 984 | { |
---|
| 985 | PurpleNotifyUserInfoEntry *e = l->data; |
---|
| 986 | |
---|
| 987 | switch( purple_notify_user_info_entry_get_type( e ) ) |
---|
| 988 | { |
---|
| 989 | case PURPLE_NOTIFY_USER_INFO_ENTRY_PAIR: |
---|
| 990 | case PURPLE_NOTIFY_USER_INFO_ENTRY_SECTION_HEADER: |
---|
| 991 | key = g_strdup( purple_notify_user_info_entry_get_label( e ) ); |
---|
| 992 | value = purple_notify_user_info_entry_get_value( e ); |
---|
| 993 | |
---|
| 994 | if( key ) |
---|
| 995 | { |
---|
| 996 | strip_html( key ); |
---|
| 997 | g_string_append_printf( info, "%s: ", key ); |
---|
| 998 | |
---|
| 999 | if( value ) |
---|
| 1000 | { |
---|
| 1001 | n = strlen( value ) - 1; |
---|
| 1002 | while( isspace( value[n] ) ) |
---|
| 1003 | n --; |
---|
| 1004 | g_string_append_len( info, value, n + 1 ); |
---|
| 1005 | } |
---|
| 1006 | g_string_append_c( info, '\n' ); |
---|
| 1007 | g_free( key ); |
---|
| 1008 | } |
---|
| 1009 | |
---|
| 1010 | break; |
---|
| 1011 | case PURPLE_NOTIFY_USER_INFO_ENTRY_SECTION_BREAK: |
---|
| 1012 | g_string_append( info, "------------------------\n" ); |
---|
| 1013 | break; |
---|
| 1014 | } |
---|
| 1015 | |
---|
| 1016 | l = l->next; |
---|
| 1017 | } |
---|
| 1018 | |
---|
| 1019 | imcb_log( ic, "User %s info:\n%s", who, info->str ); |
---|
| 1020 | g_string_free( info, TRUE ); |
---|
| 1021 | |
---|
| 1022 | return NULL; |
---|
| 1023 | } |
---|
| 1024 | |
---|
[437bd9b] | 1025 | static PurpleNotifyUiOps bee_notify_uiops = |
---|
[bab1c86] | 1026 | { |
---|
| 1027 | NULL, |
---|
| 1028 | prplcb_notify_email, |
---|
[e77c264] | 1029 | NULL, |
---|
| 1030 | NULL, |
---|
| 1031 | NULL, |
---|
| 1032 | NULL, |
---|
| 1033 | prplcb_notify_userinfo, |
---|
[bab1c86] | 1034 | }; |
---|
| 1035 | |
---|
[2309152] | 1036 | extern PurpleXferUiOps bee_xfer_uiops; |
---|
[edfc6db] | 1037 | |
---|
[860ba6a] | 1038 | static void purple_ui_init() |
---|
| 1039 | { |
---|
[7da726b] | 1040 | purple_blist_set_ui_ops( &bee_blist_uiops ); |
---|
[860ba6a] | 1041 | purple_connections_set_ui_ops( &bee_conn_uiops ); |
---|
| 1042 | purple_conversations_set_ui_ops( &bee_conv_uiops ); |
---|
[0ac1a375] | 1043 | purple_request_set_ui_ops( &bee_request_uiops ); |
---|
[437bd9b] | 1044 | purple_notify_set_ui_ops( &bee_notify_uiops ); |
---|
| 1045 | purple_xfers_set_ui_ops( &bee_xfer_uiops ); |
---|
[05a8932] | 1046 | purple_privacy_set_ui_ops( &bee_privacy_uiops ); |
---|
[2c5fabc] | 1047 | |
---|
| 1048 | if( getenv( "BITLBEE_DEBUG" ) ) |
---|
| 1049 | purple_debug_set_ui_ops( &bee_debug_uiops ); |
---|
[860ba6a] | 1050 | } |
---|
| 1051 | |
---|
[796da03] | 1052 | void purple_initmodule() |
---|
| 1053 | { |
---|
[cd741d8] | 1054 | struct prpl funcs; |
---|
[796da03] | 1055 | GList *prots; |
---|
[e5d8d21] | 1056 | GString *help; |
---|
[796da03] | 1057 | |
---|
[e046390] | 1058 | if( B_EV_IO_READ != PURPLE_INPUT_READ || |
---|
| 1059 | B_EV_IO_WRITE != PURPLE_INPUT_WRITE ) |
---|
| 1060 | { |
---|
| 1061 | /* FIXME FIXME FIXME FIXME FIXME :-) */ |
---|
| 1062 | exit( 1 ); |
---|
| 1063 | } |
---|
| 1064 | |
---|
[796da03] | 1065 | purple_util_set_user_dir("/tmp"); |
---|
| 1066 | purple_debug_set_enabled(FALSE); |
---|
| 1067 | purple_core_set_ui_ops(&bee_core_uiops); |
---|
| 1068 | purple_eventloop_set_ui_ops(&glib_eventloops); |
---|
| 1069 | if( !purple_core_init( "BitlBee") ) |
---|
| 1070 | { |
---|
| 1071 | /* Initializing the core failed. Terminate. */ |
---|
| 1072 | fprintf( stderr, "libpurple initialization failed.\n" ); |
---|
| 1073 | abort(); |
---|
| 1074 | } |
---|
| 1075 | |
---|
| 1076 | /* This seems like stateful shit we don't want... */ |
---|
| 1077 | purple_set_blist(purple_blist_new()); |
---|
| 1078 | purple_blist_load(); |
---|
| 1079 | |
---|
| 1080 | /* Meh? */ |
---|
| 1081 | purple_prefs_load(); |
---|
| 1082 | |
---|
[cd741d8] | 1083 | memset( &funcs, 0, sizeof( funcs ) ); |
---|
| 1084 | funcs.login = purple_login; |
---|
| 1085 | funcs.init = purple_init; |
---|
| 1086 | funcs.logout = purple_logout; |
---|
| 1087 | funcs.buddy_msg = purple_buddy_msg; |
---|
| 1088 | funcs.away_states = purple_away_states; |
---|
| 1089 | funcs.set_away = purple_set_away; |
---|
| 1090 | funcs.add_buddy = purple_add_buddy; |
---|
| 1091 | funcs.remove_buddy = purple_remove_buddy; |
---|
[05a8932] | 1092 | funcs.add_permit = purple_add_permit; |
---|
| 1093 | funcs.add_deny = purple_add_deny; |
---|
| 1094 | funcs.rem_permit = purple_rem_permit; |
---|
| 1095 | funcs.rem_deny = purple_rem_deny; |
---|
[e77c264] | 1096 | funcs.get_info = purple_get_info; |
---|
[cd741d8] | 1097 | funcs.keepalive = purple_keepalive; |
---|
| 1098 | funcs.send_typing = purple_send_typing; |
---|
| 1099 | funcs.handle_cmp = g_strcasecmp; |
---|
[f485008] | 1100 | /* TODO(wilmer): Set these only for protocols that support them? */ |
---|
| 1101 | funcs.chat_msg = purple_chat_msg; |
---|
[8ad5c34] | 1102 | funcs.chat_with = purple_chat_with; |
---|
| 1103 | funcs.chat_invite = purple_chat_invite; |
---|
[15794dc] | 1104 | funcs.chat_leave = purple_chat_leave; |
---|
[c3caa46] | 1105 | funcs.chat_join = purple_chat_join; |
---|
[edfc6db] | 1106 | funcs.transfer_request = purple_transfer_request; |
---|
[cd741d8] | 1107 | |
---|
[e5d8d21] | 1108 | help = g_string_new("BitlBee libpurple module supports the following IM protocols:\n"); |
---|
| 1109 | |
---|
[bab1c86] | 1110 | /* Add a protocol entry to BitlBee's structures for every protocol |
---|
| 1111 | supported by this libpurple instance. */ |
---|
[796da03] | 1112 | for( prots = purple_plugins_get_protocols(); prots; prots = prots->next ) |
---|
| 1113 | { |
---|
| 1114 | PurplePlugin *prot = prots->data; |
---|
[cd741d8] | 1115 | struct prpl *ret; |
---|
[796da03] | 1116 | |
---|
[cd741d8] | 1117 | ret = g_memdup( &funcs, sizeof( funcs ) ); |
---|
| 1118 | ret->name = ret->data = prot->info->id; |
---|
| 1119 | if( strncmp( ret->name, "prpl-", 5 ) == 0 ) |
---|
| 1120 | ret->name += 5; |
---|
[796da03] | 1121 | register_protocol( ret ); |
---|
[cd741d8] | 1122 | |
---|
[e5d8d21] | 1123 | g_string_append_printf( help, "\n* %s (%s)", ret->name, prot->info->name ); |
---|
| 1124 | |
---|
[bab1c86] | 1125 | /* libpurple doesn't define a protocol called OSCAR, but we |
---|
| 1126 | need it to be compatible with normal BitlBee. */ |
---|
[cd741d8] | 1127 | if( g_strcasecmp( prot->info->id, "prpl-aim" ) == 0 ) |
---|
| 1128 | { |
---|
| 1129 | ret = g_memdup( &funcs, sizeof( funcs ) ); |
---|
| 1130 | ret->name = "oscar"; |
---|
| 1131 | ret->data = prot->info->id; |
---|
| 1132 | register_protocol( ret ); |
---|
| 1133 | } |
---|
[796da03] | 1134 | } |
---|
[e5d8d21] | 1135 | |
---|
[7c5affca] | 1136 | g_string_append( help, "\n\nFor used protocols, more information about available " |
---|
| 1137 | "settings can be found using \x02help purple <protocol name>\x02" ); |
---|
| 1138 | |
---|
[bab1c86] | 1139 | /* Add a simple dynamically-generated help item listing all |
---|
| 1140 | the supported protocols. */ |
---|
[e5d8d21] | 1141 | help_add_mem( &global.help, "purple", help->str ); |
---|
| 1142 | g_string_free( help, TRUE ); |
---|
[796da03] | 1143 | } |
---|