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, 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 | |
---|
52 | int 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 | |
---|
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, BEE_USER_LOCAL ); |
---|
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_times( struct im_connection *ic, const char *handle, time_t login, time_t idle ) |
---|
195 | { |
---|
196 | bee_t *bee = ic->bee; |
---|
197 | bee_user_t *bu; |
---|
198 | |
---|
199 | if( !( bu = bee_user_by_handle( bee, ic, handle ) ) ) |
---|
200 | return; |
---|
201 | |
---|
202 | bu->login_time = login; |
---|
203 | bu->idle_time = idle; |
---|
204 | } |
---|
205 | |
---|
206 | void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, uint32_t flags, time_t sent_at ) |
---|
207 | { |
---|
208 | bee_t *bee = ic->bee; |
---|
209 | bee_user_t *bu; |
---|
210 | |
---|
211 | bu = bee_user_by_handle( bee, ic, handle ); |
---|
212 | |
---|
213 | if( !bu ) |
---|
214 | { |
---|
215 | char *h = set_getstr( &bee->set, "handle_unknown" ); |
---|
216 | |
---|
217 | if( g_strcasecmp( h, "ignore" ) == 0 ) |
---|
218 | { |
---|
219 | return; |
---|
220 | } |
---|
221 | else if( g_strncasecmp( h, "add", 3 ) == 0 ) |
---|
222 | { |
---|
223 | bu = bee_user_new( bee, ic, handle, BEE_USER_LOCAL ); |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) || |
---|
228 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) ) |
---|
229 | strip_html( msg ); |
---|
230 | |
---|
231 | if( bee->ui->user_msg && bu ) |
---|
232 | bee->ui->user_msg( bee, bu, msg, sent_at ); |
---|
233 | else |
---|
234 | imcb_log( ic, "Message from unknown handle %s:\n%s", handle, msg ); |
---|
235 | } |
---|
236 | |
---|
237 | void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags ) |
---|
238 | { |
---|
239 | bee_user_t *bu; |
---|
240 | |
---|
241 | if( ic->bee->ui->user_typing && |
---|
242 | ( bu = bee_user_by_handle( ic->bee, ic, handle ) ) ) |
---|
243 | { |
---|
244 | ic->bee->ui->user_typing( ic->bee, bu, flags ); |
---|
245 | } |
---|
246 | } |
---|