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->irc = irc; |
---|
33 | iu->nick = g_strdup( nick ); |
---|
34 | iu->user = iu->host = iu->fullname = iu->nick; |
---|
35 | |
---|
36 | iu->is_private = set_getbool( &irc->b->set, "private" ); |
---|
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 | |
---|
49 | int irc_user_free( irc_t *irc, const char *nick ) |
---|
50 | { |
---|
51 | irc_user_t *iu; |
---|
52 | |
---|
53 | if( !( iu = irc_user_find( irc, nick ) ) ) |
---|
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 | |
---|
59 | g_free( iu->nick ); |
---|
60 | if( iu->nick != iu->user ) g_free( iu->user ); |
---|
61 | if( iu->nick != iu->host ) g_free( iu->host ); |
---|
62 | if( iu->nick != iu->fullname ) g_free( iu->fullname ); |
---|
63 | g_free( iu->sendbuf ); |
---|
64 | if( iu->sendbuf_timer ) b_event_remove( iu->sendbuf_timer ); |
---|
65 | g_free( iu->key ); |
---|
66 | |
---|
67 | return 1; |
---|
68 | } |
---|
69 | |
---|
70 | irc_user_t *irc_user_find( irc_t *irc, const char *nick ) |
---|
71 | { |
---|
72 | char key[strlen(nick)+1]; |
---|
73 | |
---|
74 | strcpy( key, nick ); |
---|
75 | if( nick_lc( key ) ) |
---|
76 | return g_hash_table_lookup( irc->nick_user_hash, key ); |
---|
77 | else |
---|
78 | return NULL; |
---|
79 | } |
---|
80 | |
---|
81 | int irc_user_rename( irc_t *irc, const char *old, const char *new ) |
---|
82 | { |
---|
83 | irc_user_t *iu = irc_user_find( irc, old ); |
---|
84 | char key[strlen(new)+1]; |
---|
85 | |
---|
86 | strcpy( key, new ); |
---|
87 | if( iu == NULL || !nick_lc( key ) || irc_user_find( irc, new ) ) |
---|
88 | return 0; |
---|
89 | |
---|
90 | irc->users = g_slist_remove( irc->users, iu ); |
---|
91 | g_hash_table_remove( irc->nick_user_hash, iu->key ); |
---|
92 | |
---|
93 | if( iu->nick == iu->user ) iu->user = NULL; |
---|
94 | if( iu->nick == iu->host ) iu->host = NULL; |
---|
95 | if( iu->nick == iu->fullname ) iu->fullname = NULL; |
---|
96 | g_free( iu->nick ); |
---|
97 | iu->nick = g_strdup( new ); |
---|
98 | if( iu->user == NULL ) iu->user = g_strdup( iu->nick ); |
---|
99 | if( iu->host == NULL ) iu->host = g_strdup( iu->nick ); |
---|
100 | if( iu->fullname == NULL ) iu->fullname = g_strdup( iu->nick ); |
---|
101 | |
---|
102 | iu->key = g_strdup( key ); |
---|
103 | g_hash_table_insert( irc->nick_user_hash, iu->key, iu ); |
---|
104 | irc->users = g_slist_insert_sorted( irc->users, iu, irc_user_cmp ); |
---|
105 | |
---|
106 | return 1; |
---|
107 | } |
---|
108 | |
---|
109 | gint irc_user_cmp( gconstpointer a_, gconstpointer b_ ) |
---|
110 | { |
---|
111 | const irc_user_t *a = a_, *b = b_; |
---|
112 | |
---|
113 | return strcmp( a->key, b->key ); |
---|
114 | } |
---|