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