[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 ); |
---|
| 61 | g_free( bu->group ); |
---|
[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 | |
---|
| 105 | /* IM->UI callbacks */ |
---|
| 106 | void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message ) |
---|
| 107 | { |
---|
| 108 | bee_t *bee = ic->bee; |
---|
| 109 | bee_user_t *bu, *old; |
---|
| 110 | |
---|
| 111 | if( !( bu = bee_user_by_handle( bee, ic, handle ) ) ) |
---|
| 112 | { |
---|
| 113 | if( g_strcasecmp( set_getstr( &ic->bee->set, "handle_unknown" ), "add" ) == 0 ) |
---|
| 114 | { |
---|
| 115 | bu = bee_user_new( bee, ic, handle ); |
---|
| 116 | } |
---|
| 117 | else |
---|
| 118 | { |
---|
[f012a9f] | 119 | if( g_strcasecmp( set_getstr( &ic->bee->set, "handle_unknown" ), "ignore" ) != 0 ) |
---|
[d860a8d] | 120 | { |
---|
[f012a9f] | 121 | imcb_log( ic, "imcb_buddy_status() for unknown handle %s:\n" |
---|
| 122 | "flags = %d, state = %s, message = %s", handle, flags, |
---|
| 123 | state ? state : "NULL", message ? message : "NULL" ); |
---|
[d860a8d] | 124 | } |
---|
| 125 | |
---|
| 126 | return; |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /* May be nice to give the UI something to compare against. */ |
---|
| 131 | old = g_memdup( bu, sizeof( bee_user_t ) ); |
---|
| 132 | |
---|
| 133 | /* TODO(wilmer): OPT_AWAY, or just state == NULL ? */ |
---|
[e63507a] | 134 | bu->flags = flags; |
---|
[d860a8d] | 135 | bu->status = g_strdup( ( flags & OPT_AWAY ) && state == NULL ? "Away" : state ); |
---|
| 136 | bu->status_msg = g_strdup( message ); |
---|
| 137 | |
---|
| 138 | if( bee->ui->user_status ) |
---|
| 139 | bee->ui->user_status( bee, bu, old ); |
---|
| 140 | |
---|
| 141 | g_free( old->status_msg ); |
---|
| 142 | g_free( old->status ); |
---|
| 143 | g_free( old ); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, uint32_t flags, time_t sent_at ) |
---|
| 147 | { |
---|
| 148 | bee_t *bee = ic->bee; |
---|
[f012a9f] | 149 | bee_user_t *bu; |
---|
[d860a8d] | 150 | |
---|
[f012a9f] | 151 | bu = bee_user_by_handle( bee, ic, handle ); |
---|
[d860a8d] | 152 | |
---|
[f012a9f] | 153 | if( !bu ) |
---|
[d860a8d] | 154 | { |
---|
| 155 | char *h = set_getstr( &bee->set, "handle_unknown" ); |
---|
| 156 | |
---|
| 157 | if( g_strcasecmp( h, "ignore" ) == 0 ) |
---|
| 158 | { |
---|
| 159 | return; |
---|
| 160 | } |
---|
| 161 | else if( g_strncasecmp( h, "add", 3 ) == 0 ) |
---|
| 162 | { |
---|
[f012a9f] | 163 | bu = bee_user_new( bee, ic, handle ); |
---|
[d860a8d] | 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) || |
---|
| 168 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) ) |
---|
| 169 | strip_html( msg ); |
---|
[f012a9f] | 170 | |
---|
| 171 | if( bee->ui->user_msg && bu ) |
---|
| 172 | bee->ui->user_msg( bee, bu, msg, sent_at ); |
---|
| 173 | else |
---|
| 174 | imcb_log( ic, "Message from unknown handle %s:\n%s", handle, msg ); |
---|
[d860a8d] | 175 | } |
---|
[573dab0] | 176 | |
---|
| 177 | void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags ) |
---|
| 178 | { |
---|
| 179 | bee_user_t *bu; |
---|
| 180 | |
---|
| 181 | if( ic->bee->ui->user_typing && |
---|
| 182 | ( bu = bee_user_by_handle( ic->bee, ic, handle ) ) ) |
---|
| 183 | { |
---|
| 184 | ic->bee->ui->user_typing( ic->bee, bu, flags ); |
---|
| 185 | } |
---|
| 186 | } |
---|