[5ebff60] | 1 | /********************************************************************\ |
---|
[10a96f4] | 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; |
---|
[6f10697] | 22 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
| 23 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
[10a96f4] | 24 | */ |
---|
| 25 | |
---|
| 26 | #define BITLBEE_CORE |
---|
| 27 | #include "bitlbee.h" |
---|
| 28 | |
---|
[5ebff60] | 29 | bee_user_t *bee_user_new(bee_t *bee, struct im_connection *ic, const char *handle, bee_user_flags_t flags) |
---|
[10a96f4] | 30 | { |
---|
| 31 | bee_user_t *bu; |
---|
[5ebff60] | 32 | |
---|
| 33 | if (bee_user_by_handle(bee, ic, handle) != NULL) { |
---|
[10a96f4] | 34 | return NULL; |
---|
[5ebff60] | 35 | } |
---|
| 36 | |
---|
| 37 | bu = g_new0(bee_user_t, 1); |
---|
[10a96f4] | 38 | bu->bee = bee; |
---|
| 39 | bu->ic = ic; |
---|
[ad404ab] | 40 | bu->flags = flags; |
---|
[5ebff60] | 41 | bu->handle = g_strdup(handle); |
---|
| 42 | bee->users = g_slist_prepend(bee->users, bu); |
---|
| 43 | |
---|
[9e83b15] | 44 | if (ic->bee_users) { |
---|
| 45 | g_hash_table_insert(ic->bee_users, bu->handle, bu); |
---|
| 46 | } |
---|
[5ebff60] | 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 | |
---|
[eb50495] | 54 | /* Offline by default. This will set the right flags. */ |
---|
[5ebff60] | 55 | imcb_buddy_status(ic, handle, 0, NULL, NULL); |
---|
| 56 | |
---|
[10a96f4] | 57 | return bu; |
---|
| 58 | } |
---|
| 59 | |
---|
[5ebff60] | 60 | int bee_user_free(bee_t *bee, bee_user_t *bu) |
---|
[10a96f4] | 61 | { |
---|
[5ebff60] | 62 | if (!bu) { |
---|
[10a96f4] | 63 | return 0; |
---|
[5ebff60] | 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 | } |
---|
[9e83b15] | 72 | if (bu->ic->bee_users) { |
---|
| 73 | g_hash_table_remove(bu->ic->bee_users, bu->handle); |
---|
| 74 | } |
---|
[05816dd] | 75 | bee->users = g_slist_remove(bee->users, bu); |
---|
| 76 | |
---|
[5ebff60] | 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 | |
---|
[10a96f4] | 84 | return 1; |
---|
| 85 | } |
---|
| 86 | |
---|
[9e83b15] | 87 | bee_user_t *bee_user_by_handle_slow(bee_t *bee, struct im_connection *ic, const char *handle) |
---|
[10a96f4] | 88 | { |
---|
| 89 | GSList *l; |
---|
[5ebff60] | 90 | |
---|
| 91 | for (l = bee->users; l; l = l->next) { |
---|
[10a96f4] | 92 | bee_user_t *bu = l->data; |
---|
[5ebff60] | 93 | |
---|
| 94 | if (bu->ic == ic && ic->acc->prpl->handle_cmp(bu->handle, handle) == 0) { |
---|
[10a96f4] | 95 | return bu; |
---|
[5ebff60] | 96 | } |
---|
[10a96f4] | 97 | } |
---|
[5ebff60] | 98 | |
---|
[10a96f4] | 99 | return NULL; |
---|
| 100 | } |
---|
[d860a8d] | 101 | |
---|
[9e83b15] | 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 | |
---|
[5ebff60] | 111 | int bee_user_msg(bee_t *bee, bee_user_t *bu, const char *msg, int flags) |
---|
[d860a8d] | 112 | { |
---|
| 113 | char *buf = NULL; |
---|
| 114 | int st; |
---|
[5ebff60] | 115 | |
---|
| 116 | if ((bu->ic->flags & OPT_DOES_HTML) && (g_strncasecmp(msg, "<html>", 6) != 0)) { |
---|
| 117 | buf = escape_html(msg); |
---|
[d860a8d] | 118 | msg = buf; |
---|
[5ebff60] | 119 | } else { |
---|
| 120 | buf = g_strdup(msg); |
---|
[d860a8d] | 121 | } |
---|
[5ebff60] | 122 | |
---|
| 123 | st = bu->ic->acc->prpl->buddy_msg(bu->ic, bu->handle, buf, flags); |
---|
| 124 | g_free(buf); |
---|
| 125 | |
---|
[d860a8d] | 126 | return st; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | |
---|
[7aadd71] | 130 | /* Groups */ |
---|
[5ebff60] | 131 | static bee_group_t *bee_group_new(bee_t *bee, const char *name) |
---|
[7aadd71] | 132 | { |
---|
[5ebff60] | 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 | |
---|
[7aadd71] | 139 | return bg; |
---|
| 140 | } |
---|
| 141 | |
---|
[5ebff60] | 142 | bee_group_t *bee_group_by_name(bee_t *bee, const char *name, gboolean creat) |
---|
[7aadd71] | 143 | { |
---|
| 144 | GSList *l; |
---|
| 145 | char *key; |
---|
[5ebff60] | 146 | |
---|
| 147 | if (name == NULL) { |
---|
[7aadd71] | 148 | return NULL; |
---|
[5ebff60] | 149 | } |
---|
| 150 | |
---|
| 151 | key = g_utf8_casefold(name, -1); |
---|
| 152 | for (l = bee->groups; l; l = l->next) { |
---|
[7aadd71] | 153 | bee_group_t *bg = l->data; |
---|
[5ebff60] | 154 | if (strcmp(bg->key, key) == 0) { |
---|
[7aadd71] | 155 | break; |
---|
[5ebff60] | 156 | } |
---|
[7aadd71] | 157 | } |
---|
[5ebff60] | 158 | g_free(key); |
---|
| 159 | |
---|
| 160 | if (!l) { |
---|
| 161 | return creat ? bee_group_new(bee, name) : NULL; |
---|
| 162 | } else { |
---|
[7aadd71] | 163 | return l->data; |
---|
[5ebff60] | 164 | } |
---|
[7aadd71] | 165 | } |
---|
| 166 | |
---|
[5ebff60] | 167 | void bee_group_free(bee_t *bee) |
---|
[7aadd71] | 168 | { |
---|
[5ebff60] | 169 | while (bee->groups) { |
---|
[7aadd71] | 170 | bee_group_t *bg = bee->groups->data; |
---|
[5ebff60] | 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); |
---|
[7aadd71] | 175 | } |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | |
---|
[d860a8d] | 179 | /* IM->UI callbacks */ |
---|
[5ebff60] | 180 | void imcb_buddy_status(struct im_connection *ic, const char *handle, int flags, const char *state, const char *message) |
---|
[d860a8d] | 181 | { |
---|
| 182 | bee_t *bee = ic->bee; |
---|
| 183 | bee_user_t *bu, *old; |
---|
[5ebff60] | 184 | |
---|
| 185 | if (!(bu = bee_user_by_handle(bee, ic, handle))) { |
---|
[7801298] | 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) { |
---|
[5ebff60] | 190 | bu = bee_user_new(bee, ic, handle, BEE_USER_LOCAL); |
---|
| 191 | } else { |
---|
[7801298] | 192 | if (g_strcasecmp(h, "ignore") != 0) { |
---|
[5ebff60] | 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"); |
---|
[d860a8d] | 196 | } |
---|
[5ebff60] | 197 | |
---|
[d860a8d] | 198 | return; |
---|
| 199 | } |
---|
| 200 | } |
---|
[5ebff60] | 201 | |
---|
[d860a8d] | 202 | /* May be nice to give the UI something to compare against. */ |
---|
[552da22] | 203 | old = g_memdup2(bu, sizeof(bee_user_t)); |
---|
[5ebff60] | 204 | |
---|
[d860a8d] | 205 | /* TODO(wilmer): OPT_AWAY, or just state == NULL ? */ |
---|
[e63507a] | 206 | bu->flags = flags; |
---|
[5ebff60] | 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 { |
---|
[d93c0eb9] | 213 | bu->status = NULL; |
---|
[5ebff60] | 214 | } |
---|
| 215 | |
---|
| 216 | if (bu->status == NULL && (flags & OPT_MOBILE) && |
---|
| 217 | set_getbool(&bee->set, "mobile_is_away")) { |
---|
[0ebf919] | 218 | bu->flags |= BEE_USER_AWAY; |
---|
[5ebff60] | 219 | bu->status = g_strdup("Mobile"); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | if (bee->ui->user_status) { |
---|
| 223 | bee->ui->user_status(bee, bu, old); |
---|
[0ebf919] | 224 | } |
---|
[5ebff60] | 225 | |
---|
| 226 | g_free(old->status_msg); |
---|
| 227 | g_free(old->status); |
---|
| 228 | g_free(old); |
---|
[d860a8d] | 229 | } |
---|
| 230 | |
---|
[d93c0eb9] | 231 | /* Same, but only change the away/status message, not any away/online state info. */ |
---|
[5ebff60] | 232 | void imcb_buddy_status_msg(struct im_connection *ic, const char *handle, const char *message) |
---|
[d93c0eb9] | 233 | { |
---|
| 234 | bee_t *bee = ic->bee; |
---|
| 235 | bee_user_t *bu, *old; |
---|
[5ebff60] | 236 | |
---|
| 237 | if (!(bu = bee_user_by_handle(bee, ic, handle))) { |
---|
[d93c0eb9] | 238 | return; |
---|
| 239 | } |
---|
[5ebff60] | 240 | |
---|
[552da22] | 241 | old = g_memdup2(bu, sizeof(bee_user_t)); |
---|
[5ebff60] | 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); |
---|
[d93c0eb9] | 251 | } |
---|
| 252 | |
---|
[5ebff60] | 253 | void imcb_buddy_times(struct im_connection *ic, const char *handle, time_t login, time_t idle) |
---|
[56699f0] | 254 | { |
---|
| 255 | bee_t *bee = ic->bee; |
---|
| 256 | bee_user_t *bu; |
---|
[5ebff60] | 257 | |
---|
| 258 | if (!(bu = bee_user_by_handle(bee, ic, handle))) { |
---|
[56699f0] | 259 | return; |
---|
[5ebff60] | 260 | } |
---|
| 261 | |
---|
[56699f0] | 262 | bu->login_time = login; |
---|
| 263 | bu->idle_time = idle; |
---|
| 264 | } |
---|
| 265 | |
---|
[345577b] | 266 | void imcb_buddy_msg(struct im_connection *ic, const char *handle, const char *msg, guint32 flags, time_t sent_at) |
---|
[d860a8d] | 267 | { |
---|
| 268 | bee_t *bee = ic->bee; |
---|
[f012a9f] | 269 | bee_user_t *bu; |
---|
[5ebff60] | 270 | |
---|
| 271 | bu = bee_user_by_handle(bee, ic, handle); |
---|
| 272 | |
---|
| 273 | if (!bu && !(ic->flags & OPT_LOGGING_OUT)) { |
---|
[7801298] | 274 | char *h = set_getstr(&ic->acc->set, "handle_unknown") ? : |
---|
| 275 | set_getstr(&ic->bee->set, "handle_unknown"); |
---|
[5ebff60] | 276 | |
---|
| 277 | if (g_strcasecmp(h, "ignore") == 0) { |
---|
[d860a8d] | 278 | return; |
---|
[5ebff60] | 279 | } else if (g_strncasecmp(h, "add", 3) == 0) { |
---|
| 280 | bu = bee_user_new(bee, ic, handle, BEE_USER_LOCAL); |
---|
[d860a8d] | 281 | } |
---|
| 282 | } |
---|
[5ebff60] | 283 | |
---|
| 284 | if (bee->ui->user_msg && bu) { |
---|
[345577b] | 285 | bee->ui->user_msg(bee, bu, msg, flags, sent_at); |
---|
[5ebff60] | 286 | } else { |
---|
| 287 | imcb_log(ic, "Message from unknown handle %s:\n%s", handle, msg); |
---|
| 288 | } |
---|
[d860a8d] | 289 | } |
---|
[573dab0] | 290 | |
---|
[0864a52] | 291 | void imcb_notify_email(struct im_connection *ic, char *format, ...) |
---|
[dd43c62] | 292 | { |
---|
[0864a52] | 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 */ |
---|
[b38f655] | 306 | handle = set_getstr(&ic->acc->set, "mail_notifications_handle"); |
---|
[0864a52] | 307 | |
---|
[dd43c62] | 308 | if (handle != NULL) { |
---|
[0864a52] | 309 | imcb_buddy_msg(ic, handle, msg, 0, 0); |
---|
[dd43c62] | 310 | } else { |
---|
| 311 | imcb_log(ic, "%s", msg); |
---|
| 312 | } |
---|
[0864a52] | 313 | |
---|
| 314 | g_free(msg); |
---|
[dd43c62] | 315 | } |
---|
| 316 | |
---|
[345577b] | 317 | void imcb_buddy_typing(struct im_connection *ic, const char *handle, guint32 flags) |
---|
[573dab0] | 318 | { |
---|
| 319 | bee_user_t *bu; |
---|
[5ebff60] | 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); |
---|
[573dab0] | 324 | } |
---|
| 325 | } |
---|
[d88c92a] | 326 | |
---|
[5ebff60] | 327 | void imcb_buddy_action_response(bee_user_t *bu, const char *action, char * const args[], void *data) |
---|
[d88c92a] | 328 | { |
---|
[5ebff60] | 329 | if (bu->bee->ui->user_action_response) { |
---|
| 330 | bu->bee->ui->user_action_response(bu->bee, bu, action, args, data); |
---|
| 331 | } |
---|
[d88c92a] | 332 | } |
---|