[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 | |
---|
| 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 | |
---|
[eb50495] | 51 | /* Offline by default. This will set the right flags. */ |
---|
[5ebff60] | 52 | imcb_buddy_status(ic, handle, 0, NULL, NULL); |
---|
| 53 | |
---|
[10a96f4] | 54 | return bu; |
---|
| 55 | } |
---|
| 56 | |
---|
[5ebff60] | 57 | int bee_user_free(bee_t *bee, bee_user_t *bu) |
---|
[10a96f4] | 58 | { |
---|
[5ebff60] | 59 | if (!bu) { |
---|
[10a96f4] | 60 | return 0; |
---|
[5ebff60] | 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 | |
---|
[05816dd] | 70 | bee->users = g_slist_remove(bee->users, bu); |
---|
| 71 | |
---|
[5ebff60] | 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 | |
---|
[10a96f4] | 79 | return 1; |
---|
| 80 | } |
---|
| 81 | |
---|
[5ebff60] | 82 | bee_user_t *bee_user_by_handle(bee_t *bee, struct im_connection *ic, const char *handle) |
---|
[10a96f4] | 83 | { |
---|
| 84 | GSList *l; |
---|
[5ebff60] | 85 | |
---|
| 86 | for (l = bee->users; l; l = l->next) { |
---|
[10a96f4] | 87 | bee_user_t *bu = l->data; |
---|
[5ebff60] | 88 | |
---|
| 89 | if (bu->ic == ic && ic->acc->prpl->handle_cmp(bu->handle, handle) == 0) { |
---|
[10a96f4] | 90 | return bu; |
---|
[5ebff60] | 91 | } |
---|
[10a96f4] | 92 | } |
---|
[5ebff60] | 93 | |
---|
[10a96f4] | 94 | return NULL; |
---|
| 95 | } |
---|
[d860a8d] | 96 | |
---|
[5ebff60] | 97 | int bee_user_msg(bee_t *bee, bee_user_t *bu, const char *msg, int flags) |
---|
[d860a8d] | 98 | { |
---|
| 99 | char *buf = NULL; |
---|
| 100 | int st; |
---|
[5ebff60] | 101 | |
---|
| 102 | if ((bu->ic->flags & OPT_DOES_HTML) && (g_strncasecmp(msg, "<html>", 6) != 0)) { |
---|
| 103 | buf = escape_html(msg); |
---|
[d860a8d] | 104 | msg = buf; |
---|
[5ebff60] | 105 | } else { |
---|
| 106 | buf = g_strdup(msg); |
---|
[d860a8d] | 107 | } |
---|
[5ebff60] | 108 | |
---|
| 109 | st = bu->ic->acc->prpl->buddy_msg(bu->ic, bu->handle, buf, flags); |
---|
| 110 | g_free(buf); |
---|
| 111 | |
---|
[d860a8d] | 112 | return st; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | |
---|
[7aadd71] | 116 | /* Groups */ |
---|
[5ebff60] | 117 | static bee_group_t *bee_group_new(bee_t *bee, const char *name) |
---|
[7aadd71] | 118 | { |
---|
[5ebff60] | 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 | |
---|
[7aadd71] | 125 | return bg; |
---|
| 126 | } |
---|
| 127 | |
---|
[5ebff60] | 128 | bee_group_t *bee_group_by_name(bee_t *bee, const char *name, gboolean creat) |
---|
[7aadd71] | 129 | { |
---|
| 130 | GSList *l; |
---|
| 131 | char *key; |
---|
[5ebff60] | 132 | |
---|
| 133 | if (name == NULL) { |
---|
[7aadd71] | 134 | return NULL; |
---|
[5ebff60] | 135 | } |
---|
| 136 | |
---|
| 137 | key = g_utf8_casefold(name, -1); |
---|
| 138 | for (l = bee->groups; l; l = l->next) { |
---|
[7aadd71] | 139 | bee_group_t *bg = l->data; |
---|
[5ebff60] | 140 | if (strcmp(bg->key, key) == 0) { |
---|
[7aadd71] | 141 | break; |
---|
[5ebff60] | 142 | } |
---|
[7aadd71] | 143 | } |
---|
[5ebff60] | 144 | g_free(key); |
---|
| 145 | |
---|
| 146 | if (!l) { |
---|
| 147 | return creat ? bee_group_new(bee, name) : NULL; |
---|
| 148 | } else { |
---|
[7aadd71] | 149 | return l->data; |
---|
[5ebff60] | 150 | } |
---|
[7aadd71] | 151 | } |
---|
| 152 | |
---|
[5ebff60] | 153 | void bee_group_free(bee_t *bee) |
---|
[7aadd71] | 154 | { |
---|
[5ebff60] | 155 | while (bee->groups) { |
---|
[7aadd71] | 156 | bee_group_t *bg = bee->groups->data; |
---|
[5ebff60] | 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); |
---|
[7aadd71] | 161 | } |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | |
---|
[d860a8d] | 165 | /* IM->UI callbacks */ |
---|
[5ebff60] | 166 | void imcb_buddy_status(struct im_connection *ic, const char *handle, int flags, const char *state, const char *message) |
---|
[d860a8d] | 167 | { |
---|
| 168 | bee_t *bee = ic->bee; |
---|
| 169 | bee_user_t *bu, *old; |
---|
[5ebff60] | 170 | |
---|
| 171 | if (!(bu = bee_user_by_handle(bee, ic, handle))) { |
---|
[7801298] | 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) { |
---|
[5ebff60] | 176 | bu = bee_user_new(bee, ic, handle, BEE_USER_LOCAL); |
---|
| 177 | } else { |
---|
[7801298] | 178 | if (g_strcasecmp(h, "ignore") != 0) { |
---|
[5ebff60] | 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"); |
---|
[d860a8d] | 182 | } |
---|
[5ebff60] | 183 | |
---|
[d860a8d] | 184 | return; |
---|
| 185 | } |
---|
| 186 | } |
---|
[5ebff60] | 187 | |
---|
[d860a8d] | 188 | /* May be nice to give the UI something to compare against. */ |
---|
[5ebff60] | 189 | old = g_memdup(bu, sizeof(bee_user_t)); |
---|
| 190 | |
---|
[d860a8d] | 191 | /* TODO(wilmer): OPT_AWAY, or just state == NULL ? */ |
---|
[e63507a] | 192 | bu->flags = flags; |
---|
[5ebff60] | 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 { |
---|
[d93c0eb9] | 199 | bu->status = NULL; |
---|
[5ebff60] | 200 | } |
---|
| 201 | |
---|
| 202 | if (bu->status == NULL && (flags & OPT_MOBILE) && |
---|
| 203 | set_getbool(&bee->set, "mobile_is_away")) { |
---|
[0ebf919] | 204 | bu->flags |= BEE_USER_AWAY; |
---|
[5ebff60] | 205 | bu->status = g_strdup("Mobile"); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | if (bee->ui->user_status) { |
---|
| 209 | bee->ui->user_status(bee, bu, old); |
---|
[0ebf919] | 210 | } |
---|
[5ebff60] | 211 | |
---|
| 212 | g_free(old->status_msg); |
---|
| 213 | g_free(old->status); |
---|
| 214 | g_free(old); |
---|
[d860a8d] | 215 | } |
---|
| 216 | |
---|
[d93c0eb9] | 217 | /* Same, but only change the away/status message, not any away/online state info. */ |
---|
[5ebff60] | 218 | void imcb_buddy_status_msg(struct im_connection *ic, const char *handle, const char *message) |
---|
[d93c0eb9] | 219 | { |
---|
| 220 | bee_t *bee = ic->bee; |
---|
| 221 | bee_user_t *bu, *old; |
---|
[5ebff60] | 222 | |
---|
| 223 | if (!(bu = bee_user_by_handle(bee, ic, handle))) { |
---|
[d93c0eb9] | 224 | return; |
---|
| 225 | } |
---|
[5ebff60] | 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); |
---|
[d93c0eb9] | 237 | } |
---|
| 238 | |
---|
[5ebff60] | 239 | void imcb_buddy_times(struct im_connection *ic, const char *handle, time_t login, time_t idle) |
---|
[56699f0] | 240 | { |
---|
| 241 | bee_t *bee = ic->bee; |
---|
| 242 | bee_user_t *bu; |
---|
[5ebff60] | 243 | |
---|
| 244 | if (!(bu = bee_user_by_handle(bee, ic, handle))) { |
---|
[56699f0] | 245 | return; |
---|
[5ebff60] | 246 | } |
---|
| 247 | |
---|
[56699f0] | 248 | bu->login_time = login; |
---|
| 249 | bu->idle_time = idle; |
---|
| 250 | } |
---|
| 251 | |
---|
[345577b] | 252 | void imcb_buddy_msg(struct im_connection *ic, const char *handle, const char *msg, guint32 flags, time_t sent_at) |
---|
[d860a8d] | 253 | { |
---|
| 254 | bee_t *bee = ic->bee; |
---|
[f012a9f] | 255 | bee_user_t *bu; |
---|
[5ebff60] | 256 | |
---|
| 257 | bu = bee_user_by_handle(bee, ic, handle); |
---|
| 258 | |
---|
| 259 | if (!bu && !(ic->flags & OPT_LOGGING_OUT)) { |
---|
[7801298] | 260 | char *h = set_getstr(&ic->acc->set, "handle_unknown") ? : |
---|
| 261 | set_getstr(&ic->bee->set, "handle_unknown"); |
---|
[5ebff60] | 262 | |
---|
| 263 | if (g_strcasecmp(h, "ignore") == 0) { |
---|
[d860a8d] | 264 | return; |
---|
[5ebff60] | 265 | } else if (g_strncasecmp(h, "add", 3) == 0) { |
---|
| 266 | bu = bee_user_new(bee, ic, handle, BEE_USER_LOCAL); |
---|
[d860a8d] | 267 | } |
---|
| 268 | } |
---|
[5ebff60] | 269 | |
---|
| 270 | if (bee->ui->user_msg && bu) { |
---|
[345577b] | 271 | bee->ui->user_msg(bee, bu, msg, flags, sent_at); |
---|
[5ebff60] | 272 | } else { |
---|
| 273 | imcb_log(ic, "Message from unknown handle %s:\n%s", handle, msg); |
---|
| 274 | } |
---|
[d860a8d] | 275 | } |
---|
[573dab0] | 276 | |
---|
[0864a52] | 277 | void imcb_notify_email(struct im_connection *ic, char *format, ...) |
---|
[dd43c62] | 278 | { |
---|
[0864a52] | 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 */ |
---|
[b38f655] | 292 | handle = set_getstr(&ic->acc->set, "mail_notifications_handle"); |
---|
[0864a52] | 293 | |
---|
[dd43c62] | 294 | if (handle != NULL) { |
---|
[0864a52] | 295 | imcb_buddy_msg(ic, handle, msg, 0, 0); |
---|
[dd43c62] | 296 | } else { |
---|
| 297 | imcb_log(ic, "%s", msg); |
---|
| 298 | } |
---|
[0864a52] | 299 | |
---|
| 300 | g_free(msg); |
---|
[dd43c62] | 301 | } |
---|
| 302 | |
---|
[345577b] | 303 | void imcb_buddy_typing(struct im_connection *ic, const char *handle, guint32 flags) |
---|
[573dab0] | 304 | { |
---|
| 305 | bee_user_t *bu; |
---|
[5ebff60] | 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); |
---|
[573dab0] | 310 | } |
---|
| 311 | } |
---|
[d88c92a] | 312 | |
---|
[5ebff60] | 313 | void imcb_buddy_action_response(bee_user_t *bu, const char *action, char * const args[], void *data) |
---|
[d88c92a] | 314 | { |
---|
[5ebff60] | 315 | if (bu->bee->ui->user_action_response) { |
---|
| 316 | bu->bee->ui->user_action_response(bu->bee, bu, action, args, data); |
---|
| 317 | } |
---|
[d88c92a] | 318 | } |
---|