[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))) { |
---|
| 172 | if (g_strcasecmp(set_getstr(&ic->bee->set, "handle_unknown"), "add") == 0) { |
---|
| 173 | bu = bee_user_new(bee, ic, handle, BEE_USER_LOCAL); |
---|
| 174 | } else { |
---|
| 175 | if (g_strcasecmp(set_getstr(&ic->bee->set, "handle_unknown"), "ignore") != 0) { |
---|
| 176 | imcb_log(ic, "imcb_buddy_status() for unknown handle %s:\n" |
---|
| 177 | "flags = %d, state = %s, message = %s", handle, flags, |
---|
| 178 | state ? state : "NULL", message ? message : "NULL"); |
---|
[d860a8d] | 179 | } |
---|
[5ebff60] | 180 | |
---|
[d860a8d] | 181 | return; |
---|
| 182 | } |
---|
| 183 | } |
---|
[5ebff60] | 184 | |
---|
[d860a8d] | 185 | /* May be nice to give the UI something to compare against. */ |
---|
[5ebff60] | 186 | old = g_memdup(bu, sizeof(bee_user_t)); |
---|
| 187 | |
---|
[d860a8d] | 188 | /* TODO(wilmer): OPT_AWAY, or just state == NULL ? */ |
---|
[e63507a] | 189 | bu->flags = flags; |
---|
[5ebff60] | 190 | bu->status_msg = g_strdup(message); |
---|
| 191 | if (state && *state) { |
---|
| 192 | bu->status = g_strdup(state); |
---|
| 193 | } else if (flags & OPT_AWAY) { |
---|
| 194 | bu->status = g_strdup("Away"); |
---|
| 195 | } else { |
---|
[d93c0eb9] | 196 | bu->status = NULL; |
---|
[5ebff60] | 197 | } |
---|
| 198 | |
---|
| 199 | if (bu->status == NULL && (flags & OPT_MOBILE) && |
---|
| 200 | set_getbool(&bee->set, "mobile_is_away")) { |
---|
[0ebf919] | 201 | bu->flags |= BEE_USER_AWAY; |
---|
[5ebff60] | 202 | bu->status = g_strdup("Mobile"); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | if (bee->ui->user_status) { |
---|
| 206 | bee->ui->user_status(bee, bu, old); |
---|
[0ebf919] | 207 | } |
---|
[5ebff60] | 208 | |
---|
| 209 | g_free(old->status_msg); |
---|
| 210 | g_free(old->status); |
---|
| 211 | g_free(old); |
---|
[d860a8d] | 212 | } |
---|
| 213 | |
---|
[d93c0eb9] | 214 | /* Same, but only change the away/status message, not any away/online state info. */ |
---|
[5ebff60] | 215 | void imcb_buddy_status_msg(struct im_connection *ic, const char *handle, const char *message) |
---|
[d93c0eb9] | 216 | { |
---|
| 217 | bee_t *bee = ic->bee; |
---|
| 218 | bee_user_t *bu, *old; |
---|
[5ebff60] | 219 | |
---|
| 220 | if (!(bu = bee_user_by_handle(bee, ic, handle))) { |
---|
[d93c0eb9] | 221 | return; |
---|
| 222 | } |
---|
[5ebff60] | 223 | |
---|
| 224 | old = g_memdup(bu, sizeof(bee_user_t)); |
---|
| 225 | |
---|
| 226 | bu->status_msg = message && *message ? g_strdup(message) : NULL; |
---|
| 227 | |
---|
| 228 | if (bee->ui->user_status) { |
---|
| 229 | bee->ui->user_status(bee, bu, old); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | g_free(old->status_msg); |
---|
| 233 | g_free(old); |
---|
[d93c0eb9] | 234 | } |
---|
| 235 | |
---|
[5ebff60] | 236 | void imcb_buddy_times(struct im_connection *ic, const char *handle, time_t login, time_t idle) |
---|
[56699f0] | 237 | { |
---|
| 238 | bee_t *bee = ic->bee; |
---|
| 239 | bee_user_t *bu; |
---|
[5ebff60] | 240 | |
---|
| 241 | if (!(bu = bee_user_by_handle(bee, ic, handle))) { |
---|
[56699f0] | 242 | return; |
---|
[5ebff60] | 243 | } |
---|
| 244 | |
---|
[56699f0] | 245 | bu->login_time = login; |
---|
| 246 | bu->idle_time = idle; |
---|
| 247 | } |
---|
| 248 | |
---|
[ce70e88] | 249 | void imcb_buddy_msg(struct im_connection *ic, const char *handle, const char *msg, uint32_t flags, time_t sent_at) |
---|
[d860a8d] | 250 | { |
---|
| 251 | bee_t *bee = ic->bee; |
---|
[f012a9f] | 252 | bee_user_t *bu; |
---|
[5ebff60] | 253 | |
---|
| 254 | bu = bee_user_by_handle(bee, ic, handle); |
---|
| 255 | |
---|
| 256 | if (!bu && !(ic->flags & OPT_LOGGING_OUT)) { |
---|
| 257 | char *h = set_getstr(&bee->set, "handle_unknown"); |
---|
| 258 | |
---|
| 259 | if (g_strcasecmp(h, "ignore") == 0) { |
---|
[d860a8d] | 260 | return; |
---|
[5ebff60] | 261 | } else if (g_strncasecmp(h, "add", 3) == 0) { |
---|
| 262 | bu = bee_user_new(bee, ic, handle, BEE_USER_LOCAL); |
---|
[d860a8d] | 263 | } |
---|
| 264 | } |
---|
[5ebff60] | 265 | |
---|
| 266 | if (bee->ui->user_msg && bu) { |
---|
| 267 | bee->ui->user_msg(bee, bu, msg, sent_at); |
---|
| 268 | } else { |
---|
| 269 | imcb_log(ic, "Message from unknown handle %s:\n%s", handle, msg); |
---|
| 270 | } |
---|
[d860a8d] | 271 | } |
---|
[573dab0] | 272 | |
---|
[5ebff60] | 273 | void imcb_buddy_typing(struct im_connection *ic, const char *handle, uint32_t flags) |
---|
[573dab0] | 274 | { |
---|
| 275 | bee_user_t *bu; |
---|
[5ebff60] | 276 | |
---|
| 277 | if (ic->bee->ui->user_typing && |
---|
| 278 | (bu = bee_user_by_handle(ic->bee, ic, handle))) { |
---|
| 279 | ic->bee->ui->user_typing(ic->bee, bu, flags); |
---|
[573dab0] | 280 | } |
---|
| 281 | } |
---|
[d88c92a] | 282 | |
---|
[5ebff60] | 283 | void imcb_buddy_action_response(bee_user_t *bu, const char *action, char * const args[], void *data) |
---|
[d88c92a] | 284 | { |
---|
[5ebff60] | 285 | if (bu->bee->ui->user_action_response) { |
---|
| 286 | bu->bee->ui->user_action_response(bu->bee, bu, action, args, data); |
---|
| 287 | } |
---|
[d88c92a] | 288 | } |
---|