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 | |
---|
32 | iu->nick = g_strdup( nick ); |
---|
33 | iu->user = iu->host = iu->fullname = iu->nick; |
---|
34 | |
---|
35 | iu->is_private = set_getbool( &irc->b->set, "private" ); |
---|
36 | |
---|
37 | iu->key = g_strdup( nick ); |
---|
38 | nick_lc( iu->key ); |
---|
39 | /* Using the hash table for speed and irc->users for easy iteration |
---|
40 | through the list (since the GLib API doesn't have anything sane |
---|
41 | for that.) */ |
---|
42 | g_hash_table_insert( irc->nick_user_hash, iu->key, iu ); |
---|
43 | irc->users = g_slist_insert_sorted( irc->users, iu, irc_user_cmp ); |
---|
44 | |
---|
45 | return iu; |
---|
46 | } |
---|
47 | |
---|
48 | int irc_user_free( irc_t *irc, const char *nick ) |
---|
49 | { |
---|
50 | irc_user_t *iu; |
---|
51 | |
---|
52 | if( !( iu = irc_user_find( irc, nick ) ) ) |
---|
53 | return 0; |
---|
54 | |
---|
55 | irc->users = g_slist_remove( irc->users, iu ); |
---|
56 | g_hash_table_remove( irc->nick_user_hash, iu->key ); |
---|
57 | |
---|
58 | g_free( iu->nick ); |
---|
59 | if( iu->nick != iu->user ) g_free( iu->user ); |
---|
60 | if( iu->nick != iu->host ) g_free( iu->host ); |
---|
61 | if( iu->nick != iu->fullname ) g_free( iu->fullname ); |
---|
62 | g_free( iu->sendbuf ); |
---|
63 | if( iu->sendbuf_timer ) b_event_remove( iu->sendbuf_timer ); |
---|
64 | g_free( iu->key ); |
---|
65 | |
---|
66 | return 1; |
---|
67 | } |
---|
68 | |
---|
69 | irc_user_t *irc_user_find( irc_t *irc, const char *nick ) |
---|
70 | { |
---|
71 | char key[strlen(nick)+1]; |
---|
72 | |
---|
73 | strcpy( key, nick ); |
---|
74 | if( nick_lc( key ) ) |
---|
75 | return g_hash_table_lookup( irc->nick_user_hash, key ); |
---|
76 | else |
---|
77 | return NULL; |
---|
78 | } |
---|
79 | |
---|
80 | int irc_user_rename( irc_t *irc, const char *old, const char *new ) |
---|
81 | { |
---|
82 | irc_user_t *iu = irc_user_find( irc, old ); |
---|
83 | char key[strlen(new)+1]; |
---|
84 | |
---|
85 | strcpy( key, new ); |
---|
86 | if( iu == NULL || !nick_lc( key ) || irc_user_find( irc, new ) ) |
---|
87 | return 0; |
---|
88 | |
---|
89 | irc->users = g_slist_remove( irc->users, iu ); |
---|
90 | g_hash_table_remove( irc->nick_user_hash, iu->key ); |
---|
91 | |
---|
92 | if( iu->nick == iu->user ) iu->user = NULL; |
---|
93 | if( iu->nick == iu->host ) iu->host = NULL; |
---|
94 | if( iu->nick == iu->fullname ) iu->fullname = NULL; |
---|
95 | g_free( iu->nick ); |
---|
96 | iu->nick = g_strdup( new ); |
---|
97 | if( iu->user == NULL ) iu->user = g_strdup( iu->nick ); |
---|
98 | if( iu->host == NULL ) iu->host = g_strdup( iu->nick ); |
---|
99 | if( iu->fullname == NULL ) iu->fullname = g_strdup( iu->nick ); |
---|
100 | |
---|
101 | iu->key = g_strdup( key ); |
---|
102 | g_hash_table_insert( irc->nick_user_hash, iu->key, iu ); |
---|
103 | irc->users = g_slist_insert_sorted( irc->users, iu, irc_user_cmp ); |
---|
104 | |
---|
105 | return 1; |
---|
106 | } |
---|
107 | |
---|
108 | gint irc_user_cmp( gconstpointer a_, gconstpointer b_ ) |
---|
109 | { |
---|
110 | const irc_user_t *a = a_, *b = b_; |
---|
111 | |
---|
112 | return strcmp( a->key, b->key ); |
---|
113 | } |
---|