Mercurial > pidgin
annotate src/protocols/msn/nexus.c @ 19816:1aa5494587e8
[gaim-migrate @ 16973]
change the soap process to below:
Post one Soap request
After finish one soap request,close the connection,
If there are more, reconect and process it.
to avoid the below problem:
in one socket, If post soap request one by one, The second will always read 0 bytes.
comitted by Ma Yuan<mayuan2006@gmail.com>
committer: Ethan Blanton <elb@pidgin.im>
author | Ma Yuan <mayuan2006@gmail.com> |
---|---|
date | Tue, 22 Aug 2006 06:10:01 +0000 |
parents | bffd1b96207c |
children | cb7433549a6f |
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" |
19784 | 25 #include "soap.h" |
8171 | 26 #include "nexus.h" |
27 #include "notification.h" | |
28 | |
19789 | 29 /*Local Function Prototype*/ |
30 static void nexus_login_connect_cb(gpointer data, GaimSslConnection *gsc,GaimInputCondition cond); | |
31 | |
8171 | 32 /************************************************************************** |
10481 | 33 * Main |
34 **************************************************************************/ | |
35 | |
36 MsnNexus * | |
37 msn_nexus_new(MsnSession *session) | |
38 { | |
39 MsnNexus *nexus; | |
40 | |
41 nexus = g_new0(MsnNexus, 1); | |
42 nexus->session = session; | |
19784 | 43 /*we must use SSL connection to do Windows Live ID authentication*/ |
19785 | 44 nexus->soapconn = msn_soap_new(session,nexus,1); |
45 | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
46 nexus->challenge_data = g_hash_table_new_full(g_str_hash, |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
47 g_str_equal, g_free, g_free); |
10481 | 48 |
49 return nexus; | |
50 } | |
51 | |
52 void | |
53 msn_nexus_destroy(MsnNexus *nexus) | |
54 { | |
55 if (nexus->challenge_data != NULL) | |
56 g_hash_table_destroy(nexus->challenge_data); | |
57 | |
19784 | 58 msn_soap_destroy(nexus->soapconn); |
10481 | 59 g_free(nexus); |
60 } | |
61 | |
62 /************************************************************************** | |
8171 | 63 * Login |
64 **************************************************************************/ | |
65 static void | |
19789 | 66 nexus_login_error_cb(GaimSslConnection *gsc, GaimSslErrorType error, void *data) |
8171 | 67 { |
19784 | 68 MsnSoapConn * soapconn = data; |
8171 | 69 MsnSession *session; |
70 | |
19784 | 71 session = soapconn->session; |
8171 | 72 g_return_if_fail(session != NULL); |
73 | |
19784 | 74 msn_session_set_error(session, MSN_ERROR_AUTH, _("Windows Live ID authentication:Unable to connect")); |
10568 | 75 /* the above line will result in nexus being destroyed, so we don't want |
76 * to destroy it here, or we'd crash */ | |
8171 | 77 } |
78 | |
19789 | 79 /*process the SOAP reply, get the Authentication Info*/ |
8171 | 80 static void |
19784 | 81 nexus_login_read_cb(gpointer data, gint source, GaimInputCondition cond) |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
82 { |
19784 | 83 MsnSoapConn * soapconn = data; |
84 MsnNexus *nexus; | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
85 MsnSession *session; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
86 |
19789 | 87 char *base, *c; |
88 char *msn_twn_t,*msn_twn_p; | |
89 char *login_params; | |
90 char **elems, **cur, **tokens; | |
91 char * cert_str; | |
92 | |
19784 | 93 nexus = soapconn->parent; |
94 g_return_if_fail(nexus != NULL); | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
95 session = nexus->session; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
96 g_return_if_fail(session != NULL); |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
97 |
19786 | 98 // gaim_debug_misc("msn", "TWN Server Reply: {%s}\n", soapconn->read_buf); |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
99 |
19789 | 100 /*reply OK, we should process the SOAP body*/ |
101 gaim_debug_info("MaYuan","Windows Live ID Reply OK!\n"); | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
102 |
19789 | 103 //TODO: we should parse it using XML |
104 base = g_strstr_len(soapconn->read_buf, soapconn->read_len, TWN_START_TOKEN); | |
105 base += strlen(TWN_START_TOKEN); | |
106 c = g_strstr_len(soapconn->read_buf, soapconn->read_len, TWN_END_TOKEN); | |
107 login_params = g_strndup(base, c - base); | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
108 |
19789 | 109 // gaim_debug_info("msn", "TWN Cert: {%s}\n", login_params); |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
110 |
19789 | 111 /* Parse the challenge data. */ |
112 elems = g_strsplit(login_params, "&", 0); | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
113 |
19789 | 114 for (cur = elems; *cur != NULL; cur++){ |
115 tokens = g_strsplit(*cur, "=", 2); | |
116 g_hash_table_insert(nexus->challenge_data, tokens[0], tokens[1]); | |
117 /* Don't free each of the tokens, only the array. */ | |
118 g_free(tokens); | |
119 } | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
120 |
19789 | 121 g_strfreev(elems); |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
122 |
19789 | 123 msn_twn_t = (char *)g_hash_table_lookup(nexus->challenge_data, "t"); |
124 msn_twn_p = (char *)g_hash_table_lookup(nexus->challenge_data, "p"); | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
125 |
19789 | 126 /*setup the t and p parameter for session*/ |
127 if (session->passport_info.t != NULL){ | |
128 g_free(session->passport_info.t); | |
129 } | |
130 session->passport_info.t = g_strdup(msn_twn_t); | |
19783 | 131 |
19789 | 132 if (session->passport_info.p != NULL) |
133 g_free(session->passport_info.p); | |
134 session->passport_info.p = g_strdup(msn_twn_p); | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
135 |
19789 | 136 cert_str = g_strdup_printf("t=%s&p=%s",msn_twn_t,msn_twn_p); |
137 msn_got_login_params(session, cert_str); | |
19784 | 138 |
19789 | 139 gaim_debug_info("MaYuan","close nexus connection! \n"); |
140 g_free(cert_str); | |
141 g_free(login_params); | |
142 msn_nexus_destroy(nexus); | |
143 session->nexus = NULL; | |
19784 | 144 |
19789 | 145 return; |
19784 | 146 } |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
147 |
19784 | 148 static void |
149 nexus_login_written_cb(gpointer data, gint source, GaimInputCondition cond) | |
150 { | |
151 MsnSoapConn * soapconn = data; | |
152 | |
153 soapconn->read_cb = nexus_login_read_cb; | |
19810 | 154 // msn_soap_read_cb(data,source,cond); |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
155 } |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
156 |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
157 |
19789 | 158 /*when connect, do the SOAP Style windows Live ID authentication */ |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
159 void |
19789 | 160 nexus_login_connect_cb(gpointer data, GaimSslConnection *gsc, |
8171 | 161 GaimInputCondition cond) |
162 { | |
19784 | 163 MsnSoapConn *soapconn; |
164 MsnNexus * nexus; | |
8171 | 165 MsnSession *session; |
19815 | 166 char *ru,*lc,*id,*tw,*ct,*kpp,*kv,*ver,*rn,*tpf; |
167 char *fs0,*fs; | |
8171 | 168 char *username, *password; |
19783 | 169 char *request_str, *head, *tail,*challenge_str; |
8171 | 170 |
19783 | 171 gaim_debug_info("MaYuan","starting Windows Live ID authentication\n"); |
19784 | 172 |
173 soapconn = data; | |
174 g_return_if_fail(soapconn != NULL); | |
175 | |
176 nexus = soapconn->parent; | |
8171 | 177 g_return_if_fail(nexus != NULL); |
178 | |
19784 | 179 session = soapconn->session; |
8171 | 180 g_return_if_fail(session != NULL); |
181 | |
10519 | 182 msn_session_set_login_step(session, MSN_LOGIN_STEP_GET_COOKIE); |
10481 | 183 |
19784 | 184 /*prepare the Windows Live ID authentication token*/ |
19783 | 185 username = g_strdup(gaim_account_get_username(session->account)); |
186 password = g_strdup(gaim_connection_get_password(session->account->gc)); | |
12457 | 187 |
19808 | 188 lc = (char *)g_hash_table_lookup(nexus->challenge_data, "lc"); |
189 id = (char *)g_hash_table_lookup(nexus->challenge_data, "id"); | |
190 tw = (char *)g_hash_table_lookup(nexus->challenge_data, "tw"); | |
19815 | 191 fs0= (char *)g_hash_table_lookup(nexus->challenge_data, "fs"); |
19808 | 192 ru = (char *)g_hash_table_lookup(nexus->challenge_data, "ru"); |
193 ct = (char *)g_hash_table_lookup(nexus->challenge_data, "ct"); | |
194 kpp= (char *)g_hash_table_lookup(nexus->challenge_data, "kpp"); | |
195 kv = (char *)g_hash_table_lookup(nexus->challenge_data, "kv"); | |
19815 | 196 ver= (char *)g_hash_table_lookup(nexus->challenge_data, "ver"); |
197 rn = (char *)g_hash_table_lookup(nexus->challenge_data, "rn"); | |
19808 | 198 tpf= (char *)g_hash_table_lookup(nexus->challenge_data, "tpf"); |
199 | |
19815 | 200 /* |
201 * add some fail-safe code to avoid windows Gaim Crash bug #1540454 | |
202 * If any of these string is NULL, will return Authentication Fail! | |
203 * for when windows g_strdup_printf() implementation get NULL point,It crashed! | |
204 */ | |
19808 | 205 if(!(lc && id && tw && ru && ct && kpp && kv && ver && tpf)){ |
206 gaim_debug_error("MaYuan","WLM Authenticate Key Error!\n"); | |
207 msn_session_set_error(session, MSN_ERROR_AUTH, _("Windows Live ID authentication Failed")); | |
208 g_free(username); | |
209 g_free(password); | |
210 gaim_ssl_close(gsc); | |
211 msn_nexus_destroy(nexus); | |
212 session->nexus = NULL; | |
213 return; | |
214 } | |
215 | |
19815 | 216 /* |
217 * in old MSN NS server's "USR TWN S" return,didn't include fs string | |
218 * so we use a default "1" for fs. | |
219 */ | |
220 if(fs0){ | |
221 fs = g_strdup(fs0); | |
222 }else{ | |
223 fs = g_strdup("1"); | |
19812 | 224 } |
19783 | 225 challenge_str = g_strdup_printf( |
19812 | 226 "lc=%s&id=%s&tw=%s&fs=%s&ru=%s&ct=%s&kpp=%s&kv=%s&ver=%s&rn=%s&tpf=%s\r\n", |
227 lc,id,tw,fs,ru,ct,kpp,kv,ver,rn,tpf | |
19783 | 228 ); |
19815 | 229 g_free(fs); |
19783 | 230 |
231 /*build the SOAP windows Live ID XML body */ | |
232 tail = g_strdup_printf(TWN_ENVELOP_TEMPLATE,username,password,challenge_str ); | |
8171 | 233 |
19784 | 234 soapconn->login_path = g_strdup(TWN_POST_URL); |
19783 | 235 head = g_strdup_printf( |
236 "POST %s HTTP/1.1\r\n" | |
237 "Accept: text/*\r\n" | |
238 "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n" | |
239 "Host: %s\r\n" | |
240 "Content-Length: %d\r\n" | |
241 "Connection: Keep-Alive\r\n" | |
242 "Cache-Control: no-cache\r\n\r\n", | |
19815 | 243 soapconn->login_path,soapconn->login_host,(int)strlen(tail)); |
8171 | 244 |
19783 | 245 request_str = g_strdup_printf("%s%s", head,tail); |
19786 | 246 // gaim_debug_misc("msn", "TWN Sending: {%s}\n", request_str); |
12457 | 247 |
248 g_free(head); | |
249 g_free(tail); | |
8171 | 250 g_free(username); |
251 g_free(password); | |
252 | |
19784 | 253 /*prepare to send the SOAP request*/ |
254 msn_soap_write(soapconn,request_str,nexus_login_written_cb); | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
255 |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
256 return; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
257 } |
8171 | 258 |
19783 | 259 /************************************************************************** |
260 * Connect | |
261 **************************************************************************/ | |
262 void | |
263 msn_nexus_connect(MsnNexus *nexus) | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
264 { |
19783 | 265 /* Authenticate via Windows Live ID. */ |
266 gaim_debug_info("MaYuan","msn_nexus_connect...\n"); | |
19789 | 267 msn_soap_init(nexus->soapconn,MSN_TWN_SERVER,1,nexus_login_connect_cb,nexus_login_error_cb); |
19806 | 268 msn_soap_connect(nexus->soapconn); |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12457
diff
changeset
|
269 } |
19784 | 270 |