source: protocols/bee_user.c @ 84c3a72

Last change on this file since 84c3a72 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.0 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->status );
63        g_free( bu->status_msg );
64        g_free( bu );
65       
66        bee->users = g_slist_remove( bee->users, bu );
67       
68        return 1;
69}
70
71bee_user_t *bee_user_by_handle( bee_t *bee, struct im_connection *ic, const char *handle )
72{
73        GSList *l;
74       
75        for( l = bee->users; l; l = l->next )
76        {
77                bee_user_t *bu = l->data;
78               
79                if( bu->ic == ic && ic->acc->prpl->handle_cmp( bu->handle, handle ) == 0 )
80                        return bu;
81        }
82       
83        return NULL;
84}
85
86int bee_user_msg( bee_t *bee, bee_user_t *bu, const char *msg, int flags )
87{
88        char *buf = NULL;
89        int st;
90       
91        if( ( bu->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
92        {
93                buf = escape_html( msg );
94                msg = buf;
95        }
96        else
97                buf = g_strdup( msg );
98       
99        st = bu->ic->acc->prpl->buddy_msg( bu->ic, bu->handle, buf, flags );
100        g_free( buf );
101       
102        return st;
103}
104
105
106/* Groups */
107static bee_group_t *bee_group_new( bee_t *bee, const char *name )
108{
109        bee_group_t *bg = g_new0( bee_group_t, 1 );
110       
111        bg->name = g_strdup( name );
112        bg->key = g_utf8_casefold( name, -1 );
113        bee->groups = g_slist_prepend( bee->groups, bg );
114       
115        return bg;
116}
117
118bee_group_t *bee_group_by_name( bee_t *bee, const char *name, gboolean creat )
119{
120        GSList *l;
121        char *key;
122       
123        if( name == NULL )
124                return NULL;
125       
126        key = g_utf8_casefold( name, -1 );
127        for( l = bee->groups; l; l = l->next )
128        {
129                bee_group_t *bg = l->data;
130                if( strcmp( bg->key, key ) == 0 )
131                        break;
132        }
133        g_free( key );
134       
135        if( !l )
136                return creat ? bee_group_new( bee, name ) : NULL;
137        else
138                return l->data;
139}
140
141void bee_group_free( bee_t *bee )
142{
143        while( bee->groups )
144        {
145                bee_group_t *bg = bee->groups->data;
146                g_free( bg->name );
147                g_free( bg->key );
148                g_free( bg );
149                bee->groups = g_slist_remove( bee->groups, bee->groups->data );
150        }
151}
152
153
154/* IM->UI callbacks */
155void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message )
156{
157        bee_t *bee = ic->bee;
158        bee_user_t *bu, *old;
159       
160        if( !( bu = bee_user_by_handle( bee, ic, handle ) ) )
161        {
162                if( g_strcasecmp( set_getstr( &ic->bee->set, "handle_unknown" ), "add" ) == 0 )
163                {
164                        bu = bee_user_new( bee, ic, handle, BEE_USER_LOCAL );
165                }
166                else
167                {
168                        if( g_strcasecmp( set_getstr( &ic->bee->set, "handle_unknown" ), "ignore" ) != 0 )
169                        {
170                                imcb_log( ic, "imcb_buddy_status() for unknown handle %s:\n"
171                                              "flags = %d, state = %s, message = %s", handle, flags,
172                                              state ? state : "NULL", message ? message : "NULL" );
173                        }
174                       
175                        return;
176                }
177        }
178       
179        /* May be nice to give the UI something to compare against. */
180        old = g_memdup( bu, sizeof( bee_user_t ) );
181       
182        /* TODO(wilmer): OPT_AWAY, or just state == NULL ? */
183        bu->flags = flags;
184        bu->status = g_strdup( ( flags & OPT_AWAY ) && state == NULL ? "Away" : state );
185        bu->status_msg = g_strdup( message );
186       
187        if( bee->ui->user_status )
188                bee->ui->user_status( bee, bu, old );
189       
190        g_free( old->status_msg );
191        g_free( old->status );
192        g_free( old );
193}
194
195void imcb_buddy_times( struct im_connection *ic, const char *handle, time_t login, time_t idle )
196{
197        bee_t *bee = ic->bee;
198        bee_user_t *bu;
199       
200        if( !( bu = bee_user_by_handle( bee, ic, handle ) ) )
201                return;
202       
203        bu->login_time = login;
204        bu->idle_time = idle;
205}
206
207void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, uint32_t flags, time_t sent_at )
208{
209        bee_t *bee = ic->bee;
210        bee_user_t *bu;
211       
212        bu = bee_user_by_handle( bee, ic, handle );
213       
214        if( !bu )
215        {
216                char *h = set_getstr( &bee->set, "handle_unknown" );
217               
218                if( g_strcasecmp( h, "ignore" ) == 0 )
219                {
220                        return;
221                }
222                else if( g_strncasecmp( h, "add", 3 ) == 0 )
223                {
224                        bu = bee_user_new( bee, ic, handle, BEE_USER_LOCAL );
225                }
226        }
227       
228        if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) ||
229            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) )
230                strip_html( msg );
231       
232        if( bee->ui->user_msg && bu )
233                bee->ui->user_msg( bee, bu, msg, sent_at );
234        else
235                imcb_log( ic, "Message from unknown handle %s:\n%s", handle, msg );
236}
237
238void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags )
239{
240        bee_user_t *bu;
241       
242        if( ic->bee->ui->user_typing &&
243            ( bu = bee_user_by_handle( ic->bee, ic, handle ) ) )
244        {
245                ic->bee->ui->user_typing( ic->bee, bu, flags );
246        }
247}
Note: See TracBrowser for help on using the repository browser.