source: irc_user.c @ e21c0f8

Last change on this file since e21c0f8 was eabc9d2, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-11T22:49:32Z

Fixed cleanup issues when turning off an account. Also fixed syntax of
*_user_free().

  • Property mode set to 100644
File size: 4.5 KB
Line 
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
28irc_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
49int irc_user_free( irc_t *irc, irc_user_t *iu )
50{
51        GSList *l;
52       
53        if( !iu )
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        for( l = irc->channels; l; l = l->next )
60                irc_channel_del_user( (irc_channel_t*) l->data, iu );
61       
62        g_free( iu->nick );
63        if( iu->nick != iu->user ) g_free( iu->user );
64        if( iu->nick != iu->host ) g_free( iu->host );
65        if( iu->nick != iu->fullname ) g_free( iu->fullname );
66        g_free( iu->sendbuf );
67        if( iu->sendbuf_timer ) b_event_remove( iu->sendbuf_timer );
68        g_free( iu->key );
69       
70        return 1;
71}
72
73irc_user_t *irc_user_by_name( irc_t *irc, const char *nick )
74{
75        char key[strlen(nick)+1];
76       
77        strcpy( key, nick );
78        if( nick_lc( key ) )
79                return g_hash_table_lookup( irc->nick_user_hash, key );
80        else
81                return NULL;
82}
83
84int irc_user_set_nick( irc_user_t *iu, const char *new )
85{
86        irc_t *irc = iu->irc;
87        char key[strlen(new)+1];
88        GSList *cl;
89       
90        strcpy( key, new );
91        if( iu == NULL || !nick_lc( key ) || irc_user_by_name( irc, new ) )
92                return 0;
93       
94        for( cl = irc->channels; cl; cl = cl->next )
95        {
96                irc_channel_t *ic = cl->data;
97               
98                /* Send a NICK update if we're renaming our user, or someone
99                   who's in the same channel like our user. */
100                if( iu == irc->user ||
101                    ( ( ic->flags & IRC_CHANNEL_JOINED ) &&
102                      irc_channel_has_user( ic, iu ) ) )
103                {
104                        irc_send_nick( iu, new );
105                        break;
106                }
107        }
108       
109        irc->users = g_slist_remove( irc->users, iu );
110        g_hash_table_remove( irc->nick_user_hash, iu->key );
111       
112        if( iu->nick == iu->user ) iu->user = NULL;
113        if( iu->nick == iu->host ) iu->host = NULL;
114        if( iu->nick == iu->fullname ) iu->fullname = NULL;
115        g_free( iu->nick );
116        iu->nick = g_strdup( new );
117        if( iu->user == NULL ) iu->user = g_strdup( iu->nick );
118        if( iu->host == NULL ) iu->host = g_strdup( iu->nick );
119        if( iu->fullname == NULL ) iu->fullname = g_strdup( iu->nick );
120       
121        iu->key = g_strdup( key );
122        g_hash_table_insert( irc->nick_user_hash, iu->key, iu );
123        irc->users = g_slist_insert_sorted( irc->users, iu, irc_user_cmp );
124       
125        return 1;
126}
127
128gint irc_user_cmp( gconstpointer a_, gconstpointer b_ )
129{
130        const irc_user_t *a = a_, *b = b_;
131       
132        return strcmp( a->key, b->key );
133}
134
135/* User-type dependent functions, for root/NickServ: */
136static gboolean root_privmsg( irc_user_t *iu, const char *msg )
137{
138        char cmd[strlen(msg)+1];
139       
140        g_free( iu->irc->last_root_cmd );
141        iu->irc->last_root_cmd = g_strdup( iu->nick );
142       
143        strcpy( cmd, msg );
144        root_command_string( iu->irc, cmd );
145       
146        return TRUE;
147}
148
149const struct irc_user_funcs irc_user_root_funcs = {
150        root_privmsg,
151};
152
153/* Echo to yourself: */
154static gboolean self_privmsg( irc_user_t *iu, const char *msg )
155{
156        irc_send_msg_raw( iu, "PRIVMSG", iu->nick, msg );
157       
158        return TRUE;
159}
160
161const struct irc_user_funcs irc_user_self_funcs = {
162        self_privmsg,
163};
Note: See TracBrowser for help on using the repository browser.