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