[10a96f4] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
| 4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
| 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* Stuff to handle, save and search buddies */ |
---|
| 8 | |
---|
| 9 | /* |
---|
| 10 | This program is free software; you can redistribute it and/or modify |
---|
| 11 | it under the terms of the GNU General Public License as published by |
---|
| 12 | the Free Software Foundation; either version 2 of the License, or |
---|
| 13 | (at your option) any later version. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, |
---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 18 | GNU General Public License for more details. |
---|
| 19 | |
---|
| 20 | You should have received a copy of the GNU General Public License with |
---|
| 21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
| 22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
| 23 | Suite 330, Boston, MA 02111-1307 USA |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #define BITLBEE_CORE |
---|
| 27 | #include "bitlbee.h" |
---|
| 28 | |
---|
[ad404ab] | 29 | bee_user_t *bee_user_new( bee_t *bee, struct im_connection *ic, const char *handle, bee_user_flags_t flags ) |
---|
[10a96f4] | 30 | { |
---|
| 31 | bee_user_t *bu; |
---|
| 32 | |
---|
| 33 | if( bee_user_by_handle( bee, ic, handle ) != NULL ) |
---|
| 34 | return NULL; |
---|
| 35 | |
---|
| 36 | bu = g_new0( bee_user_t, 1 ); |
---|
| 37 | bu->bee = bee; |
---|
| 38 | bu->ic = ic; |
---|
[ad404ab] | 39 | bu->flags = flags; |
---|
[10a96f4] | 40 | bu->handle = g_strdup( handle ); |
---|
| 41 | bee->users = g_slist_prepend( bee->users, bu ); |
---|
| 42 | |
---|
| 43 | if( bee->ui->user_new ) |
---|
| 44 | bee->ui->user_new( bee, bu ); |
---|
[203a2d2] | 45 | if( ic->acc->prpl->buddy_data_add ) |
---|
| 46 | ic->acc->prpl->buddy_data_add( bu ); |
---|
[10a96f4] | 47 | |
---|
[eb50495] | 48 | /* Offline by default. This will set the right flags. */ |
---|
| 49 | imcb_buddy_status( ic, handle, 0, NULL, NULL ); |
---|
| 50 | |
---|
[10a96f4] | 51 | return bu; |
---|
| 52 | } |
---|
| 53 | |
---|
[eabc9d2] | 54 | int bee_user_free( bee_t *bee, bee_user_t *bu ) |
---|
[10a96f4] | 55 | { |
---|
[eabc9d2] | 56 | if( !bu ) |
---|
[10a96f4] | 57 | return 0; |
---|
| 58 | |
---|
| 59 | if( bee->ui->user_free ) |
---|
| 60 | bee->ui->user_free( bee, bu ); |
---|
[203a2d2] | 61 | if( bu->ic->acc->prpl->buddy_data_free ) |
---|
| 62 | bu->ic->acc->prpl->buddy_data_free( bu ); |
---|
[10a96f4] | 63 | |
---|
| 64 | g_free( bu->handle ); |
---|
| 65 | g_free( bu->fullname ); |
---|
[69b896b] | 66 | g_free( bu->nick ); |
---|
[81e04e1] | 67 | g_free( bu->status ); |
---|
[10a96f4] | 68 | g_free( bu->status_msg ); |
---|
[d7db346] | 69 | g_free( bu ); |
---|
[10a96f4] | 70 | |
---|
| 71 | bee->users = g_slist_remove( bee->users, bu ); |
---|
| 72 | |
---|
| 73 | return 1; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | bee_user_t *bee_user_by_handle( bee_t *bee, struct im_connection *ic, const char *handle ) |
---|
| 77 | { |
---|
| 78 | GSList *l; |
---|
| 79 | |
---|
| 80 | for( l = bee->users; l; l = l->next ) |
---|
| 81 | { |
---|
| 82 | bee_user_t *bu = l->data; |
---|
| 83 | |
---|
[231b08b] | 84 | if( bu->ic == ic && ic->acc->prpl->handle_cmp( bu->handle, handle ) == 0 ) |
---|
[10a96f4] | 85 | return bu; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | return NULL; |
---|
| 89 | } |
---|
[d860a8d] | 90 | |
---|
| 91 | int bee_user_msg( bee_t *bee, bee_user_t *bu, const char *msg, int flags ) |
---|
| 92 | { |
---|
| 93 | char *buf = NULL; |
---|
| 94 | int st; |
---|
| 95 | |
---|
| 96 | if( ( bu->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) |
---|
| 97 | { |
---|
| 98 | buf = escape_html( msg ); |
---|
| 99 | msg = buf; |
---|
| 100 | } |
---|
[fb117aee] | 101 | else |
---|
| 102 | buf = g_strdup( msg ); |
---|
[d860a8d] | 103 | |
---|
[fb117aee] | 104 | st = bu->ic->acc->prpl->buddy_msg( bu->ic, bu->handle, buf, flags ); |
---|
[d860a8d] | 105 | g_free( buf ); |
---|
| 106 | |
---|
| 107 | return st; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | |
---|
[7aadd71] | 111 | /* Groups */ |
---|
| 112 | static bee_group_t *bee_group_new( bee_t *bee, const char *name ) |
---|
| 113 | { |
---|
| 114 | bee_group_t *bg = g_new0( bee_group_t, 1 ); |
---|
| 115 | |
---|
| 116 | bg->name = g_strdup( name ); |
---|
| 117 | bg->key = g_utf8_casefold( name, -1 ); |
---|
| 118 | bee->groups = g_slist_prepend( bee->groups, bg ); |
---|
| 119 | |
---|
| 120 | return bg; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | bee_group_t *bee_group_by_name( bee_t *bee, const char *name, gboolean creat ) |
---|
| 124 | { |
---|
| 125 | GSList *l; |
---|
| 126 | char *key; |
---|
| 127 | |
---|
| 128 | if( name == NULL ) |
---|
| 129 | return NULL; |
---|
| 130 | |
---|
| 131 | key = g_utf8_casefold( name, -1 ); |
---|
| 132 | for( l = bee->groups; l; l = l->next ) |
---|
| 133 | { |
---|
| 134 | bee_group_t *bg = l->data; |
---|
| 135 | if( strcmp( bg->key, key ) == 0 ) |
---|
| 136 | break; |
---|
| 137 | } |
---|
| 138 | g_free( key ); |
---|
| 139 | |
---|
| 140 | if( !l ) |
---|
| 141 | return creat ? bee_group_new( bee, name ) : NULL; |
---|
| 142 | else |
---|
| 143 | return l->data; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | void bee_group_free( bee_t *bee ) |
---|
| 147 | { |
---|
| 148 | while( bee->groups ) |
---|
| 149 | { |
---|
| 150 | bee_group_t *bg = bee->groups->data; |
---|
| 151 | g_free( bg->name ); |
---|
| 152 | g_free( bg->key ); |
---|
| 153 | g_free( bg ); |
---|
| 154 | bee->groups = g_slist_remove( bee->groups, bee->groups->data ); |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | |
---|
[d860a8d] | 159 | /* IM->UI callbacks */ |
---|
| 160 | void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message ) |
---|
| 161 | { |
---|
| 162 | bee_t *bee = ic->bee; |
---|
| 163 | bee_user_t *bu, *old; |
---|
| 164 | |
---|
| 165 | if( !( bu = bee_user_by_handle( bee, ic, handle ) ) ) |
---|
| 166 | { |
---|
| 167 | if( g_strcasecmp( set_getstr( &ic->bee->set, "handle_unknown" ), "add" ) == 0 ) |
---|
| 168 | { |
---|
[ad404ab] | 169 | bu = bee_user_new( bee, ic, handle, BEE_USER_LOCAL ); |
---|
[d860a8d] | 170 | } |
---|
| 171 | else |
---|
| 172 | { |
---|
[f012a9f] | 173 | if( g_strcasecmp( set_getstr( &ic->bee->set, "handle_unknown" ), "ignore" ) != 0 ) |
---|
[d860a8d] | 174 | { |
---|
[f012a9f] | 175 | imcb_log( ic, "imcb_buddy_status() for unknown handle %s:\n" |
---|
| 176 | "flags = %d, state = %s, message = %s", handle, flags, |
---|
| 177 | state ? state : "NULL", message ? message : "NULL" ); |
---|
[d860a8d] | 178 | } |
---|
| 179 | |
---|
| 180 | return; |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | /* May be nice to give the UI something to compare against. */ |
---|
| 185 | old = g_memdup( bu, sizeof( bee_user_t ) ); |
---|
| 186 | |
---|
| 187 | /* TODO(wilmer): OPT_AWAY, or just state == NULL ? */ |
---|
[e63507a] | 188 | bu->flags = flags; |
---|
[d860a8d] | 189 | bu->status_msg = g_strdup( message ); |
---|
[d93c0eb9] | 190 | if( state && *state ) |
---|
| 191 | bu->status = g_strdup( state ); |
---|
| 192 | else if( flags & OPT_AWAY ) |
---|
| 193 | bu->status = g_strdup( "Away" ); |
---|
| 194 | else |
---|
| 195 | bu->status = NULL; |
---|
[d860a8d] | 196 | |
---|
[0ebf919] | 197 | if( bu->status == NULL && ( flags & OPT_MOBILE ) && |
---|
| 198 | set_getbool( &bee->set, "mobile_is_away" ) ) |
---|
| 199 | { |
---|
| 200 | bu->flags |= BEE_USER_AWAY; |
---|
| 201 | bu->status = g_strdup( "Mobile" ); |
---|
| 202 | } |
---|
| 203 | |
---|
[d860a8d] | 204 | if( bee->ui->user_status ) |
---|
| 205 | bee->ui->user_status( bee, bu, old ); |
---|
| 206 | |
---|
| 207 | g_free( old->status_msg ); |
---|
| 208 | g_free( old->status ); |
---|
| 209 | g_free( old ); |
---|
| 210 | } |
---|
| 211 | |
---|
[d93c0eb9] | 212 | /* Same, but only change the away/status message, not any away/online state info. */ |
---|
| 213 | void imcb_buddy_status_msg( struct im_connection *ic, const char *handle, const char *message ) |
---|
| 214 | { |
---|
| 215 | bee_t *bee = ic->bee; |
---|
| 216 | bee_user_t *bu, *old; |
---|
| 217 | |
---|
| 218 | if( !( bu = bee_user_by_handle( bee, ic, handle ) ) ) |
---|
| 219 | { |
---|
| 220 | return; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | old = g_memdup( bu, sizeof( bee_user_t ) ); |
---|
| 224 | |
---|
[9b1d2d6] | 225 | bu->status_msg = message && *message ? g_strdup( message ) : NULL; |
---|
[d93c0eb9] | 226 | |
---|
| 227 | if( bee->ui->user_status ) |
---|
| 228 | bee->ui->user_status( bee, bu, old ); |
---|
| 229 | |
---|
| 230 | g_free( old->status_msg ); |
---|
| 231 | g_free( old ); |
---|
| 232 | } |
---|
| 233 | |
---|
[56699f0] | 234 | void imcb_buddy_times( struct im_connection *ic, const char *handle, time_t login, time_t idle ) |
---|
| 235 | { |
---|
| 236 | bee_t *bee = ic->bee; |
---|
| 237 | bee_user_t *bu; |
---|
| 238 | |
---|
| 239 | if( !( bu = bee_user_by_handle( bee, ic, handle ) ) ) |
---|
| 240 | return; |
---|
| 241 | |
---|
| 242 | bu->login_time = login; |
---|
| 243 | bu->idle_time = idle; |
---|
| 244 | } |
---|
| 245 | |
---|
[d860a8d] | 246 | void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, uint32_t flags, time_t sent_at ) |
---|
| 247 | { |
---|
| 248 | bee_t *bee = ic->bee; |
---|
[f012a9f] | 249 | bee_user_t *bu; |
---|
[d860a8d] | 250 | |
---|
[f012a9f] | 251 | bu = bee_user_by_handle( bee, ic, handle ); |
---|
[d860a8d] | 252 | |
---|
[8e3890b] | 253 | if( !bu && !( ic->flags & OPT_LOGGING_OUT ) ) |
---|
[d860a8d] | 254 | { |
---|
| 255 | char *h = set_getstr( &bee->set, "handle_unknown" ); |
---|
| 256 | |
---|
| 257 | if( g_strcasecmp( h, "ignore" ) == 0 ) |
---|
| 258 | { |
---|
| 259 | return; |
---|
| 260 | } |
---|
| 261 | else if( g_strncasecmp( h, "add", 3 ) == 0 ) |
---|
| 262 | { |
---|
[ad404ab] | 263 | bu = bee_user_new( bee, ic, handle, BEE_USER_LOCAL ); |
---|
[d860a8d] | 264 | } |
---|
| 265 | } |
---|
| 266 | |
---|
[f012a9f] | 267 | if( bee->ui->user_msg && bu ) |
---|
| 268 | bee->ui->user_msg( bee, bu, msg, sent_at ); |
---|
| 269 | else |
---|
| 270 | imcb_log( ic, "Message from unknown handle %s:\n%s", handle, msg ); |
---|
[d860a8d] | 271 | } |
---|
[573dab0] | 272 | |
---|
[bad41f56] | 273 | void imcb_buddy_typing( struct im_connection *ic, const char *handle, uint32_t flags ) |
---|
[573dab0] | 274 | { |
---|
| 275 | bee_user_t *bu; |
---|
| 276 | |
---|
| 277 | if( ic->bee->ui->user_typing && |
---|
| 278 | ( bu = bee_user_by_handle( ic->bee, ic, handle ) ) ) |
---|
| 279 | { |
---|
| 280 | ic->bee->ui->user_typing( ic->bee, bu, flags ); |
---|
| 281 | } |
---|
| 282 | } |
---|
[d88c92a] | 283 | |
---|
| 284 | void imcb_buddy_action_response( bee_user_t *bu, const char *action, char * const args[], void *data ) |
---|
| 285 | { |
---|
| 286 | if( bu->bee->ui->user_action_response ) |
---|
| 287 | bu->bee->ui->user_action_response( bu->bee, bu, action, args, data ); |
---|
| 288 | } |
---|