comparison libpurple/protocols/jabber/chat.c @ 28741:724e77faee1a

jabber: Refactor the chat-joining code
author Paul Aurich <paul@darkrain42.org>
date Mon, 12 Oct 2009 18:11:32 +0000
parents c42d5c23a9e8
children 63dc67b32577
comparison
equal deleted inserted replaced
28740:51d507e6c8f3 28741:724e77faee1a
207 { 207 {
208 GHashTable *hash_table = (GHashTable *)user_data; 208 GHashTable *hash_table = (GHashTable *)user_data;
209 g_hash_table_insert(hash_table, g_strdup(key), g_strdup(value)); 209 g_hash_table_insert(hash_table, g_strdup(key), g_strdup(value));
210 } 210 }
211 211
212 void jabber_chat_join(PurpleConnection *gc, GHashTable *data) 212 static JabberChat *jabber_chat_new(JabberStream *js, const char *room,
213 const char *server, const char *handle,
214 const char *password, GHashTable *data)
213 { 215 {
214 JabberChat *chat; 216 JabberChat *chat;
215 char *room, *server, *handle, *passwd; 217 char *jid;
218
219 g_return_val_if_fail(jabber_chat_find(js, room, server) == NULL, NULL);
220
221 chat = g_new0(JabberChat, 1);
222 chat->js = js;
223
224 chat->room = g_strdup(room);
225 chat->server = g_strdup(server);
226 chat->handle = g_strdup(handle);
227
228 /* Copy the data hash table to chat->components */
229 /* TODO: Create entries in data table if data is NULL... */
230 chat->components = g_hash_table_new_full(g_str_hash, g_str_equal,
231 g_free, g_free);
232 g_hash_table_foreach(data, insert_in_hash_table, chat->components);
233
234 chat->members = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
235 (GDestroyNotify)jabber_chat_member_free);
236
237 jid = g_strdup_printf("%s@%s", room, server);
238 g_hash_table_insert(js->chats, jid, chat);
239
240 return chat;
241 }
242
243 JabberChat *jabber_join_chat(JabberStream *js, const char *room,
244 const char *server, const char *handle,
245 const char *password, GHashTable *data)
246 {
247 JabberChat *chat;
248
249 PurpleConnection *gc;
250 PurpleAccount *account;
251 PurpleStatus *status;
252
216 xmlnode *presence, *x; 253 xmlnode *presence, *x;
217 char *tmp, *room_jid, *full_jid;
218 JabberStream *js = gc->proto_data;
219 PurplePresence *gpresence;
220 PurpleStatus *status;
221 JabberBuddyState state; 254 JabberBuddyState state;
222 char *msg; 255 char *msg;
223 int priority; 256 int priority;
257
258 char *jid;
259
260 chat = jabber_chat_new(js, room, server, handle, password, data);
261 g_return_val_if_fail(chat != NULL, NULL);
262
263 gc = js->gc;
264 account = purple_connection_get_account(gc);
265 status = purple_account_get_active_status(account);
266 purple_status_to_jabber(status, &state, &msg, &priority);
267
268 presence = jabber_presence_create_js(js, state, msg, priority);
269 g_free(msg);
270
271 jid = g_strdup_printf("%s@%s/%s", room, server, handle);
272 xmlnode_set_attrib(presence, "to", jid);
273 g_free(jid);
274
275 x = xmlnode_new_child(presence, "x");
276 xmlnode_set_namespace(x, "http://jabber.org/protocol/muc");
277
278 if (password && *password) {
279 xmlnode *p = xmlnode_new_child(x, "password");
280 xmlnode_insert_data(p, password, -1);
281 }
282
283 jabber_send(js, presence);
284 xmlnode_free(presence);
285
286 return chat;
287 }
288
289 void jabber_chat_join(PurpleConnection *gc, GHashTable *data)
290 {
291 char *room, *server, *handle, *passwd;
292 JabberID *jid;
293 JabberStream *js = gc->proto_data;
294 char *tmp;
224 295
225 room = g_hash_table_lookup(data, "room"); 296 room = g_hash_table_lookup(data, "room");
226 server = g_hash_table_lookup(data, "server"); 297 server = g_hash_table_lookup(data, "server");
227 handle = g_hash_table_lookup(data, "handle"); 298 handle = g_hash_table_lookup(data, "handle");
228 passwd = g_hash_table_lookup(data, "password"); 299 passwd = g_hash_table_lookup(data, "password");
254 purple_serv_got_join_chat_failed(gc, data); 325 purple_serv_got_join_chat_failed(gc, data);
255 g_free(buf); 326 g_free(buf);
256 return; 327 return;
257 } 328 }
258 329
259 if(jabber_chat_find(js, room, server)) 330 /* Normalize the room and server parameters */
260 return;
261
262 tmp = g_strdup_printf("%s@%s", room, server); 331 tmp = g_strdup_printf("%s@%s", room, server);
263 room_jid = g_strdup(jabber_normalize(NULL, tmp)); 332 jid = jabber_id_new(tmp);
264 g_free(tmp); 333 g_free(tmp);
265 334
266 chat = g_new0(JabberChat, 1); 335 if (jid == NULL) {
267 chat->js = gc->proto_data; 336 /* TODO: Error message */
268 337
269 chat->room = g_strdup(room); 338 g_return_if_reached();
270 chat->server = g_strdup(server); 339 }
271 chat->handle = g_strdup(handle); 340
272 341 /*
273 /* Copy the data hash table to chat->components */ 342 * Now that we've done all that nice core-interface stuff, let's join
274 chat->components = g_hash_table_new_full(g_str_hash, g_str_equal, 343 * this room!
275 g_free, g_free); 344 */
276 g_hash_table_foreach(data, insert_in_hash_table, chat->components); 345 jabber_join_chat(js, jid->node, jid->domain, handle, passwd, data);
277 346 jabber_id_free(jid);
278 chat->members = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
279 (GDestroyNotify)jabber_chat_member_free);
280
281 g_hash_table_insert(js->chats, room_jid, chat);
282
283 gpresence = purple_account_get_presence(gc->account);
284 status = purple_presence_get_active_status(gpresence);
285
286 purple_status_to_jabber(status, &state, &msg, &priority);
287
288 presence = jabber_presence_create_js(js, state, msg, priority);
289 full_jid = g_strdup_printf("%s/%s", room_jid, handle);
290 xmlnode_set_attrib(presence, "to", full_jid);
291 g_free(full_jid);
292 g_free(msg);
293
294 x = xmlnode_new_child(presence, "x");
295 xmlnode_set_namespace(x, "http://jabber.org/protocol/muc");
296
297 if(passwd && *passwd) {
298 xmlnode *password = xmlnode_new_child(x, "password");
299 xmlnode_insert_data(password, passwd, -1);
300 }
301
302 jabber_send(js, presence);
303 xmlnode_free(presence);
304 } 347 }
305 348
306 void jabber_chat_leave(PurpleConnection *gc, int id) 349 void jabber_chat_leave(PurpleConnection *gc, int id)
307 { 350 {
308 JabberStream *js = gc->proto_data; 351 JabberStream *js = gc->proto_data;