[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 = g_strdup( ( flags & OPT_AWAY ) && state == NULL ? "Away" : state ); |
---|
| 190 | bu->status_msg = g_strdup( message ); |
---|
| 191 | |
---|
| 192 | if( bee->ui->user_status ) |
---|
| 193 | bee->ui->user_status( bee, bu, old ); |
---|
| 194 | |
---|
| 195 | g_free( old->status_msg ); |
---|
| 196 | g_free( old->status ); |
---|
| 197 | g_free( old ); |
---|
| 198 | } |
---|
| 199 | |
---|
[56699f0] | 200 | void imcb_buddy_times( struct im_connection *ic, const char *handle, time_t login, time_t idle ) |
---|
| 201 | { |
---|
| 202 | bee_t *bee = ic->bee; |
---|
| 203 | bee_user_t *bu; |
---|
| 204 | |
---|
| 205 | if( !( bu = bee_user_by_handle( bee, ic, handle ) ) ) |
---|
| 206 | return; |
---|
| 207 | |
---|
| 208 | bu->login_time = login; |
---|
| 209 | bu->idle_time = idle; |
---|
| 210 | } |
---|
| 211 | |
---|
[d860a8d] | 212 | void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, uint32_t flags, time_t sent_at ) |
---|
| 213 | { |
---|
| 214 | bee_t *bee = ic->bee; |
---|
[f012a9f] | 215 | bee_user_t *bu; |
---|
[d860a8d] | 216 | |
---|
[f012a9f] | 217 | bu = bee_user_by_handle( bee, ic, handle ); |
---|
[d860a8d] | 218 | |
---|
[f012a9f] | 219 | if( !bu ) |
---|
[d860a8d] | 220 | { |
---|
| 221 | char *h = set_getstr( &bee->set, "handle_unknown" ); |
---|
| 222 | |
---|
| 223 | if( g_strcasecmp( h, "ignore" ) == 0 ) |
---|
| 224 | { |
---|
| 225 | return; |
---|
| 226 | } |
---|
| 227 | else if( g_strncasecmp( h, "add", 3 ) == 0 ) |
---|
| 228 | { |
---|
[ad404ab] | 229 | bu = bee_user_new( bee, ic, handle, BEE_USER_LOCAL ); |
---|
[d860a8d] | 230 | } |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) || |
---|
| 234 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) ) |
---|
| 235 | strip_html( msg ); |
---|
[f012a9f] | 236 | |
---|
| 237 | if( bee->ui->user_msg && bu ) |
---|
| 238 | bee->ui->user_msg( bee, bu, msg, sent_at ); |
---|
| 239 | else |
---|
| 240 | imcb_log( ic, "Message from unknown handle %s:\n%s", handle, msg ); |
---|
[d860a8d] | 241 | } |
---|
[573dab0] | 242 | |
---|
| 243 | void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags ) |
---|
| 244 | { |
---|
| 245 | bee_user_t *bu; |
---|
| 246 | |
---|
| 247 | if( ic->bee->ui->user_typing && |
---|
| 248 | ( bu = bee_user_by_handle( ic->bee, ic, handle ) ) ) |
---|
| 249 | { |
---|
| 250 | ic->bee->ui->user_typing( ic->bee, bu, flags ); |
---|
| 251 | } |
---|
| 252 | } |
---|