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