Mon, 29 Sep 2003 15:23:19 +0000
[gaim-migrate @ 7577]
Here it is, the bulk of the new Jabber prpl.
Left to do:
- Implement registration
- Implement password changing
- Keep track of conversation threads (since I apparently have to)
- Fix the bugs that always magically appear in code after I commit
| 7014 | 1 | /* |
| 2 | * gaim - Jabber Protocol Plugin | |
| 3 | * | |
| 4 | * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com> | |
| 5 | * | |
| 6 | * This program is free software; you can redistribute it and/or modify | |
| 7 | * it under the terms of the GNU General Public License as published by | |
| 8 | * the Free Software Foundation; either version 2 of the License, or | |
| 9 | * (at your option) any later version. | |
| 10 | * | |
| 11 | * This program is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | * GNU General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU General Public License | |
| 17 | * along with this program; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 | * | |
| 20 | */ | |
| 21 | #include "internal.h" | |
| 22 | ||
| 23 | #include "debug.h" | |
| 24 | #include "html.h" | |
| 25 | #include "notify.h" | |
| 26 | #include "request.h" | |
| 27 | #include "server.h" | |
| 28 | ||
| 29 | #include "buddy.h" | |
| 30 | #include "chat.h" | |
| 31 | #include "presence.h" | |
| 32 | #include "iq.h" | |
| 33 | #include "jutil.h" | |
| 34 | #include "xmlnode.h" | |
| 35 | ||
| 36 | ||
| 37 | static void chats_send_presence_foreach(gpointer key, gpointer val, | |
| 38 | gpointer user_data) | |
| 39 | { | |
| 40 | JabberChat *chat = val; | |
| 41 | xmlnode *presence = user_data; | |
| 42 | char *chat_jid = key; | |
| 43 | ||
| 44 | xmlnode_set_attrib(presence, "to", chat_jid); | |
| 45 | jabber_send(chat->js, presence); | |
| 46 | } | |
| 47 | ||
| 48 | ||
| 49 | void jabber_presence_send(GaimConnection *gc, const char *state, | |
| 50 | const char *msg) | |
| 51 | { | |
| 52 | JabberStream *js = gc->proto_data; | |
| 53 | xmlnode *presence; | |
| 54 | char *stripped = NULL; | |
| 55 | ||
| 56 | if(msg) { | |
| 57 | html_to_xhtml(msg, NULL, &stripped); | |
| 58 | } else { | |
| 59 | stripped = g_strdup(""); | |
| 60 | } | |
| 61 | ||
| 62 | gc->away = stripped; | |
| 63 | ||
| 64 | presence = jabber_presence_create(state, msg); | |
| 65 | jabber_send(js, presence); | |
| 66 | g_hash_table_foreach(js->chats, chats_send_presence_foreach, presence); | |
| 67 | xmlnode_free(presence); | |
| 68 | } | |
| 69 | ||
| 70 | xmlnode *jabber_presence_create(const char *state, const char *msg) | |
| 71 | { | |
| 72 | xmlnode *show, *status, *presence; | |
| 73 | ||
| 74 | ||
| 75 | presence = xmlnode_new("presence"); | |
| 76 | ||
| 77 | if(state) { | |
| 78 | const char *show_string = NULL; | |
| 79 | if(!strcmp(state, _("Chatty"))) | |
| 80 | show_string = "chat"; | |
| 81 | else if(!strcmp(state, _("Away")) || | |
| 82 | (msg && !strcmp(state, GAIM_AWAY_CUSTOM))) | |
| 83 | show_string = "away"; | |
| 84 | else if(!strcmp(state, _("Extended Away"))) | |
| 85 | show_string = "xa"; | |
| 86 | else if(!strcmp(state, _("Do Not Disturb"))) | |
| 87 | show_string = "dnd"; | |
| 88 | else if(!strcmp(state, _("Invisible"))) { | |
| 89 | xmlnode_set_attrib(presence, "type", "invisible"); | |
| 90 | } | |
| 91 | ||
| 92 | if(show_string) { | |
| 93 | show = xmlnode_new_child(presence, "show"); | |
| 94 | xmlnode_insert_data(show, show_string, -1); | |
| 95 | } | |
| 96 | } | |
| 97 | ||
| 98 | if(msg && *msg) { | |
| 99 | status = xmlnode_new_child(presence, "status"); | |
| 100 | xmlnode_insert_data(status, msg, -1); | |
| 101 | } | |
| 102 | ||
| 103 | return presence; | |
| 104 | } | |
| 105 | ||
| 106 | struct _jabber_add_permit { | |
| 107 | GaimConnection *gc; | |
| 108 | char *who; | |
| 109 | }; | |
| 110 | ||
| 111 | static void authorize_add_cb(struct _jabber_add_permit *jap) | |
| 112 | { | |
| 113 | if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
| 114 | jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
| 115 | "subscribed"); | |
| 116 | ||
| 117 | if(!gaim_find_buddy(jap->gc->account, jap->who)) | |
| 118 | show_got_added(jap->gc, NULL, jap->who, NULL, NULL); | |
| 119 | } | |
| 120 | ||
| 121 | g_free(jap->who); | |
| 122 | g_free(jap); | |
| 123 | } | |
| 124 | ||
| 125 | static void deny_add_cb(struct _jabber_add_permit *jap) | |
| 126 | { | |
| 127 | if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
| 128 | jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
| 129 | "unsubscribed"); | |
| 130 | ||
| 131 | if(!gaim_find_buddy(jap->gc->account, jap->who)) | |
| 132 | show_got_added(jap->gc, NULL, jap->who, NULL, NULL); | |
| 133 | } | |
| 134 | ||
| 135 | g_free(jap->who); | |
| 136 | g_free(jap); | |
| 137 | } | |
| 138 | ||
| 139 | void jabber_presence_parse(JabberStream *js, xmlnode *packet) | |
| 140 | { | |
| 141 | const char *from = xmlnode_get_attrib(packet, "from"); | |
| 142 | const char *type = xmlnode_get_attrib(packet, "type"); | |
| 143 | char *status = NULL; | |
| 144 | int priority = 0; | |
| 145 | JabberID *jid; | |
| 146 | JabberChat *chat; | |
| 147 | JabberBuddy *jb; | |
| 148 | JabberBuddyResource *jbr; | |
| 149 | GaimBuddy *b; | |
| 150 | char *buddy_name; | |
| 151 | int state = 0; | |
| 152 | xmlnode *y; | |
| 153 | gboolean muc = FALSE; | |
| 154 | ||
| 155 | jb = jabber_buddy_find(js, from, TRUE); | |
| 156 | ||
| 157 | if(jb->error_msg) { | |
| 158 | g_free(jb->error_msg); | |
| 159 | jb->error_msg = NULL; | |
| 160 | } | |
| 161 | ||
| 162 | if(type && !strcasecmp(type, "error")) { | |
| 163 | state = JABBER_STATE_ERROR; | |
| 164 | if((y = xmlnode_get_child(packet, "error")) != NULL) { | |
| 165 | char *txt = xmlnode_get_data(y); | |
| 166 | jb->error_msg = g_strdup_printf(_("%s (Code %s)"), | |
| 167 | txt, xmlnode_get_attrib(y, "code")); | |
| 168 | g_free(txt); | |
| 169 | } else { | |
| 170 | jb->error_msg = g_strdup(_("Unknown Error in presence")); | |
| 171 | } | |
| 172 | } else if(type && !strcasecmp(type, "subscribe")) { | |
| 173 | struct _jabber_add_permit *jap = g_new0(struct _jabber_add_permit, 1); | |
| 174 | char *msg = g_strdup_printf(_("The user %s wants to add you to their buddy list."), from); | |
| 175 | jap->gc = js->gc; | |
| 176 | jap->who = g_strdup(from); | |
| 177 | ||
| 178 | gaim_request_action(js->gc, NULL, msg, NULL, 0, jap, 2, | |
| 179 | _("Authorize"), G_CALLBACK(authorize_add_cb), | |
| 180 | _("Deny"), G_CALLBACK(deny_add_cb)); | |
| 181 | g_free(msg); | |
| 182 | } else if(type && (!strcmp(type, "subscribed") || | |
| 183 | !strcmp(type, "unsubscribed"))) { | |
| 184 | /* we've been allowed to see their presence, but we don't care */ | |
| 185 | return; | |
| 186 | } else { | |
| 187 | if((y = xmlnode_get_child(packet, "show"))) { | |
| 188 | char *show = xmlnode_get_data(y); | |
| 189 | if(!show) { | |
| 190 | state = 0; | |
| 191 | } else if(!strcasecmp(show, "away")) { | |
| 192 | state = JABBER_STATE_AWAY; | |
| 193 | } else if(!strcasecmp(show, "chat")) { | |
| 194 | state = JABBER_STATE_CHAT; | |
| 195 | } else if(!strcasecmp(show, "xa")) { | |
| 196 | state = JABBER_STATE_XA; | |
| 197 | } else if(!strcasecmp(show, "dnd")) { | |
| 198 | state = JABBER_STATE_DND; | |
| 199 | } | |
| 200 | g_free(show); | |
| 201 | } else { | |
| 202 | state = 0; | |
| 203 | } | |
| 204 | } | |
| 205 | ||
| 206 | for(y = packet->child; y; y = y->next) { | |
| 207 | if(y->type != NODE_TYPE_TAG) | |
| 208 | continue; | |
| 209 | ||
| 210 | if(!strcmp(y->name, "status")) { | |
| 211 | status = xmlnode_get_data(y); | |
| 212 | } else if(!strcmp(y->name, "priority")) { | |
| 213 | char *p = xmlnode_get_data(y); | |
| 214 | if(p) { | |
| 215 | priority = atoi(p); | |
| 216 | g_free(p); | |
| 217 | } | |
| 218 | } else if(!strcmp(y->name, "x")) { | |
| 219 | const char *xmlns = xmlnode_get_attrib(y, "xmlns"); | |
| 220 | if(xmlns && !strcmp(xmlns, "http://jabber.org/protocol/muc#user")) { | |
| 221 | /* this is where we'd normally get the "op" status of the | |
| 222 | * user, but since we don't have a good way to show that yet | |
| 223 | * we'll ignore it */ | |
| 224 | muc = TRUE; | |
| 225 | } | |
| 226 | } | |
| 227 | } | |
| 228 | ||
| 229 | jid = jabber_id_new(from); | |
| 230 | ||
| 231 | if((chat = jabber_chat_find(js, jid->node, jid->domain))) { | |
| 232 | static int i = 0; | |
| 233 | char *room_jid = g_strdup_printf("%s@%s", jid->node, jid->domain); | |
| 234 | ||
| 235 | if(state == JABBER_STATE_ERROR) { | |
| 236 | const char *code = NULL; | |
| 237 | char *text = NULL; | |
| 238 | char *buf; | |
| 239 | xmlnode *error = xmlnode_get_child(packet, "error"); | |
| 240 | if(error) { | |
| 241 | /* I should make my own messages so they can be | |
| 242 | * translated, but i'm tired */ | |
| 243 | code = xmlnode_get_attrib(error, "code"); | |
| 244 | text = xmlnode_get_data(error); | |
| 245 | } | |
| 246 | ||
| 247 | if(!code) | |
| 248 | code = ""; | |
| 249 | if(!text) | |
| 250 | text = g_strdup(_("Unable to join chat")); | |
| 251 | ||
| 252 | buf = g_strdup_printf("Error %s joining chat %s: %s", | |
| 253 | code, from, text); | |
| 254 | gaim_notify_error(js->gc, _("Error"), _("Error"), buf); | |
| 255 | g_free(text); | |
| 256 | g_free(buf); | |
| 257 | ||
| 258 | jabber_chat_destroy(chat); | |
| 259 | return; | |
| 260 | } | |
| 261 | ||
| 262 | ||
| 263 | if(!chat->conv) { | |
| 264 | chat->id = i++; | |
| 265 | chat->muc = muc; | |
| 266 | chat->conv = serv_got_joined_chat(js->gc, chat->id, room_jid); | |
| 267 | } | |
| 268 | ||
| 269 | if(type && !strcasecmp(type, "unavailable")) { | |
| 270 | if(!strcmp(jid->resource, chat->nick)) { | |
| 271 | serv_got_chat_left(js->gc, chat->id); | |
| 272 | jabber_chat_destroy(chat); | |
| 273 | } else { | |
| 274 | gaim_chat_remove_user(GAIM_CHAT(chat->conv), jid->resource, | |
| 275 | NULL); | |
| 276 | } | |
| 277 | } else { | |
| 278 | if(!jabber_chat_find_buddy(chat->conv, jid->resource)) | |
| 279 | gaim_chat_add_user(GAIM_CHAT(chat->conv), jid->resource, | |
| 280 | NULL); | |
| 281 | } | |
| 282 | g_free(room_jid); | |
| 283 | } else { | |
| 284 | if(!(jb->subscription & JABBER_SUB_TO)) { | |
| 285 | gaim_debug(GAIM_DEBUG_INFO, "jabber", | |
| 286 | "got unexpected presence from %s, ignoring\n", from); | |
| 287 | jabber_id_free(jid); | |
| 288 | return; | |
| 289 | } | |
| 290 | ||
| 291 | buddy_name = g_strdup_printf("%s@%s", jid->node, jid->domain); | |
| 292 | if((b = gaim_find_buddy(js->gc->account, buddy_name)) == NULL) { | |
| 293 | jabber_id_free(jid); | |
| 294 | g_free(buddy_name); | |
| 295 | return; | |
| 296 | } | |
| 297 | ||
| 298 | if(state == JABBER_STATE_ERROR || | |
| 299 | (type && !strcasecmp(type, "unavailable"))) | |
| 300 | jabber_buddy_remove_resource(jb, jid->resource); | |
| 301 | else | |
| 302 | jabber_buddy_track_resource(jb, jid->resource, priority, state, | |
| 303 | status); | |
| 304 | ||
| 305 | jbr = jabber_buddy_find_resource(jb, jid->resource); | |
| 306 | ||
| 307 | if(jbr) | |
| 308 | serv_got_update(js->gc, buddy_name, 1, 0, b->signon, b->idle, | |
| 309 | jbr->state); | |
| 310 | else | |
| 311 | serv_got_update(js->gc, buddy_name, 0, 0, 0, 0, 0); | |
| 312 | #if 0 | |
| 313 | iq = jabber_iq_new_query(js, JABBER_IQ_GET, | |
| 314 | "http://jabber.org/protocol/disco#items"); | |
| 315 | query = xmlnode_get_child(iq->node, "query"); | |
| 316 | xmlnode_set_attrib(query, "node", | |
| 317 | "http://jabber.org/protocol/avatar"); | |
| 318 | xmlnode_set_attrib(iq->node, "to", buddy_name); | |
| 319 | jabber_iq_send(iq); | |
| 320 | #endif | |
| 321 | ||
| 322 | g_free(buddy_name); | |
| 323 | } | |
| 324 | g_free(status); | |
| 325 | jabber_id_free(jid); | |
| 326 | } | |
| 327 | ||
| 328 | void jabber_presence_subscription_set(JabberStream *js, const char *who, const char *type) | |
| 329 | { | |
| 330 | xmlnode *presence = xmlnode_new("presence"); | |
| 331 | ||
| 332 | xmlnode_set_attrib(presence, "to", who); | |
| 333 | xmlnode_set_attrib(presence, "type", type); | |
| 334 | ||
| 335 | jabber_send(js, presence); | |
| 336 | xmlnode_free(presence); | |
| 337 | } |