| 1 /** |
|
| 2 * @file transaction.c MSN transaction functions |
|
| 3 * |
|
| 4 * purple |
|
| 5 * |
|
| 6 * Purple is the legal property of its developers, whose names are too numerous |
|
| 7 * to list here. Please refer to the COPYRIGHT file distributed with this |
|
| 8 * source distribution. |
|
| 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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
|
| 23 */ |
|
| 24 #include "msn.h" |
|
| 25 #include "transaction.h" |
|
| 26 |
|
| 27 MsnTransaction * |
|
| 28 msn_transaction_new(MsnCmdProc *cmdproc, const char *command, |
|
| 29 const char *format, ...) |
|
| 30 { |
|
| 31 MsnTransaction *trans; |
|
| 32 va_list arg; |
|
| 33 |
|
| 34 g_return_val_if_fail(command != NULL, NULL); |
|
| 35 |
|
| 36 trans = g_new0(MsnTransaction, 1); |
|
| 37 |
|
| 38 trans->cmdproc = cmdproc; |
|
| 39 trans->command = g_strdup(command); |
|
| 40 |
|
| 41 if (format != NULL) |
|
| 42 { |
|
| 43 va_start(arg, format); |
|
| 44 trans->params = g_strdup_vprintf(format, arg); |
|
| 45 va_end(arg); |
|
| 46 } |
|
| 47 |
|
| 48 /* trans->queue = g_queue_new(); */ |
|
| 49 |
|
| 50 return trans; |
|
| 51 } |
|
| 52 |
|
| 53 void |
|
| 54 msn_transaction_destroy(MsnTransaction *trans) |
|
| 55 { |
|
| 56 g_return_if_fail(trans != NULL); |
|
| 57 |
|
| 58 g_free(trans->command); |
|
| 59 g_free(trans->params); |
|
| 60 g_free(trans->payload); |
|
| 61 |
|
| 62 #if 0 |
|
| 63 if (trans->pendent_cmd != NULL) |
|
| 64 msn_message_unref(trans->pendent_msg); |
|
| 65 #endif |
|
| 66 |
|
| 67 #if 0 |
|
| 68 MsnTransaction *elem; |
|
| 69 if (trans->queue != NULL) |
|
| 70 { |
|
| 71 while ((elem = g_queue_pop_head(trans->queue)) != NULL) |
|
| 72 msn_transaction_destroy(elem); |
|
| 73 |
|
| 74 g_queue_free(trans->queue); |
|
| 75 } |
|
| 76 #endif |
|
| 77 |
|
| 78 if (trans->callbacks != NULL && trans->has_custom_callbacks) |
|
| 79 g_hash_table_destroy(trans->callbacks); |
|
| 80 |
|
| 81 if (trans->timer) |
|
| 82 purple_timeout_remove(trans->timer); |
|
| 83 |
|
| 84 g_free(trans); |
|
| 85 } |
|
| 86 |
|
| 87 char * |
|
| 88 msn_transaction_to_string(MsnTransaction *trans) |
|
| 89 { |
|
| 90 char *str; |
|
| 91 |
|
| 92 g_return_val_if_fail(trans != NULL, FALSE); |
|
| 93 |
|
| 94 if (trans->params != NULL) |
|
| 95 str = g_strdup_printf("%s %u %s\r\n", trans->command, trans->trId, trans->params); |
|
| 96 else |
|
| 97 str = g_strdup_printf("%s %u\r\n", trans->command, trans->trId); |
|
| 98 |
|
| 99 return str; |
|
| 100 } |
|
| 101 |
|
| 102 void |
|
| 103 msn_transaction_queue_cmd(MsnTransaction *trans, MsnCommand *cmd) |
|
| 104 { |
|
| 105 purple_debug_info("msn", "queueing command.\n"); |
|
| 106 trans->pendent_cmd = cmd; |
|
| 107 msn_command_ref(cmd); |
|
| 108 } |
|
| 109 |
|
| 110 void |
|
| 111 msn_transaction_unqueue_cmd(MsnTransaction *trans, MsnCmdProc *cmdproc) |
|
| 112 { |
|
| 113 MsnCommand *cmd; |
|
| 114 |
|
| 115 if (!cmdproc->servconn->connected) |
|
| 116 return; |
|
| 117 |
|
| 118 purple_debug_info("msn", "unqueueing command.\n"); |
|
| 119 cmd = trans->pendent_cmd; |
|
| 120 |
|
| 121 g_return_if_fail(cmd != NULL); |
|
| 122 |
|
| 123 msn_cmdproc_process_cmd(cmdproc, cmd); |
|
| 124 msn_command_unref(cmd); |
|
| 125 |
|
| 126 trans->pendent_cmd = NULL; |
|
| 127 } |
|
| 128 |
|
| 129 #if 0 |
|
| 130 void |
|
| 131 msn_transaction_queue(MsnTransaction *trans, MsnTransaction *elem) |
|
| 132 { |
|
| 133 if (trans->queue == NULL) |
|
| 134 trans->queue = g_queue_new(); |
|
| 135 |
|
| 136 g_queue_push_tail(trans->queue, elem); |
|
| 137 } |
|
| 138 |
|
| 139 void |
|
| 140 msn_transaction_unqueue(MsnTransaction *trans, MsnCmdProc *cmdproc) |
|
| 141 { |
|
| 142 MsnTransaction *elem; |
|
| 143 |
|
| 144 while ((elem = g_queue_pop_head(trans->queue)) != NULL) |
|
| 145 msn_cmdproc_send_trans(cmdproc, elem); |
|
| 146 } |
|
| 147 #endif |
|
| 148 |
|
| 149 void |
|
| 150 msn_transaction_set_payload(MsnTransaction *trans, |
|
| 151 const char *payload, int payload_len) |
|
| 152 { |
|
| 153 g_return_if_fail(trans != NULL); |
|
| 154 g_return_if_fail(payload != NULL); |
|
| 155 |
|
| 156 trans->payload = g_strdup(payload); |
|
| 157 trans->payload_len = payload_len ? payload_len : strlen(trans->payload); |
|
| 158 } |
|
| 159 |
|
| 160 void |
|
| 161 msn_transaction_set_data(MsnTransaction *trans, void *data) |
|
| 162 { |
|
| 163 g_return_if_fail(trans != NULL); |
|
| 164 |
|
| 165 trans->data = data; |
|
| 166 } |
|
| 167 |
|
| 168 void |
|
| 169 msn_transaction_add_cb(MsnTransaction *trans, char *answer, |
|
| 170 MsnTransCb cb) |
|
| 171 { |
|
| 172 g_return_if_fail(trans != NULL); |
|
| 173 g_return_if_fail(answer != NULL); |
|
| 174 |
|
| 175 if (trans->callbacks == NULL) |
|
| 176 { |
|
| 177 trans->has_custom_callbacks = TRUE; |
|
| 178 trans->callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, |
|
| 179 NULL); |
|
| 180 } |
|
| 181 else if (trans->has_custom_callbacks != TRUE) |
|
| 182 g_return_if_reached (); |
|
| 183 |
|
| 184 g_hash_table_insert(trans->callbacks, answer, cb); |
|
| 185 } |
|
| 186 |
|
| 187 static gboolean |
|
| 188 transaction_timeout(gpointer data) |
|
| 189 { |
|
| 190 MsnTransaction *trans; |
|
| 191 |
|
| 192 trans = data; |
|
| 193 g_return_val_if_fail(trans != NULL, FALSE); |
|
| 194 |
|
| 195 #if 0 |
|
| 196 purple_debug_info("msn", "timed out: %s %d %s\n", trans->command, trans->trId, trans->params); |
|
| 197 #endif |
|
| 198 |
|
| 199 if (trans->timeout_cb != NULL) |
|
| 200 trans->timeout_cb(trans->cmdproc, trans); |
|
| 201 |
|
| 202 return FALSE; |
|
| 203 } |
|
| 204 |
|
| 205 void |
|
| 206 msn_transaction_set_timeout_cb(MsnTransaction *trans, MsnTimeoutCb cb) |
|
| 207 { |
|
| 208 if (trans->timer) |
|
| 209 { |
|
| 210 purple_debug_error("msn", "This shouldn't be happening\n"); |
|
| 211 purple_timeout_remove(trans->timer); |
|
| 212 } |
|
| 213 trans->timeout_cb = cb; |
|
| 214 trans->timer = purple_timeout_add_seconds(60, transaction_timeout, trans); |
|
| 215 } |
|
| 216 |
|
| 217 void |
|
| 218 msn_transaction_set_error_cb(MsnTransaction *trans, MsnErrorCb cb) |
|
| 219 { |
|
| 220 trans->error_cb = cb; |
|
| 221 } |
|