purple: don't use PurpleXfer.account
[siplcs.git] / src / purple / purple-ft.c
blob3097f4e279baf90e057ac00ca7aba63ff1982915
1 /**
2 * @file purple-ft.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2013 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2010 Jakub Adam <jakub.adam@ktknet.cz>
8 * Copyright (C) 2010 Tomáš Hrabčík <tomas.hrabcik@tieto.com>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
29 #include <string.h>
30 #include <fcntl.h>
31 #include <errno.h>
33 #include <glib.h>
35 #include "version.h"
36 #if PURPLE_VERSION_CHECK(3,0,0)
37 #include "xfer.h"
38 #else
39 #include "ft.h"
40 #endif
42 #ifdef _WIN32
43 /* wrappers for write() & friends for socket handling */
44 #include "win32/win32dep.h"
45 #endif
47 #include "sipe-common.h"
48 #include "sipe-backend.h"
49 #include "sipe-core.h"
51 #include "purple-private.h"
53 #define FT_TO_PURPLE_XFER ((PurpleXfer *) ft->backend_private)
54 #if PURPLE_VERSION_CHECK(3,0,0)
55 #define PURPLE_XFER_DATA purple_xfer_get_protocol_data(xfer)
56 #define PURPLE_XFER_TO_SIPE_CORE_PUBLIC ((struct sipe_core_public *) purple_connection_get_protocol_data(purple_account_get_connection(purple_xfer_get_account(xfer))))
57 #else
58 #define PURPLE_XFER_DATA xfer->data
59 #define PURPLE_XFER_TO_SIPE_CORE_PUBLIC ((struct sipe_core_public *) purple_account_get_connection(xfer->account)->proto_data)
60 #endif
61 #define PURPLE_XFER_TO_SIPE_FILE_TRANSFER ((struct sipe_file_transfer *) PURPLE_XFER_DATA)
63 void sipe_backend_ft_error(struct sipe_file_transfer *ft,
64 const char *errmsg)
66 PurpleXfer *xfer = FT_TO_PURPLE_XFER;
67 purple_xfer_error(purple_xfer_get_type(xfer),
68 purple_xfer_get_account(xfer),
69 xfer->who,
70 errmsg);
73 const gchar *sipe_backend_ft_get_error(SIPE_UNUSED_PARAMETER struct sipe_file_transfer *ft)
75 return strerror(errno);
78 void sipe_backend_ft_deallocate(struct sipe_file_transfer *ft)
80 PurpleXfer *xfer = FT_TO_PURPLE_XFER;
81 PurpleXferStatusType status = purple_xfer_get_status(xfer);
83 // If file transfer is not finished, cancel it
84 if ( status != PURPLE_XFER_STATUS_DONE
85 && status != PURPLE_XFER_STATUS_CANCEL_LOCAL
86 && status != PURPLE_XFER_STATUS_CANCEL_REMOTE) {
87 purple_xfer_set_cancel_recv_fnc(xfer, NULL);
88 purple_xfer_set_cancel_send_fnc(xfer, NULL);
89 purple_xfer_cancel_remote(xfer);
93 gssize sipe_backend_ft_read(struct sipe_file_transfer *ft,
94 guchar *data,
95 gsize size)
97 gssize bytes_read = read(FT_TO_PURPLE_XFER->fd, data, size);
98 if (bytes_read == 0) {
99 /* Sender canceled transfer before it was finished */
100 return -2;
101 } else if (bytes_read == -1) {
102 if (errno == EAGAIN)
103 return 0;
104 else
105 return -1;
107 return bytes_read;
110 gssize sipe_backend_ft_write(struct sipe_file_transfer *ft,
111 const guchar *data,
112 gsize size)
114 gssize bytes_written = write(FT_TO_PURPLE_XFER->fd, data, size);
115 if (bytes_written == -1) {
116 if (errno == EAGAIN)
117 return 0;
118 else
119 return -1;
121 return bytes_written;
124 void sipe_backend_ft_cancel_local(struct sipe_file_transfer *ft)
126 purple_xfer_cancel_local(FT_TO_PURPLE_XFER);
129 void sipe_backend_ft_cancel_remote(struct sipe_file_transfer *ft)
131 purple_xfer_cancel_remote(FT_TO_PURPLE_XFER);
134 static void
135 ft_free_xfer_struct(PurpleXfer *xfer)
137 struct sipe_file_transfer *ft = PURPLE_XFER_TO_SIPE_FILE_TRANSFER;
139 if (ft) {
140 if (xfer->watcher) {
141 purple_input_remove(xfer->watcher);
142 xfer->watcher = 0;
144 sipe_core_ft_deallocate(ft);
145 PURPLE_XFER_DATA = NULL;
149 static void
150 ft_request_denied(PurpleXfer *xfer)
152 if (xfer->type == PURPLE_XFER_RECEIVE)
153 sipe_core_ft_cancel(PURPLE_XFER_TO_SIPE_FILE_TRANSFER);
154 ft_free_xfer_struct(xfer);
157 static void
158 ft_incoming_init(PurpleXfer *xfer)
160 sipe_core_ft_incoming_init(PURPLE_XFER_TO_SIPE_FILE_TRANSFER);
163 static void
164 tftp_incoming_start(PurpleXfer *xfer)
166 sipe_core_tftp_incoming_start(PURPLE_XFER_TO_SIPE_FILE_TRANSFER,
167 xfer->size);
170 static void
171 tftp_incoming_stop(PurpleXfer *xfer)
173 if (sipe_core_tftp_incoming_stop(PURPLE_XFER_TO_SIPE_FILE_TRANSFER)) {
174 /* We're done with this transfer */
175 ft_free_xfer_struct(xfer);
176 } else {
177 unlink(xfer->local_filename);
181 static gssize
182 tftp_read(guchar **buffer, PurpleXfer *xfer)
184 return sipe_core_tftp_read(PURPLE_XFER_TO_SIPE_FILE_TRANSFER,
185 buffer,
186 purple_xfer_get_bytes_remaining(xfer),
187 xfer->current_buffer_size);
190 static void
191 ft_outgoing_init(PurpleXfer *xfer)
193 sipe_core_ft_outgoing_init(PURPLE_XFER_TO_SIPE_FILE_TRANSFER,
194 purple_xfer_get_filename(xfer),
195 purple_xfer_get_size(xfer),
196 xfer->who);
199 static void
200 tftp_outgoing_start(PurpleXfer *xfer)
202 /* Set socket to non-blocking mode */
203 int flags = fcntl(xfer->fd, F_GETFL, 0);
204 if (flags == -1)
205 flags = 0;
206 /* @TODO: ignoring potential error return - how to handle? */
207 (void) fcntl(xfer->fd, F_SETFL, flags | O_NONBLOCK);
209 sipe_core_tftp_outgoing_start(PURPLE_XFER_TO_SIPE_FILE_TRANSFER,
210 xfer->size);
213 static void
214 tftp_outgoing_stop(PurpleXfer *xfer)
216 if (sipe_core_tftp_outgoing_stop(PURPLE_XFER_TO_SIPE_FILE_TRANSFER)) {
217 /* We're done with this transfer */
218 ft_free_xfer_struct(xfer);
222 static gssize
223 tftp_write(const guchar *buffer, size_t size, PurpleXfer *xfer)
225 gssize bytes_written = sipe_core_tftp_write(PURPLE_XFER_TO_SIPE_FILE_TRANSFER,
226 buffer, size);
228 if ((xfer->bytes_remaining - bytes_written) == 0)
229 purple_xfer_set_completed(xfer, TRUE);
231 return bytes_written;
234 //******************************************************************************
236 void sipe_backend_ft_incoming(struct sipe_core_public *sipe_public,
237 struct sipe_file_transfer *ft,
238 const gchar *who,
239 const gchar *file_name,
240 gsize file_size)
242 struct sipe_backend_private *purple_private = sipe_public->backend_private;
243 PurpleXfer *xfer;
245 xfer = purple_xfer_new(purple_private->account,
246 PURPLE_XFER_RECEIVE,
247 who);
249 if (xfer) {
250 ft->backend_private = (struct sipe_backend_file_transfer *)xfer;
251 PURPLE_XFER_DATA = ft;
253 purple_xfer_set_filename(xfer, file_name);
254 purple_xfer_set_size(xfer, file_size);
256 purple_xfer_set_init_fnc(xfer, ft_incoming_init);
257 purple_xfer_set_request_denied_fnc(xfer, ft_request_denied);
258 purple_xfer_set_cancel_send_fnc(xfer, ft_free_xfer_struct);
259 purple_xfer_set_cancel_recv_fnc(xfer, ft_free_xfer_struct);
260 purple_xfer_set_start_fnc(xfer, tftp_incoming_start);
261 purple_xfer_set_end_fnc(xfer, tftp_incoming_stop);
262 purple_xfer_set_read_fnc(xfer, tftp_read);
264 purple_xfer_request(xfer);
268 static void
269 connect_cb(gpointer data, gint fd, SIPE_UNUSED_PARAMETER const gchar *error_message)
271 struct sipe_file_transfer *ft = data;
273 if (fd < 0) {
274 purple_xfer_cancel_local(FT_TO_PURPLE_XFER);
275 return;
278 purple_xfer_start(FT_TO_PURPLE_XFER, fd, NULL, 0);
281 void
282 sipe_backend_ft_start(struct sipe_file_transfer *ft, struct sipe_backend_fd *fd,
283 const char* ip, unsigned port)
285 if (ip && port && !sipe_backend_ft_is_incoming(ft)) {
286 /* Purple accepts ip & port only for incoming file transfers.
287 * If we want to send file with Sender-Connect = TRUE negotiated,
288 * we have to open the connection ourselves and pass the file
289 * descriptor to purple_xfer_start. */
290 purple_proxy_connect(NULL,
291 purple_xfer_get_account(FT_TO_PURPLE_XFER),
293 port,
294 connect_cb,
295 ft);
296 return;
299 purple_xfer_start(FT_TO_PURPLE_XFER, fd ? fd->fd : -1, ip, port);
302 void sipe_purple_ft_send_file(PurpleConnection *gc,
303 const char *who,
304 const char *file)
306 PurpleXfer *xfer = sipe_purple_ft_new_xfer(gc, who);
308 if (xfer) {
309 if (file != NULL)
310 purple_xfer_request_accepted(xfer, file);
311 else
312 purple_xfer_request(xfer);
316 PurpleXfer *sipe_purple_ft_new_xfer(PurpleConnection *gc, const char *who)
318 PurpleXfer *xfer = NULL;
320 if (PURPLE_CONNECTION_IS_VALID(gc)) {
321 xfer = purple_xfer_new(purple_connection_get_account(gc),
322 PURPLE_XFER_SEND, who);
324 if (xfer) {
325 struct sipe_file_transfer *ft = sipe_core_ft_allocate(PURPLE_GC_TO_SIPE_CORE_PUBLIC);
327 ft->backend_private = (struct sipe_backend_file_transfer *)xfer;
328 PURPLE_XFER_DATA = ft;
330 purple_xfer_set_init_fnc(xfer, ft_outgoing_init);
331 purple_xfer_set_request_denied_fnc(xfer, ft_request_denied);
332 purple_xfer_set_cancel_send_fnc(xfer, ft_free_xfer_struct);
333 purple_xfer_set_cancel_recv_fnc(xfer, ft_free_xfer_struct);
334 purple_xfer_set_start_fnc(xfer, tftp_outgoing_start);
335 purple_xfer_set_end_fnc(xfer, tftp_outgoing_stop);
336 purple_xfer_set_write_fnc(xfer, tftp_write);
340 return xfer;
343 gboolean
344 sipe_backend_ft_is_incoming(struct sipe_file_transfer *ft)
346 return purple_xfer_get_type(FT_TO_PURPLE_XFER) == PURPLE_XFER_RECEIVE;
350 Local Variables:
351 mode: c
352 c-file-style: "bsd"
353 indent-tabs-mode: t
354 tab-width: 8
355 End: