[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 ); |
---|
| 45 | |
---|
[eb50495] | 46 | /* Offline by default. This will set the right flags. */ |
---|
| 47 | imcb_buddy_status( ic, handle, 0, NULL, NULL ); |
---|
| 48 | |
---|
[10a96f4] | 49 | return bu; |
---|
| 50 | } |
---|
| 51 | |
---|
[eabc9d2] | 52 | int bee_user_free( bee_t *bee, bee_user_t *bu ) |
---|
[10a96f4] | 53 | { |
---|
[eabc9d2] | 54 | if( !bu ) |
---|
[10a96f4] | 55 | return 0; |
---|
| 56 | |
---|
| 57 | if( bee->ui->user_free ) |
---|
| 58 | bee->ui->user_free( bee, bu ); |
---|
| 59 | |
---|
| 60 | g_free( bu->handle ); |
---|
| 61 | g_free( bu->fullname ); |
---|
[81e04e1] | 62 | g_free( bu->status ); |
---|
[10a96f4] | 63 | g_free( bu->status_msg ); |
---|
| 64 | |
---|
| 65 | bee->users = g_slist_remove( bee->users, bu ); |
---|
| 66 | |
---|
| 67 | return 1; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | bee_user_t *bee_user_by_handle( bee_t *bee, struct im_connection *ic, const char *handle ) |
---|
| 71 | { |
---|
| 72 | GSList *l; |
---|
| 73 | |
---|
| 74 | for( l = bee->users; l; l = l->next ) |
---|
| 75 | { |
---|
| 76 | bee_user_t *bu = l->data; |
---|
| 77 | |
---|
[231b08b] | 78 | if( bu->ic == ic && ic->acc->prpl->handle_cmp( bu->handle, handle ) == 0 ) |
---|
[10a96f4] | 79 | return bu; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | return NULL; |
---|
| 83 | } |
---|
[d860a8d] | 84 | |
---|
| 85 | int bee_user_msg( bee_t *bee, bee_user_t *bu, const char *msg, int flags ) |
---|
| 86 | { |
---|
| 87 | char *buf = NULL; |
---|
| 88 | int st; |
---|
| 89 | |
---|
| 90 | if( ( bu->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) |
---|
| 91 | { |
---|
| 92 | buf = escape_html( msg ); |
---|
| 93 | msg = buf; |
---|
| 94 | } |
---|
[fb117aee] | 95 | else |
---|
| 96 | buf = g_strdup( msg ); |
---|
[d860a8d] | 97 | |
---|
[fb117aee] | 98 | st = bu->ic->acc->prpl->buddy_msg( bu->ic, bu->handle, buf, flags ); |
---|
[d860a8d] | 99 | g_free( buf ); |
---|
| 100 | |
---|
| 101 | return st; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | |
---|
[7aadd71] | 105 | /* Groups */ |
---|
| 106 | static bee_group_t *bee_group_new( bee_t *bee, const char *name ) |
---|
| 107 | { |
---|
| 108 | bee_group_t *bg = g_new0( bee_group_t, 1 ); |
---|
| 109 | |
---|
| 110 | bg->name = g_strdup( name ); |
---|
| 111 | bg->key = g_utf8_casefold( name, -1 ); |
---|
| 112 | bee->groups = g_slist_prepend( bee->groups, bg ); |
---|
| 113 | |
---|
| 114 | return bg; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | bee_group_t *bee_group_by_name( bee_t *bee, const char *name, gboolean creat ) |
---|
| 118 | { |
---|
| 119 | GSList *l; |
---|
| 120 | char *key; |
---|
| 121 | |
---|
| 122 | if( name == NULL ) |
---|
| 123 | return NULL; |
---|
| 124 | |
---|
| 125 | key = g_utf8_casefold( name, -1 ); |
---|
| 126 | for( l = bee->groups; l; l = l->next ) |
---|
| 127 | { |
---|
| 128 | bee_group_t *bg = l->data; |
---|
| 129 | if( strcmp( bg->key, key ) == 0 ) |
---|
| 130 | break; |
---|
| 131 | } |
---|
| 132 | g_free( key ); |
---|
| 133 | |
---|
| 134 | if( !l ) |
---|
| 135 | return creat ? bee_group_new( bee, name ) : NULL; |
---|
| 136 | else |
---|
| 137 | return l->data; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | void bee_group_free( bee_t *bee ) |
---|
| 141 | { |
---|
| 142 | while( bee->groups ) |
---|
| 143 | { |
---|
| 144 | bee_group_t *bg = bee->groups->data; |
---|
| 145 | g_free( bg->name ); |
---|
| 146 | g_free( bg->key ); |
---|
| 147 | g_free( bg ); |
---|
| 148 | bee->groups = g_slist_remove( bee->groups, bee->groups->data ); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
[d860a8d] | 153 | /* IM->UI callbacks */ |
---|
| 154 | void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message ) |
---|
| 155 | { |
---|
| 156 | bee_t *bee = ic->bee; |
---|
| 157 | bee_user_t *bu, *old; |
---|
| 158 | |
---|
| 159 | if( !( bu = bee_user_by_handle( bee, ic, handle ) ) ) |
---|
| 160 | { |
---|
| 161 | if( g_strcasecmp( set_getstr( &ic->bee->set, "handle_unknown" ), "add" ) == 0 ) |
---|
| 162 | { |
---|
[ad404ab] | 163 | bu = bee_user_new( bee, ic, handle, BEE_USER_LOCAL ); |
---|
[d860a8d] | 164 | } |
---|
| 165 | else |
---|
| 166 | { |
---|
[f012a9f] | 167 | if( g_strcasecmp( set_getstr( &ic->bee->set, "handle_unknown" ), "ignore" ) != 0 ) |
---|
[d860a8d] | 168 | { |
---|
[f012a9f] | 169 | imcb_log( ic, "imcb_buddy_status() for unknown handle %s:\n" |
---|
| 170 | "flags = %d, state = %s, message = %s", handle, flags, |
---|
| 171 | state ? state : "NULL", message ? message : "NULL" ); |
---|
[d860a8d] | 172 | } |
---|
| 173 | |
---|
| 174 | return; |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | /* May be nice to give the UI something to compare against. */ |
---|
| 179 | old = g_memdup( bu, sizeof( bee_user_t ) ); |
---|
| 180 | |
---|
| 181 | /* TODO(wilmer): OPT_AWAY, or just state == NULL ? */ |
---|
[e63507a] | 182 | bu->flags = flags; |
---|
[d860a8d] | 183 | bu->status = g_strdup( ( flags & OPT_AWAY ) && state == NULL ? "Away" : state ); |
---|
| 184 | bu->status_msg = g_strdup( message ); |
---|
| 185 | |
---|
| 186 | if( bee->ui->user_status ) |
---|
| 187 | bee->ui->user_status( bee, bu, old ); |
---|
| 188 | |
---|
| 189 | g_free( old->status_msg ); |
---|
| 190 | g_free( old->status ); |
---|
| 191 | g_free( old ); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, uint32_t flags, time_t sent_at ) |
---|
| 195 | { |
---|
| 196 | bee_t *bee = ic->bee; |
---|
[f012a9f] | 197 | bee_user_t *bu; |
---|
[d860a8d] | 198 | |
---|
[f012a9f] | 199 | bu = bee_user_by_handle( bee, ic, handle ); |
---|
[d860a8d] | 200 | |
---|
[f012a9f] | 201 | if( !bu ) |
---|
[d860a8d] | 202 | { |
---|
| 203 | char *h = set_getstr( &bee->set, "handle_unknown" ); |
---|
| 204 | |
---|
| 205 | if( g_strcasecmp( h, "ignore" ) == 0 ) |
---|
| 206 | { |
---|
| 207 | return; |
---|
| 208 | } |
---|
| 209 | else if( g_strncasecmp( h, "add", 3 ) == 0 ) |
---|
| 210 | { |
---|
[ad404ab] | 211 | bu = bee_user_new( bee, ic, handle, BEE_USER_LOCAL ); |
---|
[d860a8d] | 212 | } |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) || |
---|
| 216 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) ) |
---|
| 217 | strip_html( msg ); |
---|
[f012a9f] | 218 | |
---|
| 219 | if( bee->ui->user_msg && bu ) |
---|
| 220 | bee->ui->user_msg( bee, bu, msg, sent_at ); |
---|
| 221 | else |
---|
| 222 | imcb_log( ic, "Message from unknown handle %s:\n%s", handle, msg ); |
---|
[d860a8d] | 223 | } |
---|
[573dab0] | 224 | |
---|
| 225 | void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags ) |
---|
| 226 | { |
---|
| 227 | bee_user_t *bu; |
---|
| 228 | |
---|
| 229 | if( ic->bee->ui->user_typing && |
---|
| 230 | ( bu = bee_user_by_handle( ic->bee, ic, handle ) ) ) |
---|
| 231 | { |
---|
| 232 | ic->bee->ui->user_typing( ic->bee, bu, flags ); |
---|
| 233 | } |
---|
| 234 | } |
---|