source: protocols/bee_user.c @ 0d4a068

Last change on this file since 0d4a068 was 0d4a068, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-05-03T22:58:15Z

Removed some disabled code related to away_devoice (now implemented anyway).

  • Property mode set to 100644
File size: 4.7 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 )
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->handle = g_strdup( handle );
40        bee->users = g_slist_prepend( bee->users, bu );
41       
42        if( bee->ui->user_new )
43                bee->ui->user_new( bee, bu );
44       
45        return bu;
46}
47
48int bee_user_free( bee_t *bee, bee_user_t *bu )
49{
50        if( !bu )
51                return 0;
52       
53        if( bee->ui->user_free )
54                bee->ui->user_free( bee, bu );
55       
56        g_free( bu->handle );
57        g_free( bu->fullname );
58        g_free( bu->group );
59        g_free( bu->status );
60        g_free( bu->status_msg );
61       
62        bee->users = g_slist_remove( bee->users, bu );
63       
64        return 1;
65}
66
67bee_user_t *bee_user_by_handle( bee_t *bee, struct im_connection *ic, const char *handle )
68{
69        GSList *l;
70       
71        for( l = bee->users; l; l = l->next )
72        {
73                bee_user_t *bu = l->data;
74               
75                if( bu->ic == ic && ic->acc->prpl->handle_cmp( bu->handle, handle ) == 0 )
76                        return bu;
77        }
78       
79        return NULL;
80}
81
82int bee_user_msg( bee_t *bee, bee_user_t *bu, const char *msg, int flags )
83{
84        char *buf = NULL;
85        int st;
86       
87        if( ( bu->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
88        {
89                buf = escape_html( msg );
90                msg = buf;
91        }
92        else
93                buf = g_strdup( msg );
94       
95        st = bu->ic->acc->prpl->buddy_msg( bu->ic, bu->handle, buf, flags );
96        g_free( buf );
97       
98        return st;
99}
100
101
102/* IM->UI callbacks */
103void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message )
104{
105        bee_t *bee = ic->bee;
106        bee_user_t *bu, *old;
107       
108        if( !( bu = bee_user_by_handle( bee, ic, handle ) ) )
109        {
110                if( g_strcasecmp( set_getstr( &ic->bee->set, "handle_unknown" ), "add" ) == 0 )
111                {
112                        bu = bee_user_new( bee, ic, handle );
113                }
114                else
115                {
116                        if( g_strcasecmp( set_getstr( &ic->bee->set, "handle_unknown" ), "ignore" ) != 0 )
117                        {
118                                imcb_log( ic, "imcb_buddy_status() for unknown handle %s:\n"
119                                              "flags = %d, state = %s, message = %s", handle, flags,
120                                              state ? state : "NULL", message ? message : "NULL" );
121                        }
122                       
123                        return;
124                }
125        }
126       
127        /* May be nice to give the UI something to compare against. */
128        old = g_memdup( bu, sizeof( bee_user_t ) );
129       
130        /* TODO(wilmer): OPT_AWAY, or just state == NULL ? */
131        bu->flags = flags;
132        bu->status = g_strdup( ( flags & OPT_AWAY ) && state == NULL ? "Away" : state );
133        bu->status_msg = g_strdup( message );
134       
135        if( bee->ui->user_status )
136                bee->ui->user_status( bee, bu, old );
137       
138        g_free( old->status_msg );
139        g_free( old->status );
140        g_free( old );
141}
142
143void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, uint32_t flags, time_t sent_at )
144{
145        bee_t *bee = ic->bee;
146        bee_user_t *bu;
147       
148        bu = bee_user_by_handle( bee, ic, handle );
149       
150        if( !bu )
151        {
152                char *h = set_getstr( &bee->set, "handle_unknown" );
153               
154                if( g_strcasecmp( h, "ignore" ) == 0 )
155                {
156                        return;
157                }
158                else if( g_strncasecmp( h, "add", 3 ) == 0 )
159                {
160                        bu = bee_user_new( bee, ic, handle );
161                }
162        }
163       
164        if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) ||
165            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) )
166                strip_html( msg );
167       
168        if( bee->ui->user_msg && bu )
169                bee->ui->user_msg( bee, bu, msg, sent_at );
170        else
171                imcb_log( ic, "Message from unknown handle %s:\n%s", handle, msg );
172}
173
174void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags )
175{
176        bee_user_t *bu;
177       
178        if( ic->bee->ui->user_typing &&
179            ( bu = bee_user_by_handle( ic->bee, ic, handle ) ) )
180        {
181                ic->bee->ui->user_typing( ic->bee, bu, flags );
182        }
183}
Note: See TracBrowser for help on using the repository browser.