16395
|
1 /* MySpaceIM Protocol Plugin
|
|
2 *
|
|
3 * \author Jeff Connelly
|
|
4 *
|
|
5 * Copyright (C) 2007, Jeff Connelly <myspaceim@xyzzy.cjb.net>
|
|
6 *
|
16397
|
7 * Based on Purple's "C Plugin HOWTO" hello world example.
|
16395
|
8 *
|
|
9 * Code also drawn from myspace:
|
16397
|
10 * http://snarfed.org/space/purple+mock+protocol+plugin
|
16395
|
11 * Copyright (C) 2004-2007, Ryan Barrett <mockprpl@ryanb.org>
|
|
12 *
|
16397
|
13 * and some constructs also based on existing Purple plugins, which are:
|
|
14 * Copyright (C) 2003, Robbert Haarman <purple@inglorion.net>
|
16395
|
15 * Copyright (C) 2003, Ethan Blanton <eblanton@cs.purdue.edu>
|
|
16 * Copyright (C) 2000-2003, Rob Flynn <rob@tgflinux.com>
|
|
17 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
|
|
18 *
|
|
19 * This program is free software; you can redistribute it and/or modify
|
|
20 * it under the terms of the GNU General Public License as published by
|
|
21 * the Free Software Foundation; either version 2 of the License, or
|
|
22 * (at your option) any later version.
|
|
23 *
|
|
24 * This program is distributed in the hope that it will be useful,
|
|
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
27 * GNU General Public License for more details.
|
|
28 *
|
|
29 * You should have received a copy of the GNU General Public License
|
|
30 * along with this program; if not, write to the Free Software
|
|
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
32 */
|
|
33
|
16397
|
34 #define PURPLE_PLUGIN
|
16395
|
35
|
|
36 #include <string.h>
|
|
37 #include <errno.h> /* for EAGAIN */
|
|
38
|
|
39 #include <glib.h>
|
|
40
|
|
41 #ifdef _WIN32
|
|
42 #include "win32dep.h"
|
|
43 #else
|
|
44 /* For recv() and send(); needed to match Win32 */
|
|
45 #include <sys/types.h>
|
|
46 #include <sys/socket.h>
|
|
47 #endif
|
|
48
|
16397
|
49 #include "internal.h"
|
|
50
|
16395
|
51 #include "notify.h"
|
|
52 #include "plugin.h"
|
|
53 #include "version.h"
|
|
54 #include "cipher.h" /* for SHA-1 */
|
|
55 #include "util.h" /* for base64 */
|
16397
|
56 #include "debug.h" /* for purple_debug_info */
|
|
57
|
|
58 #include "crypt-rc4.h" /* TODO: use rc4 */
|
16395
|
59
|
|
60 #define MSIM_STATUS_ONLINE "online"
|
|
61 #define MSIM_STATUS_AWAY "away"
|
|
62 #define MSIM_STATUS_OFFLINE "offline"
|
|
63 #define MSIM_STATUS_INVISIBLE "invisible"
|
|
64
|
|
65 /* Build version of MySpaceIM to report to servers */
|
|
66 #define MSIM_CLIENT_VERSION 673
|
|
67
|
|
68 #define MSIM_SERVER "im.myspace.akadns.net"
|
|
69 //#define MSIM_SERVER "localhost"
|
|
70 #define MSIM_PORT 1863 /* TODO: alternate ports and automatic */
|
|
71
|
|
72 /* Constants */
|
|
73 #define HASH_SIZE 0x14 /**< Size of SHA-1 hash for login */
|
|
74 #define NONCE_HALF_SIZE 0x20 /**< Half of decoded 'nc' field */
|
|
75 #define MSIM_READ_BUF_SIZE 5*1024 /**< Receive buffer size */
|
|
76 #define MSIM_FINAL_STRING "\\final\\" /**< Message end marker */
|
|
77
|
|
78 /* Messages */
|
|
79 #define MSIM_BM_INSTANT 1
|
|
80 #define MSIM_BM_STATUS 100
|
|
81 #define MSIM_BM_ACTION 121
|
|
82 /*#define MSIM_BM_UNKNOWN1 122*/
|
|
83
|
|
84 /* Random number in every MsimSession, to ensure it is valid. */
|
|
85 #define MSIM_SESSION_STRUCT_MAGIC 0xe4a6752b
|
|
86
|
|
87 /* Everything needed to keep track of a session. */
|
|
88 typedef struct _MsimSession
|
|
89 {
|
|
90 guint magic; /**< MSIM_SESSION_STRUCT_MAGIC */
|
16397
|
91 PurpleAccount *account;
|
|
92 PurpleConnection *gc;
|
16395
|
93 gchar *sesskey; /**< Session key text string from server */
|
|
94 gchar *userid; /**< This user's numeric user ID */
|
|
95 gint fd; /**< File descriptor to/from server */
|
|
96
|
|
97 GHashTable *user_lookup_cb; /**< Username -> userid lookup callback */
|
|
98 GHashTable *user_lookup_cb_data; /**< Username -> userid lookup callback data */
|
|
99 GHashTable *user_lookup_cache; /**< Cached information on users */
|
|
100
|
|
101 gchar *rxbuf; /**< Receive buffer */
|
|
102 guint rxoff; /**< Receive buffer offset */
|
|
103 } MsimSession;
|
|
104
|
|
105 #define MSIM_SESSION_VALID(s) (session != NULL && session->magic == MSIM_SESSION_STRUCT_MAGIC)
|
|
106
|
|
107 /* Callback for when a user's information is received, initiated from a user lookup. */
|
|
108 typedef void (*MSIM_USER_LOOKUP_CB)(MsimSession *session, GHashTable *userinfo, gpointer data);
|
|
109
|
|
110 /* Passed to MSIM_USER_LOOKUP_CB for msim_send_im_cb - called when
|
|
111 * user information is available, ready to send a message. */
|
|
112 typedef struct _send_im_cb_struct
|
|
113 {
|
|
114 gchar *who;
|
|
115 gchar *message;
|
16397
|
116 PurpleMessageFlags flags;
|
16395
|
117 } send_im_cb_struct;
|
|
118
|
|
119
|
|
120 /* TODO: .h file */
|
|
121 static void msim_lookup_user(MsimSession *session, const gchar *user, MSIM_USER_LOOKUP_CB cb, gpointer data);
|
|
122 static inline gboolean msim_is_userid(const gchar *user);
|
|
123 static void msim_session_destroy(MsimSession *session);
|
|
124
|
16397
|
125 static void init_plugin(PurplePlugin *plugin)
|
16395
|
126 {
|
16397
|
127 purple_notify_message(plugin, PURPLE_NOTIFY_MSG_INFO, "Hello World!",
|
16395
|
128 "This is the Hello World! plugin :)", NULL, NULL, NULL);
|
|
129 }
|
|
130
|
|
131 /**
|
|
132 * Get possible user status types. Based on mockprpl.
|
|
133 *
|
|
134 * @return GList of status types.
|
|
135 */
|
16397
|
136 static GList *msim_status_types(PurpleAccount *acct)
|
16395
|
137 {
|
|
138 GList *types;
|
16397
|
139 PurpleStatusType *type;
|
16395
|
140
|
16397
|
141 purple_debug_info("myspace", "returning status types for %s: %s, %s, %s\n",
|
16395
|
142 acct->username,
|
|
143 MSIM_STATUS_ONLINE, MSIM_STATUS_AWAY, MSIM_STATUS_OFFLINE, MSIM_STATUS_INVISIBLE);
|
|
144
|
|
145
|
|
146 types = NULL;
|
|
147
|
16397
|
148 type = purple_status_type_new(PURPLE_STATUS_AVAILABLE, MSIM_STATUS_ONLINE,
|
16395
|
149 MSIM_STATUS_ONLINE, TRUE);
|
16397
|
150 purple_status_type_add_attr(type, "message", "Online",
|
|
151 purple_value_new(PURPLE_TYPE_STRING));
|
16395
|
152 types = g_list_append(types, type);
|
|
153
|
16397
|
154 type = purple_status_type_new(PURPLE_STATUS_AWAY, MSIM_STATUS_AWAY,
|
16395
|
155 MSIM_STATUS_AWAY, TRUE);
|
16397
|
156 purple_status_type_add_attr(type, "message", "Away",
|
|
157 purple_value_new(PURPLE_TYPE_STRING));
|
16395
|
158 types = g_list_append(types, type);
|
|
159
|
16397
|
160 type = purple_status_type_new(PURPLE_STATUS_OFFLINE, MSIM_STATUS_OFFLINE,
|
16395
|
161 MSIM_STATUS_OFFLINE, TRUE);
|
16397
|
162 purple_status_type_add_attr(type, "message", "Offline",
|
|
163 purple_value_new(PURPLE_TYPE_STRING));
|
16395
|
164 types = g_list_append(types, type);
|
|
165
|
16397
|
166 type = purple_status_type_new(PURPLE_STATUS_INVISIBLE, MSIM_STATUS_INVISIBLE,
|
16395
|
167 MSIM_STATUS_INVISIBLE, TRUE);
|
16397
|
168 purple_status_type_add_attr(type, "message", "Invisible",
|
|
169 purple_value_new(PURPLE_TYPE_STRING));
|
16395
|
170 types = g_list_append(types, type);
|
|
171
|
|
172 return types;
|
|
173 }
|
|
174
|
|
175 /**
|
|
176 * Parse a MySpaceIM protocol message into a hash table.
|
|
177 *
|
|
178 * @param msg The message string to parse, will be g_free()'d.
|
|
179 *
|
|
180 * @return Hash table of message. Caller should destroy with
|
|
181 * g_hash_table_destroy() when done.
|
|
182 */
|
|
183 static GHashTable* msim_parse(gchar* msg)
|
|
184 {
|
|
185 GHashTable *table;
|
|
186 gchar *token;
|
|
187 gchar **tokens;
|
|
188 gchar *key;
|
|
189 gchar *value;
|
|
190 int i;
|
|
191
|
|
192 g_return_val_if_fail(msg != NULL, NULL);
|
|
193
|
16397
|
194 purple_debug_info("msim", "msim_parse: got <%s>\n", msg);
|
16395
|
195
|
|
196 key = NULL;
|
|
197
|
|
198 /* All messages begin with a \ */
|
|
199 if (msg[0] != '\\' || msg[1] == 0)
|
|
200 {
|
16397
|
201 purple_debug_info("msim", "msim_parse: incomplete/bad msg, "
|
16395
|
202 "missing initial backslash: <%s>", msg);
|
|
203 /* XXX: Should we try to recover, and read to first backslash? */
|
|
204
|
|
205 g_free(msg);
|
|
206 return NULL;
|
|
207 }
|
|
208
|
|
209 /* Create table of strings, set to call g_free on destroy. */
|
|
210 table = g_hash_table_new_full((GHashFunc)g_str_hash,
|
|
211 (GEqualFunc)g_str_equal, g_free, g_free);
|
|
212
|
|
213 for (tokens = g_strsplit(msg + 1, "\\", 0), i = 0;
|
|
214 (token = tokens[i]);
|
|
215 i++)
|
|
216 {
|
|
217 //printf("tok=<%s>, i%2=%d\n", token, i % 2);
|
|
218 if (i % 2)
|
|
219 {
|
|
220 value = token;
|
|
221
|
|
222 /* Check if key already exists */
|
|
223 if (g_hash_table_lookup(table, key) == NULL)
|
|
224 {
|
|
225 //printf("insert: |%s|=|%s|\n", key, value);
|
|
226 g_hash_table_insert(table, g_strdup(key), g_strdup(value));
|
|
227 } else {
|
|
228 /* TODO: Some dictionaries have multiple values for the same
|
|
229 * key. Should append to a GList to handle this case. */
|
16397
|
230 purple_debug_info("msim", "msim_parse: key %s already exists, "
|
16395
|
231 "not overwriting or replacing; ignoring new value %s", key,
|
|
232 value);
|
|
233 }
|
|
234 } else {
|
|
235 key = token;
|
|
236 }
|
|
237 }
|
|
238 g_strfreev(tokens);
|
|
239
|
|
240 /* Can free now since all data was copied to hash key/values */
|
|
241 g_free(msg);
|
|
242
|
|
243 return table;
|
|
244 }
|
|
245
|
|
246 /**
|
|
247 * Compute the base64'd login challenge response based on username, password, nonce, and IPs.
|
|
248 *
|
|
249 * @param nonce The base64 encoded nonce ('nc') field from the server.
|
|
250 * @param email User's email address (used as login name).
|
|
251 * @param password User's cleartext password.
|
|
252 *
|
|
253 * @return Encoded login challenge response, ready to send to the server. Must be g_free()'d
|
|
254 * when finished.
|
|
255 */
|
|
256 static gchar* msim_compute_login_response(guchar nonce[2*NONCE_HALF_SIZE], gchar* email, gchar* password)
|
|
257 {
|
16397
|
258 PurpleCipherContext *key_context;
|
|
259 PurpleCipher *sha1;
|
16395
|
260 rc4_state_struct rc4;
|
|
261 guchar hash_pw[HASH_SIZE];
|
|
262 guchar key[HASH_SIZE];
|
|
263 gchar* password_utf16le;
|
|
264 guchar* data;
|
|
265 gchar* response;
|
|
266 int i, data_len;
|
|
267
|
|
268 //memset(nonce, 0, NONCE_HALF_SIZE);
|
|
269 //memset(nonce + NONCE_HALF_SIZE, 1, NONCE_HALF_SIZE);
|
|
270
|
|
271 /* Convert ASCII password to UTF16 little endian */
|
|
272 /* TODO: use the built-in facility to do this, like Nathan Peterson does. */
|
16397
|
273 purple_debug_info("msim", "converting password to utf16le\n");
|
16395
|
274 //printf("pw=<%s>\n",password);
|
|
275 password_utf16le = g_new0(gchar, strlen(password) * 2);
|
|
276 for (i = 0; i < strlen(password) * 2; i += 2)
|
|
277 {
|
|
278 password_utf16le[i] = password[i / 2];
|
|
279 password_utf16le[i + 1] = 0;
|
|
280 }
|
|
281
|
|
282 /* Compute password hash */
|
16397
|
283 purple_cipher_digest_region("sha1", (guchar*)password_utf16le, strlen(password) * 2,
|
16395
|
284 sizeof(hash_pw), hash_pw, NULL);
|
|
285
|
|
286 #ifdef MSIM_DEBUG_LOGIN_CHALLENGE
|
|
287 printf("pwhash = ");
|
|
288 for (i = 0; i < sizeof(hash_pw); i++)
|
|
289 printf("%.2x ", hash_pw[i]);
|
|
290 printf("\n");
|
|
291 #endif
|
|
292
|
|
293 /* key = sha1(sha1(pw) + nonce2) */
|
16397
|
294 sha1 = purple_ciphers_find_cipher("sha1");
|
|
295 key_context = purple_cipher_context_new(sha1, NULL);
|
|
296 purple_cipher_context_append(key_context, hash_pw, HASH_SIZE);
|
|
297 purple_cipher_context_append(key_context, nonce + NONCE_HALF_SIZE, NONCE_HALF_SIZE);
|
|
298 purple_cipher_context_digest(key_context, sizeof(key), key, NULL);
|
16395
|
299
|
|
300 #ifdef MSIM_DEBUG_LOGIN_CHALLENGE
|
|
301 printf("key = ");
|
|
302 for (i = 0; i < sizeof(key); i++)
|
|
303 {
|
|
304 printf("%.2x ", key[i]);
|
|
305 }
|
|
306 printf("\n");
|
|
307 #endif
|
|
308
|
|
309 /* Note: 'key' variable is 0x14 bytes (from SHA-1 hash),
|
|
310 * but only first 0x10 used for the RC4 key. */
|
|
311 crypt_rc4_init(&rc4, key, 0x10);
|
|
312
|
|
313 /* TODO: obtain IPs of network interfaces. This is not immediately
|
|
314 * important because you can still connect and perform basic
|
|
315 * functions of the protocol. There is also a high chance that the addreses
|
|
316 * are RFC1918 private, so the servers couldn't do anything with them
|
|
317 * anyways except make note of that fact. Probably important for any
|
|
318 * kind of direct connection, or file transfer functionality.
|
|
319 */
|
|
320 /* rc4 encrypt:
|
|
321 * nonce1+email+IP list */
|
|
322 data_len = NONCE_HALF_SIZE + strlen(email) + 25;
|
|
323 data = g_new0(guchar, data_len);
|
|
324 memcpy(data, nonce, NONCE_HALF_SIZE);
|
|
325 memcpy(data + NONCE_HALF_SIZE, email, strlen(email));
|
|
326 memcpy(data + NONCE_HALF_SIZE + strlen(email),
|
|
327 /* IP addresses of network interfaces */
|
|
328 "\x00\x00\x00\x00\x05\x7f\x00\x00\x01\x00\x00\x00\x00\x0a\x00\x00\x40\xc0\xa8\x58\x01\xc0\xa8\x3c\x01", 25);
|
|
329 crypt_rc4(&rc4, data, data_len);
|
|
330
|
16397
|
331 response = purple_base64_encode(data, data_len);
|
16395
|
332 #ifdef MSIM_DEBUG_LOGIN_CHALLENGE
|
|
333 printf("response=<%s>\n", response);
|
|
334 #endif
|
|
335
|
|
336 return response;
|
|
337 }
|
|
338
|
|
339 static void print_hash_item(gpointer key, gpointer value, gpointer user_data)
|
|
340 {
|
|
341 printf("%s=%s\n", (char*)key, (char*)value);
|
|
342 }
|
|
343
|
|
344 /**
|
|
345 * Send an arbitrary protocol message.
|
|
346 *
|
|
347 * @param session
|
|
348 * @param msg The textual, encoded message to send.
|
|
349 *
|
|
350 * Note: this does not send instant messages. For that, see msim_send_im.
|
|
351 */
|
|
352 static void msim_send(MsimSession *session, const gchar *msg)
|
|
353 {
|
|
354 int ret;
|
|
355
|
|
356 g_return_if_fail(MSIM_SESSION_VALID(session));
|
|
357 g_return_if_fail(msg != NULL);
|
|
358
|
16397
|
359 purple_debug_info("msim", "msim_send: writing <%s>\n", msg);
|
16395
|
360
|
|
361 ret = send(session->fd, msg, strlen(msg), 0);
|
|
362
|
|
363 if (ret != strlen(msg))
|
|
364 {
|
16397
|
365 purple_debug_info("msim", "msim_send(%s): strlen=%d, but only wrote %s\n",
|
16395
|
366 msg, strlen(msg), ret);
|
|
367 /* TODO: better error */
|
|
368 }
|
|
369 }
|
|
370
|
|
371 /**
|
|
372 * Process a login challenge, sending a response.
|
|
373 *
|
|
374 * @param session
|
|
375 * @param table Hash table of login challenge message.
|
|
376 *
|
|
377 * @return 0, since the 'table' parameter is no longer needed.
|
|
378 */
|
|
379 static int msim_login_challenge(MsimSession *session, GHashTable *table)
|
|
380 {
|
16397
|
381 PurpleAccount *account;
|
16395
|
382 gchar *nc_str;
|
|
383 guchar *nc;
|
|
384 gchar *response_str;
|
|
385 gsize nc_len;
|
|
386 gchar *buf;
|
|
387
|
|
388 g_return_val_if_fail(MSIM_SESSION_VALID(session), 0);
|
|
389 g_return_val_if_fail(table != NULL, 0);
|
|
390
|
|
391 nc_str = g_hash_table_lookup(table, "nc");
|
|
392
|
|
393 account = session->account;
|
|
394 //assert(account);
|
|
395
|
16397
|
396 purple_connection_update_progress(session->gc, "Reading challenge", 1, 4);
|
16395
|
397
|
16397
|
398 purple_debug_info("msim", "nc=<%s>\n", nc_str);
|
16395
|
399
|
16397
|
400 nc = (guchar*)purple_base64_decode(nc_str, &nc_len);
|
|
401 purple_debug_info("msim", "base64 decoded to %d bytes\n", nc_len);
|
16395
|
402 if (nc_len != 0x40)
|
|
403 {
|
16397
|
404 purple_debug_info("msim", "bad nc length: %x != 0x40\n", nc_len);
|
|
405 purple_connection_error(session->gc, "Unexpected challenge length from server");
|
16395
|
406 return 0;
|
|
407 }
|
|
408
|
16397
|
409 purple_connection_update_progress(session->gc, "Logging in", 2, 4);
|
16395
|
410
|
|
411 printf("going to compute login response\n");
|
|
412 //response_str = msim_compute_login_response(nc_str, "testuser", "testpw"); //session->gc->account->username, session->gc->account->password);
|
|
413 response_str = msim_compute_login_response(nc, account->username, account->password);
|
|
414 printf("got back login response\n");
|
|
415
|
|
416 g_free(nc);
|
|
417
|
|
418 /* Reply */
|
|
419 buf = g_strdup_printf("\\login2\\%d\\username\\%s\\response\\%s\\clientver\\%d\\reconn\\%d\\status\\%d\\id\\1\\final\\",
|
|
420 196610, account->username, response_str, MSIM_CLIENT_VERSION, 0, 100);
|
|
421
|
|
422 g_free(response_str);
|
|
423
|
16397
|
424 purple_debug_info("msim", "response=<%s>\n", buf);
|
16395
|
425
|
|
426 msim_send(session, buf);
|
|
427
|
|
428 g_free(buf);
|
|
429
|
|
430 return 0;
|
|
431 }
|
|
432
|
|
433 /**
|
|
434 * Parse a \x1c-separated "dictionary" of key=value pairs into a hash table.
|
|
435 *
|
|
436 * @param body_str The text of the dictionary to parse. Often the
|
|
437 * value for the 'body' field.
|
|
438 *
|
|
439 * @return Hash table of the keys and values. Must g_hash_table_destroy() when done.
|
|
440 */
|
|
441 static GHashTable *msim_parse_body(const gchar *body_str)
|
|
442 {
|
|
443 GHashTable *table;
|
|
444 gchar *item;
|
|
445 gchar **items;
|
|
446 gchar **elements;
|
|
447 guint i;
|
|
448
|
|
449 g_return_val_if_fail(body_str != NULL, NULL);
|
|
450
|
|
451 table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
|
452
|
|
453 for (items = g_strsplit(body_str, "\x1c", 0), i = 0;
|
|
454 (item = items[i]);
|
|
455 i++)
|
|
456 {
|
|
457 gchar *key, *value;
|
|
458
|
|
459 //printf("TOK=<%s>\n", token);
|
|
460 elements = g_strsplit(item, "=", 2);
|
|
461
|
|
462 key = elements[0];
|
|
463 if (!key)
|
|
464 {
|
16397
|
465 purple_debug_info("msim", "msim_parse_body(%s): null key", body_str);
|
16395
|
466 g_strfreev(elements);
|
|
467 break;
|
|
468 }
|
|
469
|
|
470 value = elements[1];
|
|
471 if (!value)
|
|
472 {
|
16397
|
473 purple_debug_info("msim", "msim_parse_body(%s): null value", body_str);
|
16395
|
474 g_strfreev(elements);
|
|
475 break;
|
|
476 }
|
|
477
|
|
478 //printf("-- %s: %s\n", key, value);
|
|
479
|
|
480 /* XXX: This overwrites duplicates. */
|
|
481 /* TODO: make the GHashTable values be GList's, and append to the list if
|
|
482 * there is already a value of the same key name. This is important for
|
|
483 * the WebChallenge message. */
|
|
484 g_hash_table_insert(table, g_strdup(key), g_strdup(value));
|
|
485
|
|
486 g_strfreev(elements);
|
|
487 }
|
|
488
|
|
489 g_strfreev(items);
|
|
490
|
|
491 return table;
|
|
492 }
|
|
493
|
|
494 /**
|
|
495 * Immediately send an IM to a user, by their numeric user ID.
|
|
496 *
|
|
497 * @param session
|
|
498 * @param userid ASCII numeric userid.
|
|
499 * @param message Text of message to send.
|
16397
|
500 * @param flags Purple instant message flags.
|
16395
|
501 *
|
|
502 * @return 0, since the 'table' parameter is no longer needed.
|
|
503 *
|
|
504 */
|
16397
|
505 static int msim_send_im_by_userid(MsimSession *session, const gchar *userid, const gchar *message, PurpleMessageFlags flags)
|
16395
|
506 {
|
|
507 gchar *msg_string;
|
|
508
|
|
509 g_return_val_if_fail(MSIM_SESSION_VALID(session), 0);
|
|
510 g_return_val_if_fail(userid != NULL, 0);
|
|
511 g_return_val_if_fail(msim_is_userid(userid) == TRUE, 0);
|
|
512 g_return_val_if_fail(message != NULL, 0);
|
|
513
|
|
514 /* TODO: constants for bm types */
|
|
515 msg_string = g_strdup_printf("\\bm\\121\\sesskey\\%s\\t\\%s\\cv\\%d\\msg\\%s\\final\\",
|
|
516 session->sesskey, userid, MSIM_CLIENT_VERSION, message);
|
|
517
|
16397
|
518 purple_debug_info("msim", "going to write: %s\n", msg_string);
|
16395
|
519
|
|
520 msim_send(session, msg_string);
|
|
521
|
16397
|
522 /* TODO: notify Purple that we sent the IM. */
|
16395
|
523
|
|
524 /* Not needed since sending messages to yourself is allowed by MSIM! */
|
|
525 /*if (strcmp(from_username, who) == 0)
|
16397
|
526 serv_got_im(gc, from_username, message, PURPLE_MESSAGE_RECV, time(NULL));
|
16395
|
527 */
|
|
528
|
|
529 return 0;
|
|
530 }
|
|
531
|
|
532
|
|
533 /**
|
|
534 * Callback called when ready to send an IM by userid (the userid has been looked up).
|
|
535 * Calls msim_send_im_by_userid.
|
|
536 *
|
|
537 * @param session
|
|
538 * @param userinfo User info message from server containing a 'body' field
|
|
539 * with a 'UserID' key. This is where the user ID is taken from.
|
|
540 * @param data A send_im_cb_struct* of information on the IM to send.
|
|
541 *
|
|
542 */
|
|
543 static void msim_send_im_by_userid_cb(MsimSession *session, GHashTable *userinfo, gpointer data)
|
|
544 {
|
|
545 send_im_cb_struct *s;
|
|
546 gchar *userid;
|
|
547 GHashTable *body;
|
|
548
|
|
549 g_return_if_fail(MSIM_SESSION_VALID(session));
|
|
550 g_return_if_fail(userinfo != NULL);
|
|
551
|
|
552 body = msim_parse_body(g_hash_table_lookup(userinfo, "body"));
|
|
553 g_assert(body);
|
|
554
|
|
555 userid = g_hash_table_lookup(body, "UserID");
|
|
556
|
|
557 s = (send_im_cb_struct*)data;
|
|
558 msim_send_im_by_userid(session, userid, s->message, s->flags);
|
|
559
|
|
560 g_hash_table_destroy(body);
|
|
561 g_hash_table_destroy(userinfo);
|
|
562 g_free(s->message);
|
|
563 g_free(s->who);
|
|
564 }
|
|
565
|
|
566 /**
|
|
567 * Process a message reply from the server.
|
|
568 *
|
|
569 * @param session
|
|
570 * @param table Message reply from server.
|
|
571 *
|
|
572 * @return 0, since the 'table' field is no longer needed.
|
|
573 */
|
|
574 static int msim_process_reply(MsimSession *session, GHashTable *table)
|
|
575 {
|
|
576 gchar *rid_str;
|
|
577
|
|
578 g_return_val_if_fail(MSIM_SESSION_VALID(session), 0);
|
|
579 g_return_val_if_fail(table != NULL, 0);
|
|
580
|
|
581 rid_str = g_hash_table_lookup(table, "rid");
|
|
582
|
|
583 if (rid_str) /* msim_lookup_user sets callback for here */
|
|
584 {
|
|
585 MSIM_USER_LOOKUP_CB cb;
|
|
586 gpointer data;
|
|
587 guint rid;
|
|
588
|
|
589 GHashTable *body;
|
|
590 gchar *username;
|
|
591
|
|
592 rid = atol(rid_str);
|
|
593
|
|
594 /* Cache the user info. Currently, the GHashTable of user info in
|
|
595 * this cache never expires so is never freed. TODO: expire and destroy
|
|
596 *
|
|
597 * Some information never changes (username->userid map), some does.
|
|
598 * TODO: Cache what doesn't change only
|
|
599 */
|
|
600 body = msim_parse_body(g_hash_table_lookup(table, "body"));
|
|
601 username = g_hash_table_lookup(body, "UserName");
|
|
602 if (username)
|
|
603 {
|
|
604 g_hash_table_insert(session->user_lookup_cache, g_strdup(username), body);
|
|
605 } else {
|
16397
|
606 purple_debug_info("msim", "msim_process_reply: not caching <%s>, no UserName",
|
16395
|
607 g_hash_table_lookup(table, "body"));
|
|
608 }
|
|
609
|
|
610 /* If a callback is registered for this userid lookup, call it. */
|
|
611
|
|
612 cb = g_hash_table_lookup(session->user_lookup_cb, GUINT_TO_POINTER(rid));
|
|
613 data = g_hash_table_lookup(session->user_lookup_cb_data, GUINT_TO_POINTER(rid));
|
|
614
|
|
615 if (cb)
|
|
616 {
|
16397
|
617 purple_debug_info("msim", "msim_process_body: calling callback now\n");
|
16395
|
618 cb(session, table, data);
|
|
619 g_hash_table_remove(session->user_lookup_cb, GUINT_TO_POINTER(rid));
|
|
620 g_hash_table_remove(session->user_lookup_cb_data, GUINT_TO_POINTER(rid));
|
|
621
|
|
622 /* Return 1 to tell caller of msim_process (msim_input_cb) to
|
|
623 * not destroy 'table'; allow 'cb' to hang on to it and destroy
|
|
624 * it when it wants. */
|
|
625 return 1;
|
|
626 } else {
|
16397
|
627 purple_debug_info("msim", "msim_process_body: no callback for rid %d\n", rid);
|
16395
|
628 }
|
|
629 }
|
|
630 return 0;
|
|
631 }
|
|
632
|
|
633 /**
|
|
634 * Handle an error from the server.
|
|
635 *
|
|
636 * @param session
|
|
637 * @param table The message.
|
|
638 *
|
|
639 * @return 0, since 'table' can be freed.
|
|
640 */
|
|
641 static int msim_error(MsimSession *session, GHashTable *table)
|
|
642 {
|
|
643 gchar *err, *errmsg, *full_errmsg;
|
|
644
|
|
645 g_return_val_if_fail(MSIM_SESSION_VALID(session), 0);
|
|
646 g_return_val_if_fail(table != NULL, 0);
|
|
647
|
|
648 err = g_hash_table_lookup(table, "err");
|
|
649 errmsg = g_hash_table_lookup(table, "errmsg");
|
|
650
|
|
651 full_errmsg = g_strdup_printf("Protocol error, code %s: %s", err, errmsg);
|
|
652
|
16397
|
653 purple_debug_info("msim", "msim_error: %s\n", full_errmsg);
|
16395
|
654
|
|
655 /* TODO: check 'fatal' and die if asked to.
|
|
656 * TODO: do something with the error # (localization of errmsg?) */
|
16397
|
657 purple_notify_error(session->account, g_strdup("MySpaceIM Error"),
|
16395
|
658 full_errmsg, NULL);
|
|
659
|
|
660 if (g_hash_table_lookup(table, "fatal"))
|
|
661 {
|
16397
|
662 purple_debug_info("msim", "fatal error, destroy session\n");
|
|
663 purple_connection_error(session->gc, full_errmsg);
|
16395
|
664 close(session->fd);
|
|
665 //msim_session_destroy(session);
|
|
666 }
|
|
667
|
|
668 return 0;
|
|
669 }
|
|
670
|
|
671 /**
|
|
672 * Callback to handle incoming messages, after resolving userid.
|
|
673 *
|
|
674 * @param session
|
|
675 * @param userinfo Message from server on user's info, containing UserName.
|
|
676 * @param data A gchar* of the incoming instant message's text.
|
|
677 */
|
|
678 static void msim_incoming_im_cb(MsimSession *session, GHashTable *userinfo, gpointer data)
|
|
679 {
|
|
680 gchar *msg;
|
|
681 gchar *username;
|
|
682 GHashTable *body;
|
|
683
|
|
684 g_return_if_fail(MSIM_SESSION_VALID(session));
|
|
685 g_return_if_fail(userinfo != NULL);
|
|
686
|
|
687 body = msim_parse_body(g_hash_table_lookup(userinfo, "body"));
|
|
688 g_assert(body != NULL);
|
|
689
|
|
690 username = g_hash_table_lookup(body, "UserName");
|
|
691
|
|
692 msg = (gchar*)data;
|
16397
|
693 serv_got_im(session->gc, username, msg, PURPLE_MESSAGE_RECV, time(NULL));
|
16395
|
694
|
|
695 g_hash_table_destroy(body);
|
|
696 g_hash_table_destroy(userinfo);
|
|
697 }
|
|
698
|
|
699 /**
|
|
700 * Handle an incoming message.
|
|
701 *
|
|
702 * @param session The session
|
|
703 * @param table Message from the server, containing 'f' (userid from) and 'msg'.
|
|
704 *
|
|
705 * @return 0, since table can be freed.
|
|
706 */
|
|
707 static int msim_incoming_im(MsimSession *session, GHashTable *table)
|
|
708 {
|
|
709 gchar *userid;
|
|
710 gchar *msg;
|
|
711
|
|
712 g_return_val_if_fail(MSIM_SESSION_VALID(session), 0);
|
|
713 g_return_val_if_fail(table != NULL, 0);
|
|
714
|
|
715
|
|
716 userid = g_hash_table_lookup(table, "f");
|
|
717 msg = g_hash_table_lookup(table, "msg");
|
|
718
|
16397
|
719 purple_debug_info("msim", "msim_incoming_im: got msg <%s> from <%s>, resolving username\n",
|
16395
|
720 msg, userid);
|
|
721
|
|
722 msim_lookup_user(session, userid, msim_incoming_im_cb, g_strdup(msg));
|
|
723
|
|
724 return 0;
|
|
725 }
|
|
726
|
|
727 #if 0
|
|
728 /* Not sure about this */
|
|
729 static void msim_status_now(gchar *who, gpointer data)
|
|
730 {
|
|
731 printf("msim_status_now: %s\n", who);
|
|
732 }
|
|
733 #endif
|
|
734
|
|
735 /**
|
|
736 * Callback to update incoming status messages, after looked up username.
|
|
737 *
|
|
738 * @param session
|
|
739 * @param userinfo Looked up user information from server.
|
|
740 * @param data gchar* status string.
|
|
741 *
|
|
742 */
|
|
743 static void msim_status_cb(MsimSession *session, GHashTable *userinfo, gpointer data)
|
|
744 {
|
16397
|
745 PurpleBuddyList *blist;
|
|
746 PurpleBuddy *buddy;
|
|
747 PurplePresence *presence;
|
16395
|
748 GHashTable *body;
|
16397
|
749 //PurpleStatus *status;
|
16395
|
750 gchar **status_array;
|
|
751 GList *list;
|
|
752 gchar *status_text, *status_code;
|
|
753 gchar *status_str;
|
|
754 gint i;
|
|
755 gchar *username;
|
|
756
|
|
757 g_return_if_fail(MSIM_SESSION_VALID(session));
|
|
758 g_return_if_fail(userinfo != NULL);
|
|
759
|
|
760 status_str = (gchar*)data;
|
|
761
|
|
762 body = msim_parse_body(g_hash_table_lookup(userinfo, "body"));
|
|
763 g_assert(body);
|
|
764
|
|
765 username = g_hash_table_lookup(body, "UserName");
|
|
766 /* Note: DisplayName doesn't seem to be resolvable. It could be displayed on
|
|
767 * the buddy list, if the UserID was stored along with it. */
|
|
768
|
|
769 if (!username)
|
|
770 {
|
16397
|
771 purple_debug_info("msim", "msim_status_cb: no username?!\n");
|
16395
|
772 return;
|
|
773 }
|
|
774
|
16397
|
775 purple_debug_info("msim", "msim_status_cb: updating status for <%s> to <%s>\n", username, status_str);
|
16395
|
776
|
|
777 /* TODO: generic functions to split into a GList */
|
|
778 status_array = g_strsplit(status_str, "|", 0);
|
|
779 for (list = NULL, i = 0;
|
|
780 status_array[i];
|
|
781 i++)
|
|
782 {
|
|
783 list = g_list_append(list, status_array[i]);
|
|
784 }
|
|
785
|
|
786 /* Example fields: |s|0|ss|Offline */
|
|
787 status_code = g_list_nth_data(list, 2);
|
|
788 status_text = g_list_nth_data(list, 4);
|
|
789
|
16397
|
790 blist = purple_get_blist();
|
16395
|
791
|
|
792 /* Add buddy if not found */
|
16397
|
793 buddy = purple_find_buddy(session->account, username);
|
16395
|
794 if (!buddy)
|
|
795 {
|
16397
|
796 /* TODO: purple aliases, userids and usernames */
|
|
797 purple_debug_info("msim", "msim_status: making new buddy for %s\n", username);
|
|
798 buddy = purple_buddy_new(session->account, username, NULL);
|
16395
|
799
|
|
800 /* TODO: sometimes (when click on it), buddy list disappears. Fix. */
|
16397
|
801 purple_blist_add_buddy(buddy, NULL, NULL, NULL);
|
16395
|
802 } else {
|
16397
|
803 purple_debug_info("msim", "msim_status: found buddy %s\n", username);
|
16395
|
804 }
|
|
805
|
|
806 /* For now, always set status to online.
|
|
807 * TODO: make status reflect reality
|
|
808 * TODO: show headline */
|
16397
|
809 presence = purple_presence_new_for_buddy(buddy);
|
|
810 purple_presence_set_status_active(presence, MSIM_STATUS_ONLINE, TRUE);
|
16395
|
811
|
|
812 g_strfreev(status_array);
|
|
813 g_list_free(list);
|
|
814 g_hash_table_destroy(body);
|
|
815 g_hash_table_destroy(userinfo);
|
|
816 /* Do not free status_str - it will be freed by g_hash_table_destroy on session->userid_lookup_cb_data */
|
|
817 }
|
|
818
|
|
819 /**
|
|
820 * Process incoming status messages.
|
|
821 *
|
|
822 * @param session
|
|
823 * @param table Status update message.
|
|
824 *
|
|
825 * @return 0, since 'table' can be freed.
|
|
826 */
|
|
827 static int msim_status(MsimSession *session, GHashTable *table)
|
|
828 {
|
|
829 gchar *status_str;
|
|
830 gchar *userid;
|
|
831
|
|
832 g_return_val_if_fail(MSIM_SESSION_VALID(session), 0);
|
|
833 g_return_val_if_fail(table != NULL, 0);
|
|
834
|
|
835 status_str = g_hash_table_lookup(table, "msg");
|
|
836 if (!status_str)
|
|
837 {
|
16397
|
838 purple_debug_info("msim", "msim_status: bm=100 but no status msg\n");
|
16395
|
839 return 0;
|
|
840 }
|
|
841
|
|
842 userid = g_hash_table_lookup(table, "f");
|
|
843 if (!userid)
|
|
844 {
|
16397
|
845 purple_debug_info("msim", "msim_status: bm=100 but no f field\n");
|
16395
|
846 return 0;
|
|
847 }
|
|
848
|
|
849 /* TODO: if buddies were identified on buddy list by uid, wouldn't have to lookup
|
|
850 * before updating the status! Much more efficient. */
|
16397
|
851 purple_debug_info("msim", "msim_status: got status msg <%s> for <%s>, scheduling lookup\n",
|
16395
|
852 status_str, userid);
|
|
853
|
|
854 /* Actually update status once obtain username */
|
|
855 msim_lookup_user(session, userid, msim_status_cb, g_strdup(status_str));
|
|
856
|
|
857 return 0;
|
|
858 }
|
|
859
|
|
860
|
|
861 /**
|
|
862 * Process a message.
|
|
863 *
|
|
864 * @param gc Connection.
|
|
865 * @param table Any message from the server.
|
|
866 *
|
|
867 * @return The return value of the function used to process the message, or -1 if
|
|
868 * called with invalid parameters.
|
|
869 */
|
16397
|
870 static int msim_process(PurpleConnection *gc, GHashTable *table)
|
16395
|
871 {
|
|
872 MsimSession *session;
|
|
873
|
|
874 g_return_val_if_fail(gc != NULL, -1);
|
|
875 g_return_val_if_fail(table != NULL, -1);
|
|
876
|
|
877 session = (MsimSession*)gc->proto_data;
|
|
878
|
|
879 printf("-------- message -------------\n");
|
|
880 g_hash_table_foreach(table, print_hash_item, NULL);
|
|
881 printf("------------------------------\n");
|
|
882
|
|
883 if (g_hash_table_lookup(table, "nc"))
|
|
884 {
|
|
885 return msim_login_challenge(session, table);
|
|
886 } else if (g_hash_table_lookup(table, "sesskey")) {
|
|
887 printf("SESSKEY=<%s>\n", (gchar*)g_hash_table_lookup(table, "sesskey"));
|
|
888
|
16397
|
889 purple_connection_update_progress(gc, "Connected", 3, 4);
|
16395
|
890
|
|
891 session->sesskey = g_strdup(g_hash_table_lookup(table, "sesskey"));
|
|
892
|
|
893 /* Comes with: proof,profileid,userid,uniquenick -- all same values
|
|
894 * (at least for me). */
|
|
895 session->userid = g_strdup(g_hash_table_lookup(table, "userid"));
|
|
896
|
16397
|
897 purple_connection_set_state(gc, PURPLE_CONNECTED);
|
16395
|
898
|
|
899 return 0;
|
|
900 } else if (g_hash_table_lookup(table, "bm")) {
|
|
901 guint bm;
|
|
902
|
|
903 bm = atoi(g_hash_table_lookup(table, "bm"));
|
|
904 switch (bm)
|
|
905 {
|
|
906 case MSIM_BM_STATUS:
|
|
907 return msim_status(session, table);
|
|
908 case MSIM_BM_INSTANT:
|
|
909 return msim_incoming_im(session, table);
|
|
910 default:
|
|
911 /* Not really an IM, but show it for informational
|
|
912 * purposes during development. */
|
|
913 return msim_incoming_im(session, table);
|
|
914 }
|
|
915
|
|
916 if (bm == MSIM_BM_STATUS)
|
|
917 {
|
|
918 return msim_status(session, table);
|
|
919 } else { /* else if strcmp(bm, "1") == 0) */
|
|
920 return msim_incoming_im(session, table);
|
|
921 }
|
|
922 } else if (g_hash_table_lookup(table, "rid")) {
|
|
923 return msim_process_reply(session, table);
|
|
924 } else if (g_hash_table_lookup(table, "error")) {
|
|
925 return msim_error(session, table);
|
|
926 } else if (g_hash_table_lookup(table, "ka")) {
|
16397
|
927 purple_debug_info("msim", "msim_process: got keep alive\n");
|
16395
|
928 return 0;
|
|
929 } else {
|
|
930 printf("<<unhandled>>\n");
|
|
931 return 0;
|
|
932 }
|
|
933 }
|
|
934
|
|
935 /**
|
|
936 * Callback when input available.
|
|
937 *
|
16397
|
938 * @param gc_uncasted A PurpleConnection pointer.
|
16395
|
939 * @param source File descriptor.
|
16397
|
940 * @param cond PURPLE_INPUT_READ
|
16395
|
941 *
|
|
942 * Reads the input, and dispatches calls msim_process to handle it.
|
|
943 */
|
16397
|
944 static void msim_input_cb(gpointer gc_uncasted, gint source, PurpleInputCondition cond)
|
16395
|
945 {
|
16397
|
946 PurpleConnection *gc;
|
|
947 PurpleAccount *account;
|
16395
|
948 MsimSession *session;
|
|
949 gchar *end;
|
|
950 int n;
|
|
951
|
|
952 g_return_if_fail(gc_uncasted != NULL);
|
|
953 g_return_if_fail(source >= 0); /* Note: 0 is a valid fd */
|
|
954
|
16397
|
955 gc = (PurpleConnection*)(gc_uncasted);
|
|
956 account = purple_connection_get_account(gc);
|
16395
|
957 session = gc->proto_data;
|
|
958
|
|
959 g_return_if_fail(MSIM_SESSION_VALID(session));
|
|
960
|
16397
|
961 g_assert(cond == PURPLE_INPUT_READ);
|
16395
|
962
|
|
963 /* Only can handle so much data at once...
|
|
964 * If this happens, try recompiling with a higher MSIM_READ_BUF_SIZE.
|
|
965 * Should be large enough to hold the largest protocol message.
|
|
966 */
|
|
967 if (session->rxoff == MSIM_READ_BUF_SIZE)
|
|
968 {
|
16397
|
969 purple_debug_error("msim", "msim_input_cb: %d-byte read buffer full!\n",
|
16395
|
970 MSIM_READ_BUF_SIZE);
|
16397
|
971 purple_connection_error(gc, "Read buffer full");
|
16395
|
972 /* TODO: fix 100% CPU after closing */
|
|
973 close(source);
|
|
974 return;
|
|
975 }
|
|
976
|
16397
|
977 purple_debug_info("msim", "buffer at %d (max %d), reading up to %d\n",
|
16395
|
978 session->rxoff, MSIM_READ_BUF_SIZE, MSIM_READ_BUF_SIZE - session->rxoff);
|
|
979
|
|
980 /* Read into buffer. On Win32, need recv() not read(). session->fd also holds
|
|
981 * the file descriptor, but it sometimes differs from the 'source' parameter.
|
|
982 */
|
|
983 n = recv(session->fd, session->rxbuf + session->rxoff, MSIM_READ_BUF_SIZE - session->rxoff, 0);
|
|
984
|
|
985 if (n < 0 && errno == EAGAIN)
|
|
986 {
|
|
987 return;
|
|
988 }
|
|
989 else if (n < 0)
|
|
990 {
|
16397
|
991 purple_connection_error(gc, "Read error");
|
|
992 purple_debug_error("msim", "msim_input_cb: read error, ret=%d, "
|
16395
|
993 "error=%s, source=%d, fd=%d (%X))\n",
|
|
994 n, strerror(errno), source, session->fd, session->fd);
|
|
995 close(source);
|
|
996 return;
|
|
997 }
|
|
998 else if (n == 0)
|
|
999 {
|
16397
|
1000 purple_debug_info("msim", "msim_input_cb: server disconnected\n");
|
|
1001 purple_connection_error(gc, "Server has disconnected");
|
16395
|
1002 return;
|
|
1003 }
|
|
1004
|
|
1005 /* Null terminate */
|
|
1006 session->rxbuf[session->rxoff + n] = 0;
|
|
1007
|
|
1008 /* Check for embedded NULs. I don't handle them, and they shouldn't occur. */
|
|
1009 if (strlen(session->rxbuf + session->rxoff) != n)
|
|
1010 {
|
|
1011 /* Occurs after login, but it is not a null byte. */
|
16397
|
1012 purple_debug_info("msim", "msim_input_cb: strlen=%d, but read %d bytes"
|
16395
|
1013 "--null byte encountered?\n", strlen(session->rxbuf + session->rxoff), n);
|
16397
|
1014 //purple_connection_error(gc, "Invalid message - null byte on input");
|
16395
|
1015 return;
|
|
1016 }
|
|
1017
|
|
1018 session->rxoff += n;
|
16397
|
1019 purple_debug_info("msim", "msim_input_cb: read=%d\n", n);
|
16395
|
1020
|
|
1021 //printf("buf=<%s>\n", session->rxbuf);
|
|
1022
|
|
1023 /* Look for \\final\\ end markers. If found, process message. */
|
|
1024 while((end = strstr(session->rxbuf, MSIM_FINAL_STRING)))
|
|
1025 {
|
|
1026 GHashTable *table;
|
|
1027
|
|
1028 //printf("in loop: buf=<%s>\n", session->rxbuf);
|
|
1029 *end = 0;
|
|
1030 table = msim_parse(g_strdup(session->rxbuf));
|
|
1031 if (!table)
|
|
1032 {
|
16397
|
1033 purple_debug_info("msim", "msim_input_cb: couldn't parse <%s>\n", session->rxbuf);
|
|
1034 purple_connection_error(gc, "Unparseable message");
|
16395
|
1035 }
|
|
1036 else
|
|
1037 {
|
|
1038 /* Process message. Returns 0 to free */
|
|
1039 if (msim_process(gc, table) == 0)
|
|
1040 g_hash_table_destroy(table);
|
|
1041 }
|
|
1042
|
|
1043 /* Move remaining part of buffer to beginning. */
|
|
1044 session->rxoff -= strlen(session->rxbuf) + strlen(MSIM_FINAL_STRING);
|
|
1045 memmove(session->rxbuf, end + strlen(MSIM_FINAL_STRING),
|
|
1046 MSIM_READ_BUF_SIZE - (end + strlen(MSIM_FINAL_STRING) - session->rxbuf));
|
|
1047
|
|
1048 /* Clear end of buffer */
|
|
1049 //memset(end, 0, MSIM_READ_BUF_SIZE - (end - session->rxbuf));
|
|
1050 }
|
|
1051 }
|
|
1052
|
|
1053 /**
|
|
1054 * Callback when connected. Sets up input handlers.
|
|
1055 *
|
16397
|
1056 * @param data A PurpleConnection pointer.
|
16395
|
1057 * @param source File descriptor.
|
|
1058 * @param error_message
|
|
1059 */
|
|
1060 static void msim_connect_cb(gpointer data, gint source, const gchar *error_message)
|
|
1061 {
|
16397
|
1062 PurpleConnection *gc;
|
16395
|
1063 MsimSession *session;
|
|
1064
|
|
1065 g_return_if_fail(data != NULL);
|
|
1066
|
16397
|
1067 gc = (PurpleConnection*)data;
|
16395
|
1068 session = gc->proto_data;
|
|
1069
|
|
1070 if (source < 0)
|
|
1071 {
|
16397
|
1072 purple_connection_error(gc, "Couldn't connect to host");
|
|
1073 purple_connection_error(gc, g_strdup_printf("Couldn't connect to host: %s (%d)",
|
16395
|
1074 error_message, source));
|
|
1075 return;
|
|
1076 }
|
|
1077
|
|
1078 session->fd = source;
|
|
1079
|
16397
|
1080 gc->inpa = purple_input_add(source, PURPLE_INPUT_READ, msim_input_cb, gc);
|
16395
|
1081 }
|
|
1082
|
|
1083 /* Session methods */
|
|
1084
|
|
1085 /**
|
|
1086 * Create a new MSIM session.
|
|
1087 *
|
|
1088 * @param acct The account to create the session from.
|
|
1089 *
|
|
1090 * @return Pointer to a new session. Free with msim_session_destroy.
|
|
1091 */
|
16397
|
1092 static MsimSession *msim_session_new(PurpleAccount *acct)
|
16395
|
1093 {
|
|
1094 MsimSession *session;
|
|
1095
|
|
1096 g_return_val_if_fail(acct != NULL, NULL);
|
|
1097
|
|
1098 session = g_new0(MsimSession, 1);
|
|
1099
|
|
1100 session->magic = MSIM_SESSION_STRUCT_MAGIC;
|
|
1101 session->account = acct;
|
16397
|
1102 session->gc = purple_account_get_connection(acct);
|
16395
|
1103 session->fd = -1;
|
|
1104 session->user_lookup_cb = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL); /* do NOT free function pointers! */
|
|
1105 session->user_lookup_cb_data = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
|
|
1106 session->user_lookup_cache = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_hash_table_destroy);
|
|
1107 session->rxoff = 0;
|
|
1108 session->rxbuf = g_new0(gchar, MSIM_READ_BUF_SIZE);
|
|
1109
|
|
1110 return session;
|
|
1111 }
|
|
1112
|
|
1113 /**
|
|
1114 * Free a session.
|
|
1115 *
|
|
1116 * @param session The session to destroy.
|
|
1117 */
|
|
1118 static void msim_session_destroy(MsimSession *session)
|
|
1119 {
|
|
1120 g_return_if_fail(MSIM_SESSION_VALID(session));
|
|
1121
|
|
1122 session->magic = -1;
|
|
1123
|
|
1124 g_free(session->rxbuf);
|
|
1125 g_free(session->userid);
|
|
1126 g_free(session->sesskey);
|
|
1127
|
|
1128 g_free(session);
|
|
1129 }
|
|
1130
|
|
1131 /**
|
|
1132 * Start logging in to the MSIM servers.
|
|
1133 *
|
|
1134 * @param acct Account information to use to login.
|
|
1135 */
|
16397
|
1136 static void msim_login(PurpleAccount *acct)
|
16395
|
1137 {
|
16397
|
1138 PurpleConnection *gc;
|
16395
|
1139 const char *host;
|
|
1140 int port;
|
|
1141
|
|
1142 g_return_if_fail(acct != NULL);
|
|
1143
|
16397
|
1144 purple_debug_info("myspace", "logging in %s\n", acct->username);
|
16395
|
1145
|
16397
|
1146 gc = purple_account_get_connection(acct);
|
16395
|
1147 gc->proto_data = msim_session_new(acct);
|
|
1148
|
|
1149 /* 1. connect to server */
|
16397
|
1150 purple_connection_update_progress(gc, "Connecting",
|
16395
|
1151 0, /* which connection step this is */
|
|
1152 4); /* total number of steps */
|
|
1153
|
|
1154 /* TODO: GUI option to be user-modifiable. */
|
16397
|
1155 host = purple_account_get_string(acct, "server", MSIM_SERVER);
|
|
1156 port = purple_account_get_int(acct, "port", MSIM_PORT);
|
16395
|
1157 /* TODO: connect */
|
16397
|
1158 /* From purple.sf.net/api:
|
16395
|
1159 * """Note that this function name can be misleading--although it is called
|
|
1160 * "proxy connect," it is used for establishing any outgoing TCP connection,
|
|
1161 * whether through a proxy or not.""" */
|
|
1162
|
|
1163 /* Calls msim_connect_cb when connected. */
|
16397
|
1164 if (purple_proxy_connect(gc, acct, host, port, msim_connect_cb, gc) == NULL)
|
16395
|
1165 {
|
|
1166 /* TODO: try other ports if in auto mode, then save
|
|
1167 * working port and try that first next time. */
|
16397
|
1168 purple_connection_error(gc, "Couldn't create socket");
|
16395
|
1169 return;
|
|
1170 }
|
|
1171
|
|
1172 }
|
|
1173
|
|
1174
|
|
1175 /**
|
|
1176 * Close the connection.
|
|
1177 *
|
|
1178 * @param gc The connection.
|
|
1179 */
|
16397
|
1180 static void msim_close(PurpleConnection *gc)
|
16395
|
1181 {
|
|
1182 g_return_if_fail(gc != NULL);
|
|
1183
|
|
1184 msim_session_destroy(gc->proto_data);
|
|
1185 }
|
|
1186
|
|
1187 /**
|
|
1188 * Return the icon name for a buddy and account.
|
|
1189 *
|
|
1190 * @param acct The account to find the icon for.
|
|
1191 * @param buddy The buddy to find the icon for, or NULL for the accoun icon.
|
|
1192 *
|
|
1193 * @return The base icon name string.
|
|
1194 */
|
16397
|
1195 static const gchar *msim_list_icon(PurpleAccount *acct, PurpleBuddy *buddy)
|
16395
|
1196 {
|
|
1197 /* TODO: use a MySpace icon. hbons submitted one to
|
|
1198 * http://developer.pidgin.im/wiki/MySpaceIM - tried placing in
|
16397
|
1199 * C:\cygwin\home\Jeff\purple-2.0.0beta6\gtk\pixmaps\status\default
|
16395
|
1200 * and returning "myspace" but icon shows up blank.
|
|
1201 */
|
|
1202 if (acct == NULL)
|
|
1203 {
|
16397
|
1204 purple_debug_info("msim", "msim_list_icon: acct == NULL!\n");
|
16395
|
1205 //exit(-2);
|
|
1206 }
|
|
1207 return "meanwhile";
|
|
1208 }
|
|
1209
|
|
1210 /**
|
|
1211 * Check if a string is a userid (all numeric).
|
|
1212 *
|
|
1213 * @param user The user id, email, or name.
|
|
1214 *
|
|
1215 * @return TRUE if is userid, FALSE if not.
|
|
1216 */
|
|
1217 static inline gboolean msim_is_userid(const gchar *user)
|
|
1218 {
|
|
1219 g_return_val_if_fail(user != NULL, FALSE);
|
|
1220
|
|
1221 return strspn(user, "0123456789") == strlen(user);
|
|
1222 }
|
|
1223
|
|
1224 /**
|
|
1225 * Check if a string is an email address (contains an @).
|
|
1226 *
|
|
1227 * @param user The user id, email, or name.
|
|
1228 *
|
|
1229 * @return TRUE if is an email, FALSE if not.
|
|
1230 *
|
|
1231 * This function is not intended to be used as a generic
|
|
1232 * means of validating email addresses, but to distinguish
|
|
1233 * between a user represented by an email address from
|
|
1234 * other forms of identification.
|
|
1235 */
|
|
1236 static inline gboolean msim_is_email(const gchar *user)
|
|
1237 {
|
|
1238 g_return_val_if_fail(user != NULL, FALSE);
|
|
1239
|
|
1240 return strchr(user, '@') != NULL;
|
|
1241 }
|
|
1242
|
|
1243
|
|
1244 /**
|
|
1245 * Asynchronously lookup user information, calling callback when receive result.
|
|
1246 *
|
|
1247 * @param session
|
|
1248 * @param user The user id, email address, or username.
|
|
1249 * @param cb Callback, called with user information when available.
|
|
1250 * @param data An arbitray data pointer passed to the callback.
|
|
1251 */
|
|
1252 static void msim_lookup_user(MsimSession *session, const gchar *user, MSIM_USER_LOOKUP_CB cb, gpointer data)
|
|
1253 {
|
|
1254 gchar *field_name;
|
|
1255 gchar *msg_string;
|
|
1256 guint rid, cmd, dsn, lid;
|
|
1257
|
|
1258 g_return_if_fail(MSIM_SESSION_VALID(session));
|
|
1259 g_return_if_fail(user != NULL);
|
|
1260 g_return_if_fail(cb != NULL);
|
|
1261
|
16397
|
1262 purple_debug_info("msim", "msim_lookup_userid", "asynchronously looking up <%s>\n", user);
|
16395
|
1263
|
|
1264 /* TODO: check if this user's info was cached and fresh; if so return immediately */
|
|
1265 #if 0
|
|
1266 /* If already know userid, then call callback immediately */
|
|
1267 cached_userid = g_hash_table_lookup(session->userid_cache, who);
|
|
1268 if (cached_userid && !by_userid)
|
|
1269 {
|
|
1270 cb(cached_userid, NULL, NULL, data);
|
|
1271 return;
|
|
1272 }
|
|
1273 #endif
|
|
1274
|
|
1275 rid = rand(); //om();
|
|
1276
|
|
1277 /* Setup callback. Response will be associated with request using 'rid'. */
|
|
1278 g_hash_table_insert(session->user_lookup_cb, GUINT_TO_POINTER(rid), cb);
|
|
1279 g_hash_table_insert(session->user_lookup_cb_data, GUINT_TO_POINTER(rid), data);
|
|
1280
|
|
1281 /* Send request */
|
|
1282
|
|
1283 cmd = 1;
|
|
1284
|
|
1285 if (msim_is_userid(user))
|
|
1286 {
|
|
1287 /* TODO: document cmd,dsn,lid */
|
|
1288 field_name = "UserID";
|
|
1289 dsn = 4;
|
|
1290 lid = 3;
|
|
1291 } else if (msim_is_email(user)) {
|
|
1292 field_name = "Email";
|
|
1293 dsn = 5;
|
|
1294 lid = 7;
|
|
1295 } else {
|
|
1296 field_name = "UserName";
|
|
1297 dsn = 5;
|
|
1298 lid = 7;
|
|
1299 }
|
|
1300
|
|
1301 msg_string = g_strdup_printf("\\persist\\1\\sesskey\\%s\\cmd\\1\\dsn\\%d\\uid\\%s\\lid\\%d\\rid\\%d\\body\\%s=%s\\final\\",
|
|
1302 session->sesskey, dsn, session->userid, lid, rid, field_name, user);
|
|
1303
|
|
1304 msim_send(session, msg_string);
|
|
1305 }
|
|
1306
|
|
1307 /**
|
|
1308 * Schedule an IM to be sent once the user ID is looked up.
|
|
1309 *
|
|
1310 * @param gc Connection.
|
|
1311 * @param who A user id, email, or username to send the message to.
|
|
1312 * @param message Instant message text to send.
|
|
1313 * @param flags Flags.
|
|
1314 *
|
|
1315 * @return 1 in all cases, even if the message delivery is destined to fail.
|
|
1316 *
|
|
1317 * Allows sending to a user by username, email address, or userid. If
|
|
1318 * a username or email address is given, the userid must be looked up.
|
|
1319 * This function does that by calling msim_lookup_user(), setting up
|
|
1320 * a msim_send_im_by_userid_cb() callback function called when the userid
|
|
1321 * response is received from the server.
|
|
1322 *
|
|
1323 * The callback function calls msim_send_im_by_userid() to send the actual
|
|
1324 * instant message. If a userid is specified directly, this function is called
|
|
1325 * immediately here.
|
|
1326 */
|
16397
|
1327 static int msim_send_im(PurpleConnection *gc, const char *who,
|
|
1328 const char *message, PurpleMessageFlags flags)
|
16395
|
1329 {
|
|
1330 MsimSession *session;
|
|
1331 const char *from_username = gc->account->username;
|
|
1332 send_im_cb_struct *cbinfo;
|
|
1333
|
|
1334 g_return_val_if_fail(gc != NULL, 0);
|
|
1335 g_return_val_if_fail(who != NULL, 0);
|
|
1336 g_return_val_if_fail(message != NULL, 0);
|
|
1337
|
16397
|
1338 purple_debug_info("msim", "sending message from %s to %s: %s\n",
|
16395
|
1339 from_username, who, message);
|
|
1340
|
|
1341 session = gc->proto_data;
|
|
1342
|
|
1343 /* If numeric ID, can send message immediately without userid lookup */
|
|
1344 if (msim_is_userid(who))
|
|
1345 {
|
16397
|
1346 purple_debug_info("msim", "msim_send_im: numeric 'who' detected, sending asap\n");
|
16395
|
1347 msim_send_im_by_userid(session, who, message, flags);
|
|
1348 return 1;
|
|
1349 }
|
|
1350
|
|
1351 /* Otherwise, add callback to IM when userid of destination is available */
|
|
1352
|
|
1353 /* Setup a callback for when the userid is available */
|
|
1354 cbinfo = g_new0(send_im_cb_struct, 1);
|
|
1355 cbinfo->who = g_strdup(who);
|
|
1356 cbinfo->message = g_strdup(message);
|
|
1357 cbinfo->flags = flags;
|
|
1358
|
|
1359 /* Send the request to lookup the userid */
|
|
1360 msim_lookup_user(session, who, msim_send_im_by_userid_cb, cbinfo);
|
|
1361
|
|
1362 /* msim_send_im_by_userid_cb will now be called once userid is looked up */
|
|
1363
|
16397
|
1364 /* Return 1 to have Purple show this IM as being sent, 0 to not. I always
|
16395
|
1365 * return 1 even if the message could not be sent, since I don't know if
|
|
1366 * it has failed yet--because the IM is only sent after the userid is
|
|
1367 * retrieved from the server (which happens after this function returns).
|
|
1368 *
|
|
1369 * TODO: In MySpace, you login with your email address, but don't talk to other
|
|
1370 * users using their email address. So there is currently an asymmetry in the
|
|
1371 * IM windows when using this plugin:
|
|
1372 *
|
|
1373 * you@example.com: hello
|
|
1374 * some_other_user: what's going on?
|
|
1375 * you@example.com: just coding a prpl
|
|
1376 *
|
|
1377 * TODO: Make the sent IM's appear as from the user's username, instead of
|
16397
|
1378 * their email address. Purple uses the login (in MSIM, the email)--change this.
|
16395
|
1379 */
|
|
1380 return 1;
|
|
1381 }
|
|
1382
|
|
1383 /**
|
|
1384 * Obtain the status text for a buddy.
|
|
1385 *
|
|
1386 * @param buddy The buddy to obtain status text for.
|
|
1387 *
|
|
1388 * @return Status text.
|
|
1389 *
|
|
1390 * Currently returns the display name.
|
|
1391 */
|
16397
|
1392 static char *msim_status_text(PurpleBuddy *buddy)
|
16395
|
1393 {
|
|
1394 MsimSession *session;
|
|
1395 GHashTable *userinfo;
|
|
1396 gchar *display_name;
|
|
1397
|
|
1398 g_return_val_if_fail(buddy != NULL, NULL);
|
|
1399
|
|
1400 session = (MsimSession*)buddy->account->gc->proto_data;
|
|
1401 g_assert(MSIM_SESSION_VALID(session));
|
|
1402 g_assert(session->user_lookup_cache != NULL);
|
|
1403
|
|
1404 userinfo = g_hash_table_lookup(session->user_lookup_cache, buddy->name);
|
|
1405 if (!userinfo)
|
|
1406 {
|
|
1407 return g_strdup("");
|
|
1408 }
|
|
1409
|
|
1410 display_name = g_hash_table_lookup(userinfo, "DisplayName");
|
|
1411 g_assert(display_name != NULL);
|
|
1412
|
|
1413 return g_strdup(display_name);
|
|
1414 }
|
|
1415
|
|
1416 /**
|
|
1417 * Obtain the tooltip text for a buddy.
|
|
1418 *
|
|
1419 * @param buddy Buddy to obtain tooltip text on.
|
|
1420 * @param user_info Variable modified to have the tooltip text.
|
|
1421 * @param full TRUE if should obtain full tooltip text.
|
|
1422 *
|
|
1423 */
|
16397
|
1424 static void msim_tooltip_text(PurpleBuddy *buddy, PurpleNotifyUserInfo *user_info, gboolean full)
|
16395
|
1425 {
|
|
1426 g_return_if_fail(buddy != NULL);
|
|
1427 g_return_if_fail(user_info != NULL);
|
|
1428
|
16397
|
1429 if (PURPLE_BUDDY_IS_ONLINE(buddy))
|
16395
|
1430 {
|
|
1431 MsimSession *session;
|
|
1432 GHashTable *userinfo;
|
|
1433
|
|
1434 session = (MsimSession*)buddy->account->gc->proto_data;
|
|
1435
|
|
1436 g_assert(MSIM_SESSION_VALID(session));
|
|
1437 g_assert(session->user_lookup_cache);
|
|
1438
|
|
1439 userinfo = g_hash_table_lookup(session->user_lookup_cache, buddy->name);
|
|
1440
|
|
1441 g_assert(userinfo != NULL);
|
|
1442
|
|
1443 // TODO: if (full), do something different
|
16397
|
1444 purple_notify_user_info_add_pair(user_info, "User ID", g_hash_table_lookup(userinfo, "UserID"));
|
|
1445 purple_notify_user_info_add_pair(user_info, "Display Name", g_hash_table_lookup(userinfo, "DisplayName"));
|
|
1446 purple_notify_user_info_add_pair(user_info, "User Name", g_hash_table_lookup(userinfo, "UserName"));
|
|
1447 purple_notify_user_info_add_pair(user_info, "Total Friends", g_hash_table_lookup(userinfo, "TotalFriends"));
|
|
1448 purple_notify_user_info_add_pair(user_info, "Song",
|
16395
|
1449 g_strdup_printf("%s - %s",
|
|
1450 (gchar*)g_hash_table_lookup(userinfo, "BandName"),
|
|
1451 (gchar*)g_hash_table_lookup(userinfo, "SongName")));
|
|
1452 }
|
|
1453 }
|
|
1454
|
16397
|
1455 /** Callbacks called by Purple, to access this plugin. */
|
|
1456 static PurplePluginProtocolInfo prpl_info =
|
16395
|
1457 {
|
|
1458 OPT_PROTO_MAIL_CHECK,/* options - TODO: myspace will notify of mail */
|
|
1459 NULL, /* user_splits */
|
|
1460 NULL, /* protocol_options */
|
|
1461 NO_BUDDY_ICONS, /* icon_spec - TODO: eventually should add this */
|
|
1462 msim_list_icon, /* list_icon */
|
|
1463 NULL, /* list_emblems */
|
|
1464 msim_status_text, /* status_text */
|
|
1465 msim_tooltip_text, /* tooltip_text */
|
|
1466 msim_status_types, /* status_types */
|
|
1467 NULL, /* blist_node_menu */
|
|
1468 NULL, /* chat_info */
|
|
1469 NULL, /* chat_info_defaults */
|
|
1470 msim_login, /* login */
|
|
1471 msim_close, /* close */
|
|
1472 msim_send_im, /* send_im */
|
|
1473 NULL, /* set_info */
|
|
1474 NULL, /* send_typing */
|
|
1475 NULL, /* get_info */
|
|
1476 NULL, /* set_away */
|
|
1477 NULL, /* set_idle */
|
|
1478 NULL, /* change_passwd */
|
|
1479 NULL, /* add_buddy */
|
|
1480 NULL, /* add_buddies */
|
|
1481 NULL, /* remove_buddy */
|
|
1482 NULL, /* remove_buddies */
|
|
1483 NULL, /* add_permit */
|
|
1484 NULL, /* add_deny */
|
|
1485 NULL, /* rem_permit */
|
|
1486 NULL, /* rem_deny */
|
|
1487 NULL, /* set_permit_deny */
|
|
1488 NULL, /* join_chat */
|
|
1489 NULL, /* reject chat invite */
|
|
1490 NULL, /* get_chat_name */
|
|
1491 NULL, /* chat_invite */
|
|
1492 NULL, /* chat_leave */
|
|
1493 NULL, /* chat_whisper */
|
|
1494 NULL, /* chat_send */
|
|
1495 NULL, /* keepalive */
|
|
1496 NULL, /* register_user */
|
|
1497 NULL, /* get_cb_info */
|
|
1498 NULL, /* get_cb_away */
|
|
1499 NULL, /* alias_buddy */
|
|
1500 NULL, /* group_buddy */
|
|
1501 NULL, /* rename_group */
|
|
1502 NULL, /* buddy_free */
|
|
1503 NULL, /* convo_closed */
|
|
1504 NULL, /* normalize */
|
|
1505 NULL, /* set_buddy_icon */
|
|
1506 NULL, /* remove_group */
|
|
1507 NULL, /* get_cb_real_name */
|
|
1508 NULL, /* set_chat_topic */
|
|
1509 NULL, /* find_blist_chat */
|
|
1510 NULL, /* roomlist_get_list */
|
|
1511 NULL, /* roomlist_cancel */
|
|
1512 NULL, /* roomlist_expand_category */
|
|
1513 NULL, /* can_receive_file */
|
|
1514 NULL, /* send_file */
|
|
1515 NULL, /* new_xfer */
|
|
1516 NULL, /* offline_message */
|
|
1517 NULL, /* whiteboard_prpl_ops */
|
|
1518 NULL, /* send_raw */
|
|
1519 NULL /* roomlist_room_serialize */
|
|
1520 };
|
|
1521
|
|
1522
|
|
1523
|
|
1524 /** Based on MSN's plugin info comments. */
|
16397
|
1525 static PurplePluginInfo info =
|
16395
|
1526 {
|
16397
|
1527 PURPLE_PLUGIN_MAGIC,
|
|
1528 PURPLE_MAJOR_VERSION,
|
|
1529 PURPLE_MINOR_VERSION,
|
|
1530 PURPLE_PLUGIN_PROTOCOL, /**< type */
|
16395
|
1531 NULL, /**< ui_requirement */
|
|
1532 0, /**< flags */
|
|
1533 NULL, /**< dependencies */
|
16397
|
1534 PURPLE_PRIORITY_DEFAULT, /**< priority */
|
16395
|
1535
|
|
1536 "prpl-myspace", /**< id */
|
|
1537 "MySpaceIM", /**< name */
|
|
1538 "0.4", /**< version */
|
|
1539 /** summary */
|
|
1540 "MySpaceIM Protocol Plugin",
|
|
1541 /** description */
|
|
1542 "MySpaceIM Protocol Plugin",
|
|
1543 "Jeff Connelly <myspaceim@xyzzy.cjb.net>", /**< author */
|
|
1544 "http://developer.pidgin.im/wiki/MySpaceIM/", /**< homepage */
|
|
1545
|
|
1546 NULL, /**< load */
|
|
1547 NULL, /**< unload */
|
|
1548 NULL, /**< destroy */
|
|
1549 NULL, /**< ui_info */
|
|
1550 &prpl_info, /**< extra_info */
|
|
1551 NULL, /**< prefs_info */
|
|
1552
|
|
1553 /* msim_actions */
|
|
1554 NULL
|
|
1555 };
|
|
1556
|
|
1557
|
16397
|
1558 PURPLE_INIT_PLUGIN(myspace, init_plugin, info);
|