source: protocols/purple/ft.c @ db5ef3a

Last change on this file since db5ef3a was 98d46d5, checked in by dequis <dx@…>, at 2015-12-26T03:18:53Z

purple: document original names of uiops next to them

  • Property mode set to 100644
File size: 9.5 KB
RevLine 
[2309152]1/***************************************************************************\
2*                                                                           *
3*  BitlBee - An IRC to IM gateway                                           *
4*  libpurple module - File transfer stuff                                   *
5*                                                                           *
6*  Copyright 2009-2010 Wilmer van der Gaast <wilmer@gaast.net>              *
7*                                                                           *
8*  This program is free software; you can redistribute it and/or modify     *
9*  it under the terms of the GNU General Public License as published by     *
10*  the Free Software Foundation; either version 2 of the License, or        *
11*  (at your option) any later version.                                      *
12*                                                                           *
13*  This program is distributed in the hope that it will be useful,          *
14*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
15*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
16*  GNU General Public License for more details.                             *
17*                                                                           *
18*  You should have received a copy of the GNU General Public License along  *
19*  with this program; if not, write to the Free Software Foundation, Inc.,  *
20*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.              *
21*                                                                           *
22\***************************************************************************/
23
[553767c]24/* Do file transfers via disk for now, since libpurple was really designed
25   for straight-to/from disk fts and is only just learning how to pass the
26   file contents the the UI instead (2.6.0 and higher it seems, and with
27   varying levels of success). */
28
[2309152]29#include "bitlbee.h"
[d93c8beb]30#include "bpurple.h"
[2309152]31
32#include <stdarg.h>
33
34#include <glib.h>
35#include <purple.h>
36
[5ebff60]37struct prpl_xfer_data {
[2309152]38        PurpleXfer *xfer;
39        file_transfer_t *ft;
[e7dc02a]40        struct im_connection *ic;
[8822d23]41        int fd;
[75c3ff7]42        char *fn, *handle;
[8822d23]43        gboolean ui_wants_data;
[2309152]44};
45
46static file_transfer_t *next_ft;
47
[5ebff60]48struct im_connection *purple_ic_by_pa(PurpleAccount *pa);
49static gboolean prplcb_xfer_new_send_cb(gpointer data, gint fd, b_input_condition cond);
50static gboolean prpl_xfer_write_request(struct file_transfer *ft);
[2309152]51
52
[c96c72f]53/* Receiving files (IM->UI): */
[5ebff60]54static void prpl_xfer_accept(struct file_transfer *ft)
[2309152]55{
56        struct prpl_xfer_data *px = ft->data;
[5ebff60]57
58        purple_xfer_request_accepted(px->xfer, NULL);
59        prpl_xfer_write_request(ft);
[2309152]60}
61
[5ebff60]62static void prpl_xfer_canceled(struct file_transfer *ft, char *reason)
[2309152]63{
64        struct prpl_xfer_data *px = ft->data;
[5ebff60]65
66        purple_xfer_request_denied(px->xfer);
[2309152]67}
68
[5ebff60]69static void prplcb_xfer_new(PurpleXfer *xfer)
[2309152]70{
[5ebff60]71        if (purple_xfer_get_type(xfer) == PURPLE_XFER_RECEIVE) {
72                struct prpl_xfer_data *px = g_new0(struct prpl_xfer_data, 1);
73
[8822d23]74                xfer->ui_data = px;
75                px->xfer = xfer;
[5ebff60]76                px->fn = mktemp(g_strdup("/tmp/bitlbee-purple-ft.XXXXXX"));
[8822d23]77                px->fd = -1;
[5ebff60]78                px->ic = purple_ic_by_pa(xfer->account);
79
80                purple_xfer_set_local_filename(xfer, px->fn);
81
[2309152]82                /* Sadly the xfer struct is still empty ATM so come back after
83                   the caller is done. */
[5ebff60]84                b_timeout_add(0, prplcb_xfer_new_send_cb, xfer);
85        } else {
[e7dc02a]86                struct file_transfer *ft = next_ft;
87                struct prpl_xfer_data *px = ft->data;
[5ebff60]88
[e7dc02a]89                xfer->ui_data = px;
[2309152]90                px->xfer = xfer;
[5ebff60]91
[2309152]92                next_ft = NULL;
93        }
94}
95
[5ebff60]96static gboolean prplcb_xfer_new_send_cb(gpointer data, gint fd, b_input_condition cond)
[2309152]97{
[553767c]98        PurpleXfer *xfer = data;
[5ebff60]99        struct im_connection *ic = purple_ic_by_pa(xfer->account);
[8822d23]100        struct prpl_xfer_data *px = xfer->ui_data;
[553767c]101        PurpleBuddy *buddy;
102        const char *who;
[5ebff60]103
104        buddy = purple_find_buddy(xfer->account, xfer->who);
105        who = buddy ? purple_buddy_get_name(buddy) : xfer->who;
106
[553767c]107        /* TODO(wilmer): After spreading some more const goodness in BitlBee,
108           remove the evil cast below. */
[5ebff60]109        px->ft = imcb_file_send_start(ic, (char *) who, xfer->filename, xfer->size);
[553767c]110        px->ft->data = px;
[5ebff60]111
[553767c]112        px->ft->accept = prpl_xfer_accept;
113        px->ft->canceled = prpl_xfer_canceled;
114        px->ft->write_request = prpl_xfer_write_request;
[5ebff60]115
[553767c]116        return FALSE;
[2309152]117}
118
[5ebff60]119gboolean try_write_to_ui(gpointer data, gint fd, b_input_condition cond)
[c96c72f]120{
121        struct file_transfer *ft = data;
122        struct prpl_xfer_data *px = ft->data;
123        struct stat fs;
124        off_t tx_bytes;
[5ebff60]125
[c96c72f]126        /* If we don't have the file opened yet, there's no data so wait. */
[5ebff60]127        if (px->fd < 0 || !px->ui_wants_data) {
[c96c72f]128                return FALSE;
[5ebff60]129        }
130
131        tx_bytes = lseek(px->fd, 0, SEEK_CUR);
132        fstat(px->fd, &fs);
133
134        if (fs.st_size > tx_bytes) {
[c96c72f]135                char buf[1024];
[5ebff60]136                size_t n = MIN(fs.st_size - tx_bytes, sizeof(buf));
137
138                if (read(px->fd, buf, n) == n && ft->write(ft, buf, n)) {
[c96c72f]139                        px->ui_wants_data = FALSE;
[5ebff60]140                } else {
141                        purple_xfer_cancel_local(px->xfer);
142                        imcb_file_canceled(px->ic, ft, "Read error");
[c96c72f]143                }
144        }
[5ebff60]145
146        if (lseek(px->fd, 0, SEEK_CUR) == px->xfer->size) {
[75c3ff7]147                /*purple_xfer_end( px->xfer );*/
[5ebff60]148                imcb_file_finished(px->ic, ft);
[c96c72f]149        }
[5ebff60]150
[c96c72f]151        return FALSE;
152}
153
154/* UI calls this when its buffer is empty and wants more data to send to the user. */
[5ebff60]155static gboolean prpl_xfer_write_request(struct file_transfer *ft)
[c96c72f]156{
157        struct prpl_xfer_data *px = ft->data;
[5ebff60]158
[c96c72f]159        px->ui_wants_data = TRUE;
[5ebff60]160        try_write_to_ui(ft, 0, 0);
161
[c96c72f]162        return FALSE;
163}
164
165
166/* Generic (IM<>UI): */
[5ebff60]167static void prplcb_xfer_destroy(PurpleXfer *xfer)
[8822d23]168{
169        struct prpl_xfer_data *px = xfer->ui_data;
[5ebff60]170
171        g_free(px->fn);
172        g_free(px->handle);
173        if (px->fd >= 0) {
174                close(px->fd);
175        }
176        g_free(px);
[8822d23]177}
178
[5ebff60]179static void prplcb_xfer_progress(PurpleXfer *xfer, double percent)
[2309152]180{
[8822d23]181        struct prpl_xfer_data *px = xfer->ui_data;
[5ebff60]182
183        if (px == NULL) {
[e7dc02a]184                return;
[5ebff60]185        }
186
187        if (purple_xfer_get_type(xfer) == PURPLE_XFER_SEND) {
188                if (*px->fn) {
[75c3ff7]189                        char *slash;
[5ebff60]190
191                        unlink(px->fn);
192                        if ((slash = strrchr(px->fn, '/'))) {
[75c3ff7]193                                *slash = '\0';
[5ebff60]194                                rmdir(px->fn);
[75c3ff7]195                        }
[e7dc02a]196                        *px->fn = '\0';
197                }
[5ebff60]198
[e7dc02a]199                return;
200        }
[5ebff60]201
202        if (px->fd == -1 && percent > 0) {
[8822d23]203                /* Weeeeeeeee, we're getting data! That means the file exists
204                   by now so open it and start sending to the UI. */
[5ebff60]205                px->fd = open(px->fn, O_RDONLY);
206
[8822d23]207                /* Unlink it now, because we don't need it after this. */
[5ebff60]208                unlink(px->fn);
[8822d23]209        }
[5ebff60]210
211        if (percent < 1) {
212                try_write_to_ui(px->ft, 0, 0);
213        } else {
[c96c72f]214                /* Another nice problem: If we have the whole file, it only
215                   gets closed when we return. Problem: There may still be
216                   stuff buffered and not written, we'll only see it after
217                   the caller close()s the file. So poll the file after that. */
[5ebff60]218                b_timeout_add(0, try_write_to_ui, px->ft);
219        }
[8822d23]220}
221
[5ebff60]222static void prplcb_xfer_cancel_remote(PurpleXfer *xfer)
[8822d23]223{
224        struct prpl_xfer_data *px = xfer->ui_data;
[5ebff60]225
226        if (px->ft) {
227                imcb_file_canceled(px->ic, px->ft, "Canceled by remote end");
228        } else {
[75c3ff7]229                /* px->ft == NULL for sends, because of the two stages. :-/ */
[5ebff60]230                imcb_error(px->ic, "File transfer cancelled by remote end");
231        }
[e7dc02a]232}
233
[c96c72f]234
235/* Sending files (UI->IM): */
[5ebff60]236static gboolean prpl_xfer_write(struct file_transfer *ft, char *buffer, unsigned int len);
237static gboolean purple_transfer_request_cb(gpointer data, gint fd, b_input_condition cond);
[e7dc02a]238
[5ebff60]239void purple_transfer_request(struct im_connection *ic, file_transfer_t *ft, char *handle)
[2309152]240{
[5ebff60]241        struct prpl_xfer_data *px = g_new0(struct prpl_xfer_data, 1);
[75c3ff7]242        char *dir, *basename;
[5ebff60]243
[e7dc02a]244        ft->data = px;
245        px->ft = ft;
[5ebff60]246
247        dir = g_strdup("/tmp/bitlbee-purple-ft.XXXXXX");
248        if (!mkdtemp(dir)) {
249                imcb_error(ic, "Could not create temporary file for file transfer");
250                g_free(px);
251                g_free(dir);
[75c3ff7]252                return;
253        }
[5ebff60]254
255        if ((basename = strrchr(ft->file_name, '/'))) {
[75c3ff7]256                basename++;
[5ebff60]257        } else {
[75c3ff7]258                basename = ft->file_name;
[5ebff60]259        }
260        px->fn = g_strdup_printf("%s/%s", dir, basename);
261        px->fd = open(px->fn, O_WRONLY | O_CREAT, 0600);
262        g_free(dir);
263
264        if (px->fd < 0) {
265                imcb_error(ic, "Could not create temporary file for file transfer");
266                g_free(px);
267                g_free(px->fn);
[75c3ff7]268                return;
269        }
[5ebff60]270
[e7dc02a]271        px->ic = ic;
[5ebff60]272        px->handle = g_strdup(handle);
273
274        imcb_log(ic,
275                 "Due to libpurple limitations, the file has to be cached locally before proceeding with the actual file transfer. Please wait...");
276
277        b_timeout_add(0, purple_transfer_request_cb, ft);
[c96c72f]278}
[2309152]279
[5ebff60]280static void purple_transfer_forward(struct file_transfer *ft)
[2309152]281{
[e7dc02a]282        struct prpl_xfer_data *px = ft->data;
[d93c8beb]283        struct purple_data *pd = px->ic->proto_data;
[5ebff60]284
[2309152]285        /* xfer_new() will pick up this variable. It's a hack but we're not
286           multi-threaded anyway. */
287        next_ft = ft;
[d93c8beb]288        serv_send_file(purple_account_get_connection(pd->account),
289                   px->handle, px->fn);
[e7dc02a]290}
291
[5ebff60]292static gboolean purple_transfer_request_cb(gpointer data, gint fd, b_input_condition cond)
[e7dc02a]293{
294        file_transfer_t *ft = data;
[4aa0f6b]295        struct prpl_xfer_data *px = ft->data;
[5ebff60]296
297        if (ft->write == NULL) {
[e7dc02a]298                ft->write = prpl_xfer_write;
[5ebff60]299                imcb_file_recv_start(px->ic, ft);
[e7dc02a]300        }
[5ebff60]301
302        ft->write_request(ft);
303
[e7dc02a]304        return FALSE;
[2309152]305}
[c96c72f]306
[5ebff60]307static gboolean prpl_xfer_write(struct file_transfer *ft, char *buffer, unsigned int len)
[e7dc02a]308{
309        struct prpl_xfer_data *px = ft->data;
[5ebff60]310
311        if (write(px->fd, buffer, len) != len) {
312                imcb_file_canceled(px->ic, ft, "Error while writing temporary file");
[e7dc02a]313                return FALSE;
314        }
[5ebff60]315
316        if (lseek(px->fd, 0, SEEK_CUR) >= ft->file_size) {
317                close(px->fd);
[e7dc02a]318                px->fd = -1;
[5ebff60]319
320                purple_transfer_forward(ft);
321                imcb_file_finished(px->ic, ft);
[e7dc02a]322                px->ft = NULL;
[5ebff60]323        } else {
324                b_timeout_add(0, purple_transfer_request_cb, ft);
[e7dc02a]325        }
[5ebff60]326
[e7dc02a]327        return TRUE;
328}
[c96c72f]329
330
331
332PurpleXferUiOps bee_xfer_uiops =
333{
[98d46d5]334        prplcb_xfer_new,           /* new_xfer */
335        prplcb_xfer_destroy,       /* destroy */
336        NULL,                      /* add_xfer */
337        prplcb_xfer_progress,      /* update_progress */
338        NULL,                      /* cancel_local */
339        prplcb_xfer_cancel_remote, /* cancel_remote */
340        NULL,                      /* ui_write */
341        NULL,                      /* ui_read */
342        NULL,                      /* data_not_sent */
[c96c72f]343};
Note: See TracBrowser for help on using the repository browser.