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