-
Notifications
You must be signed in to change notification settings - Fork 0
/
xfer.c
235 lines (190 loc) · 6.04 KB
/
xfer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* @file xfer.c MSN File Transfer functions
*
* purple
*
* Purple is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "internal.h"
#include "debug.h"
#include "msnutils.h"
#include "sbconn.h"
#include "xfer.h"
/**************************************************************************
* Xfer
**************************************************************************/
void
msn_xfer_init(PurpleXfer *xfer)
{
MsnSlpCall *slpcall;
/* MsnSlpLink *slplink; */
char *content;
purple_debug_info("msn", "xfer_init\n");
slpcall = xfer->data;
/* Send Ok */
content = g_strdup_printf("SessionID: %lu\r\n\r\n",
slpcall->session_id);
msn_slp_send_ok(slpcall, slpcall->branch, "application/x-msnmsgr-sessionreqbody",
content);
g_free(content);
msn_slplink_send_queued_slpmsgs(slpcall->slplink);
}
void
msn_xfer_cancel(PurpleXfer *xfer)
{
MsnSlpCall *slpcall;
char *content;
g_return_if_fail(xfer != NULL);
g_return_if_fail(xfer->data != NULL);
slpcall = xfer->data;
if (purple_xfer_get_status(xfer) == PURPLE_XFER_STATUS_CANCEL_LOCAL)
{
if (slpcall->started)
{
msn_slpcall_close(slpcall);
}
else
{
content = g_strdup_printf("SessionID: %lu\r\n\r\n",
slpcall->session_id);
msn_slp_send_decline(slpcall, slpcall->branch, "application/x-msnmsgr-sessionreqbody",
content);
g_free(content);
msn_slplink_send_queued_slpmsgs(slpcall->slplink);
if (purple_xfer_get_type(xfer) == PURPLE_XFER_SEND)
slpcall->wasted = TRUE;
else
msn_slpcall_destroy(slpcall);
}
}
}
gssize
msn_xfer_write(const guchar *data, gsize len, PurpleXfer *xfer)
{
MsnSlpCall *slpcall;
g_return_val_if_fail(xfer != NULL, -1);
g_return_val_if_fail(data != NULL, -1);
g_return_val_if_fail(len > 0, -1);
g_return_val_if_fail(purple_xfer_get_type(xfer) == PURPLE_XFER_SEND, -1);
slpcall = xfer->data;
/* Not sure I trust it'll be there */
g_return_val_if_fail(slpcall != NULL, -1);
g_return_val_if_fail(slpcall->xfer_msg != NULL, -1);
slpcall->u.outgoing.len = len;
slpcall->u.outgoing.data = data;
msn_slplink_send_msgpart(slpcall->slplink, slpcall->xfer_msg);
return MIN(MSN_SBCONN_MAX_SIZE, len);
}
gssize
msn_xfer_read(guchar **data, PurpleXfer *xfer)
{
MsnSlpCall *slpcall;
gsize len;
g_return_val_if_fail(xfer != NULL, -1);
g_return_val_if_fail(data != NULL, -1);
g_return_val_if_fail(purple_xfer_get_type(xfer) == PURPLE_XFER_RECEIVE, -1);
slpcall = xfer->data;
/* Not sure I trust it'll be there */
g_return_val_if_fail(slpcall != NULL, -1);
/* Just pass up the whole GByteArray. We'll make another. */
*data = slpcall->u.incoming_data->data;
len = slpcall->u.incoming_data->len;
g_byte_array_free(slpcall->u.incoming_data, FALSE);
slpcall->u.incoming_data = g_byte_array_new();
return len;
}
void
msn_xfer_end_cb(MsnSlpCall *slpcall, MsnSession *session)
{
if ((purple_xfer_get_status(slpcall->xfer) != PURPLE_XFER_STATUS_DONE) &&
(purple_xfer_get_status(slpcall->xfer) != PURPLE_XFER_STATUS_CANCEL_REMOTE) &&
(purple_xfer_get_status(slpcall->xfer) != PURPLE_XFER_STATUS_CANCEL_LOCAL))
{
purple_xfer_cancel_remote(slpcall->xfer);
}
}
void
msn_xfer_completed_cb(MsnSlpCall *slpcall, const guchar *body,
gsize size)
{
PurpleXfer *xfer = slpcall->xfer;
purple_xfer_set_completed(xfer, TRUE);
purple_xfer_end(xfer);
}
gchar *
msn_file_context_to_wire(MsnFileContext *context)
{
gchar *ret, *tmp;
tmp = ret = g_new(gchar, MSN_FILE_CONTEXT_SIZE + context->preview_len + 1);
msn_push32le(tmp, context->length);
msn_push32le(tmp, context->version);
msn_push64le(tmp, context->file_size);
msn_push32le(tmp, context->type);
memcpy(tmp, context->file_name, MAX_FILE_NAME_LEN * 2);
tmp += MAX_FILE_NAME_LEN * 2;
memcpy(tmp, context->unknown1, sizeof(context->unknown1));
tmp += sizeof(context->unknown1);
msn_push32le(tmp, context->unknown2);
if (context->preview) {
memcpy(tmp, context->preview, context->preview_len);
}
tmp[context->preview_len] = '\0';
return ret;
}
MsnFileContext *
msn_file_context_from_wire(const char *buf, gsize len)
{
MsnFileContext *context;
if (!buf || len < MSN_FILE_CONTEXT_SIZE)
return NULL;
context = g_new(MsnFileContext, 1);
context->length = msn_pop32le(buf);
context->version = msn_pop32le(buf);
if (context->version == 2) {
/* The length field is broken for this version. No check. */
context->length = MSN_FILE_CONTEXT_SIZE;
} else if (context->version == 3) {
if (context->length != MSN_FILE_CONTEXT_SIZE + 63) {
g_free(context);
return NULL;
} else if (len < MSN_FILE_CONTEXT_SIZE + 63) {
g_free(context);
return NULL;
}
} else {
purple_debug_warning("msn", "Received MsnFileContext with unknown version: %d\n", context->version);
g_free(context);
return NULL;
}
context->file_size = msn_pop64le(buf);
context->type = msn_pop32le(buf);
memcpy(context->file_name, buf, MAX_FILE_NAME_LEN * 2);
buf += MAX_FILE_NAME_LEN * 2;
memcpy(context->unknown1, buf, sizeof(context->unknown1));
buf += sizeof(context->unknown1);
context->unknown2 = msn_pop32le(buf);
if (context->type == 0 && len > context->length) {
context->preview_len = len - context->length;
context->preview = g_memdup(buf, context->preview_len);
} else {
context->preview_len = 0;
context->preview = NULL;
}
return context;
}