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., 51 Franklin St., |
---|
23 | Fifth Floor, Boston, MA 02110-1301 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 | |
---|
37 | bu = g_new0(bee_user_t, 1); |
---|
38 | bu->bee = bee; |
---|
39 | bu->ic = ic; |
---|
40 | bu->flags = flags; |
---|
41 | bu->handle = g_strdup(handle); |
---|
42 | bee->users = g_slist_prepend(bee->users, bu); |
---|
43 | |
---|
44 | if (bee->ui->user_new) { |
---|
45 | bee->ui->user_new(bee, bu); |
---|
46 | } |
---|
47 | if (ic->acc->prpl->buddy_data_add) { |
---|
48 | ic->acc->prpl->buddy_data_add(bu); |
---|
49 | } |
---|
50 | |
---|
51 | /* Offline by default. This will set the right flags. */ |
---|
52 | imcb_buddy_status(ic, handle, 0, NULL, NULL); |
---|
53 | |
---|
54 | return bu; |
---|
55 | } |
---|
56 | |
---|
57 | int bee_user_free(bee_t *bee, bee_user_t *bu) |
---|
58 | { |
---|
59 | if (!bu) { |
---|
60 | return 0; |
---|
61 | } |
---|
62 | |
---|
63 | if (bee->ui->user_free) { |
---|
64 | bee->ui->user_free(bee, bu); |
---|
65 | } |
---|
66 | if (bu->ic->acc->prpl->buddy_data_free) { |
---|
67 | bu->ic->acc->prpl->buddy_data_free(bu); |
---|
68 | } |
---|
69 | |
---|
70 | bee->users = g_slist_remove(bee->users, bu); |
---|
71 | |
---|
72 | g_free(bu->handle); |
---|
73 | g_free(bu->fullname); |
---|
74 | g_free(bu->nick); |
---|
75 | g_free(bu->status); |
---|
76 | g_free(bu->status_msg); |
---|
77 | g_free(bu); |
---|
78 | |
---|
79 | return 1; |
---|
80 | } |
---|
81 | |
---|
82 | bee_user_t *bee_user_by_handle(bee_t *bee, struct im_connection *ic, const char *handle) |
---|
83 | { |
---|
84 | GSList *l; |
---|
85 | |
---|
86 | for (l = bee->users; l; l = l->next) { |
---|
87 | bee_user_t *bu = l->data; |
---|
88 | |
---|
89 | if (bu->ic == ic && ic->acc->prpl->handle_cmp(bu->handle, handle) == 0) { |
---|
90 | return bu; |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | return NULL; |
---|
95 | } |
---|
96 | |
---|
97 | int bee_user_msg(bee_t *bee, bee_user_t *bu, const char *msg, int flags) |
---|
98 | { |
---|
99 | char *buf = NULL; |
---|
100 | int st; |
---|
101 | |
---|
102 | if ((bu->ic->flags & OPT_DOES_HTML) && (g_strncasecmp(msg, "<html>", 6) != 0)) { |
---|
103 | buf = escape_html(msg); |
---|
104 | msg = buf; |
---|
105 | } else { |
---|
106 | buf = g_strdup(msg); |
---|
107 | } |
---|
108 | |
---|
109 | st = bu->ic->acc->prpl->buddy_msg(bu->ic, bu->handle, buf, flags); |
---|
110 | g_free(buf); |
---|
111 | |
---|
112 | return st; |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | /* Groups */ |
---|
117 | static bee_group_t *bee_group_new(bee_t *bee, const char *name) |
---|
118 | { |
---|
119 | bee_group_t *bg = g_new0(bee_group_t, 1); |
---|
120 | |
---|
121 | bg->name = g_strdup(name); |
---|
122 | bg->key = g_utf8_casefold(name, -1); |
---|
123 | bee->groups = g_slist_prepend(bee->groups, bg); |
---|
124 | |
---|
125 | return bg; |
---|
126 | } |
---|
127 | |
---|
128 | bee_group_t *bee_group_by_name(bee_t *bee, const char *name, gboolean creat) |
---|
129 | { |
---|
130 | GSList *l; |
---|
131 | char *key; |
---|
132 | |
---|
133 | if (name == NULL) { |
---|
134 | return NULL; |
---|
135 | } |
---|
136 | |
---|
137 | key = g_utf8_casefold(name, -1); |
---|
138 | for (l = bee->groups; l; l = l->next) { |
---|
139 | bee_group_t *bg = l->data; |
---|
140 | if (strcmp(bg->key, key) == 0) { |
---|
141 | break; |
---|
142 | } |
---|
143 | } |
---|
144 | g_free(key); |
---|
145 | |
---|
146 | if (!l) { |
---|
147 | return creat ? bee_group_new(bee, name) : NULL; |
---|
148 | } else { |
---|
149 | return l->data; |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | void bee_group_free(bee_t *bee) |
---|
154 | { |
---|
155 | while (bee->groups) { |
---|
156 | bee_group_t *bg = bee->groups->data; |
---|
157 | g_free(bg->name); |
---|
158 | g_free(bg->key); |
---|
159 | g_free(bg); |
---|
160 | bee->groups = g_slist_remove(bee->groups, bee->groups->data); |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | /* IM->UI callbacks */ |
---|
166 | void imcb_buddy_status(struct im_connection *ic, const char *handle, int flags, const char *state, const char *message) |
---|
167 | { |
---|
168 | bee_t *bee = ic->bee; |
---|
169 | bee_user_t *bu, *old; |
---|
170 | |
---|
171 | if (!(bu = bee_user_by_handle(bee, ic, handle))) { |
---|
172 | char *h = set_getstr(&ic->acc->set, "handle_unknown") ? : |
---|
173 | set_getstr(&ic->bee->set, "handle_unknown"); |
---|
174 | |
---|
175 | if (g_strncasecmp(h, "add", 3) == 0) { |
---|
176 | bu = bee_user_new(bee, ic, handle, BEE_USER_LOCAL); |
---|
177 | } else { |
---|
178 | if (g_strcasecmp(h, "ignore") != 0) { |
---|
179 | imcb_log(ic, "imcb_buddy_status() for unknown handle %s:\n" |
---|
180 | "flags = %d, state = %s, message = %s", handle, flags, |
---|
181 | state ? state : "NULL", message ? message : "NULL"); |
---|
182 | } |
---|
183 | |
---|
184 | return; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | /* May be nice to give the UI something to compare against. */ |
---|
189 | old = g_memdup(bu, sizeof(bee_user_t)); |
---|
190 | |
---|
191 | /* TODO(wilmer): OPT_AWAY, or just state == NULL ? */ |
---|
192 | bu->flags = flags; |
---|
193 | bu->status_msg = g_strdup(message); |
---|
194 | if (state && *state) { |
---|
195 | bu->status = g_strdup(state); |
---|
196 | } else if (flags & OPT_AWAY) { |
---|
197 | bu->status = g_strdup("Away"); |
---|
198 | } else { |
---|
199 | bu->status = NULL; |
---|
200 | } |
---|
201 | |
---|
202 | if (bu->status == NULL && (flags & OPT_MOBILE) && |
---|
203 | set_getbool(&bee->set, "mobile_is_away")) { |
---|
204 | bu->flags |= BEE_USER_AWAY; |
---|
205 | bu->status = g_strdup("Mobile"); |
---|
206 | } |
---|
207 | |
---|
208 | if (bee->ui->user_status) { |
---|
209 | bee->ui->user_status(bee, bu, old); |
---|
210 | } |
---|
211 | |
---|
212 | g_free(old->status_msg); |
---|
213 | g_free(old->status); |
---|
214 | g_free(old); |
---|
215 | } |
---|
216 | |
---|
217 | /* Same, but only change the away/status message, not any away/online state info. */ |
---|
218 | void imcb_buddy_status_msg(struct im_connection *ic, const char *handle, const char *message) |
---|
219 | { |
---|
220 | bee_t *bee = ic->bee; |
---|
221 | bee_user_t *bu, *old; |
---|
222 | |
---|
223 | if (!(bu = bee_user_by_handle(bee, ic, handle))) { |
---|
224 | return; |
---|
225 | } |
---|
226 | |
---|
227 | old = g_memdup(bu, sizeof(bee_user_t)); |
---|
228 | |
---|
229 | bu->status_msg = message && *message ? g_strdup(message) : NULL; |
---|
230 | |
---|
231 | if (bee->ui->user_status) { |
---|
232 | bee->ui->user_status(bee, bu, old); |
---|
233 | } |
---|
234 | |
---|
235 | g_free(old->status_msg); |
---|
236 | g_free(old); |
---|
237 | } |
---|
238 | |
---|
239 | void imcb_buddy_times(struct im_connection *ic, const char *handle, time_t login, time_t idle) |
---|
240 | { |
---|
241 | bee_t *bee = ic->bee; |
---|
242 | bee_user_t *bu; |
---|
243 | |
---|
244 | if (!(bu = bee_user_by_handle(bee, ic, handle))) { |
---|
245 | return; |
---|
246 | } |
---|
247 | |
---|
248 | bu->login_time = login; |
---|
249 | bu->idle_time = idle; |
---|
250 | } |
---|
251 | |
---|
252 | void imcb_buddy_msg(struct im_connection *ic, const char *handle, const char *msg, guint32 flags, time_t sent_at) |
---|
253 | { |
---|
254 | bee_t *bee = ic->bee; |
---|
255 | bee_user_t *bu; |
---|
256 | |
---|
257 | bu = bee_user_by_handle(bee, ic, handle); |
---|
258 | |
---|
259 | if (!bu && !(ic->flags & OPT_LOGGING_OUT)) { |
---|
260 | char *h = set_getstr(&ic->acc->set, "handle_unknown") ? : |
---|
261 | set_getstr(&ic->bee->set, "handle_unknown"); |
---|
262 | |
---|
263 | if (g_strcasecmp(h, "ignore") == 0) { |
---|
264 | return; |
---|
265 | } else if (g_strncasecmp(h, "add", 3) == 0) { |
---|
266 | bu = bee_user_new(bee, ic, handle, BEE_USER_LOCAL); |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | if (bee->ui->user_msg && bu) { |
---|
271 | bee->ui->user_msg(bee, bu, msg, flags, sent_at); |
---|
272 | } else { |
---|
273 | imcb_log(ic, "Message from unknown handle %s:\n%s", handle, msg); |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | void imcb_notify_email(struct im_connection *ic, char *format, ...) |
---|
278 | { |
---|
279 | const char *handle; |
---|
280 | va_list params; |
---|
281 | char *msg; |
---|
282 | |
---|
283 | if (!set_getbool(&ic->acc->set, "mail_notifications")) { |
---|
284 | return; |
---|
285 | } |
---|
286 | |
---|
287 | va_start(params, format); |
---|
288 | msg = g_strdup_vprintf(format, params); |
---|
289 | va_end(params); |
---|
290 | |
---|
291 | /* up to the protocol to set_add this if they want to use this */ |
---|
292 | handle = set_getstr(&ic->acc->set, "mail_notifications_handle"); |
---|
293 | |
---|
294 | if (handle != NULL) { |
---|
295 | imcb_buddy_msg(ic, handle, msg, 0, 0); |
---|
296 | } else { |
---|
297 | imcb_log(ic, "%s", msg); |
---|
298 | } |
---|
299 | |
---|
300 | g_free(msg); |
---|
301 | } |
---|
302 | |
---|
303 | void imcb_buddy_typing(struct im_connection *ic, const char *handle, guint32 flags) |
---|
304 | { |
---|
305 | bee_user_t *bu; |
---|
306 | |
---|
307 | if (ic->bee->ui->user_typing && |
---|
308 | (bu = bee_user_by_handle(ic->bee, ic, handle))) { |
---|
309 | ic->bee->ui->user_typing(ic->bee, bu, flags); |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | void imcb_buddy_action_response(bee_user_t *bu, const char *action, char * const args[], void *data) |
---|
314 | { |
---|
315 | if (bu->bee->ui->user_action_response) { |
---|
316 | bu->bee->ui->user_action_response(bu->bee, bu, action, args, data); |
---|
317 | } |
---|
318 | } |
---|