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