source: irc_user.c @ 92c8d41

Last change on this file since 92c8d41 was 92c8d41, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-26T21:26:41Z

Remember in which channel the user talked to someone and show responses in
that same channel.

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