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