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