source: protocols/bee_user.c @ 2e0eaac

Last change on this file since 2e0eaac was 69b896b, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-05T12:01:28Z

When addressing people in a chatroom, try to translate the nickname to the
original unstripped version (without ugly underscores, also).

  • Property mode set to 100644
File size: 6.1 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2010 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* Stuff to handle, save and search 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#define BITLBEE_CORE
27#include "bitlbee.h"
28
29bee_user_t *bee_user_new( bee_t *bee, struct im_connection *ic, const char *handle, bee_user_flags_t flags )
30{
31        bee_user_t *bu;
32       
33        if( bee_user_by_handle( bee, ic, handle ) != NULL )
34                return NULL;
35       
36        bu = g_new0( bee_user_t, 1 );
37        bu->bee = bee;
38        bu->ic = ic;
39        bu->flags = flags;
40        bu->handle = g_strdup( handle );
41        bee->users = g_slist_prepend( bee->users, bu );
42       
43        if( bee->ui->user_new )
44                bee->ui->user_new( bee, bu );
45       
46        /* Offline by default. This will set the right flags. */
47        imcb_buddy_status( ic, handle, 0, NULL, NULL );
48       
49        return bu;
50}
51
52int bee_user_free( bee_t *bee, bee_user_t *bu )
53{
54        if( !bu )
55                return 0;
56       
57        if( bee->ui->user_free )
58                bee->ui->user_free( bee, bu );
59       
60        g_free( bu->handle );
61        g_free( bu->fullname );
62        g_free( bu->nick );
63        g_free( bu->status );
64        g_free( bu->status_msg );
65        g_free( bu );
66       
67        bee->users = g_slist_remove( bee->users, bu );
68       
69        return 1;
70}
71
72bee_user_t *bee_user_by_handle( bee_t *bee, struct im_connection *ic, const char *handle )
73{
74        GSList *l;
75       
76        for( l = bee->users; l; l = l->next )
77        {
78                bee_user_t *bu = l->data;
79               
80                if( bu->ic == ic && ic->acc->prpl->handle_cmp( bu->handle, handle ) == 0 )
81                        return bu;
82        }
83       
84        return NULL;
85}
86
87int bee_user_msg( bee_t *bee, bee_user_t *bu, const char *msg, int flags )
88{
89        char *buf = NULL;
90        int st;
91       
92        if( ( bu->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
93        {
94                buf = escape_html( msg );
95                msg = buf;
96        }
97        else
98                buf = g_strdup( msg );
99       
100        st = bu->ic->acc->prpl->buddy_msg( bu->ic, bu->handle, buf, flags );
101        g_free( buf );
102       
103        return st;
104}
105
106
107/* Groups */
108static bee_group_t *bee_group_new( bee_t *bee, const char *name )
109{
110        bee_group_t *bg = g_new0( bee_group_t, 1 );
111       
112        bg->name = g_strdup( name );
113        bg->key = g_utf8_casefold( name, -1 );
114        bee->groups = g_slist_prepend( bee->groups, bg );
115       
116        return bg;
117}
118
119bee_group_t *bee_group_by_name( bee_t *bee, const char *name, gboolean creat )
120{
121        GSList *l;
122        char *key;
123       
124        if( name == NULL )
125                return NULL;
126       
127        key = g_utf8_casefold( name, -1 );
128        for( l = bee->groups; l; l = l->next )
129        {
130                bee_group_t *bg = l->data;
131                if( strcmp( bg->key, key ) == 0 )
132                        break;
133        }
134        g_free( key );
135       
136        if( !l )
137                return creat ? bee_group_new( bee, name ) : NULL;
138        else
139                return l->data;
140}
141
142void bee_group_free( bee_t *bee )
143{
144        while( bee->groups )
145        {
146                bee_group_t *bg = bee->groups->data;
147                g_free( bg->name );
148                g_free( bg->key );
149                g_free( bg );
150                bee->groups = g_slist_remove( bee->groups, bee->groups->data );
151        }
152}
153
154
155/* IM->UI callbacks */
156void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message )
157{
158        bee_t *bee = ic->bee;
159        bee_user_t *bu, *old;
160       
161        if( !( bu = bee_user_by_handle( bee, ic, handle ) ) )
162        {
163                if( g_strcasecmp( set_getstr( &ic->bee->set, "handle_unknown" ), "add" ) == 0 )
164                {
165                        bu = bee_user_new( bee, ic, handle, BEE_USER_LOCAL );
166                }
167                else
168                {
169                        if( g_strcasecmp( set_getstr( &ic->bee->set, "handle_unknown" ), "ignore" ) != 0 )
170                        {
171                                imcb_log( ic, "imcb_buddy_status() for unknown handle %s:\n"
172                                              "flags = %d, state = %s, message = %s", handle, flags,
173                                              state ? state : "NULL", message ? message : "NULL" );
174                        }
175                       
176                        return;
177                }
178        }
179       
180        /* May be nice to give the UI something to compare against. */
181        old = g_memdup( bu, sizeof( bee_user_t ) );
182       
183        /* TODO(wilmer): OPT_AWAY, or just state == NULL ? */
184        bu->flags = flags;
185        bu->status = g_strdup( ( flags & OPT_AWAY ) && state == NULL ? "Away" : state );
186        bu->status_msg = g_strdup( message );
187       
188        if( bee->ui->user_status )
189                bee->ui->user_status( bee, bu, old );
190       
191        g_free( old->status_msg );
192        g_free( old->status );
193        g_free( old );
194}
195
196void imcb_buddy_times( struct im_connection *ic, const char *handle, time_t login, time_t idle )
197{
198        bee_t *bee = ic->bee;
199        bee_user_t *bu;
200       
201        if( !( bu = bee_user_by_handle( bee, ic, handle ) ) )
202                return;
203       
204        bu->login_time = login;
205        bu->idle_time = idle;
206}
207
208void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, uint32_t flags, time_t sent_at )
209{
210        bee_t *bee = ic->bee;
211        bee_user_t *bu;
212       
213        bu = bee_user_by_handle( bee, ic, handle );
214       
215        if( !bu )
216        {
217                char *h = set_getstr( &bee->set, "handle_unknown" );
218               
219                if( g_strcasecmp( h, "ignore" ) == 0 )
220                {
221                        return;
222                }
223                else if( g_strncasecmp( h, "add", 3 ) == 0 )
224                {
225                        bu = bee_user_new( bee, ic, handle, BEE_USER_LOCAL );
226                }
227        }
228       
229        if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) ||
230            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) )
231                strip_html( msg );
232       
233        if( bee->ui->user_msg && bu )
234                bee->ui->user_msg( bee, bu, msg, sent_at );
235        else
236                imcb_log( ic, "Message from unknown handle %s:\n%s", handle, msg );
237}
238
239void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags )
240{
241        bee_user_t *bu;
242       
243        if( ic->bee->ui->user_typing &&
244            ( bu = bee_user_by_handle( ic->bee, ic, handle ) ) )
245        {
246                ic->bee->ui->user_typing( ic->bee, bu, flags );
247        }
248}
Note: See TracBrowser for help on using the repository browser.