2
* @file transaction.c MSN transaction functions
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
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.
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.
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
29
#include "transaction.h"
32
msn_transaction_new(MsnCmdProc *cmdproc, const char *command,
33
const char *format, ...)
35
MsnTransaction *trans;
38
g_return_val_if_fail(command != NULL, NULL);
40
trans = g_new0(MsnTransaction, 1);
42
trans->cmdproc = cmdproc;
43
trans->command = g_strdup(command);
44
trans->saveable = TRUE;
48
va_start(arg, format);
49
trans->params = g_strdup_vprintf(format, arg);
53
/* trans->queue = g_queue_new(); */
59
msn_transaction_destroy(MsnTransaction *trans)
61
g_return_if_fail(trans != NULL);
63
g_free(trans->command);
64
g_free(trans->params);
65
g_free(trans->payload);
68
trans->data_free(trans->data);
71
if (trans->pendent_cmd != NULL)
72
msn_message_unref(trans->pendent_msg);
77
if (trans->queue != NULL)
79
while ((elem = g_queue_pop_head(trans->queue)) != NULL)
80
msn_transaction_destroy(elem);
82
g_queue_free(trans->queue);
86
if (trans->callbacks != NULL && trans->has_custom_callbacks)
87
g_hash_table_destroy(trans->callbacks);
90
purple_timeout_remove(trans->timer);
96
msn_transaction_to_string(MsnTransaction *trans)
100
g_return_val_if_fail(trans != NULL, FALSE);
102
if (trans->params != NULL)
103
str = g_strdup_printf("%s %u %s\r\n", trans->command, trans->trId, trans->params);
104
else if (trans->saveable)
105
str = g_strdup_printf("%s %u\r\n", trans->command, trans->trId);
107
str = g_strdup_printf("%s\r\n", trans->command);
113
msn_transaction_queue_cmd(MsnTransaction *trans, MsnCommand *cmd)
115
purple_debug_info("msn", "queueing command.\n");
116
trans->pendent_cmd = cmd;
117
msn_command_ref(cmd);
121
msn_transaction_unqueue_cmd(MsnTransaction *trans, MsnCmdProc *cmdproc)
125
if (!cmdproc->servconn->connected)
128
purple_debug_info("msn", "unqueueing command.\n");
129
cmd = trans->pendent_cmd;
131
g_return_if_fail(cmd != NULL);
133
msn_cmdproc_process_cmd(cmdproc, cmd);
134
msn_command_unref(cmd);
136
trans->pendent_cmd = NULL;
141
msn_transaction_queue(MsnTransaction *trans, MsnTransaction *elem)
143
if (trans->queue == NULL)
144
trans->queue = g_queue_new();
146
g_queue_push_tail(trans->queue, elem);
150
msn_transaction_unqueue(MsnTransaction *trans, MsnCmdProc *cmdproc)
152
MsnTransaction *elem;
154
while ((elem = g_queue_pop_head(trans->queue)) != NULL)
155
msn_cmdproc_send_trans(cmdproc, elem);
160
msn_transaction_set_payload(MsnTransaction *trans,
161
const char *payload, int payload_len)
163
g_return_if_fail(trans != NULL);
164
g_return_if_fail(payload != NULL);
166
trans->payload = g_strdup(payload);
167
trans->payload_len = payload_len ? payload_len : strlen(trans->payload);
171
msn_transaction_set_data(MsnTransaction *trans, void *data)
173
g_return_if_fail(trans != NULL);
178
void msn_transaction_set_data_free(MsnTransaction *trans, GDestroyNotify fn)
180
g_return_if_fail(trans != NULL);
181
trans->data_free = fn;
185
msn_transaction_set_saveable(MsnTransaction *trans, gboolean saveable)
187
g_return_if_fail(trans != NULL);
189
trans->saveable = saveable;
193
msn_transaction_add_cb(MsnTransaction *trans, char *answer,
196
g_return_if_fail(trans != NULL);
197
g_return_if_fail(answer != NULL);
199
if (trans->callbacks == NULL)
201
trans->has_custom_callbacks = TRUE;
202
trans->callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
205
else if (trans->has_custom_callbacks != TRUE)
206
g_return_if_reached ();
208
g_hash_table_insert(trans->callbacks, answer, cb);
212
transaction_timeout(gpointer data)
214
MsnTransaction *trans;
217
g_return_val_if_fail(trans != NULL, FALSE);
220
purple_debug_info("msn", "timed out: %s %d %s\n", trans->command, trans->trId, trans->params);
225
if (trans->timeout_cb != NULL)
226
trans->timeout_cb(trans->cmdproc, trans);
232
msn_transaction_set_timeout_cb(MsnTransaction *trans, MsnTimeoutCb cb)
236
purple_debug_error("msn", "This shouldn't be happening\n");
237
purple_timeout_remove(trans->timer);
239
trans->timeout_cb = cb;
240
trans->timer = purple_timeout_add_seconds(60, transaction_timeout, trans);
244
msn_transaction_set_error_cb(MsnTransaction *trans, MsnErrorCb cb)
246
trans->error_cb = cb;