Mercurial > pidgin.yaz
annotate src/protocols/msn/nexus.c @ 9290:2e5faf12a24c
[gaim-migrate @ 10093]
Don't try to print a NULL. Windows no likey it.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Tue, 15 Jun 2004 05:59:39 +0000 |
parents | ab6636c5a136 |
children | b15c177895b9 |
rev | line source |
---|---|
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
1 /** |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
2 * @file nexus.c MSN Nexus functions |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
3 * |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
4 * gaim |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
5 * |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
6 * Gaim is the legal property of its developers, whose names are too numerous |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
7 * to list here. Please refer to the COPYRIGHT file distributed with this |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
8 * source distribution. |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
9 * |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
10 * This program is free software; you can redistribute it and/or modify |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
11 * it under the terms of the GNU General Public License as published by |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
12 * the Free Software Foundation; either version 2 of the License, or |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
13 * (at your option) any later version. |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
14 * |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
15 * This program is distributed in the hope that it will be useful, |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
18 * GNU General Public License for more details. |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
19 * |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
20 * You should have received a copy of the GNU General Public License |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
21 * along with this program; if not, write to the Free Software |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
23 */ |
8171 | 24 #include "msn.h" |
25 #include "nexus.h" | |
26 #include "notification.h" | |
27 | |
28 /************************************************************************** | |
29 * Util | |
30 **************************************************************************/ | |
31 | |
32 static size_t | |
33 msn_ssl_read(GaimSslConnection *gsc, char **dest_buffer) | |
34 { | |
8925 | 35 gssize size = 0, s; |
8171 | 36 char *buffer = NULL; |
37 char temp_buf[4096]; | |
38 | |
39 while ((s = gaim_ssl_read(gsc, temp_buf, sizeof(temp_buf))) > 0) | |
40 { | |
41 buffer = g_realloc(buffer, size + s + 1); | |
42 | |
43 strncpy(buffer + size, temp_buf, s); | |
44 | |
45 buffer[size + s] = '\0'; | |
46 | |
47 size += s; | |
48 } | |
49 | |
50 *dest_buffer = buffer; | |
51 | |
52 return size; | |
53 } | |
54 | |
55 /************************************************************************** | |
56 * Login | |
57 **************************************************************************/ | |
58 | |
59 static void | |
60 login_error_cb(GaimSslConnection *gsc, GaimSslErrorType error, void *data) | |
61 { | |
62 MsnNexus *nexus; | |
63 MsnSession *session; | |
64 GaimAccount *account; | |
65 GaimConnection *gc; | |
66 | |
67 nexus = data; | |
68 g_return_if_fail(nexus != NULL); | |
69 | |
70 session = nexus->session; | |
71 g_return_if_fail(session != NULL); | |
72 | |
73 account = session->account; | |
74 g_return_if_fail(account != NULL); | |
75 | |
76 gc = gaim_account_get_connection(account); | |
77 g_return_if_fail(gc != NULL); | |
78 | |
79 gaim_connection_error(gc, _("Unable to connect to server")); | |
80 | |
81 msn_nexus_destroy(nexus); | |
82 session->nexus = NULL; | |
83 } | |
84 | |
85 static void | |
86 login_connect_cb(gpointer data, GaimSslConnection *gsc, | |
87 GaimInputCondition cond) | |
88 { | |
89 MsnNexus *nexus; | |
90 MsnSession *session; | |
91 char *username, *password; | |
92 char *request_str; | |
93 char *buffer = NULL; | |
94 size_t s; | |
95 | |
96 nexus = data; | |
97 g_return_if_fail(nexus != NULL); | |
98 | |
99 session = nexus->session; | |
100 g_return_if_fail(session != NULL); | |
101 | |
102 username = | |
103 g_strdup(gaim_url_encode(gaim_account_get_username(session->account))); | |
104 | |
105 password = | |
106 g_strdup(gaim_url_encode(gaim_account_get_password(session->account))); | |
107 | |
108 request_str = g_strdup_printf( | |
109 "GET %s HTTP/1.1\r\n" | |
110 "Authorization: Passport1.4 OrgVerb=GET,OrgURL=%s,sign-in=%s,pwd=%s," | |
111 "lc=%s,id=%s,tw=%s,fs=%s,ru=%s,ct=%s,kpp=%s,kv=%s,ver=%s,tpf=%s\r\n" | |
112 "User-Agent: MSMSGS\r\n" | |
113 "Host: %s\r\n" | |
114 "Connection: Keep-Alive\r\n" | |
115 "Cache-Control: no-cache\r\n" | |
116 "\r\n", | |
117 nexus->login_path, | |
118 (char *)g_hash_table_lookup(nexus->challenge_data, "ru"), | |
119 username, password, | |
120 (char *)g_hash_table_lookup(nexus->challenge_data, "lc"), | |
121 (char *)g_hash_table_lookup(nexus->challenge_data, "id"), | |
122 (char *)g_hash_table_lookup(nexus->challenge_data, "tw"), | |
123 (char *)g_hash_table_lookup(nexus->challenge_data, "fs"), | |
124 (char *)g_hash_table_lookup(nexus->challenge_data, "ru"), | |
125 (char *)g_hash_table_lookup(nexus->challenge_data, "ct"), | |
126 (char *)g_hash_table_lookup(nexus->challenge_data, "kpp"), | |
127 (char *)g_hash_table_lookup(nexus->challenge_data, "kv"), | |
128 (char *)g_hash_table_lookup(nexus->challenge_data, "ver"), | |
129 (char *)g_hash_table_lookup(nexus->challenge_data, "tpf"), | |
130 nexus->login_host); | |
131 | |
132 gaim_debug(GAIM_DEBUG_MISC, "msn", "Sending: {%s}\n", request_str); | |
133 | |
134 g_free(username); | |
135 g_free(password); | |
136 | |
137 if ((s = gaim_ssl_write(gsc, request_str, strlen(request_str))) <= 0) | |
138 { | |
139 g_free(request_str); | |
140 | |
141 return; | |
142 } | |
143 | |
144 g_free(request_str); | |
145 | |
146 if ((s = msn_ssl_read(gsc, &buffer)) <= 0) | |
147 return; | |
148 | |
149 gaim_ssl_close(gsc); | |
150 | |
151 gaim_debug(GAIM_DEBUG_MISC, "msn", "ssl buffer: {%s}", buffer); | |
152 | |
153 if (strstr(buffer, "HTTP/1.1 302") != NULL) | |
154 { | |
155 /* Redirect. */ | |
156 char *location, *c; | |
157 | |
158 location = strstr(buffer, "Location: "); | |
159 if (location == NULL) | |
160 { | |
161 g_free(buffer); | |
162 | |
163 return; | |
164 } | |
165 location = strchr(location, ' ') + 1; | |
166 | |
167 if ((c = strchr(location, '\r')) != NULL) | |
168 *c = '\0'; | |
169 | |
170 /* Skip the http:// */ | |
171 if ((c = strchr(location, '/')) != NULL) | |
172 location = c + 2; | |
173 | |
174 if ((c = strchr(location, '/')) != NULL) | |
175 { | |
176 g_free(nexus->login_path); | |
177 nexus->login_path = g_strdup(c); | |
178 | |
179 *c = '\0'; | |
180 } | |
181 | |
182 g_free(nexus->login_host); | |
183 nexus->login_host = g_strdup(location); | |
184 | |
185 gaim_ssl_connect(session->account, nexus->login_host, | |
186 GAIM_SSL_DEFAULT_PORT, login_connect_cb, | |
187 login_error_cb, nexus); | |
188 } | |
189 else if (strstr(buffer, "HTTP/1.1 401 Unauthorized") != NULL) | |
190 { | |
191 GaimConnection *gc; | |
192 const char *error, *c; | |
193 char *temp; | |
194 | |
195 if ((error = strstr(buffer, "WWW-Authenticate")) != NULL) | |
196 { | |
197 if ((error = strstr(error, "cbtxt=")) != NULL) | |
198 { | |
199 error += strlen("cbtxt="); | |
200 | |
201 if ((c = strchr(error, '\n')) == NULL) | |
202 c = error + strlen(error); | |
203 | |
204 temp = g_strndup(error, c - error); | |
205 error = gaim_url_decode(temp); | |
206 g_free(temp); | |
207 } | |
208 } | |
209 | |
210 gc = gaim_account_get_connection(session->account); | |
211 | |
212 if (error == NULL) | |
213 { | |
214 gaim_connection_error(gc, | |
215 _("Unknown error when attempting to authorize with " | |
216 "MSN login server.")); | |
217 } | |
218 else | |
219 gaim_connection_error(gc, error); | |
220 } | |
221 else if (strstr(buffer, "HTTP/1.1 200 OK")) | |
222 { | |
223 char *base, *c; | |
224 char *login_params; | |
225 | |
226 #if 0 | |
227 /* All your base are belong to us. */ | |
228 base = buffer; | |
229 | |
230 /* For great cookie! */ | |
231 while ((base = strstr(base, "Set-Cookie: ")) != NULL) | |
232 { | |
233 base += strlen("Set-Cookie: "); | |
234 | |
235 c = strchr(base, ';'); | |
236 | |
237 session->login_cookies = | |
238 g_list_append(session->login_cookies, | |
239 g_strndup(base, c - base)); | |
240 } | |
241 #endif | |
242 | |
243 base = strstr(buffer, "Authentication-Info: "); | |
244 | |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
8925
diff
changeset
|
245 g_return_if_fail(base != NULL); |
8171 | 246 |
247 base = strstr(base, "from-PP='"); | |
248 base += strlen("from-PP='"); | |
249 c = strchr(base, '\''); | |
250 | |
251 login_params = g_strndup(base, c - base); | |
252 | |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
8925
diff
changeset
|
253 msn_got_login_params(session, login_params); |
8171 | 254 |
255 g_free(login_params); | |
256 | |
257 msn_nexus_destroy(nexus); | |
258 session->nexus = NULL; | |
259 } | |
260 | |
261 g_free(buffer); | |
262 } | |
263 | |
264 static void | |
265 nexus_connect_cb(gpointer data, GaimSslConnection *gsc, | |
266 GaimInputCondition cond) | |
267 { | |
268 MsnNexus *nexus; | |
269 MsnSession *session; | |
270 char *request_str; | |
271 char *da_login; | |
272 char *base, *c; | |
273 char *buffer = NULL; | |
274 size_t s; | |
275 | |
276 nexus = data; | |
277 g_return_if_fail(nexus != NULL); | |
278 | |
279 session = nexus->session; | |
280 g_return_if_fail(session != NULL); | |
281 | |
282 request_str = g_strdup_printf("GET /rdr/pprdr.asp\r\n\r\n"); | |
283 | |
284 if ((s = gaim_ssl_write(gsc, request_str, strlen(request_str))) <= 0) | |
285 { | |
286 g_free(request_str); | |
287 return; | |
288 } | |
289 | |
290 g_free(request_str); | |
291 | |
292 /* Get the PassportURLs line. */ | |
293 if ((s = msn_ssl_read(gsc, &buffer)) <= 0) | |
294 return; | |
295 | |
296 base = strstr(buffer, "PassportURLs"); | |
297 | |
298 if (base == NULL) | |
299 { | |
300 g_free(buffer); | |
301 return; | |
302 } | |
303 | |
304 if ((da_login = strstr(base, "DALogin=")) != NULL) | |
305 { | |
306 if ((da_login = strchr(da_login, '=')) != NULL) | |
307 da_login++; | |
308 | |
309 if ((c = strchr(da_login, ',')) != NULL) | |
310 *c = '\0'; | |
311 | |
312 if ((c = strchr(da_login, '/')) != NULL) | |
313 { | |
314 nexus->login_path = g_strdup(c); | |
315 | |
316 *c = '\0'; | |
317 } | |
318 | |
319 nexus->login_host = g_strdup(da_login); | |
320 } | |
321 | |
322 g_free(buffer); | |
323 | |
324 gaim_ssl_close(gsc); | |
325 | |
326 /* Now begin the connection to the login server. */ | |
327 gaim_ssl_connect(session->account, nexus->login_host, | |
328 GAIM_SSL_DEFAULT_PORT, login_connect_cb, | |
329 login_error_cb, nexus); | |
330 } | |
331 | |
332 /************************************************************************** | |
333 * Nexus | |
334 **************************************************************************/ | |
335 | |
336 MsnNexus * | |
337 msn_nexus_new(MsnSession *session) | |
338 { | |
339 MsnNexus *nexus; | |
340 | |
341 nexus = g_new0(MsnNexus, 1); | |
342 nexus->session = session; | |
343 nexus->challenge_data = g_hash_table_new_full(g_str_hash, g_str_equal, | |
344 g_free, g_free); | |
345 | |
346 return nexus; | |
347 } | |
348 | |
349 void | |
350 msn_nexus_destroy(MsnNexus *nexus) | |
351 { | |
352 if (nexus->login_host != NULL) | |
353 g_free(nexus->login_host); | |
354 | |
355 if (nexus->login_path != NULL) | |
356 g_free(nexus->login_path); | |
357 | |
358 if (nexus->challenge_data != NULL) | |
359 g_hash_table_destroy(nexus->challenge_data); | |
360 | |
361 g_free(nexus); | |
362 } | |
363 | |
364 void | |
365 msn_nexus_connect(MsnNexus *nexus) | |
366 { | |
367 gaim_ssl_connect(nexus->session->account, "nexus.passport.com", | |
368 GAIM_SSL_DEFAULT_PORT, nexus_connect_cb, | |
369 login_error_cb, nexus); | |
370 } |