source: irc_user.c @ 0b5cc72

Last change on this file since 0b5cc72 was 0b5cc72, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-05T00:39:04Z

Send nickname change notifications when necessary.

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