Thu, 16 Oct 2003 04:17:25 +0000
[gaim-migrate @ 7859]
fix © and ® in gtkimhtml
fix a potential segfault on jabber
| 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 "notify.h" | |
| 25 | #include "request.h" | |
| 26 | #include "server.h" | |
|
7095
17d2b54254f8
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7015
diff
changeset
|
27 | #include "util.h" |
| 7014 | 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) { | |
|
7095
17d2b54254f8
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7015
diff
changeset
|
57 | gaim_markup_html_to_xhtml(msg, NULL, &stripped); |
| 7249 | 58 | } else if(!state || strcmp(state, GAIM_AWAY_CUSTOM)) { |
| 7247 | 59 | /* i can't wait until someone re-writes the status/away stuff */ |
| 7014 | 60 | stripped = g_strdup(""); |
| 61 | } | |
| 62 | ||
| 7248 | 63 | if(gc->away) |
| 64 | g_free(gc->away); | |
| 7014 | 65 | gc->away = stripped; |
| 66 | ||
| 67 | presence = jabber_presence_create(state, msg); | |
| 68 | jabber_send(js, presence); | |
| 69 | g_hash_table_foreach(js->chats, chats_send_presence_foreach, presence); | |
| 70 | xmlnode_free(presence); | |
| 71 | } | |
| 72 | ||
| 73 | xmlnode *jabber_presence_create(const char *state, const char *msg) | |
| 74 | { | |
| 75 | xmlnode *show, *status, *presence; | |
| 76 | ||
| 77 | ||
| 78 | presence = xmlnode_new("presence"); | |
| 79 | ||
| 80 | if(state) { | |
| 81 | const char *show_string = NULL; | |
| 82 | if(!strcmp(state, _("Chatty"))) | |
| 83 | show_string = "chat"; | |
| 84 | else if(!strcmp(state, _("Away")) || | |
| 85 | (msg && !strcmp(state, GAIM_AWAY_CUSTOM))) | |
| 86 | show_string = "away"; | |
| 87 | else if(!strcmp(state, _("Extended Away"))) | |
| 88 | show_string = "xa"; | |
| 89 | else if(!strcmp(state, _("Do Not Disturb"))) | |
| 90 | show_string = "dnd"; | |
| 91 | else if(!strcmp(state, _("Invisible"))) { | |
| 92 | xmlnode_set_attrib(presence, "type", "invisible"); | |
| 93 | } | |
| 94 | ||
| 95 | if(show_string) { | |
| 96 | show = xmlnode_new_child(presence, "show"); | |
| 97 | xmlnode_insert_data(show, show_string, -1); | |
| 98 | } | |
| 99 | } | |
| 100 | ||
| 101 | if(msg && *msg) { | |
| 102 | status = xmlnode_new_child(presence, "status"); | |
| 103 | xmlnode_insert_data(status, msg, -1); | |
| 104 | } | |
| 105 | ||
| 106 | return presence; | |
| 107 | } | |
| 108 | ||
| 109 | struct _jabber_add_permit { | |
| 110 | GaimConnection *gc; | |
| 111 | char *who; | |
| 112 | }; | |
| 113 | ||
| 114 | static void authorize_add_cb(struct _jabber_add_permit *jap) | |
| 115 | { | |
| 116 | if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
| 117 | jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
| 118 | "subscribed"); | |
| 119 | ||
| 120 | if(!gaim_find_buddy(jap->gc->account, jap->who)) | |
|
7015
bea9111282b3
[gaim-migrate @ 7578]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
121 | gaim_account_notify_added(jap->gc->account, NULL, jap->who, NULL, NULL); |
| 7014 | 122 | } |
| 123 | ||
| 124 | g_free(jap->who); | |
| 125 | g_free(jap); | |
| 126 | } | |
| 127 | ||
| 128 | static void deny_add_cb(struct _jabber_add_permit *jap) | |
| 129 | { | |
| 130 | if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
| 131 | jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
| 132 | "unsubscribed"); | |
| 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 | ||
| 7280 | 157 | if(!jb) |
| 158 | return; | |
| 159 | ||
| 7014 | 160 | if(jb->error_msg) { |
| 161 | g_free(jb->error_msg); | |
| 162 | jb->error_msg = NULL; | |
| 163 | } | |
| 164 | ||
| 165 | if(type && !strcasecmp(type, "error")) { | |
| 166 | state = JABBER_STATE_ERROR; | |
| 167 | if((y = xmlnode_get_child(packet, "error")) != NULL) { | |
| 7184 | 168 | /* XXX: need to handle new XMPP-style errors */ |
| 7014 | 169 | char *txt = xmlnode_get_data(y); |
| 170 | jb->error_msg = g_strdup_printf(_("%s (Code %s)"), | |
| 7184 | 171 | txt ? txt : "Error", xmlnode_get_attrib(y, "code")); |
| 172 | if(txt) | |
| 173 | g_free(txt); | |
| 7014 | 174 | } else { |
| 175 | jb->error_msg = g_strdup(_("Unknown Error in presence")); | |
| 176 | } | |
| 177 | } else if(type && !strcasecmp(type, "subscribe")) { | |
| 178 | struct _jabber_add_permit *jap = g_new0(struct _jabber_add_permit, 1); | |
| 179 | char *msg = g_strdup_printf(_("The user %s wants to add you to their buddy list."), from); | |
| 180 | jap->gc = js->gc; | |
| 181 | jap->who = g_strdup(from); | |
| 182 | ||
| 183 | gaim_request_action(js->gc, NULL, msg, NULL, 0, jap, 2, | |
| 184 | _("Authorize"), G_CALLBACK(authorize_add_cb), | |
| 185 | _("Deny"), G_CALLBACK(deny_add_cb)); | |
| 186 | g_free(msg); | |
| 7145 | 187 | return; |
| 7014 | 188 | } else if(type && (!strcmp(type, "subscribed") || |
| 189 | !strcmp(type, "unsubscribed"))) { | |
| 190 | /* we've been allowed to see their presence, but we don't care */ | |
| 191 | return; | |
| 192 | } else { | |
| 193 | if((y = xmlnode_get_child(packet, "show"))) { | |
| 194 | char *show = xmlnode_get_data(y); | |
| 195 | if(!show) { | |
| 196 | state = 0; | |
| 197 | } else if(!strcasecmp(show, "away")) { | |
| 198 | state = JABBER_STATE_AWAY; | |
| 199 | } else if(!strcasecmp(show, "chat")) { | |
| 200 | state = JABBER_STATE_CHAT; | |
| 201 | } else if(!strcasecmp(show, "xa")) { | |
| 202 | state = JABBER_STATE_XA; | |
| 203 | } else if(!strcasecmp(show, "dnd")) { | |
| 204 | state = JABBER_STATE_DND; | |
| 205 | } | |
| 206 | g_free(show); | |
| 207 | } else { | |
| 208 | state = 0; | |
| 209 | } | |
| 210 | } | |
| 211 | ||
| 212 | for(y = packet->child; y; y = y->next) { | |
| 213 | if(y->type != NODE_TYPE_TAG) | |
| 214 | continue; | |
| 215 | ||
| 216 | if(!strcmp(y->name, "status")) { | |
| 217 | status = xmlnode_get_data(y); | |
| 218 | } else if(!strcmp(y->name, "priority")) { | |
| 219 | char *p = xmlnode_get_data(y); | |
| 220 | if(p) { | |
| 221 | priority = atoi(p); | |
| 222 | g_free(p); | |
| 223 | } | |
| 224 | } else if(!strcmp(y->name, "x")) { | |
| 225 | const char *xmlns = xmlnode_get_attrib(y, "xmlns"); | |
| 226 | if(xmlns && !strcmp(xmlns, "http://jabber.org/protocol/muc#user")) { | |
| 227 | /* this is where we'd normally get the "op" status of the | |
| 228 | * user, but since we don't have a good way to show that yet | |
| 229 | * we'll ignore it */ | |
| 230 | muc = TRUE; | |
| 231 | } | |
| 232 | } | |
| 233 | } | |
| 234 | ||
| 235 | jid = jabber_id_new(from); | |
| 236 | ||
| 237 | if((chat = jabber_chat_find(js, jid->node, jid->domain))) { | |
| 238 | static int i = 0; | |
| 239 | char *room_jid = g_strdup_printf("%s@%s", jid->node, jid->domain); | |
| 240 | ||
| 241 | if(state == JABBER_STATE_ERROR) { | |
| 242 | const char *code = NULL; | |
| 243 | char *text = NULL; | |
| 244 | char *buf; | |
| 245 | xmlnode *error = xmlnode_get_child(packet, "error"); | |
| 246 | if(error) { | |
| 247 | /* I should make my own messages so they can be | |
| 248 | * translated, but i'm tired */ | |
| 249 | code = xmlnode_get_attrib(error, "code"); | |
| 250 | text = xmlnode_get_data(error); | |
| 251 | } | |
| 252 | ||
| 253 | if(!code) | |
| 254 | code = ""; | |
| 255 | if(!text) | |
| 256 | text = g_strdup(_("Unable to join chat")); | |
| 257 | ||
| 258 | buf = g_strdup_printf("Error %s joining chat %s: %s", | |
| 259 | code, from, text); | |
| 260 | gaim_notify_error(js->gc, _("Error"), _("Error"), buf); | |
| 261 | g_free(text); | |
| 262 | g_free(buf); | |
| 263 | ||
| 264 | jabber_chat_destroy(chat); | |
| 265 | return; | |
| 266 | } | |
| 267 | ||
| 268 | ||
| 269 | if(!chat->conv) { | |
| 270 | chat->id = i++; | |
| 271 | chat->muc = muc; | |
| 272 | chat->conv = serv_got_joined_chat(js->gc, chat->id, room_jid); | |
| 273 | } | |
| 274 | ||
| 275 | if(type && !strcasecmp(type, "unavailable")) { | |
| 276 | if(!strcmp(jid->resource, chat->nick)) { | |
| 277 | serv_got_chat_left(js->gc, chat->id); | |
| 278 | jabber_chat_destroy(chat); | |
| 279 | } else { | |
|
7118
280b3b85a28a
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7095
diff
changeset
|
280 | gaim_conv_chat_remove_user(GAIM_CONV_CHAT(chat->conv), jid->resource, |
| 7014 | 281 | NULL); |
| 282 | } | |
| 283 | } else { | |
| 284 | if(!jabber_chat_find_buddy(chat->conv, jid->resource)) | |
|
7118
280b3b85a28a
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7095
diff
changeset
|
285 | gaim_conv_chat_add_user(GAIM_CONV_CHAT(chat->conv), jid->resource, |
| 7014 | 286 | NULL); |
| 287 | } | |
| 288 | g_free(room_jid); | |
| 289 | } else { | |
| 7124 | 290 | if(state != JABBER_STATE_ERROR && !(jb->subscription & JABBER_SUB_TO)) { |
| 7014 | 291 | gaim_debug(GAIM_DEBUG_INFO, "jabber", |
| 292 | "got unexpected presence from %s, ignoring\n", from); | |
| 293 | jabber_id_free(jid); | |
| 294 | return; | |
| 295 | } | |
| 296 | ||
| 297 | buddy_name = g_strdup_printf("%s@%s", jid->node, jid->domain); | |
| 298 | if((b = gaim_find_buddy(js->gc->account, buddy_name)) == NULL) { | |
| 299 | jabber_id_free(jid); | |
| 300 | g_free(buddy_name); | |
| 301 | return; | |
| 302 | } | |
| 303 | ||
| 304 | if(state == JABBER_STATE_ERROR || | |
| 305 | (type && !strcasecmp(type, "unavailable"))) | |
| 306 | jabber_buddy_remove_resource(jb, jid->resource); | |
| 307 | else | |
| 308 | jabber_buddy_track_resource(jb, jid->resource, priority, state, | |
| 309 | status); | |
| 310 | ||
| 311 | jbr = jabber_buddy_find_resource(jb, jid->resource); | |
| 312 | ||
| 313 | if(jbr) | |
| 314 | serv_got_update(js->gc, buddy_name, 1, 0, b->signon, b->idle, | |
| 315 | jbr->state); | |
| 316 | else | |
| 317 | serv_got_update(js->gc, buddy_name, 0, 0, 0, 0, 0); | |
| 318 | ||
| 319 | g_free(buddy_name); | |
| 320 | } | |
| 321 | g_free(status); | |
| 322 | jabber_id_free(jid); | |
| 323 | } | |
| 324 | ||
| 325 | void jabber_presence_subscription_set(JabberStream *js, const char *who, const char *type) | |
| 326 | { | |
| 327 | xmlnode *presence = xmlnode_new("presence"); | |
| 328 | ||
| 329 | xmlnode_set_attrib(presence, "to", who); | |
| 330 | xmlnode_set_attrib(presence, "type", type); | |
| 331 | ||
| 332 | jabber_send(js, presence); | |
| 333 | xmlnode_free(presence); | |
| 334 | } |