Mercurial > pidgin.yaz
annotate src/protocols/msn/cmdproc.c @ 9873:aa0761051017
[gaim-migrate @ 10752]
NEWS
committer: Tailor Script <tailor@pidgin.im>
author | Tim Ringenbach <marv@pidgin.im> |
---|---|
date | Wed, 25 Aug 2004 22:41:54 +0000 |
parents | 9981fcce85c1 |
children | 17dbed6eebf5 |
rev | line source |
---|---|
8810 | 1 /** |
2 * @file cmdproc.c MSN command processor functions | |
3 * | |
4 * gaim | |
5 * | |
9198
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. |
8810 | 9 * |
10 * This program is free software; you can redistribute it and/or modify | |
11 * it under the terms of the GNU General Public License as published by | |
12 * the Free Software Foundation; either version 2 of the License, or | |
13 * (at your option) any later version. | |
14 * | |
15 * This program is distributed in the hope that it will be useful, | |
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 * GNU General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program; if not, write to the Free Software | |
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
23 */ | |
24 #include "msn.h" | |
25 #include "cmdproc.h" | |
26 | |
27 MsnCmdProc * | |
28 msn_cmdproc_new(MsnSession *session) | |
29 { | |
30 MsnCmdProc *cmdproc; | |
31 | |
32 cmdproc = g_new0(MsnCmdProc, 1); | |
33 | |
34 cmdproc->session = session; | |
35 cmdproc->txqueue = g_queue_new(); | |
36 cmdproc->history = msn_history_new(); | |
37 | |
38 return cmdproc; | |
39 } | |
40 | |
41 void | |
42 msn_cmdproc_destroy(MsnCmdProc *cmdproc) | |
43 { | |
44 MsnTransaction *trans; | |
45 | |
46 if (cmdproc->last_trans != NULL) | |
47 g_free(cmdproc->last_trans); | |
48 | |
49 while ((trans = g_queue_pop_head(cmdproc->txqueue)) != NULL) | |
50 msn_transaction_destroy(trans); | |
51 | |
52 g_queue_free(cmdproc->txqueue); | |
53 | |
54 msn_history_destroy(cmdproc->history); | |
55 } | |
56 | |
57 void | |
58 msn_cmdproc_process_queue(MsnCmdProc *cmdproc) | |
59 { | |
60 MsnTransaction *trans; | |
61 | |
62 while ((trans = g_queue_pop_head(cmdproc->txqueue)) != NULL && | |
63 cmdproc->error == 0) | |
64 { | |
65 msn_cmdproc_send_trans(cmdproc, trans); | |
66 } | |
67 } | |
68 | |
69 void | |
70 msn_cmdproc_queue_trans(MsnCmdProc *cmdproc, MsnTransaction *trans) | |
71 { | |
72 g_return_if_fail(cmdproc != NULL); | |
73 g_return_if_fail(trans != NULL); | |
74 | |
75 g_queue_push_tail(cmdproc->txqueue, trans); | |
76 } | |
77 | |
78 static void | |
79 show_debug_cmd(MsnCmdProc *cmdproc, gboolean incoming, const char *command) | |
80 { | |
81 MsnServConn *servconn; | |
82 const char *names[] = { "NS", "SB" }; | |
83 char *show; | |
84 char tmp; | |
85 size_t len; | |
86 | |
87 servconn = cmdproc->servconn; | |
88 len = strlen(command); | |
89 show = g_strdup(command); | |
90 | |
91 tmp = (incoming) ? 'S' : 'C'; | |
92 | |
93 if ((show[len - 1] == '\n') && (show[len - 2] == '\r')) | |
94 { | |
95 show[len - 2] = '\0'; | |
96 } | |
97 | |
98 gaim_debug_misc("msn", "%c: %s %03d: %s\n", tmp, | |
99 names[servconn->type], servconn->num, show); | |
100 | |
101 g_free(show); | |
102 } | |
103 | |
104 void | |
105 msn_cmdproc_send_trans(MsnCmdProc *cmdproc, MsnTransaction *trans) | |
106 { | |
107 MsnServConn *servconn; | |
108 char *data; | |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
109 size_t len; |
8810 | 110 |
111 g_return_if_fail(cmdproc != NULL); | |
112 g_return_if_fail(trans != NULL); | |
113 | |
114 servconn = cmdproc->servconn; | |
115 msn_history_add(cmdproc->history, trans); | |
116 | |
117 data = msn_transaction_to_string(trans); | |
9158
c30d81b4dd22
[gaim-migrate @ 9942]
Christian Hammond <chipx86@chipx86.com>
parents:
8830
diff
changeset
|
118 |
8810 | 119 cmdproc->last_trans = g_strdup(data); |
120 | |
121 len = strlen(data); | |
122 | |
123 show_debug_cmd(cmdproc, FALSE, data); | |
124 | |
125 if (trans->callbacks == NULL) | |
126 trans->callbacks = g_hash_table_lookup(cmdproc->cbs_table->cmds, | |
127 trans->command); | |
128 | |
129 if (trans->payload != NULL) | |
130 { | |
131 data = g_realloc(data, len + trans->payload_len); | |
132 memcpy(data + len, trans->payload, trans->payload_len); | |
133 len += trans->payload_len; | |
134 } | |
135 | |
136 msn_servconn_write(servconn, data, len); | |
137 | |
138 g_free(data); | |
139 } | |
140 | |
141 void | |
142 msn_cmdproc_send_quick(MsnCmdProc *cmdproc, const char *command, | |
143 const char *format, ...) | |
144 { | |
145 MsnServConn *servconn; | |
146 char *data; | |
8830
f8038b1f7449
[gaim-migrate @ 9594]
Christian Hammond <chipx86@chipx86.com>
parents:
8810
diff
changeset
|
147 char *params = NULL; |
8810 | 148 va_list arg; |
149 size_t len; | |
150 | |
151 g_return_if_fail(cmdproc != NULL); | |
152 g_return_if_fail(command != NULL); | |
153 | |
154 servconn = cmdproc->servconn; | |
155 | |
9158
c30d81b4dd22
[gaim-migrate @ 9942]
Christian Hammond <chipx86@chipx86.com>
parents:
8830
diff
changeset
|
156 if (format != NULL) |
c30d81b4dd22
[gaim-migrate @ 9942]
Christian Hammond <chipx86@chipx86.com>
parents:
8830
diff
changeset
|
157 { |
8830
f8038b1f7449
[gaim-migrate @ 9594]
Christian Hammond <chipx86@chipx86.com>
parents:
8810
diff
changeset
|
158 va_start(arg, format); |
f8038b1f7449
[gaim-migrate @ 9594]
Christian Hammond <chipx86@chipx86.com>
parents:
8810
diff
changeset
|
159 params = g_strdup_vprintf(format, arg); |
f8038b1f7449
[gaim-migrate @ 9594]
Christian Hammond <chipx86@chipx86.com>
parents:
8810
diff
changeset
|
160 va_end(arg); |
f8038b1f7449
[gaim-migrate @ 9594]
Christian Hammond <chipx86@chipx86.com>
parents:
8810
diff
changeset
|
161 } |
8810 | 162 |
163 if (params != NULL) | |
164 data = g_strdup_printf("%s %s\r\n", command, params); | |
165 else | |
166 data = g_strdup_printf("%s\r\n", command); | |
167 | |
168 g_free(params); | |
169 | |
170 len = strlen(data); | |
171 | |
172 show_debug_cmd(cmdproc, FALSE, data); | |
173 | |
174 msn_servconn_write(servconn, data, len); | |
175 | |
176 g_free(data); | |
177 } | |
178 | |
179 void | |
180 msn_cmdproc_send(MsnCmdProc *cmdproc, const char *command, | |
181 const char *format, ...) | |
182 { | |
183 MsnTransaction *trans; | |
184 va_list arg; | |
185 | |
186 g_return_if_fail(cmdproc != NULL); | |
187 g_return_if_fail(command != NULL); | |
188 | |
189 trans = g_new0(MsnTransaction, 1); | |
190 | |
191 trans->command = g_strdup(command); | |
192 | |
9158
c30d81b4dd22
[gaim-migrate @ 9942]
Christian Hammond <chipx86@chipx86.com>
parents:
8830
diff
changeset
|
193 if (format != NULL) |
c30d81b4dd22
[gaim-migrate @ 9942]
Christian Hammond <chipx86@chipx86.com>
parents:
8830
diff
changeset
|
194 { |
8830
f8038b1f7449
[gaim-migrate @ 9594]
Christian Hammond <chipx86@chipx86.com>
parents:
8810
diff
changeset
|
195 va_start(arg, format); |
f8038b1f7449
[gaim-migrate @ 9594]
Christian Hammond <chipx86@chipx86.com>
parents:
8810
diff
changeset
|
196 trans->params = g_strdup_vprintf(format, arg); |
f8038b1f7449
[gaim-migrate @ 9594]
Christian Hammond <chipx86@chipx86.com>
parents:
8810
diff
changeset
|
197 va_end(arg); |
f8038b1f7449
[gaim-migrate @ 9594]
Christian Hammond <chipx86@chipx86.com>
parents:
8810
diff
changeset
|
198 } |
8810 | 199 |
200 msn_cmdproc_send_trans(cmdproc, trans); | |
201 } | |
202 | |
203 void | |
9158
c30d81b4dd22
[gaim-migrate @ 9942]
Christian Hammond <chipx86@chipx86.com>
parents:
8830
diff
changeset
|
204 msn_cmdproc_process_payload(MsnCmdProc *cmdproc, char *payload, |
c30d81b4dd22
[gaim-migrate @ 9942]
Christian Hammond <chipx86@chipx86.com>
parents:
8830
diff
changeset
|
205 int payload_len) |
c30d81b4dd22
[gaim-migrate @ 9942]
Christian Hammond <chipx86@chipx86.com>
parents:
8830
diff
changeset
|
206 { |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
207 MsnCommand *last; |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
208 |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
209 g_return_if_fail(cmdproc != NULL); |
9158
c30d81b4dd22
[gaim-migrate @ 9942]
Christian Hammond <chipx86@chipx86.com>
parents:
8830
diff
changeset
|
210 |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
211 last = cmdproc->last_cmd; |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
212 last->payload = g_memdup(payload, payload_len); |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
213 last->payload_len = payload_len; |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
214 |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
215 if (last->payload_cb != NULL) |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
216 last->payload_cb(cmdproc, last, payload, payload_len); |
9158
c30d81b4dd22
[gaim-migrate @ 9942]
Christian Hammond <chipx86@chipx86.com>
parents:
8830
diff
changeset
|
217 } |
c30d81b4dd22
[gaim-migrate @ 9942]
Christian Hammond <chipx86@chipx86.com>
parents:
8830
diff
changeset
|
218 |
c30d81b4dd22
[gaim-migrate @ 9942]
Christian Hammond <chipx86@chipx86.com>
parents:
8830
diff
changeset
|
219 void |
8810 | 220 msn_cmdproc_process_msg(MsnCmdProc *cmdproc, MsnMessage *msg) |
221 { | |
222 MsnMsgCb cb; | |
223 | |
224 cb = g_hash_table_lookup(cmdproc->cbs_table->msgs, | |
225 msn_message_get_content_type(msg)); | |
226 | |
227 if (cb == NULL) | |
228 { | |
9158
c30d81b4dd22
[gaim-migrate @ 9942]
Christian Hammond <chipx86@chipx86.com>
parents:
8830
diff
changeset
|
229 gaim_debug_warning("msn", "Unhandled content-type '%s'\n", |
c30d81b4dd22
[gaim-migrate @ 9942]
Christian Hammond <chipx86@chipx86.com>
parents:
8830
diff
changeset
|
230 msn_message_get_content_type(msg)); |
8810 | 231 |
232 return; | |
233 } | |
234 | |
235 cb(cmdproc, msg); | |
236 } | |
237 | |
238 void | |
239 msn_cmdproc_process_cmd(MsnCmdProc *cmdproc, MsnCommand *cmd) | |
240 { | |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
241 MsnTransCb cb = NULL; |
8810 | 242 MsnTransaction *trans = NULL; |
243 | |
244 if (cmd->trId) | |
245 trans = msn_history_find(cmdproc->history, cmd->trId); | |
246 | |
247 if (g_ascii_isdigit(cmd->command[0])) | |
248 { | |
249 if (trans != NULL) | |
250 { | |
251 MsnErrorCb error_cb = NULL; | |
252 int error; | |
253 | |
254 error = atoi(cmd->command); | |
255 if (cmdproc->cbs_table->errors != NULL) | |
256 error_cb = g_hash_table_lookup(cmdproc->cbs_table->errors, trans->command); | |
257 | |
258 if (error_cb != NULL) | |
259 error_cb(cmdproc, trans, error); | |
260 else | |
261 { | |
262 #if 1 | |
263 msn_error_handle(cmdproc->session, error); | |
264 #else | |
265 gaim_debug_warning("msn", "Unhandled error '%s'\n", | |
266 cmd->command); | |
267 #endif | |
268 } | |
269 | |
270 return; | |
271 } | |
272 } | |
273 | |
274 if (cmdproc->cbs_table->async != NULL) | |
275 cb = g_hash_table_lookup(cmdproc->cbs_table->async, cmd->command); | |
276 | |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
277 if (cb == NULL && trans != NULL) |
8810 | 278 { |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
279 cmd->trans = trans; |
8810 | 280 |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
281 if (trans->callbacks != NULL) |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
282 cb = g_hash_table_lookup(trans->callbacks, cmd->command); |
8810 | 283 } |
284 | |
285 if (cb != NULL) | |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
286 { |
8810 | 287 cb(cmdproc, cmd); |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
288 } |
8810 | 289 else |
290 { | |
291 gaim_debug_warning("msn", "Unhandled command '%s'\n", | |
9158
c30d81b4dd22
[gaim-migrate @ 9942]
Christian Hammond <chipx86@chipx86.com>
parents:
8830
diff
changeset
|
292 cmd->command); |
8810 | 293 } |
294 | |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
295 #if 1 |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
296 /* TODO this is really ugly */ |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
297 /* Since commands have not stored payload and we need it for pendent |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
298 * commands at the time we process again the same command we will try |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
299 * to read again the payload of payload_len size but we will actually |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
300 * read sometime else, and reading from server syncronization goes to |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
301 * hell. */ |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
302 /* Now we store the payload in the command when we queue them :D */ |
8810 | 303 |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
304 if (trans != NULL && trans->pendent_cmd != NULL) |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
305 if (cmdproc->ready) |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
306 msn_transaction_unqueue_cmd(trans, cmdproc); |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
307 #endif |
8810 | 308 } |
309 | |
310 void | |
311 msn_cmdproc_process_cmd_text(MsnCmdProc *cmdproc, const char *command) | |
312 { | |
313 show_debug_cmd(cmdproc, TRUE, command); | |
314 | |
315 if (cmdproc->last_cmd != NULL) | |
316 msn_command_destroy(cmdproc->last_cmd); | |
317 | |
318 cmdproc->last_cmd = msn_command_from_string(command); | |
319 | |
320 msn_cmdproc_process_cmd(cmdproc, cmdproc->last_cmd); | |
321 } | |
322 | |
323 void | |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
324 msn_cmdproc_show_error(MsnCmdProc *cmdproc, int error) |
8810 | 325 { |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
326 GaimConnection *gc = |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
327 gaim_account_get_connection(cmdproc->session->account); |
8810 | 328 |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
329 char *tmp; |
8810 | 330 |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
331 tmp = NULL; |
8810 | 332 |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
333 switch (error) |
8810 | 334 { |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
335 case MSN_ERROR_MISC: |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
336 tmp = _("Miscellaneous error"); break; |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
337 case MSN_ERROR_SIGNOTHER: |
9641 | 338 gc->wants_to_die = TRUE; |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
339 tmp = _("You have signed on from another location."); break; |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
340 case MSN_ERROR_SERVDOWN: |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
341 tmp = _("The MSN servers are going down temporarily."); break; |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
342 default: |
8810 | 343 break; |
344 } | |
345 | |
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
346 if (tmp != NULL) |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
347 { |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
348 gaim_connection_error(gc, tmp); |
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
9158
diff
changeset
|
349 } |
8810 | 350 } |