[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 | |
---|
| 45 | return bu; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | int bee_user_free( bee_t *bee, struct im_connection *ic, const char *handle ) |
---|
| 49 | { |
---|
| 50 | bee_user_t *bu; |
---|
| 51 | |
---|
| 52 | if( ( bu = bee_user_by_handle( bee, ic, handle ) ) == NULL ) |
---|
| 53 | return 0; |
---|
| 54 | |
---|
| 55 | if( bee->ui->user_free ) |
---|
| 56 | bee->ui->user_free( bee, bu ); |
---|
| 57 | |
---|
| 58 | g_free( bu->handle ); |
---|
| 59 | g_free( bu->fullname ); |
---|
| 60 | g_free( bu->group ); |
---|
[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 | |
---|
| 77 | if( bu->ic == ic && ic->acc->prpl->handle_cmp( bu->handle, handle ) ) |
---|
| 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 | } |
---|
| 94 | |
---|
| 95 | st = bu->ic->acc->prpl->buddy_msg( bu->ic, bu->handle, msg, flags ); |
---|
| 96 | g_free( buf ); |
---|
| 97 | |
---|
| 98 | return st; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | /* IM->UI callbacks */ |
---|
| 103 | void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message ) |
---|
| 104 | { |
---|
| 105 | bee_t *bee = ic->bee; |
---|
| 106 | bee_user_t *bu, *old; |
---|
| 107 | |
---|
| 108 | if( !( bu = bee_user_by_handle( bee, ic, handle ) ) ) |
---|
| 109 | { |
---|
| 110 | if( g_strcasecmp( set_getstr( &ic->bee->set, "handle_unknown" ), "add" ) == 0 ) |
---|
| 111 | { |
---|
| 112 | bu = bee_user_new( bee, ic, handle ); |
---|
| 113 | } |
---|
| 114 | else |
---|
| 115 | { |
---|
| 116 | if( set_getbool( &ic->bee->set, "debug" ) || g_strcasecmp( set_getstr( &ic->bee->set, "handle_unknown" ), "ignore" ) != 0 ) |
---|
| 117 | { |
---|
| 118 | imcb_log( ic, "imcb_buddy_status() for unknown handle %s:", handle ); |
---|
| 119 | imcb_log( ic, "flags = %d, state = %s, message = %s", flags, |
---|
| 120 | state ? state : "NULL", message ? message : "NULL" ); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | return; |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /* May be nice to give the UI something to compare against. */ |
---|
| 128 | old = g_memdup( bu, sizeof( bee_user_t ) ); |
---|
| 129 | |
---|
| 130 | /* TODO(wilmer): OPT_AWAY, or just state == NULL ? */ |
---|
[e63507a] | 131 | bu->flags = flags; |
---|
[d860a8d] | 132 | bu->status = g_strdup( ( flags & OPT_AWAY ) && state == NULL ? "Away" : state ); |
---|
| 133 | bu->status_msg = g_strdup( message ); |
---|
| 134 | |
---|
| 135 | if( bee->ui->user_status ) |
---|
| 136 | bee->ui->user_status( bee, bu, old ); |
---|
| 137 | |
---|
| 138 | g_free( old->status_msg ); |
---|
| 139 | g_free( old->status ); |
---|
| 140 | g_free( old ); |
---|
| 141 | #if 0 |
---|
| 142 | oa = u->away != NULL; |
---|
| 143 | oo = u->online; |
---|
| 144 | |
---|
| 145 | g_free( u->away ); |
---|
| 146 | g_free( u->status_msg ); |
---|
| 147 | u->away = u->status_msg = NULL; |
---|
| 148 | |
---|
| 149 | if( ( flags & OPT_LOGGED_IN ) && !u->online ) |
---|
| 150 | { |
---|
| 151 | irc_spawn( ic->irc, u ); |
---|
| 152 | u->online = 1; |
---|
| 153 | } |
---|
| 154 | else if( !( flags & OPT_LOGGED_IN ) && u->online ) |
---|
| 155 | { |
---|
| 156 | struct groupchat *c; |
---|
| 157 | |
---|
| 158 | irc_kill( ic->irc, u ); |
---|
| 159 | u->online = 0; |
---|
| 160 | |
---|
| 161 | /* Remove him/her from the groupchats to prevent PART messages after he/she QUIT already */ |
---|
| 162 | for( c = ic->groupchats; c; c = c->next ) |
---|
| 163 | remove_chat_buddy_silent( c, handle ); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | if( flags & OPT_AWAY ) |
---|
| 167 | { |
---|
| 168 | if( state && message ) |
---|
| 169 | { |
---|
| 170 | u->away = g_strdup_printf( "%s (%s)", state, message ); |
---|
| 171 | } |
---|
| 172 | else if( state ) |
---|
| 173 | { |
---|
| 174 | u->away = g_strdup( state ); |
---|
| 175 | } |
---|
| 176 | else if( message ) |
---|
| 177 | { |
---|
| 178 | u->away = g_strdup( message ); |
---|
| 179 | } |
---|
| 180 | else |
---|
| 181 | { |
---|
| 182 | u->away = g_strdup( "Away" ); |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | else |
---|
| 186 | { |
---|
| 187 | u->status_msg = g_strdup( message ); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /* LISPy... */ |
---|
| 191 | if( ( set_getbool( &ic->bee->set, "away_devoice" ) ) && /* Don't do a thing when user doesn't want it */ |
---|
| 192 | ( u->online ) && /* Don't touch offline people */ |
---|
| 193 | ( ( ( u->online != oo ) && !u->away ) || /* Voice joining people */ |
---|
| 194 | ( ( u->online == oo ) && ( oa == !u->away ) ) ) ) /* (De)voice people changing state */ |
---|
| 195 | { |
---|
| 196 | char *from; |
---|
| 197 | |
---|
| 198 | if( set_getbool( &ic->bee->set, "simulate_netsplit" ) ) |
---|
| 199 | { |
---|
| 200 | from = g_strdup( ic->irc->myhost ); |
---|
| 201 | } |
---|
| 202 | else |
---|
| 203 | { |
---|
| 204 | from = g_strdup_printf( "%s!%s@%s", ic->irc->mynick, ic->irc->mynick, |
---|
| 205 | ic->irc->myhost ); |
---|
| 206 | } |
---|
| 207 | irc_write( ic->irc, ":%s MODE %s %cv %s", from, ic->irc->channel, |
---|
| 208 | u->away?'-':'+', u->nick ); |
---|
| 209 | g_free( from ); |
---|
| 210 | } |
---|
| 211 | #endif |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, uint32_t flags, time_t sent_at ) |
---|
| 215 | { |
---|
| 216 | #if 0 |
---|
| 217 | bee_t *bee = ic->bee; |
---|
| 218 | char *wrapped; |
---|
| 219 | user_t *u; |
---|
| 220 | |
---|
| 221 | u = user_findhandle( ic, handle ); |
---|
| 222 | |
---|
| 223 | if( !u ) |
---|
| 224 | { |
---|
| 225 | char *h = set_getstr( &bee->set, "handle_unknown" ); |
---|
| 226 | |
---|
| 227 | if( g_strcasecmp( h, "ignore" ) == 0 ) |
---|
| 228 | { |
---|
| 229 | if( set_getbool( &bee->set, "debug" ) ) |
---|
| 230 | imcb_log( ic, "Ignoring message from unknown handle %s", handle ); |
---|
| 231 | |
---|
| 232 | return; |
---|
| 233 | } |
---|
| 234 | else if( g_strncasecmp( h, "add", 3 ) == 0 ) |
---|
| 235 | { |
---|
| 236 | int private = set_getbool( &bee->set, "private" ); |
---|
| 237 | |
---|
| 238 | if( h[3] ) |
---|
| 239 | { |
---|
| 240 | if( g_strcasecmp( h + 3, "_private" ) == 0 ) |
---|
| 241 | private = 1; |
---|
| 242 | else if( g_strcasecmp( h + 3, "_channel" ) == 0 ) |
---|
| 243 | private = 0; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | imcb_add_buddy( ic, handle, NULL ); |
---|
| 247 | u = user_findhandle( ic, handle ); |
---|
| 248 | u->is_private = private; |
---|
| 249 | } |
---|
| 250 | else |
---|
| 251 | { |
---|
| 252 | imcb_log( ic, "Message from unknown handle %s:", handle ); |
---|
| 253 | u = user_find( irc, irc->mynick ); |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) || |
---|
| 258 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) ) |
---|
| 259 | strip_html( msg ); |
---|
| 260 | |
---|
| 261 | wrapped = word_wrap( msg, 425 ); |
---|
| 262 | irc_msgfrom( irc, u->nick, wrapped ); |
---|
| 263 | g_free( wrapped ); |
---|
| 264 | #endif |
---|
| 265 | } |
---|