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