[ebaebfe] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
| 4 | * Copyright 2002-2004 Wilmer van der Gaast and others * |
---|
| 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* Stuff to handle, save and search IRC 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 | #include "bitlbee.h" |
---|
[debe871] | 27 | #include "ipc.h" |
---|
[ebaebfe] | 28 | |
---|
| 29 | irc_user_t *irc_user_new( irc_t *irc, const char *nick ) |
---|
| 30 | { |
---|
| 31 | irc_user_t *iu = g_new0( irc_user_t, 1 ); |
---|
| 32 | |
---|
[b95932e] | 33 | iu->irc = irc; |
---|
[ebaebfe] | 34 | iu->nick = g_strdup( nick ); |
---|
| 35 | iu->user = iu->host = iu->fullname = iu->nick; |
---|
| 36 | |
---|
[92c8d41] | 37 | if( set_getbool( &irc->b->set, "private" ) ) |
---|
| 38 | iu->last_channel = NULL; |
---|
| 39 | else |
---|
| 40 | iu->last_channel = irc->default_channel; |
---|
[ebaebfe] | 41 | |
---|
| 42 | iu->key = g_strdup( nick ); |
---|
| 43 | nick_lc( iu->key ); |
---|
| 44 | /* Using the hash table for speed and irc->users for easy iteration |
---|
| 45 | through the list (since the GLib API doesn't have anything sane |
---|
| 46 | for that.) */ |
---|
| 47 | g_hash_table_insert( irc->nick_user_hash, iu->key, iu ); |
---|
| 48 | irc->users = g_slist_insert_sorted( irc->users, iu, irc_user_cmp ); |
---|
| 49 | |
---|
| 50 | return iu; |
---|
| 51 | } |
---|
| 52 | |
---|
[eabc9d2] | 53 | int irc_user_free( irc_t *irc, irc_user_t *iu ) |
---|
[ebaebfe] | 54 | { |
---|
[0bd948e] | 55 | static struct im_connection *last_ic; |
---|
| 56 | static char *msg; |
---|
[ebaebfe] | 57 | |
---|
[eabc9d2] | 58 | if( !iu ) |
---|
[ebaebfe] | 59 | return 0; |
---|
| 60 | |
---|
[0bd948e] | 61 | if( iu->bu && |
---|
| 62 | ( iu->bu->ic->flags & OPT_LOGGING_OUT ) && |
---|
| 63 | iu->bu->ic != last_ic ) |
---|
[1f0224c] | 64 | { |
---|
[0bd948e] | 65 | char host_prefix[] = "bitlbee."; |
---|
| 66 | char *s; |
---|
[1f0224c] | 67 | |
---|
[0bd948e] | 68 | /* Irssi recognises netsplits by quitmsgs with two |
---|
| 69 | hostnames, where a hostname is a "word" with one |
---|
| 70 | of more dots. Mangle no-dot hostnames a bit. */ |
---|
| 71 | if( strchr( irc->root->host, '.' ) ) |
---|
| 72 | *host_prefix = '\0'; |
---|
| 73 | |
---|
| 74 | last_ic = iu->bu->ic; |
---|
| 75 | g_free( msg ); |
---|
| 76 | if( !set_getbool( &irc->b->set, "simulate_netsplit" ) ) |
---|
| 77 | msg = g_strdup( "Account off-line" ); |
---|
| 78 | else if( ( s = strchr( iu->bu->ic->acc->user, '@' ) ) ) |
---|
| 79 | msg = g_strdup_printf( "%s%s %s", host_prefix, |
---|
| 80 | irc->root->host, s + 1 ); |
---|
| 81 | else |
---|
| 82 | msg = g_strdup_printf( "%s%s %s.%s", |
---|
| 83 | host_prefix, irc->root->host, |
---|
| 84 | iu->bu->ic->acc->prpl->name, irc->root->host ); |
---|
| 85 | } |
---|
| 86 | else if( !iu->bu || !( iu->bu->ic->flags & OPT_LOGGING_OUT ) ) |
---|
| 87 | { |
---|
| 88 | g_free( msg ); |
---|
| 89 | msg = g_strdup( "Removed" ); |
---|
| 90 | last_ic = NULL; |
---|
[1f0224c] | 91 | } |
---|
[0bd948e] | 92 | irc_user_quit( iu, msg ); |
---|
| 93 | |
---|
| 94 | irc->users = g_slist_remove( irc->users, iu ); |
---|
| 95 | g_hash_table_remove( irc->nick_user_hash, iu->key ); |
---|
[38ee021] | 96 | |
---|
[ebaebfe] | 97 | g_free( iu->nick ); |
---|
| 98 | if( iu->nick != iu->user ) g_free( iu->user ); |
---|
| 99 | if( iu->nick != iu->host ) g_free( iu->host ); |
---|
| 100 | if( iu->nick != iu->fullname ) g_free( iu->fullname ); |
---|
[619dd18] | 101 | g_free( iu->pastebuf ); |
---|
| 102 | if( iu->pastebuf_timer ) b_event_remove( iu->pastebuf_timer ); |
---|
[ebaebfe] | 103 | g_free( iu->key ); |
---|
[d7db346] | 104 | g_free( iu ); |
---|
[ebaebfe] | 105 | |
---|
| 106 | return 1; |
---|
| 107 | } |
---|
| 108 | |
---|
[280c56a] | 109 | irc_user_t *irc_user_by_name( irc_t *irc, const char *nick ) |
---|
[ebaebfe] | 110 | { |
---|
| 111 | char key[strlen(nick)+1]; |
---|
| 112 | |
---|
| 113 | strcpy( key, nick ); |
---|
| 114 | if( nick_lc( key ) ) |
---|
| 115 | return g_hash_table_lookup( irc->nick_user_hash, key ); |
---|
| 116 | else |
---|
| 117 | return NULL; |
---|
| 118 | } |
---|
| 119 | |
---|
[57c96f7] | 120 | int irc_user_set_nick( irc_user_t *iu, const char *new ) |
---|
[ebaebfe] | 121 | { |
---|
[57c96f7] | 122 | irc_t *irc = iu->irc; |
---|
[9a9b520] | 123 | irc_user_t *new_iu; |
---|
[ebaebfe] | 124 | char key[strlen(new)+1]; |
---|
[0b5cc72] | 125 | GSList *cl; |
---|
[ebaebfe] | 126 | |
---|
| 127 | strcpy( key, new ); |
---|
[9a9b520] | 128 | if( iu == NULL || !nick_lc( key ) || |
---|
| 129 | ( ( new_iu = irc_user_by_name( irc, new ) ) && new_iu != iu ) ) |
---|
[ebaebfe] | 130 | return 0; |
---|
| 131 | |
---|
[0b5cc72] | 132 | for( cl = irc->channels; cl; cl = cl->next ) |
---|
| 133 | { |
---|
| 134 | irc_channel_t *ic = cl->data; |
---|
| 135 | |
---|
| 136 | /* Send a NICK update if we're renaming our user, or someone |
---|
| 137 | who's in the same channel like our user. */ |
---|
| 138 | if( iu == irc->user || |
---|
[57c96f7] | 139 | ( ( ic->flags & IRC_CHANNEL_JOINED ) && |
---|
[0b5cc72] | 140 | irc_channel_has_user( ic, iu ) ) ) |
---|
| 141 | { |
---|
| 142 | irc_send_nick( iu, new ); |
---|
| 143 | break; |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
[ebaebfe] | 147 | irc->users = g_slist_remove( irc->users, iu ); |
---|
| 148 | g_hash_table_remove( irc->nick_user_hash, iu->key ); |
---|
| 149 | |
---|
| 150 | if( iu->nick == iu->user ) iu->user = NULL; |
---|
| 151 | if( iu->nick == iu->host ) iu->host = NULL; |
---|
| 152 | if( iu->nick == iu->fullname ) iu->fullname = NULL; |
---|
| 153 | g_free( iu->nick ); |
---|
| 154 | iu->nick = g_strdup( new ); |
---|
| 155 | if( iu->user == NULL ) iu->user = g_strdup( iu->nick ); |
---|
| 156 | if( iu->host == NULL ) iu->host = g_strdup( iu->nick ); |
---|
| 157 | if( iu->fullname == NULL ) iu->fullname = g_strdup( iu->nick ); |
---|
| 158 | |
---|
[c1d40e7] | 159 | g_free( iu->key ); |
---|
[ebaebfe] | 160 | iu->key = g_strdup( key ); |
---|
| 161 | g_hash_table_insert( irc->nick_user_hash, iu->key, iu ); |
---|
| 162 | irc->users = g_slist_insert_sorted( irc->users, iu, irc_user_cmp ); |
---|
| 163 | |
---|
[debe871] | 164 | if( iu == irc->user ) |
---|
| 165 | ipc_to_master_str( "NICK :%s\r\n", new ); |
---|
| 166 | |
---|
[ebaebfe] | 167 | return 1; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | gint irc_user_cmp( gconstpointer a_, gconstpointer b_ ) |
---|
| 171 | { |
---|
| 172 | const irc_user_t *a = a_, *b = b_; |
---|
| 173 | |
---|
| 174 | return strcmp( a->key, b->key ); |
---|
| 175 | } |
---|
[280c56a] | 176 | |
---|
[003a12b] | 177 | const char *irc_user_get_away( irc_user_t *iu ) |
---|
| 178 | { |
---|
| 179 | irc_t *irc = iu->irc; |
---|
| 180 | bee_user_t *bu = iu->bu; |
---|
| 181 | |
---|
| 182 | if( iu == irc->user ) |
---|
| 183 | return set_getstr( &irc->b->set, "away" ); |
---|
| 184 | else if( bu ) |
---|
| 185 | { |
---|
| 186 | if( !bu->flags & BEE_USER_ONLINE ) |
---|
| 187 | return "Offline"; |
---|
| 188 | else if( bu->flags & BEE_USER_AWAY ) |
---|
[4c3519a] | 189 | { |
---|
| 190 | if( bu->status_msg ) |
---|
| 191 | { |
---|
| 192 | static char ret[MAX_STRING]; |
---|
| 193 | g_snprintf( ret, MAX_STRING - 1, "%s (%s)", |
---|
| 194 | bu->status ? : "Away", bu->status_msg ); |
---|
| 195 | return ret; |
---|
| 196 | } |
---|
| 197 | else |
---|
| 198 | return bu->status ? : "Away"; |
---|
| 199 | } |
---|
[003a12b] | 200 | } |
---|
| 201 | |
---|
| 202 | return NULL; |
---|
| 203 | } |
---|
| 204 | |
---|
[0bd948e] | 205 | void irc_user_quit( irc_user_t *iu, const char *msg ) |
---|
| 206 | { |
---|
| 207 | GSList *l; |
---|
| 208 | gboolean send_quit = FALSE; |
---|
| 209 | |
---|
| 210 | if( !iu ) |
---|
| 211 | return; |
---|
| 212 | |
---|
| 213 | for( l = iu->irc->channels; l; l = l->next ) |
---|
[4ffd757] | 214 | { |
---|
| 215 | irc_channel_t *ic = l->data; |
---|
| 216 | send_quit |= irc_channel_del_user( ic, iu, IRC_CDU_SILENT, NULL ) && |
---|
| 217 | ( ic->flags & IRC_CHANNEL_JOINED ); |
---|
| 218 | } |
---|
[0bd948e] | 219 | |
---|
| 220 | if( send_quit ) |
---|
| 221 | irc_send_quit( iu, msg ); |
---|
| 222 | } |
---|
| 223 | |
---|
[280c56a] | 224 | /* User-type dependent functions, for root/NickServ: */ |
---|
| 225 | static gboolean root_privmsg( irc_user_t *iu, const char *msg ) |
---|
| 226 | { |
---|
[fb117aee] | 227 | char cmd[strlen(msg)+1]; |
---|
| 228 | |
---|
| 229 | strcpy( cmd, msg ); |
---|
| 230 | root_command_string( iu->irc, cmd ); |
---|
[280c56a] | 231 | |
---|
| 232 | return TRUE; |
---|
| 233 | } |
---|
| 234 | |
---|
[24b8bbb] | 235 | static gboolean root_ctcp( irc_user_t *iu, char * const *ctcp ) |
---|
| 236 | { |
---|
| 237 | if( g_strcasecmp( ctcp[0], "VERSION" ) == 0 ) |
---|
| 238 | { |
---|
[7b59872] | 239 | irc_send_msg_f( iu, "NOTICE", iu->irc->user->nick, "\001%s %s\001", |
---|
| 240 | ctcp[0], "BitlBee " BITLBEE_VERSION " " ARCH "/" CPU ); |
---|
| 241 | } |
---|
| 242 | else if( g_strcasecmp( ctcp[0], "PING" ) == 0 ) |
---|
| 243 | { |
---|
| 244 | irc_send_msg_f( iu, "NOTICE", iu->irc->user->nick, "\001%s %s\001", |
---|
| 245 | ctcp[0], ctcp[1] ? : "" ); |
---|
[24b8bbb] | 246 | } |
---|
| 247 | |
---|
| 248 | return TRUE; |
---|
| 249 | } |
---|
| 250 | |
---|
[280c56a] | 251 | const struct irc_user_funcs irc_user_root_funcs = { |
---|
| 252 | root_privmsg, |
---|
[24b8bbb] | 253 | root_ctcp, |
---|
[280c56a] | 254 | }; |
---|
| 255 | |
---|
| 256 | /* Echo to yourself: */ |
---|
| 257 | static gboolean self_privmsg( irc_user_t *iu, const char *msg ) |
---|
| 258 | { |
---|
[917a83e] | 259 | irc_send_msg( iu, "PRIVMSG", iu->nick, msg, NULL ); |
---|
[280c56a] | 260 | |
---|
| 261 | return TRUE; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | const struct irc_user_funcs irc_user_self_funcs = { |
---|
| 265 | self_privmsg, |
---|
| 266 | }; |
---|