Mon, 30 Apr 2007 04:34:04 +0000
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).
| 16322 | 1 | /* MySpaceIM Protocol Plugin |
| 2 | * | |
| 3 | * \author Jeff Connelly | |
| 4 | * | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
5 | * Copyright (C) 2007, Jeff Connelly <jeff2@homing.pidgin.im> |
| 16322 | 6 | * |
| 16324 | 7 | * Based on Purple's "C Plugin HOWTO" hello world example. |
| 16322 | 8 | * |
| 9 | * Code also drawn from myspace: | |
| 16324 | 10 | * http://snarfed.org/space/purple+mock+protocol+plugin |
| 16322 | 11 | * Copyright (C) 2004-2007, Ryan Barrett <mockprpl@ryanb.org> |
| 12 | * | |
| 16324 | 13 | * and some constructs also based on existing Purple plugins, which are: |
| 14 | * Copyright (C) 2003, Robbert Haarman <purple@inglorion.net> | |
| 16322 | 15 | * Copyright (C) 2003, Ethan Blanton <eblanton@cs.purdue.edu> |
| 16 | * Copyright (C) 2000-2003, Rob Flynn <rob@tgflinux.com> | |
| 17 | * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
| 18 | * | |
| 19 | * This program is free software; you can redistribute it and/or modify | |
| 20 | * it under the terms of the GNU General Public License as published by | |
| 21 | * the Free Software Foundation; either version 2 of the License, or | |
| 22 | * (at your option) any later version. | |
| 23 | * | |
| 24 | * This program is distributed in the hope that it will be useful, | |
| 25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 27 | * GNU General Public License for more details. | |
| 28 | * | |
| 29 | * You should have received a copy of the GNU General Public License | |
| 30 | * along with this program; if not, write to the Free Software | |
| 31 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 32 | */ | |
| 33 | ||
| 16324 | 34 | #define PURPLE_PLUGIN |
| 16322 | 35 | |
| 36 | #include <string.h> | |
| 37 | #include <errno.h> /* for EAGAIN */ | |
| 38 | ||
| 39 | #include <glib.h> | |
| 40 | ||
| 41 | #ifdef _WIN32 | |
| 42 | #include "win32dep.h" | |
| 43 | #else | |
| 44 | /* For recv() and send(); needed to match Win32 */ | |
| 45 | #include <sys/types.h> | |
| 46 | #include <sys/socket.h> | |
| 47 | #endif | |
| 48 | ||
| 16324 | 49 | #include "internal.h" |
| 50 | ||
| 16322 | 51 | #include "notify.h" |
| 52 | #include "plugin.h" | |
| 53 | #include "version.h" | |
| 54 | #include "cipher.h" /* for SHA-1 */ | |
| 55 | #include "util.h" /* for base64 */ | |
| 16324 | 56 | #include "debug.h" /* for purple_debug_info */ |
| 57 | ||
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
58 | #include "myspace.h" |
| 16322 | 59 | |
| 16324 | 60 | static void init_plugin(PurplePlugin *plugin) |
| 16322 | 61 | { |
| 16324 | 62 | purple_notify_message(plugin, PURPLE_NOTIFY_MSG_INFO, "Hello World!", |
| 16322 | 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 | */ | |
| 16324 | 71 | static GList *msim_status_types(PurpleAccount *acct) |
| 16322 | 72 | { |
| 73 | GList *types; | |
| 16324 | 74 | PurpleStatusType *type; |
| 16322 | 75 | |
| 16324 | 76 | purple_debug_info("myspace", "returning status types for %s: %s, %s, %s\n", |
| 16322 | 77 | acct->username, |
| 78 | MSIM_STATUS_ONLINE, MSIM_STATUS_AWAY, MSIM_STATUS_OFFLINE, MSIM_STATUS_INVISIBLE); | |
| 79 | ||
| 80 | ||
| 81 | types = NULL; | |
| 82 | ||
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
83 | /* TODO: Clean up - I don't like all this repetition */ |
| 16324 | 84 | type = purple_status_type_new(PURPLE_STATUS_AVAILABLE, MSIM_STATUS_ONLINE, |
| 16322 | 85 | MSIM_STATUS_ONLINE, TRUE); |
| 16324 | 86 | purple_status_type_add_attr(type, "message", "Online", |
| 87 | purple_value_new(PURPLE_TYPE_STRING)); | |
| 16322 | 88 | types = g_list_append(types, type); |
| 89 | ||
| 16324 | 90 | type = purple_status_type_new(PURPLE_STATUS_AWAY, MSIM_STATUS_AWAY, |
| 16322 | 91 | MSIM_STATUS_AWAY, TRUE); |
| 16324 | 92 | purple_status_type_add_attr(type, "message", "Away", |
| 93 | purple_value_new(PURPLE_TYPE_STRING)); | |
| 16322 | 94 | types = g_list_append(types, type); |
| 95 | ||
| 16324 | 96 | type = purple_status_type_new(PURPLE_STATUS_OFFLINE, MSIM_STATUS_OFFLINE, |
| 16322 | 97 | MSIM_STATUS_OFFLINE, TRUE); |
| 16324 | 98 | purple_status_type_add_attr(type, "message", "Offline", |
| 99 | purple_value_new(PURPLE_TYPE_STRING)); | |
| 16322 | 100 | types = g_list_append(types, type); |
| 101 | ||
| 16324 | 102 | type = purple_status_type_new(PURPLE_STATUS_INVISIBLE, MSIM_STATUS_INVISIBLE, |
| 16322 | 103 | MSIM_STATUS_INVISIBLE, TRUE); |
| 16324 | 104 | purple_status_type_add_attr(type, "message", "Invisible", |
| 105 | purple_value_new(PURPLE_TYPE_STRING)); | |
| 16322 | 106 | types = g_list_append(types, type); |
| 107 | ||
| 108 | return types; | |
| 109 | } | |
| 110 | ||
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
111 | /** |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
112 | * Return the icon name for a buddy and account. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
113 | * |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
114 | * @param acct The account to find the icon for, or NULL for protocol icon. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
115 | * @param buddy The buddy to find the icon for, or NULL for the account icon. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
116 | * |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
117 | * @return The base icon name string. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
118 | */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
119 | static const gchar *msim_list_icon(PurpleAccount *acct, PurpleBuddy *buddy) |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
120 | { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
121 | /* TODO: use a MySpace icon. hbons submitted one to |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
122 | * http://developer.pidgin.im/wiki/MySpaceIM - tried placing in |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
123 | * C:\cygwin\home\Jeff\purple-2.0.0beta6\gtk\pixmaps\status\default |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
124 | * and returning "myspace" but icon shows up blank. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
125 | */ |
|
16334
ff6947265141
Add and use MySpaceIM icon uploaded by hbons (48x48 original and resized 22x22 and 16x16 versions).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16333
diff
changeset
|
126 | return "myspace"; |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
127 | } |
|
16337
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
128 | |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
129 | /** |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
130 | * Unescape a protocol message. |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
131 | * |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
132 | * @param msg The message to unescape. |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
133 | * |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
134 | * @return The unescaped message. Caller must g_free(). |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
135 | * |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
136 | * Messages should be unescaped after being received, as part of |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
137 | * the parsing process. |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
138 | */ |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
139 | static gchar *msim_unescape(const gchar *msg) |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
140 | { |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
141 | /* TODO: make more elegant, refactor with msim_escape */ |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
142 | gchar *tmp, *ret; |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
143 | |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
144 | tmp = str_replace(msg, "/1", "/"); |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
145 | ret = str_replace(tmp, "/2", "\\"); |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
146 | g_free(tmp); |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
147 | return ret; |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
148 | } |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
149 | |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
150 | /** |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
151 | * Escape a protocol message. |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
152 | * |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
153 | * @param msg The message to escape. |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
154 | * |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
155 | * @return The escaped message. Caller must g_free(). |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
156 | * |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
157 | * Messages should be escaped before sending. |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
158 | */ |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
159 | static gchar *msim_escape(const gchar *msg) |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
160 | { |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
161 | /* TODO: make more elegant, refactor with msim_unescape */ |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
162 | gchar *tmp, *ret; |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
163 | |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
164 | tmp = str_replace(msg, "/", "/1"); |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
165 | ret = str_replace(tmp, "\\", "/2"); |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
166 | g_free(tmp); |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
167 | |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
168 | return ret; |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
169 | } |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
170 | |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
171 | /** |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
172 | * Replace 'old' with 'new' in 'str'. |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
173 | * |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
174 | * @param str The original string. |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
175 | * @param old The substring of 'str' to replace. |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
176 | * @param new The replacement for 'old' within 'str'. |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
177 | * |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
178 | * @return A _new_ string, based on 'str', with 'old' replaced |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
179 | * by 'new'. Must be g_free()'d by caller. |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
180 | * |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
181 | * This string replace method is based on |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
182 | * http://mail.gnome.org/archives/gtk-app-devel-list/2000-July/msg00201.html |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
183 | * |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
184 | */ |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
185 | static gchar *str_replace(const gchar* str, const gchar *old, const gchar *new) |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
186 | { |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
187 | char **items; |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
188 | char *ret; |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
189 | |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
190 | items = g_strsplit(str, old, -1); |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
191 | ret = g_strjoinv(new, items); |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
192 | g_free(items); |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
193 | return ret; |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
194 | } |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
195 | |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
196 | |
| 16322 | 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 | ||
| 16324 | 216 | purple_debug_info("msim", "msim_parse: got <%s>\n", msg); |
| 16322 | 217 | |
| 218 | key = NULL; | |
| 219 | ||
| 220 | /* All messages begin with a \ */ | |
| 221 | if (msg[0] != '\\' || msg[1] == 0) | |
| 222 | { | |
| 16324 | 223 | purple_debug_info("msim", "msim_parse: incomplete/bad msg, " |
|
16332
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
224 | "missing initial backslash: <%s>\n", msg); |
| 16322 | 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 | { | |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
239 | #if MSIM_DEBUG_PARSE |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
240 | purple_debug_info("msim", "tok=<%s>, i%2=%d\n", token, i % 2); |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
241 | #endif |
| 16322 | 242 | if (i % 2) |
| 243 | { | |
|
16337
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
244 | /* Odd-numbered ordinal is a value */ |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
245 | |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
246 | /* Note: returns a new string */ |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
247 | value = msim_unescape(token); |
| 16322 | 248 | |
| 249 | /* Check if key already exists */ | |
| 250 | if (g_hash_table_lookup(table, key) == NULL) | |
| 251 | { | |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
252 | #if MSIM_DEBUG_PARSE |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
253 | purple_debug_info("msim", "insert: |%s|=|%s|\n", key, value); |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
254 | #endif |
|
16337
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
255 | /* Insert - strdup 'key' because it will be g_strfreev'd (as 'tokens'), |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
256 | * but do not strdup 'value' because it was newly allocated by |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
257 | * msim_unescape(). |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
258 | */ |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
259 | g_hash_table_insert(table, g_strdup(key), value); |
| 16322 | 260 | } else { |
| 261 | /* TODO: Some dictionaries have multiple values for the same | |
| 262 | * key. Should append to a GList to handle this case. */ | |
| 16324 | 263 | purple_debug_info("msim", "msim_parse: key %s already exists, " |
|
16332
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
264 | "not overwriting or replacing; ignoring new value %s\n", key, |
| 16322 | 265 | value); |
| 266 | } | |
| 267 | } else { | |
|
16337
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
268 | /* Even numbered index is a key name */ |
| 16322 | 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 | /** | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
281 | * Parse a \x1c-separated "dictionary" of key=value pairs into a hash table. |
| 16322 | 282 | * |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
283 | * @param body_str The text of the dictionary to parse. Often the |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
284 | * value for the 'body' field. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
285 | * |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
286 | * @return Hash table of the keys and values. Must g_hash_table_destroy() when done. |
| 16322 | 287 | */ |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
288 | static GHashTable *msim_parse_body(const gchar *body_str) |
| 16322 | 289 | { |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
290 | GHashTable *table; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
291 | gchar *item; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
292 | gchar **items; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
293 | gchar **elements; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
294 | guint i; |
| 16322 | 295 | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
296 | g_return_val_if_fail(body_str != NULL, NULL); |
|
16332
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
297 | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
298 | table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
299 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
300 | for (items = g_strsplit(body_str, "\x1c", 0), i = 0; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
301 | (item = items[i]); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
302 | i++) |
| 16322 | 303 | { |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
304 | gchar *key, *value; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
305 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
306 | elements = g_strsplit(item, "=", 2); |
| 16322 | 307 | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
308 | key = elements[0]; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
309 | if (!key) |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
310 | { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
311 | purple_debug_info("msim", "msim_parse_body(%s): null key\n", |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
312 | body_str); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
313 | g_strfreev(elements); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
314 | break; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
315 | } |
| 16322 | 316 | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
317 | value = elements[1]; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
318 | if (!value) |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
319 | { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
320 | purple_debug_info("msim", "msim_parse_body(%s): null value\n", |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
321 | body_str); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
322 | g_strfreev(elements); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
323 | break; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
324 | } |
| 16322 | 325 | |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
326 | #if MSIM_DEBUG_PARSE |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
327 | purple_debug_info("msim", "-- %s: %s\n", key, value); |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
328 | #endif |
| 16322 | 329 | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
330 | /* XXX: This overwrites duplicates. */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
331 | /* TODO: make the GHashTable values be GList's, and append to the list if |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
332 | * there is already a value of the same key name. This is important for |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
333 | * the WebChallenge message. */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
334 | g_hash_table_insert(table, g_strdup(key), g_strdup(value)); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
335 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
336 | g_strfreev(elements); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
337 | } |
|
16328
5142c7747d06
Use Purple Cipher API for RC4.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16324
diff
changeset
|
338 | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
339 | g_strfreev(items); |
| 16322 | 340 | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
341 | return table; |
| 16322 | 342 | } |
| 343 | ||
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
344 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
345 | |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
346 | #if MSIM_DEBUG_MSG |
| 16322 | 347 | static void print_hash_item(gpointer key, gpointer value, gpointer user_data) |
| 348 | { | |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
349 | purple_debug_info("msim", "%s=%s\n", (char*)key, (char*)value); |
| 16322 | 350 | } |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
351 | #endif |
| 16322 | 352 | |
| 353 | /** | |
| 354 | * Send an arbitrary protocol message. | |
| 355 | * | |
| 356 | * @param session | |
|
16337
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
357 | * @param msg The textual, packed message to send. |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
358 | * |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
359 | * @return TRUE if succeeded, FALSE if not. |
| 16322 | 360 | * |
| 361 | * Note: this does not send instant messages. For that, see msim_send_im. | |
| 362 | */ | |
|
16337
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
363 | static gboolean msim_send(MsimSession *session, const gchar *msg) |
| 16322 | 364 | { |
|
16337
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
365 | int total_bytes_sent, total_bytes; |
| 16322 | 366 | |
|
16337
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
367 | g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
368 | g_return_val_if_fail(msg != NULL, FALSE); |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
369 | |
| 16324 | 370 | purple_debug_info("msim", "msim_send: writing <%s>\n", msg); |
| 16322 | 371 | |
|
16337
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
372 | /* Loop until all data is sent, or a failure occurs. */ |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
373 | total_bytes_sent = 0; |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
374 | total_bytes = strlen(msg); |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
375 | do |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
376 | { |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
377 | int bytes_sent; |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
378 | |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
379 | bytes_sent = send(session->fd, msg + total_bytes_sent, |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
380 | total_bytes - total_bytes_sent, 0); |
| 16322 | 381 | |
|
16337
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
382 | if (bytes_sent < 0) |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
383 | { |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
384 | purple_debug_info("msim", "msim_send(%s): send() failed: %s\n", |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
385 | msg, g_strerror(errno)); |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
386 | return FALSE; |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
387 | } |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
388 | total_bytes_sent += bytes_sent; |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
389 | |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
390 | } while(total_bytes_sent < total_bytes); |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
391 | return TRUE; |
| 16322 | 392 | } |
| 393 | ||
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
394 | /** |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
395 | * Start logging in to the MSIM servers. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
396 | * |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
397 | * @param acct Account information to use to login. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
398 | */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
399 | static void msim_login(PurpleAccount *acct) |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
400 | { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
401 | PurpleConnection *gc; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
402 | const char *host; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
403 | int port; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
404 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
405 | g_return_if_fail(acct != NULL); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
406 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
407 | purple_debug_info("myspace", "logging in %s\n", acct->username); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
408 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
409 | gc = purple_account_get_connection(acct); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
410 | gc->proto_data = msim_session_new(acct); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
411 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
412 | /* 1. connect to server */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
413 | purple_connection_update_progress(gc, "Connecting", |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
414 | 0, /* which connection step this is */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
415 | 4); /* total number of steps */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
416 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
417 | /* TODO: GUI option to be user-modifiable. */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
418 | host = purple_account_get_string(acct, "server", MSIM_SERVER); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
419 | port = purple_account_get_int(acct, "port", MSIM_PORT); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
420 | /* TODO: connect */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
421 | /* From purple.sf.net/api: |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
422 | * """Note that this function name can be misleading--although it is called |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
423 | * "proxy connect," it is used for establishing any outgoing TCP connection, |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
424 | * whether through a proxy or not.""" */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
425 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
426 | /* Calls msim_connect_cb when connected. */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
427 | if (purple_proxy_connect(gc, acct, host, port, msim_connect_cb, gc) == NULL) |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
428 | { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
429 | /* TODO: try other ports if in auto mode, then save |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
430 | * working port and try that first next time. */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
431 | purple_connection_error(gc, "Couldn't create socket"); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
432 | return; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
433 | } |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
434 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
435 | } |
| 16322 | 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 | { | |
| 16324 | 446 | PurpleAccount *account; |
| 16322 | 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 | ||
| 16324 | 461 | purple_connection_update_progress(session->gc, "Reading challenge", 1, 4); |
| 16322 | 462 | |
| 16324 | 463 | purple_debug_info("msim", "nc=<%s>\n", nc_str); |
| 16322 | 464 | |
| 16324 | 465 | nc = (guchar*)purple_base64_decode(nc_str, &nc_len); |
| 466 | purple_debug_info("msim", "base64 decoded to %d bytes\n", nc_len); | |
| 16322 | 467 | if (nc_len != 0x40) |
| 468 | { | |
| 16324 | 469 | purple_debug_info("msim", "bad nc length: %x != 0x40\n", nc_len); |
| 470 | purple_connection_error(session->gc, "Unexpected challenge length from server"); | |
| 16322 | 471 | return 0; |
| 472 | } | |
| 473 | ||
| 16324 | 474 | purple_connection_update_progress(session->gc, "Logging in", 2, 4); |
| 16322 | 475 | |
| 476 | response_str = msim_compute_login_response(nc, account->username, account->password); | |
| 477 | ||
| 478 | g_free(nc); | |
| 479 | ||
| 480 | /* Reply */ | |
|
16337
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
481 | /* TODO: escape values. A response_str with a / in it (\ is not in base64) will |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
482 | * cause a login failure! / must be encoded as /1. */ |
| 16322 | 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 | ||
| 16324 | 488 | purple_debug_info("msim", "response=<%s>\n", buf); |
| 16322 | 489 | |
| 490 | msim_send(session, buf); | |
| 491 | ||
| 492 | g_free(buf); | |
| 493 | ||
| 494 | return 0; | |
| 495 | } | |
| 496 | ||
| 497 | /** | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
498 | * Compute the base64'd login challenge response based on username, password, nonce, and IPs. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
499 | * |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
500 | * @param nonce The base64 encoded nonce ('nc') field from the server. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
501 | * @param email User's email address (used as login name). |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
502 | * @param password User's cleartext password. |
| 16322 | 503 | * |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
504 | * @return Encoded login challenge response, ready to send to the server. Must be g_free()'d |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
505 | * when finished. |
| 16322 | 506 | */ |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
507 | static gchar* msim_compute_login_response(guchar nonce[2*NONCE_SIZE], |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
508 | gchar* email, gchar* password) |
| 16322 | 509 | { |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
510 | PurpleCipherContext *key_context; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
511 | PurpleCipher *sha1; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
512 | PurpleCipherContext *rc4; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
513 | guchar hash_pw[HASH_SIZE]; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
514 | guchar key[HASH_SIZE]; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
515 | gchar* password_utf16le; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
516 | guchar* data; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
517 | guchar* data_out; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
518 | gchar* response; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
519 | size_t data_len, data_out_len; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
520 | gsize conv_bytes_read, conv_bytes_written; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
521 | GError* conv_error; |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
522 | #if MSIM_DEBUG_LOGIN_CHALLENGE |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
523 | int i; |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
524 | #endif |
| 16322 | 525 | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
526 | //memset(nonce, 0, NONCE_SIZE); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
527 | //memset(nonce + NONCE_SIZE, 1, NONCE_SIZE); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
528 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
529 | /* Convert ASCII password to UTF16 little endian */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
530 | purple_debug_info("msim", "converting password to UTF-16LE\n"); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
531 | conv_error = NULL; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
532 | password_utf16le = g_convert(password, -1, "UTF-16LE", "UTF-8", |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
533 | &conv_bytes_read, &conv_bytes_written, &conv_error); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
534 | g_assert(conv_bytes_read == strlen(password)); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
535 | if (conv_error != NULL) |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
536 | { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
537 | purple_debug_error("msim", |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
538 | "g_convert password UTF8->UTF16LE failed: %s", |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
539 | conv_error->message); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
540 | g_error_free(conv_error); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
541 | } |
| 16322 | 542 | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
543 | /* Compute password hash */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
544 | purple_cipher_digest_region("sha1", (guchar*)password_utf16le, |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
545 | conv_bytes_written, sizeof(hash_pw), hash_pw, NULL); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
546 | g_free(password_utf16le); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
547 | |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
548 | #if MSIM_DEBUG_LOGIN_CHALLENGE |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
549 | purple_debug_info("msim", "pwhash = "); |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
550 | for (i = 0; i < sizeof(hash_pw); i++) |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
551 | purple_debug_info("msim", "%.2x ", hash_pw[i]); |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
552 | purple_debug_info("msim", "\n"); |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
553 | #endif |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
554 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
555 | /* key = sha1(sha1(pw) + nonce2) */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
556 | sha1 = purple_ciphers_find_cipher("sha1"); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
557 | key_context = purple_cipher_context_new(sha1, NULL); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
558 | purple_cipher_context_append(key_context, hash_pw, HASH_SIZE); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
559 | purple_cipher_context_append(key_context, nonce + NONCE_SIZE, NONCE_SIZE); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
560 | purple_cipher_context_digest(key_context, sizeof(key), key, NULL); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
561 | |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
562 | #if MSIM_DEBUG_LOGIN_CHALLENGE |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
563 | purple_debug_info("msim", "key = "); |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
564 | for (i = 0; i < sizeof(key); i++) |
| 16322 | 565 | { |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
566 | purple_debug_info("msim", "%.2x ", key[i]); |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
567 | } |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
568 | purple_debug_info("msim", "\n"); |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
569 | #endif |
| 16322 | 570 | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
571 | rc4 = purple_cipher_context_new_by_name("rc4", NULL); |
| 16322 | 572 | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
573 | /* Note: 'key' variable is 0x14 bytes (from SHA-1 hash), |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
574 | * but only first 0x10 used for the RC4 key. */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
575 | purple_cipher_context_set_option(rc4, "key_len", (gpointer)0x10); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
576 | purple_cipher_context_set_key(rc4, key); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
577 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
578 | /* TODO: obtain IPs of network interfaces. This is not immediately |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
579 | * important because you can still connect and perform basic |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
580 | * functions of the protocol. There is also a high chance that the addreses |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
581 | * are RFC1918 private, so the servers couldn't do anything with them |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
582 | * anyways except make note of that fact. Probably important for any |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
583 | * kind of direct connection, or file transfer functionality. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
584 | */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
585 | /* rc4 encrypt: |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
586 | * nonce1+email+IP list */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
587 | data_len = NONCE_SIZE + strlen(email) + 25; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
588 | data = g_new0(guchar, data_len); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
589 | memcpy(data, nonce, NONCE_SIZE); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
590 | memcpy(data + NONCE_SIZE, email, strlen(email)); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
591 | memcpy(data + NONCE_SIZE + strlen(email), |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
592 | /* IP addresses of network interfaces */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
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); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
594 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
595 | data_out = g_new0(guchar, data_len); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
596 | purple_cipher_context_encrypt(rc4, (const guchar*)data, |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
597 | data_len, data_out, &data_out_len); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
598 | g_assert(data_out_len == data_len); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
599 | purple_cipher_context_destroy(rc4); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
600 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
601 | response = purple_base64_encode(data_out, data_out_len); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
602 | g_free(data_out); |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
603 | #if MSIM_DEBUG_LOGIN_CHALLENGE |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
604 | purple_debug_info("msim", "response=<%s>\n", response); |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
605 | #endif |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
606 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
607 | return response; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
608 | } |
| 16322 | 609 | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
610 | /** |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
611 | * Schedule an IM to be sent once the user ID is looked up. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
612 | * |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
613 | * @param gc Connection. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
614 | * @param who A user id, email, or username to send the message to. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
615 | * @param message Instant message text to send. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
616 | * @param flags Flags. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
617 | * |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
618 | * @return 1 in all cases, even if the message delivery is destined to fail. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
619 | * |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
620 | * Allows sending to a user by username, email address, or userid. If |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
621 | * a username or email address is given, the userid must be looked up. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
622 | * This function does that by calling msim_lookup_user(), setting up |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
623 | * a msim_send_im_by_userid_cb() callback function called when the userid |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
624 | * response is received from the server. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
625 | * |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
626 | * The callback function calls msim_send_im_by_userid() to send the actual |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
627 | * instant message. If a userid is specified directly, this function is called |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
628 | * immediately here. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
629 | */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
630 | static int msim_send_im(PurpleConnection *gc, const char *who, |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
631 | const char *message, PurpleMessageFlags flags) |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
632 | { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
633 | MsimSession *session; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
634 | const char *from_username = gc->account->username; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
635 | send_im_cb_struct *cbinfo; |
| 16322 | 636 | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
637 | g_return_val_if_fail(gc != NULL, 0); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
638 | g_return_val_if_fail(who != NULL, 0); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
639 | g_return_val_if_fail(message != NULL, 0); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
640 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
641 | purple_debug_info("msim", "sending message from %s to %s: %s\n", |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
642 | from_username, who, message); |
| 16322 | 643 | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
644 | session = gc->proto_data; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
645 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
646 | /* If numeric ID, can send message immediately without userid lookup */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
647 | if (msim_is_userid(who)) |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
648 | { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
649 | purple_debug_info("msim", |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
650 | "msim_send_im: numeric 'who' detected, sending asap\n"); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
651 | msim_send_im_by_userid(session, who, message, flags); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
652 | return 1; |
| 16322 | 653 | } |
| 654 | ||
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
655 | /* Otherwise, add callback to IM when userid of destination is available */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
656 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
657 | /* Setup a callback for when the userid is available */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
658 | cbinfo = g_new0(send_im_cb_struct, 1); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
659 | cbinfo->who = g_strdup(who); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
660 | cbinfo->message = g_strdup(message); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
661 | cbinfo->flags = flags; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
662 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
663 | /* Send the request to lookup the userid */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
664 | msim_lookup_user(session, who, msim_send_im_by_userid_cb, cbinfo); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
665 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
666 | /* msim_send_im_by_userid_cb will now be called once userid is looked up */ |
| 16322 | 667 | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
668 | /* Return 1 to have Purple show this IM as being sent, 0 to not. I always |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
669 | * return 1 even if the message could not be sent, since I don't know if |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
670 | * it has failed yet--because the IM is only sent after the userid is |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
671 | * retrieved from the server (which happens after this function returns). |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
672 | * |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
673 | * TODO: In MySpace, you login with your email address, but don't talk to other |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
674 | * users using their email address. So there is currently an asymmetry in the |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
675 | * IM windows when using this plugin: |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
676 | * |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
677 | * you@example.com: hello |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
678 | * some_other_user: what's going on? |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
679 | * you@example.com: just coding a prpl |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
680 | * |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
681 | * TODO: Make the sent IM's appear as from the user's username, instead of |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
682 | * their email address. Purple uses the login (in MSIM, the email)--change this. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
683 | */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
684 | return 1; |
| 16322 | 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. | |
| 16324 | 693 | * @param flags Purple instant message flags. |
| 16322 | 694 | * |
| 695 | * @return 0, since the 'table' parameter is no longer needed. | |
| 696 | * | |
| 697 | */ | |
| 16324 | 698 | static int msim_send_im_by_userid(MsimSession *session, const gchar *userid, const gchar *message, PurpleMessageFlags flags) |
| 16322 | 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 | ||
|
16337
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
707 | /* XXX: delete after escape each value */ |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
708 | message = msim_escape(message); |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
709 | |
| 16322 | 710 | /* TODO: constants for bm types */ |
|
16337
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
711 | /* TODO: escape values */ |
| 16322 | 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 | ||
|
16337
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
715 | /* XXX: delete after escape each value */ |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
716 | g_free(message); |
|
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
717 | |
| 16324 | 718 | purple_debug_info("msim", "going to write: %s\n", msg_string); |
| 16322 | 719 | |
| 720 | msim_send(session, msg_string); | |
| 721 | ||
| 16324 | 722 | /* TODO: notify Purple that we sent the IM. */ |
| 16322 | 723 | |
| 724 | /* Not needed since sending messages to yourself is allowed by MSIM! */ | |
| 725 | /*if (strcmp(from_username, who) == 0) | |
| 16324 | 726 | serv_got_im(gc, from_username, message, PURPLE_MESSAGE_RECV, time(NULL)); |
| 16322 | 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 | /** | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
767 | * Callback to handle incoming messages, after resolving userid. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
768 | * |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
769 | * @param session |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
770 | * @param userinfo Message from server on user's info, containing UserName. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
771 | * @param data A gchar* of the incoming instant message's text. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
772 | */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
773 | static void msim_incoming_im_cb(MsimSession *session, GHashTable *userinfo, gpointer data) |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
774 | { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
775 | gchar *msg; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
776 | gchar *username; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
777 | GHashTable *body; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
778 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
779 | g_return_if_fail(MSIM_SESSION_VALID(session)); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
780 | g_return_if_fail(userinfo != NULL); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
781 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
782 | body = msim_parse_body(g_hash_table_lookup(userinfo, "body")); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
783 | g_assert(body != NULL); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
784 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
785 | username = g_hash_table_lookup(body, "UserName"); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
786 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
787 | msg = (gchar*)data; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
788 | serv_got_im(session->gc, username, msg, PURPLE_MESSAGE_RECV, time(NULL)); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
789 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
790 | g_hash_table_destroy(body); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
791 | g_hash_table_destroy(userinfo); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
792 | } |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
793 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
794 | /** |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
795 | * Handle an incoming message. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
796 | * |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
797 | * @param session The session |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
798 | * @param table Message from the server, containing 'f' (userid from) and 'msg'. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
799 | * |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
800 | * @return 0, since table can be freed. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
801 | */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
802 | static int msim_incoming_im(MsimSession *session, GHashTable *table) |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
803 | { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
804 | gchar *userid; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
805 | gchar *msg; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
806 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
807 | g_return_val_if_fail(MSIM_SESSION_VALID(session), 0); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
808 | g_return_val_if_fail(table != NULL, 0); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
809 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
810 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
811 | userid = g_hash_table_lookup(table, "f"); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
812 | msg = g_hash_table_lookup(table, "msg"); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
813 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
814 | purple_debug_info("msim", |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
815 | "msim_incoming_im: got msg <%s> from <%s>, resolving username\n", |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
816 | msg, userid); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
817 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
818 | msim_lookup_user(session, userid, msim_incoming_im_cb, g_strdup(msg)); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
819 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
820 | return 0; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
821 | } |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
822 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
823 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
824 | /** |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
825 | * Process a message. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
826 | * |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
827 | * @param gc Connection. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
828 | * @param table Any message from the server. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
829 | * |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
830 | * @return The return value of the function used to process the message, or -1 if |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
831 | * called with invalid parameters. |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
832 | */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
833 | static int msim_process(PurpleConnection *gc, GHashTable *table) |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
834 | { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
835 | MsimSession *session; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
836 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
837 | g_return_val_if_fail(gc != NULL, -1); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
838 | g_return_val_if_fail(table != NULL, -1); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
839 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
840 | session = (MsimSession*)gc->proto_data; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
841 | |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
842 | #if MSIM_DEBUG_MSG |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
843 | purple_debug_info("msim", "-------- message -------------\n"); |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
844 | g_hash_table_foreach(table, print_hash_item, NULL); |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
845 | purple_debug_info("msim", "------------------------------\n"); |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
846 | #endif |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
847 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
848 | if (g_hash_table_lookup(table, "nc")) |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
849 | { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
850 | return msim_login_challenge(session, table); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
851 | } else if (g_hash_table_lookup(table, "sesskey")) { |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
852 | purple_debug_info("msim", "SESSKEY=<%s>\n", (gchar*)g_hash_table_lookup(table, "sesskey")); |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
853 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
854 | purple_connection_update_progress(gc, "Connected", 3, 4); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
855 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
856 | session->sesskey = g_strdup(g_hash_table_lookup(table, "sesskey")); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
857 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
858 | /* Comes with: proof,profileid,userid,uniquenick -- all same values |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
859 | * (at least for me). */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
860 | session->userid = g_strdup(g_hash_table_lookup(table, "userid")); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
861 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
862 | purple_connection_set_state(gc, PURPLE_CONNECTED); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
863 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
864 | return 0; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
865 | } else if (g_hash_table_lookup(table, "bm")) { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
866 | guint bm; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
867 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
868 | bm = atoi(g_hash_table_lookup(table, "bm")); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
869 | switch (bm) |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
870 | { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
871 | case MSIM_BM_STATUS: |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
872 | return msim_status(session, table); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
873 | case MSIM_BM_INSTANT: |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
874 | return msim_incoming_im(session, table); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
875 | default: |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
876 | /* Not really an IM, but show it for informational |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
877 | * purposes during development. */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
878 | return msim_incoming_im(session, table); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
879 | } |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
880 | |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
881 | if (bm == MSIM_BM_STATUS) |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
882 | { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
883 | return msim_status(session, table); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
884 | } else { /* else if strcmp(bm, "1") == 0) */ |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
885 | return msim_incoming_im(session, table); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
886 | } |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
887 | } else if (g_hash_table_lookup(table, "rid")) { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
888 | return msim_process_reply(session, table); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
889 | } else if (g_hash_table_lookup(table, "error")) { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
890 | return msim_error(session, table); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
891 | } else if (g_hash_table_lookup(table, "ka")) { |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
892 | purple_debug_info("msim", "msim_process: got keep alive\n"); |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
893 | return 0; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
894 | } else { |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
895 | /* TODO: dump unknown msgs to file, so user can send them to me |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
896 | * if they wish, to help add support for new messages (inspired |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
897 | * by Alexandr Shutko, who maintains OSCAR protocol documentation). */ |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
898 | purple_debug_info("msim", "msim_process: unhandled message\n"); |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
899 | return 0; |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
900 | } |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
901 | } |
|
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
902 | /** |
| 16322 | 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 { | |
|
16332
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
942 | purple_debug_info("msim", |
|
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
943 | "msim_process_reply: not caching <%s>, no UserName\n", |
| 16322 | 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 | { | |
|
16332
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
954 | purple_debug_info("msim", |
|
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
955 | "msim_process_body: calling callback now\n"); |
| 16322 | 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 { | |
|
16332
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
965 | purple_debug_info("msim", |
|
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
966 | "msim_process_body: no callback for rid %d\n", rid); |
| 16322 | 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 | ||
| 16324 | 992 | purple_debug_info("msim", "msim_error: %s\n", full_errmsg); |
| 16322 | 993 | |
| 994 | /* TODO: check 'fatal' and die if asked to. | |
| 995 | * TODO: do something with the error # (localization of errmsg?) */ | |
| 16324 | 996 | purple_notify_error(session->account, g_strdup("MySpaceIM Error"), |
| 16322 | 997 | full_errmsg, NULL); |
| 998 | ||
| 999 | if (g_hash_table_lookup(table, "fatal")) | |
| 1000 | { | |
| 16324 | 1001 | purple_debug_info("msim", "fatal error, destroy session\n"); |
| 1002 | purple_connection_error(session->gc, full_errmsg); | |
| 16322 | 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 | { | |
| 16324 | 1028 | PurpleBuddyList *blist; |
| 1029 | PurpleBuddy *buddy; | |
| 1030 | PurplePresence *presence; | |
| 16322 | 1031 | GHashTable *body; |
| 16324 | 1032 | //PurpleStatus *status; |
| 16322 | 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 | { | |
| 16324 | 1054 | purple_debug_info("msim", "msim_status_cb: no username?!\n"); |
| 16322 | 1055 | return; |
| 1056 | } | |
| 1057 | ||
|
16332
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
1058 | purple_debug_info("msim", |
|
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
1059 | "msim_status_cb: updating status for <%s> to <%s>\n", |
|
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
1060 | username, status_str); |
| 16322 | 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 | ||
| 16324 | 1075 | blist = purple_get_blist(); |
| 16322 | 1076 | |
| 1077 | /* Add buddy if not found */ | |
| 16324 | 1078 | buddy = purple_find_buddy(session->account, username); |
| 16322 | 1079 | if (!buddy) |
| 1080 | { | |
| 16324 | 1081 | /* TODO: purple aliases, userids and usernames */ |
|
16332
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
1082 | purple_debug_info("msim", |
|
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
1083 | "msim_status: making new buddy for %s\n", username); |
| 16324 | 1084 | buddy = purple_buddy_new(session->account, username, NULL); |
| 16322 | 1085 | |
| 1086 | /* TODO: sometimes (when click on it), buddy list disappears. Fix. */ | |
| 16324 | 1087 | purple_blist_add_buddy(buddy, NULL, NULL, NULL); |
| 16322 | 1088 | } else { |
| 16324 | 1089 | purple_debug_info("msim", "msim_status: found buddy %s\n", username); |
| 16322 | 1090 | } |
| 1091 | ||
| 1092 | /* For now, always set status to online. | |
| 1093 | * TODO: make status reflect reality | |
| 1094 | * TODO: show headline */ | |
| 16324 | 1095 | presence = purple_presence_new_for_buddy(buddy); |
| 1096 | purple_presence_set_status_active(presence, MSIM_STATUS_ONLINE, TRUE); | |
| 16322 | 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 | { | |
| 16324 | 1124 | purple_debug_info("msim", "msim_status: bm=100 but no status msg\n"); |
| 16322 | 1125 | return 0; |
| 1126 | } | |
| 1127 | ||
| 1128 | userid = g_hash_table_lookup(table, "f"); | |
| 1129 | if (!userid) | |
| 1130 | { | |
| 16324 | 1131 | purple_debug_info("msim", "msim_status: bm=100 but no f field\n"); |
| 16322 | 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. */ | |
|
16332
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
1137 | purple_debug_info("msim", |
|
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
1138 | "msim_status: got status msg <%s> for <%s>, scheduling lookup\n", |
| 16322 | 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 | * | |
| 16324 | 1152 | * @param gc_uncasted A PurpleConnection pointer. |
| 16322 | 1153 | * @param source File descriptor. |
| 16324 | 1154 | * @param cond PURPLE_INPUT_READ |
| 16322 | 1155 | * |
| 1156 | * Reads the input, and dispatches calls msim_process to handle it. | |
| 1157 | */ | |
| 16324 | 1158 | static void msim_input_cb(gpointer gc_uncasted, gint source, PurpleInputCondition cond) |
| 16322 | 1159 | { |
| 16324 | 1160 | PurpleConnection *gc; |
| 1161 | PurpleAccount *account; | |
| 16322 | 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 | ||
| 16324 | 1169 | gc = (PurpleConnection*)(gc_uncasted); |
| 1170 | account = purple_connection_get_account(gc); | |
| 16322 | 1171 | session = gc->proto_data; |
| 1172 | ||
| 1173 | g_return_if_fail(MSIM_SESSION_VALID(session)); | |
| 1174 | ||
| 16324 | 1175 | g_assert(cond == PURPLE_INPUT_READ); |
| 16322 | 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 | { | |
| 16324 | 1183 | purple_debug_error("msim", "msim_input_cb: %d-byte read buffer full!\n", |
| 16322 | 1184 | MSIM_READ_BUF_SIZE); |
| 16324 | 1185 | purple_connection_error(gc, "Read buffer full"); |
| 16322 | 1186 | /* TODO: fix 100% CPU after closing */ |
| 1187 | close(source); | |
| 1188 | return; | |
| 1189 | } | |
| 1190 | ||
| 16324 | 1191 | purple_debug_info("msim", "buffer at %d (max %d), reading up to %d\n", |
|
16332
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
1192 | session->rxoff, MSIM_READ_BUF_SIZE, |
|
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
1193 | MSIM_READ_BUF_SIZE - session->rxoff); |
| 16322 | 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 | { | |
| 16324 | 1206 | purple_connection_error(gc, "Read error"); |
| 1207 | purple_debug_error("msim", "msim_input_cb: read error, ret=%d, " | |
| 16322 | 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 | { | |
| 16324 | 1215 | purple_debug_info("msim", "msim_input_cb: server disconnected\n"); |
| 1216 | purple_connection_error(gc, "Server has disconnected"); | |
| 16322 | 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. */ | |
| 16324 | 1227 | purple_debug_info("msim", "msim_input_cb: strlen=%d, but read %d bytes" |
|
16332
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
1228 | "--null byte encountered?\n", |
|
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
1229 | strlen(session->rxbuf + session->rxoff), n); |
| 16324 | 1230 | //purple_connection_error(gc, "Invalid message - null byte on input"); |
| 16322 | 1231 | return; |
| 1232 | } | |
| 1233 | ||
| 1234 | session->rxoff += n; | |
| 16324 | 1235 | purple_debug_info("msim", "msim_input_cb: read=%d\n", n); |
| 16322 | 1236 | |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
1237 | #if MSIM_DEBUG_RXBUF |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
1238 | purple_debug_info("msim", "buf=<%s>\n", session->rxbuf); |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
1239 | #endif |
| 16322 | 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 | ||
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
1246 | #if MSIM_DEBUG_RXBUF |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
1247 | purple_debug_info("msim", "in loop: buf=<%s>\n", session->rxbuf); |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
1248 | #endif |
| 16322 | 1249 | *end = 0; |
| 1250 | table = msim_parse(g_strdup(session->rxbuf)); | |
| 1251 | if (!table) | |
| 1252 | { | |
|
16332
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
1253 | purple_debug_info("msim", "msim_input_cb: couldn't parse <%s>\n", |
|
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
1254 | session->rxbuf); |
| 16324 | 1255 | purple_connection_error(gc, "Unparseable message"); |
| 16322 | 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 | * | |
| 16324 | 1277 | * @param data A PurpleConnection pointer. |
| 16322 | 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 | { | |
| 16324 | 1283 | PurpleConnection *gc; |
| 16322 | 1284 | MsimSession *session; |
| 1285 | ||
| 1286 | g_return_if_fail(data != NULL); | |
| 1287 | ||
| 16324 | 1288 | gc = (PurpleConnection*)data; |
| 16322 | 1289 | session = gc->proto_data; |
| 1290 | ||
| 1291 | if (source < 0) | |
| 1292 | { | |
| 16324 | 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)", | |
| 16322 | 1295 | error_message, source)); |
| 1296 | return; | |
| 1297 | } | |
| 1298 | ||
| 1299 | session->fd = source; | |
| 1300 | ||
| 16324 | 1301 | gc->inpa = purple_input_add(source, PURPLE_INPUT_READ, msim_input_cb, gc); |
| 16322 | 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 | */ | |
| 16324 | 1313 | static MsimSession *msim_session_new(PurpleAccount *acct) |
| 16322 | 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; | |
| 16324 | 1323 | session->gc = purple_account_get_connection(acct); |
| 16322 | 1324 | session->fd = -1; |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
1325 | session->user_lookup_cb = g_hash_table_new_full(g_direct_hash, |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
1326 | g_direct_equal, NULL, NULL); /* do NOT free function pointers! */ |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
1327 | session->user_lookup_cb_data = g_hash_table_new_full(g_direct_hash, |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
1328 | g_direct_equal, NULL, g_free); |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
1329 | session->user_lookup_cache = g_hash_table_new_full(g_str_hash, g_str_equal, |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
1330 | g_free, (GDestroyNotify)g_hash_table_destroy); |
| 16322 | 1331 | session->rxoff = 0; |
| 1332 | session->rxbuf = g_new0(gchar, MSIM_READ_BUF_SIZE); | |
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
1333 | session->next_rid = 1; |
| 16322 | 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 | } | |
|
16333
0fdea8ad21cb
Add MySpaceIM header file and reorganize functions.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16332
diff
changeset
|
1355 | |
| 16322 | 1356 | |
| 1357 | ||
| 1358 | /** | |
| 1359 | * Close the connection. | |
| 1360 | * | |
| 1361 | * @param gc The connection. | |
| 1362 | */ | |
| 16324 | 1363 | static void msim_close(PurpleConnection *gc) |
| 16322 | 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 | ||
|
16332
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
1423 | purple_debug_info("msim", "msim_lookup_userid", |
|
f0e987f024e0
Use g_convert for UTF-16LE conversion instead of converting manually.
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16330
diff
changeset
|
1424 | "asynchronously looking up <%s>\n", user); |
| 16322 | 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 | ||
|
16335
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
1437 | rid = session->next_rid; |
|
0c2b32acc27a
Replace printf() in MySpaceIM prpl with purple_debug_info().
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16334
diff
changeset
|
1438 | ++session->next_rid; |
| 16322 | 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 | ||
|
16337
ba0ded418e6a
Add escaping and unescaping functions (for /1 and /2).
Jeff Connelly <jeff2@soc.pidgin.im>
parents:
16335
diff
changeset
|
1464 | /* TODO: escape values */ |
| 16322 | 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 | */ | |
| 16324 | 1481 | static char *msim_status_text(PurpleBuddy *buddy) |
| 16322 | 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 | */ | |
| 16324 | 1513 | static void msim_tooltip_text(PurpleBuddy *buddy, PurpleNotifyUserInfo *user_info, gboolean full) |
| 16322 | 1514 | { |
| 1515 | g_return_if_fail(buddy != NULL); | |
| 1516 | g_return_if_fail(user_info != NULL); | |
| 1517 | ||
| 16324 | 1518 | if (PURPLE_BUDDY_IS_ONLINE(buddy)) |
| 16322 | 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 | |
| 16324 | 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", | |
| 16322 | 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 | ||
| 16324 | 1544 | /** Callbacks called by Purple, to access this plugin. */ |
| 1545 | static PurplePluginProtocolInfo prpl_info = | |
| 16322 | 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. */ | |
| 16324 | 1614 | static PurplePluginInfo info = |
| 16322 | 1615 | { |
| 16324 | 1616 | PURPLE_PLUGIN_MAGIC, |
| 1617 | PURPLE_MAJOR_VERSION, | |
| 1618 | PURPLE_MINOR_VERSION, | |
| 1619 | PURPLE_PLUGIN_PROTOCOL, /**< type */ | |
| 16322 | 1620 | NULL, /**< ui_requirement */ |
| 1621 | 0, /**< flags */ | |
| 1622 | NULL, /**< dependencies */ | |
| 16324 | 1623 | PURPLE_PRIORITY_DEFAULT, /**< priority */ |
| 16322 | 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 | ||
| 16324 | 1647 | PURPLE_INIT_PLUGIN(myspace, init_plugin, info); |