[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; |
---|
[ebaebfe] | 52 | |
---|
[eabc9d2] | 53 | if( !iu ) |
---|
[ebaebfe] | 54 | return 0; |
---|
| 55 | |
---|
| 56 | irc->users = g_slist_remove( irc->users, iu ); |
---|
| 57 | g_hash_table_remove( irc->nick_user_hash, iu->key ); |
---|
| 58 | |
---|
[38ee021] | 59 | for( l = irc->channels; l; l = l->next ) |
---|
| 60 | irc_channel_del_user( (irc_channel_t*) l->data, iu ); |
---|
| 61 | |
---|
[ebaebfe] | 62 | g_free( iu->nick ); |
---|
| 63 | if( iu->nick != iu->user ) g_free( iu->user ); |
---|
| 64 | if( iu->nick != iu->host ) g_free( iu->host ); |
---|
| 65 | if( iu->nick != iu->fullname ) g_free( iu->fullname ); |
---|
| 66 | g_free( iu->sendbuf ); |
---|
| 67 | if( iu->sendbuf_timer ) b_event_remove( iu->sendbuf_timer ); |
---|
| 68 | g_free( iu->key ); |
---|
| 69 | |
---|
| 70 | return 1; |
---|
| 71 | } |
---|
| 72 | |
---|
[280c56a] | 73 | irc_user_t *irc_user_by_name( irc_t *irc, const char *nick ) |
---|
[ebaebfe] | 74 | { |
---|
| 75 | char key[strlen(nick)+1]; |
---|
| 76 | |
---|
| 77 | strcpy( key, nick ); |
---|
| 78 | if( nick_lc( key ) ) |
---|
| 79 | return g_hash_table_lookup( irc->nick_user_hash, key ); |
---|
| 80 | else |
---|
| 81 | return NULL; |
---|
| 82 | } |
---|
| 83 | |
---|
[57c96f7] | 84 | int irc_user_set_nick( irc_user_t *iu, const char *new ) |
---|
[ebaebfe] | 85 | { |
---|
[57c96f7] | 86 | irc_t *irc = iu->irc; |
---|
[ebaebfe] | 87 | char key[strlen(new)+1]; |
---|
[0b5cc72] | 88 | GSList *cl; |
---|
[ebaebfe] | 89 | |
---|
| 90 | strcpy( key, new ); |
---|
[280c56a] | 91 | if( iu == NULL || !nick_lc( key ) || irc_user_by_name( irc, new ) ) |
---|
[ebaebfe] | 92 | return 0; |
---|
| 93 | |
---|
[0b5cc72] | 94 | for( cl = irc->channels; cl; cl = cl->next ) |
---|
| 95 | { |
---|
| 96 | irc_channel_t *ic = cl->data; |
---|
| 97 | |
---|
| 98 | /* Send a NICK update if we're renaming our user, or someone |
---|
| 99 | who's in the same channel like our user. */ |
---|
| 100 | if( iu == irc->user || |
---|
[57c96f7] | 101 | ( ( ic->flags & IRC_CHANNEL_JOINED ) && |
---|
[0b5cc72] | 102 | irc_channel_has_user( ic, iu ) ) ) |
---|
| 103 | { |
---|
| 104 | irc_send_nick( iu, new ); |
---|
| 105 | break; |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
[ebaebfe] | 109 | irc->users = g_slist_remove( irc->users, iu ); |
---|
| 110 | g_hash_table_remove( irc->nick_user_hash, iu->key ); |
---|
| 111 | |
---|
| 112 | if( iu->nick == iu->user ) iu->user = NULL; |
---|
| 113 | if( iu->nick == iu->host ) iu->host = NULL; |
---|
| 114 | if( iu->nick == iu->fullname ) iu->fullname = NULL; |
---|
| 115 | g_free( iu->nick ); |
---|
| 116 | iu->nick = g_strdup( new ); |
---|
| 117 | if( iu->user == NULL ) iu->user = g_strdup( iu->nick ); |
---|
| 118 | if( iu->host == NULL ) iu->host = g_strdup( iu->nick ); |
---|
| 119 | if( iu->fullname == NULL ) iu->fullname = g_strdup( iu->nick ); |
---|
| 120 | |
---|
| 121 | iu->key = g_strdup( key ); |
---|
| 122 | g_hash_table_insert( irc->nick_user_hash, iu->key, iu ); |
---|
| 123 | irc->users = g_slist_insert_sorted( irc->users, iu, irc_user_cmp ); |
---|
| 124 | |
---|
| 125 | return 1; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | gint irc_user_cmp( gconstpointer a_, gconstpointer b_ ) |
---|
| 129 | { |
---|
| 130 | const irc_user_t *a = a_, *b = b_; |
---|
| 131 | |
---|
| 132 | return strcmp( a->key, b->key ); |
---|
| 133 | } |
---|
[280c56a] | 134 | |
---|
[003a12b] | 135 | const char *irc_user_get_away( irc_user_t *iu ) |
---|
| 136 | { |
---|
| 137 | irc_t *irc = iu->irc; |
---|
| 138 | bee_user_t *bu = iu->bu; |
---|
| 139 | |
---|
| 140 | if( iu == irc->user ) |
---|
| 141 | return set_getstr( &irc->b->set, "away" ); |
---|
| 142 | else if( bu ) |
---|
| 143 | { |
---|
| 144 | if( !bu->flags & BEE_USER_ONLINE ) |
---|
| 145 | return "Offline"; |
---|
| 146 | else if( bu->flags & BEE_USER_AWAY ) |
---|
[4c3519a] | 147 | { |
---|
| 148 | if( bu->status_msg ) |
---|
| 149 | { |
---|
| 150 | static char ret[MAX_STRING]; |
---|
| 151 | g_snprintf( ret, MAX_STRING - 1, "%s (%s)", |
---|
| 152 | bu->status ? : "Away", bu->status_msg ); |
---|
| 153 | return ret; |
---|
| 154 | } |
---|
| 155 | else |
---|
| 156 | return bu->status ? : "Away"; |
---|
| 157 | } |
---|
[003a12b] | 158 | } |
---|
| 159 | |
---|
| 160 | return NULL; |
---|
| 161 | } |
---|
| 162 | |
---|
[280c56a] | 163 | /* User-type dependent functions, for root/NickServ: */ |
---|
| 164 | static gboolean root_privmsg( irc_user_t *iu, const char *msg ) |
---|
| 165 | { |
---|
[fb117aee] | 166 | char cmd[strlen(msg)+1]; |
---|
| 167 | |
---|
[74f1cde] | 168 | g_free( iu->irc->last_root_cmd ); |
---|
| 169 | iu->irc->last_root_cmd = g_strdup( iu->nick ); |
---|
| 170 | |
---|
[fb117aee] | 171 | strcpy( cmd, msg ); |
---|
| 172 | root_command_string( iu->irc, cmd ); |
---|
[280c56a] | 173 | |
---|
| 174 | return TRUE; |
---|
| 175 | } |
---|
| 176 | |
---|
[24b8bbb] | 177 | static gboolean root_ctcp( irc_user_t *iu, char * const *ctcp ) |
---|
| 178 | { |
---|
| 179 | if( g_strcasecmp( ctcp[0], "VERSION" ) == 0 ) |
---|
| 180 | { |
---|
[7b59872] | 181 | irc_send_msg_f( iu, "NOTICE", iu->irc->user->nick, "\001%s %s\001", |
---|
| 182 | ctcp[0], "BitlBee " BITLBEE_VERSION " " ARCH "/" CPU ); |
---|
| 183 | } |
---|
| 184 | else if( g_strcasecmp( ctcp[0], "PING" ) == 0 ) |
---|
| 185 | { |
---|
| 186 | irc_send_msg_f( iu, "NOTICE", iu->irc->user->nick, "\001%s %s\001", |
---|
| 187 | ctcp[0], ctcp[1] ? : "" ); |
---|
[24b8bbb] | 188 | } |
---|
| 189 | |
---|
| 190 | return TRUE; |
---|
| 191 | } |
---|
| 192 | |
---|
[280c56a] | 193 | const struct irc_user_funcs irc_user_root_funcs = { |
---|
| 194 | root_privmsg, |
---|
[24b8bbb] | 195 | root_ctcp, |
---|
[280c56a] | 196 | }; |
---|
| 197 | |
---|
| 198 | /* Echo to yourself: */ |
---|
| 199 | static gboolean self_privmsg( irc_user_t *iu, const char *msg ) |
---|
| 200 | { |
---|
[6761a40] | 201 | irc_send_msg_raw( iu, "PRIVMSG", iu->nick, msg ); |
---|
[280c56a] | 202 | |
---|
| 203 | return TRUE; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | const struct irc_user_funcs irc_user_self_funcs = { |
---|
| 207 | self_privmsg, |
---|
| 208 | }; |
---|