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