File: user.c

package info (click to toggle)
bitlbee 0.92-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,420 kB
  • ctags: 4,692
  • sloc: ansic: 40,724; xml: 965; sh: 425; makefile: 255; perl: 120
file content (223 lines) | stat: -rw-r--r-- 5,381 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  /********************************************************************\
  * BitlBee -- An IRC to other IM-networks gateway                     *
  *                                                                    *
  * Copyright 2002-2004 Wilmer van der Gaast and others                *
  \********************************************************************/

/* Stuff to handle, save and search buddies                             */

/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License with
  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  Suite 330, Boston, MA  02111-1307  USA
*/

#define BITLBEE_CORE
#include "bitlbee.h"

user_t *user_add( irc_t *irc, char *nick )
{
	user_t *u, *lu = NULL;
	char *key;
	
	if( !nick_ok( nick ) )
		return( NULL );
	
	if( user_find( irc, nick ) != NULL )
		return( NULL );
	
	if( ( u = irc->users ) )
	{
		while( u )
		{
			if( nick_cmp( nick, u->nick ) < 0 )
				break;
			
			lu = u;
			u = u->next;
		}
		
		u = g_new0( user_t, 1 );
		if( lu )
		{
			u->next = lu->next;
			lu->next = u;
		}
		else
		{
			u->next = irc->users;
			irc->users = u;
		}
	}
	else
	{
		irc->users = u = g_new0( user_t, 1 );
	}
	
	u->user = u->realname = u->host = u->nick = g_strdup( nick );
	u->is_private = set_getint( irc, "private" );
	
	key = g_strdup( nick );
	nick_lc( key );
	g_hash_table_insert( irc->userhash, key, u );
	
	return( u );
}

int user_del( irc_t *irc, char *nick )
{
	user_t *u, *t;
	char *key;
	gpointer okey, ovalue;
	
	if( !nick_ok( nick ) )
		return( 0 );
	
	u = irc->users;
	t = NULL;
	while( u )
	{
		if( nick_cmp( u->nick, nick ) == 0 )
		{
			/* Get this key now already, since "nick" might be free()d
			   at the time we start playing with the hash... */
			key = g_strdup( nick );
			nick_lc( key );
			
			if( t )
				t->next = u->next;
			else
				irc->users = u->next;
			if( u->online )
				irc_kill( irc, u );
			g_free( u->nick );
			if( u->nick != u->user ) g_free( u->user );
			if( u->nick != u->host ) g_free( u->host );
			if( u->nick != u->realname ) g_free( u->realname );
			if( u->away ) g_free( u->away );
			if( u->handle ) g_free( u->handle );
			if( u->sendbuf ) g_free( u->sendbuf );
			if( u->sendbuf_timer ) g_source_remove( u->sendbuf_timer );
			g_free( u );
			
			if( !g_hash_table_lookup_extended( irc->userhash, key, &okey, &ovalue ) || ovalue != u )
			{
				g_free( key );
				return( 1 );	/* Although this is a severe error, the user is removed from the list... */
			}
			g_hash_table_remove( irc->userhash, key );
			g_free( key );
			g_free( okey );
			
			return( 1 );
		}
		u = (t=u)->next;
	}
	
	return( 0 );
}

user_t *user_find( irc_t *irc, char *nick )
{
	char key[512] = "";
	
	strncpy( key, nick, sizeof( key ) - 1 );
	if( nick_lc( key ) )
		return( g_hash_table_lookup( irc->userhash, key ) );
	else
		return( NULL );
}

user_t *user_findhandle( struct gaim_connection *gc, char *handle )
{
	user_t *u = gc->irc->users;
	
	while( u )
	{
		if( u->gc == gc && u->handle && handle_cmp( u->handle, handle, gc->protocol ) == 0 )
			break;
		u = u->next;
	}
	
	return( u );
}

void user_rename( irc_t *irc, char *oldnick, char *newnick )
{
	user_t *u = user_find( irc, oldnick );
	gpointer okey, ovalue;
	char *key;
	
	if( !u ) return;	/* Should've been checked by the caller... */
	
	g_free( u->nick );
	if( u->nick == u->user ) u->user = NULL;
	if( u->nick == u->host ) u->host = NULL;
	if( u->nick == u->realname ) u->realname = NULL;
	u->nick = g_strdup( newnick );
	if( !u->user ) u->user = u->nick;
	if( !u->host ) u->user = u->host;
	if( !u->realname ) u->user = u->realname;
	
	/* Remove the old reference to this user from the hash and create a
	   new one with the new nick. This is indeed a bit messy. */
	key = g_strdup( oldnick );
	nick_lc( key );
	if( !g_hash_table_lookup_extended( irc->userhash, key, &okey, &ovalue ) || ovalue != u )
	{
		g_free( key );
		return;		/* This really shouldn't happen! */	
	}
	g_hash_table_remove( irc->userhash, key );
	g_free( key );
	g_free( okey );
	
	key = g_strdup( newnick );
	nick_lc( key );
	g_hash_table_insert( irc->userhash, key, u );
	
	/* Also, let's try to keep the linked list nicely sorted. Fear this
	   code. If my teacher would see this, she would cry. ;-) */
	{
		user_t *u1, *lu1;
		
		/* Remove the user from the old position in the chain. */
		if( u == irc->users )
		{
			irc->users = u->next;
		}
		else
		{
			u1 = u;
			for( lu1 = irc->users; lu1->next != u1; lu1 = lu1->next );
			lu1->next = u1->next;
		}
		
		/* Search for the new position. */
		for( lu1 = NULL, u1 = irc->users; u1; u1 = u1->next )
		{
			if( nick_cmp( newnick, u1->nick ) < 0 )
				break;
			
			lu1 = u1;
		}
		
		/* Insert it at this new position. */
		u->next = u1;
		if( lu1 )
			lu1->next = u;
		else
			irc->users = u;
	}
}