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 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 | user_t *user_add( irc_t *irc, char *nick ) |
---|
30 | { |
---|
31 | user_t *u, *lu = NULL; |
---|
32 | char *key; |
---|
33 | |
---|
34 | if( !nick_ok( nick ) ) |
---|
35 | return( NULL ); |
---|
36 | |
---|
37 | if( user_find( irc, nick ) != NULL ) |
---|
38 | return( NULL ); |
---|
39 | |
---|
40 | if( ( u = irc->users ) ) |
---|
41 | { |
---|
42 | while( u ) |
---|
43 | { |
---|
44 | if( nick_cmp( nick, u->nick ) < 0 ) |
---|
45 | break; |
---|
46 | |
---|
47 | lu = u; |
---|
48 | u = u->next; |
---|
49 | } |
---|
50 | |
---|
51 | u = g_new0( user_t, 1 ); |
---|
52 | if( lu ) |
---|
53 | { |
---|
54 | u->next = lu->next; |
---|
55 | lu->next = u; |
---|
56 | } |
---|
57 | else |
---|
58 | { |
---|
59 | u->next = irc->users; |
---|
60 | irc->users = u; |
---|
61 | } |
---|
62 | } |
---|
63 | else |
---|
64 | { |
---|
65 | irc->users = u = g_new0( user_t, 1 ); |
---|
66 | } |
---|
67 | |
---|
68 | u->user = u->realname = u->host = u->nick = g_strdup( nick ); |
---|
69 | u->is_private = set_getint( irc, "private" ); |
---|
70 | |
---|
71 | key = g_strdup( nick ); |
---|
72 | nick_lc( key ); |
---|
73 | g_hash_table_insert( irc->userhash, key, u ); |
---|
74 | |
---|
75 | return( u ); |
---|
76 | } |
---|
77 | |
---|
78 | int user_del( irc_t *irc, char *nick ) |
---|
79 | { |
---|
80 | user_t *u, *t; |
---|
81 | char *key; |
---|
82 | gpointer okey, ovalue; |
---|
83 | |
---|
84 | if( !nick_ok( nick ) ) |
---|
85 | return( 0 ); |
---|
86 | |
---|
87 | u = irc->users; |
---|
88 | t = NULL; |
---|
89 | while( u ) |
---|
90 | { |
---|
91 | if( nick_cmp( u->nick, nick ) == 0 ) |
---|
92 | { |
---|
93 | /* Get this key now already, since "nick" might be free()d |
---|
94 | at the time we start playing with the hash... */ |
---|
95 | key = g_strdup( nick ); |
---|
96 | nick_lc( key ); |
---|
97 | |
---|
98 | if( t ) |
---|
99 | t->next = u->next; |
---|
100 | else |
---|
101 | irc->users = u->next; |
---|
102 | if( u->online ) |
---|
103 | irc_kill( irc, u ); |
---|
104 | g_free( u->nick ); |
---|
105 | if( u->nick != u->user ) g_free( u->user ); |
---|
106 | if( u->nick != u->host ) g_free( u->host ); |
---|
107 | if( u->nick != u->realname ) g_free( u->realname ); |
---|
108 | if( u->away ) g_free( u->away ); |
---|
109 | if( u->handle ) g_free( u->handle ); |
---|
110 | if( u->sendbuf ) g_free( u->sendbuf ); |
---|
111 | if( u->sendbuf_timer ) g_source_remove( u->sendbuf_timer ); |
---|
112 | g_free( u ); |
---|
113 | |
---|
114 | if( !g_hash_table_lookup_extended( irc->userhash, key, &okey, &ovalue ) || ovalue != u ) |
---|
115 | { |
---|
116 | g_free( key ); |
---|
117 | return( 1 ); /* Although this is a severe error, the user is removed from the list... */ |
---|
118 | } |
---|
119 | g_hash_table_remove( irc->userhash, key ); |
---|
120 | g_free( key ); |
---|
121 | g_free( okey ); |
---|
122 | |
---|
123 | return( 1 ); |
---|
124 | } |
---|
125 | u = (t=u)->next; |
---|
126 | } |
---|
127 | |
---|
128 | return( 0 ); |
---|
129 | } |
---|
130 | |
---|
131 | user_t *user_find( irc_t *irc, char *nick ) |
---|
132 | { |
---|
133 | char key[512] = ""; |
---|
134 | |
---|
135 | strncpy( key, nick, sizeof( key ) - 1 ); |
---|
136 | if( nick_lc( key ) ) |
---|
137 | return( g_hash_table_lookup( irc->userhash, key ) ); |
---|
138 | else |
---|
139 | return( NULL ); |
---|
140 | } |
---|
141 | |
---|
142 | user_t *user_findhandle( struct gaim_connection *gc, char *handle ) |
---|
143 | { |
---|
144 | user_t *u = gc->irc->users; |
---|
145 | |
---|
146 | while( u ) |
---|
147 | { |
---|
148 | if( u->gc == gc && u->handle && gc->prpl->cmp_buddynames ( u->handle, handle ) == 0 ) |
---|
149 | break; |
---|
150 | u = u->next; |
---|
151 | } |
---|
152 | |
---|
153 | return( u ); |
---|
154 | } |
---|
155 | |
---|
156 | void user_rename( irc_t *irc, char *oldnick, char *newnick ) |
---|
157 | { |
---|
158 | user_t *u = user_find( irc, oldnick ); |
---|
159 | gpointer okey, ovalue; |
---|
160 | char *key; |
---|
161 | |
---|
162 | if( !u ) return; /* Should've been checked by the caller... */ |
---|
163 | |
---|
164 | g_free( u->nick ); |
---|
165 | if( u->nick == u->user ) u->user = NULL; |
---|
166 | if( u->nick == u->host ) u->host = NULL; |
---|
167 | if( u->nick == u->realname ) u->realname = NULL; |
---|
168 | u->nick = g_strdup( newnick ); |
---|
169 | if( !u->user ) u->user = u->nick; |
---|
170 | if( !u->host ) u->host = u->nick; |
---|
171 | if( !u->realname ) u->realname = u->nick; |
---|
172 | |
---|
173 | /* Remove the old reference to this user from the hash and create a |
---|
174 | new one with the new nick. This is indeed a bit messy. */ |
---|
175 | key = g_strdup( oldnick ); |
---|
176 | nick_lc( key ); |
---|
177 | if( !g_hash_table_lookup_extended( irc->userhash, key, &okey, &ovalue ) || ovalue != u ) |
---|
178 | { |
---|
179 | g_free( key ); |
---|
180 | return; /* This really shouldn't happen! */ |
---|
181 | } |
---|
182 | g_hash_table_remove( irc->userhash, key ); |
---|
183 | g_free( key ); |
---|
184 | g_free( okey ); |
---|
185 | |
---|
186 | key = g_strdup( newnick ); |
---|
187 | nick_lc( key ); |
---|
188 | g_hash_table_insert( irc->userhash, key, u ); |
---|
189 | |
---|
190 | /* Also, let's try to keep the linked list nicely sorted. Fear this |
---|
191 | code. If my teacher would see this, she would cry. ;-) */ |
---|
192 | { |
---|
193 | user_t *u1, *lu1; |
---|
194 | |
---|
195 | /* Remove the user from the old position in the chain. */ |
---|
196 | if( u == irc->users ) |
---|
197 | { |
---|
198 | irc->users = u->next; |
---|
199 | } |
---|
200 | else |
---|
201 | { |
---|
202 | u1 = u; |
---|
203 | for( lu1 = irc->users; lu1->next != u1; lu1 = lu1->next ); |
---|
204 | lu1->next = u1->next; |
---|
205 | } |
---|
206 | |
---|
207 | /* Search for the new position. */ |
---|
208 | for( lu1 = NULL, u1 = irc->users; u1; u1 = u1->next ) |
---|
209 | { |
---|
210 | if( nick_cmp( newnick, u1->nick ) < 0 ) |
---|
211 | break; |
---|
212 | |
---|
213 | lu1 = u1; |
---|
214 | } |
---|
215 | |
---|
216 | /* Insert it at this new position. */ |
---|
217 | u->next = u1; |
---|
218 | if( lu1 ) |
---|
219 | lu1->next = u; |
---|
220 | else |
---|
221 | irc->users = u; |
---|
222 | } |
---|
223 | } |
---|