source: irc_user.c @ f537044

Last change on this file since f537044 was 0bd948e, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-03T21:16:41Z

Show a user going offline as a QUIT, not as one or more PARTs, like in the
old-style BitlBee. This so that the IRC client will show the notification
in query windows as well. Make it a setting though, for bug #539.

  • Property mode set to 100644
File size: 6.8 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        if( set_getbool( &irc->b->set, "private" ) )
37                iu->last_channel = NULL;
38        else
39                iu->last_channel = irc->default_channel;
40       
41        iu->key = g_strdup( nick );
42        nick_lc( iu->key );
43        /* Using the hash table for speed and irc->users for easy iteration
44           through the list (since the GLib API doesn't have anything sane
45           for that.) */
46        g_hash_table_insert( irc->nick_user_hash, iu->key, iu );
47        irc->users = g_slist_insert_sorted( irc->users, iu, irc_user_cmp );
48       
49        return iu;
50}
51
52int irc_user_free( irc_t *irc, irc_user_t *iu )
53{
54        static struct im_connection *last_ic;
55        static char *msg;
56       
57        if( !iu )
58                return 0;
59       
60        if( iu->bu &&
61            ( iu->bu->ic->flags & OPT_LOGGING_OUT ) &&
62            iu->bu->ic != last_ic )
63        {
64                char host_prefix[] = "bitlbee.";
65                char *s;
66               
67                /* Irssi recognises netsplits by quitmsgs with two
68                   hostnames, where a hostname is a "word" with one
69                   of more dots. Mangle no-dot hostnames a bit. */
70                if( strchr( irc->root->host, '.' ) )
71                        *host_prefix = '\0';
72               
73                last_ic = iu->bu->ic;
74                g_free( msg );
75                if( !set_getbool( &irc->b->set, "simulate_netsplit" ) )
76                        msg = g_strdup( "Account off-line" );
77                else if( ( s = strchr( iu->bu->ic->acc->user, '@' ) ) )
78                        msg = g_strdup_printf( "%s%s %s", host_prefix,
79                                irc->root->host, s + 1 );
80                else
81                        msg = g_strdup_printf( "%s%s %s.%s",
82                                host_prefix, irc->root->host,
83                                iu->bu->ic->acc->prpl->name, irc->root->host );
84        }
85        else if( !iu->bu || !( iu->bu->ic->flags & OPT_LOGGING_OUT ) )
86        {
87                g_free( msg );
88                msg = g_strdup( "Removed" );
89                last_ic = NULL;
90        }
91        irc_user_quit( iu, msg );
92       
93        irc->users = g_slist_remove( irc->users, iu );
94        g_hash_table_remove( irc->nick_user_hash, iu->key );
95       
96        g_free( iu->nick );
97        if( iu->nick != iu->user ) g_free( iu->user );
98        if( iu->nick != iu->host ) g_free( iu->host );
99        if( iu->nick != iu->fullname ) g_free( iu->fullname );
100        g_free( iu->pastebuf );
101        if( iu->pastebuf_timer ) b_event_remove( iu->pastebuf_timer );
102        g_free( iu->key );
103        g_free( iu );
104       
105        return 1;
106}
107
108irc_user_t *irc_user_by_name( irc_t *irc, const char *nick )
109{
110        char key[strlen(nick)+1];
111       
112        strcpy( key, nick );
113        if( nick_lc( key ) )
114                return g_hash_table_lookup( irc->nick_user_hash, key );
115        else
116                return NULL;
117}
118
119int irc_user_set_nick( irc_user_t *iu, const char *new )
120{
121        irc_t *irc = iu->irc;
122        char key[strlen(new)+1];
123        GSList *cl;
124       
125        strcpy( key, new );
126        if( iu == NULL || !nick_lc( key ) || irc_user_by_name( irc, new ) )
127                return 0;
128       
129        for( cl = irc->channels; cl; cl = cl->next )
130        {
131                irc_channel_t *ic = cl->data;
132               
133                /* Send a NICK update if we're renaming our user, or someone
134                   who's in the same channel like our user. */
135                if( iu == irc->user ||
136                    ( ( ic->flags & IRC_CHANNEL_JOINED ) &&
137                      irc_channel_has_user( ic, iu ) ) )
138                {
139                        irc_send_nick( iu, new );
140                        break;
141                }
142        }
143       
144        irc->users = g_slist_remove( irc->users, iu );
145        g_hash_table_remove( irc->nick_user_hash, iu->key );
146       
147        if( iu->nick == iu->user ) iu->user = NULL;
148        if( iu->nick == iu->host ) iu->host = NULL;
149        if( iu->nick == iu->fullname ) iu->fullname = NULL;
150        g_free( iu->nick );
151        iu->nick = g_strdup( new );
152        if( iu->user == NULL ) iu->user = g_strdup( iu->nick );
153        if( iu->host == NULL ) iu->host = g_strdup( iu->nick );
154        if( iu->fullname == NULL ) iu->fullname = g_strdup( iu->nick );
155       
156        iu->key = g_strdup( key );
157        g_hash_table_insert( irc->nick_user_hash, iu->key, iu );
158        irc->users = g_slist_insert_sorted( irc->users, iu, irc_user_cmp );
159       
160        return 1;
161}
162
163gint irc_user_cmp( gconstpointer a_, gconstpointer b_ )
164{
165        const irc_user_t *a = a_, *b = b_;
166       
167        return strcmp( a->key, b->key );
168}
169
170const char *irc_user_get_away( irc_user_t *iu )
171{
172        irc_t *irc = iu->irc;
173        bee_user_t *bu = iu->bu;
174       
175        if( iu == irc->user )
176                return set_getstr( &irc->b->set, "away" );
177        else if( bu )
178        {
179                if( !bu->flags & BEE_USER_ONLINE )
180                        return "Offline";
181                else if( bu->flags & BEE_USER_AWAY )
182                {
183                        if( bu->status_msg )
184                        {
185                                static char ret[MAX_STRING];
186                                g_snprintf( ret, MAX_STRING - 1, "%s (%s)",
187                                            bu->status ? : "Away", bu->status_msg );
188                                return ret;
189                        }
190                        else
191                                return bu->status ? : "Away";
192                }
193        }
194       
195        return NULL;
196}
197
198void irc_user_quit( irc_user_t *iu, const char *msg )
199{
200        GSList *l;
201        gboolean send_quit = FALSE;
202       
203        if( !iu )
204                return;
205       
206        for( l = iu->irc->channels; l; l = l->next )
207                send_quit |= irc_channel_del_user( (irc_channel_t*) l->data, iu, TRUE, NULL );
208       
209        if( send_quit )
210                irc_send_quit( iu, msg );
211}
212
213/* User-type dependent functions, for root/NickServ: */
214static gboolean root_privmsg( irc_user_t *iu, const char *msg )
215{
216        char cmd[strlen(msg)+1];
217       
218        g_free( iu->irc->last_root_cmd );
219        iu->irc->last_root_cmd = g_strdup( iu->nick );
220       
221        strcpy( cmd, msg );
222        root_command_string( iu->irc, cmd );
223       
224        return TRUE;
225}
226
227static gboolean root_ctcp( irc_user_t *iu, char * const *ctcp )
228{
229        if( g_strcasecmp( ctcp[0], "VERSION" ) == 0 )
230        {
231                irc_send_msg_f( iu, "NOTICE", iu->irc->user->nick, "\001%s %s\001",
232                                ctcp[0], "BitlBee " BITLBEE_VERSION " " ARCH "/" CPU );
233        }
234        else if( g_strcasecmp( ctcp[0], "PING" ) == 0 )
235        {
236                irc_send_msg_f( iu, "NOTICE", iu->irc->user->nick, "\001%s %s\001",
237                                ctcp[0], ctcp[1] ? : "" );
238        }
239       
240        return TRUE;
241}
242
243const struct irc_user_funcs irc_user_root_funcs = {
244        root_privmsg,
245        root_ctcp,
246};
247
248/* Echo to yourself: */
249static gboolean self_privmsg( irc_user_t *iu, const char *msg )
250{
251        irc_send_msg( iu, "PRIVMSG", iu->nick, msg, NULL );
252       
253        return TRUE;
254}
255
256const struct irc_user_funcs irc_user_self_funcs = {
257        self_privmsg,
258};
Note: See TracBrowser for help on using the repository browser.