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->flags = set_getbool( &irc->b->set, "private" ) ? IRC_USER_PRIVATE : 0; |
---|
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 | GSList *l; |
---|
53 | |
---|
54 | if( !( iu = irc_user_by_name( irc, nick ) ) ) |
---|
55 | return 0; |
---|
56 | |
---|
57 | irc->users = g_slist_remove( irc->users, iu ); |
---|
58 | g_hash_table_remove( irc->nick_user_hash, iu->key ); |
---|
59 | |
---|
60 | for( l = irc->channels; l; l = l->next ) |
---|
61 | irc_channel_del_user( (irc_channel_t*) l->data, iu ); |
---|
62 | |
---|
63 | g_free( iu->nick ); |
---|
64 | if( iu->nick != iu->user ) g_free( iu->user ); |
---|
65 | if( iu->nick != iu->host ) g_free( iu->host ); |
---|
66 | if( iu->nick != iu->fullname ) g_free( iu->fullname ); |
---|
67 | g_free( iu->sendbuf ); |
---|
68 | if( iu->sendbuf_timer ) b_event_remove( iu->sendbuf_timer ); |
---|
69 | g_free( iu->key ); |
---|
70 | |
---|
71 | return 1; |
---|
72 | } |
---|
73 | |
---|
74 | irc_user_t *irc_user_by_name( irc_t *irc, const char *nick ) |
---|
75 | { |
---|
76 | char key[strlen(nick)+1]; |
---|
77 | |
---|
78 | strcpy( key, nick ); |
---|
79 | if( nick_lc( key ) ) |
---|
80 | return g_hash_table_lookup( irc->nick_user_hash, key ); |
---|
81 | else |
---|
82 | return NULL; |
---|
83 | } |
---|
84 | |
---|
85 | int irc_user_rename( irc_t *irc, const char *old, const char *new ) |
---|
86 | { |
---|
87 | irc_user_t *iu = irc_user_by_name( irc, old ); |
---|
88 | char key[strlen(new)+1]; |
---|
89 | |
---|
90 | strcpy( key, new ); |
---|
91 | if( iu == NULL || !nick_lc( key ) || irc_user_by_name( irc, new ) ) |
---|
92 | return 0; |
---|
93 | |
---|
94 | irc->users = g_slist_remove( irc->users, iu ); |
---|
95 | g_hash_table_remove( irc->nick_user_hash, iu->key ); |
---|
96 | |
---|
97 | if( iu->nick == iu->user ) iu->user = NULL; |
---|
98 | if( iu->nick == iu->host ) iu->host = NULL; |
---|
99 | if( iu->nick == iu->fullname ) iu->fullname = NULL; |
---|
100 | g_free( iu->nick ); |
---|
101 | iu->nick = g_strdup( new ); |
---|
102 | if( iu->user == NULL ) iu->user = g_strdup( iu->nick ); |
---|
103 | if( iu->host == NULL ) iu->host = g_strdup( iu->nick ); |
---|
104 | if( iu->fullname == NULL ) iu->fullname = g_strdup( iu->nick ); |
---|
105 | |
---|
106 | iu->key = g_strdup( key ); |
---|
107 | g_hash_table_insert( irc->nick_user_hash, iu->key, iu ); |
---|
108 | irc->users = g_slist_insert_sorted( irc->users, iu, irc_user_cmp ); |
---|
109 | |
---|
110 | return 1; |
---|
111 | } |
---|
112 | |
---|
113 | gint irc_user_cmp( gconstpointer a_, gconstpointer b_ ) |
---|
114 | { |
---|
115 | const irc_user_t *a = a_, *b = b_; |
---|
116 | |
---|
117 | return strcmp( a->key, b->key ); |
---|
118 | } |
---|
119 | |
---|
120 | /* User-type dependent functions, for root/NickServ: */ |
---|
121 | static gboolean root_privmsg( irc_user_t *iu, const char *msg ) |
---|
122 | { |
---|
123 | g_free( iu->irc->last_root_cmd ); |
---|
124 | iu->irc->last_root_cmd = g_strdup( iu->nick ); |
---|
125 | |
---|
126 | root_command_string( iu->irc, msg ); |
---|
127 | |
---|
128 | return TRUE; |
---|
129 | } |
---|
130 | |
---|
131 | const struct irc_user_funcs irc_user_root_funcs = { |
---|
132 | root_privmsg, |
---|
133 | }; |
---|
134 | |
---|
135 | /* Echo to yourself: */ |
---|
136 | static gboolean self_privmsg( irc_user_t *iu, const char *msg ) |
---|
137 | { |
---|
138 | irc_send_msg_raw( iu, "PRIVMSG", iu->nick, msg ); |
---|
139 | |
---|
140 | return TRUE; |
---|
141 | } |
---|
142 | |
---|
143 | const struct irc_user_funcs irc_user_self_funcs = { |
---|
144 | self_privmsg, |
---|
145 | }; |
---|