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
36 #if PURPLE_VERSION_CHECK(3,0,0)
43 /* wrappers for write() & friends for socket handling */
44 #include "win32/win32dep.h"
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))))
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)
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
,
66 PurpleXfer
*xfer
= FT_TO_PURPLE_XFER
;
67 purple_xfer_error(purple_xfer_get_type(xfer
),
68 purple_xfer_get_account(xfer
),
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
,
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 */
101 } else if (bytes_read
== -1) {
110 gssize
sipe_backend_ft_write(struct sipe_file_transfer
*ft
,
114 gssize bytes_written
= write(FT_TO_PURPLE_XFER
->fd
, data
, size
);
115 if (bytes_written
== -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
);
135 ft_free_xfer_struct(PurpleXfer
*xfer
)
137 struct sipe_file_transfer
*ft
= PURPLE_XFER_TO_SIPE_FILE_TRANSFER
;
141 purple_input_remove(xfer
->watcher
);
144 sipe_core_ft_deallocate(ft
);
145 PURPLE_XFER_DATA
= NULL
;
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
);
158 ft_incoming_init(PurpleXfer
*xfer
)
160 sipe_core_ft_incoming_init(PURPLE_XFER_TO_SIPE_FILE_TRANSFER
);
164 tftp_incoming_start(PurpleXfer
*xfer
)
166 sipe_core_tftp_incoming_start(PURPLE_XFER_TO_SIPE_FILE_TRANSFER
,
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
);
177 unlink(xfer
->local_filename
);
182 tftp_read(guchar
**buffer
, PurpleXfer
*xfer
)
184 return sipe_core_tftp_read(PURPLE_XFER_TO_SIPE_FILE_TRANSFER
,
186 purple_xfer_get_bytes_remaining(xfer
),
187 xfer
->current_buffer_size
);
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
),
200 tftp_outgoing_start(PurpleXfer
*xfer
)
202 /* Set socket to non-blocking mode */
203 int flags
= fcntl(xfer
->fd
, F_GETFL
, 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
,
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
);
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
,
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
,
239 const gchar
*file_name
,
242 struct sipe_backend_private
*purple_private
= sipe_public
->backend_private
;
245 xfer
= purple_xfer_new(purple_private
->account
,
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
);
269 connect_cb(gpointer data
, gint fd
, SIPE_UNUSED_PARAMETER
const gchar
*error_message
)
271 struct sipe_file_transfer
*ft
= data
;
274 purple_xfer_cancel_local(FT_TO_PURPLE_XFER
);
278 purple_xfer_start(FT_TO_PURPLE_XFER
, fd
, NULL
, 0);
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
),
299 purple_xfer_start(FT_TO_PURPLE_XFER
, fd
? fd
->fd
: -1, ip
, port
);
302 void sipe_purple_ft_send_file(PurpleConnection
*gc
,
306 PurpleXfer
*xfer
= sipe_purple_ft_new_xfer(gc
, who
);
310 purple_xfer_request_accepted(xfer
, file
);
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
);
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
);
344 sipe_backend_ft_is_incoming(struct sipe_file_transfer
*ft
)
346 return purple_xfer_get_type(FT_TO_PURPLE_XFER
) == PURPLE_XFER_RECEIVE
;