source: irc_user.c @ 46d215d

Last change on this file since 46d215d was d7db346, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-08T22:44:16Z

Some cleanup improvements.

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