source: protocols/purple/ft-direct.c @ eb73d05

Last change on this file since eb73d05 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: 7.1 KB
RevLine 
[553767c]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
24/* This code tries to do direct file transfers, i.e. without caching the file
25   locally on disk first. Since libpurple can only do this since version 2.6.0
26   and even then very unreliably (and not with all IM modules), I'm canning
27   this code for now. */
28
29#include "bitlbee.h"
[d93c8beb]30#include "bpurple.h"
[553767c]31
32#include <stdarg.h>
33
34#include <glib.h>
35#include <purple.h>
36
[5ebff60]37struct prpl_xfer_data {
[553767c]38        PurpleXfer *xfer;
39        file_transfer_t *ft;
40        gint ready_timer;
41        char *buf;
42        int buf_len;
43};
44
45static file_transfer_t *next_ft;
46
[5ebff60]47struct im_connection *purple_ic_by_pa(PurpleAccount *pa);
[553767c]48
49/* Glorious hack: We seem to have to remind at least some libpurple plugins
50   that we're ready because this info may get lost if we give it too early.
51   So just do it ten times a second. :-/ */
[5ebff60]52static gboolean prplcb_xfer_write_request_cb(gpointer data, gint fd, b_input_condition cond)
[553767c]53{
54        struct prpl_xfer_data *px = data;
[5ebff60]55
56        purple_xfer_ui_ready(px->xfer);
57
58        return purple_xfer_get_type(px->xfer) == PURPLE_XFER_RECEIVE;
[553767c]59}
60
[5ebff60]61static gboolean prpl_xfer_write_request(struct file_transfer *ft)
[553767c]62{
63        struct prpl_xfer_data *px = ft->data;
[5ebff60]64
65        px->ready_timer = b_timeout_add(100, prplcb_xfer_write_request_cb, px);
[553767c]66        return TRUE;
67}
68
[5ebff60]69static gboolean prpl_xfer_write(struct file_transfer *ft, char *buffer, unsigned int len)
[553767c]70{
71        struct prpl_xfer_data *px = ft->data;
[5ebff60]72
73        px->buf = g_memdup(buffer, len);
[553767c]74        px->buf_len = len;
75
76        //purple_xfer_ui_ready( px->xfer );
[5ebff60]77        px->ready_timer = b_timeout_add(0, prplcb_xfer_write_request_cb, px);
78
[553767c]79        return TRUE;
80}
81
[5ebff60]82static void prpl_xfer_accept(struct file_transfer *ft)
[553767c]83{
84        struct prpl_xfer_data *px = ft->data;
[5ebff60]85
86        purple_xfer_request_accepted(px->xfer, NULL);
87        prpl_xfer_write_request(ft);
[553767c]88}
89
[5ebff60]90static void prpl_xfer_canceled(struct file_transfer *ft, char *reason)
[553767c]91{
92        struct prpl_xfer_data *px = ft->data;
[5ebff60]93
94        purple_xfer_request_denied(px->xfer);
[553767c]95}
96
[5ebff60]97static gboolean prplcb_xfer_new_send_cb(gpointer data, gint fd, b_input_condition cond)
[553767c]98{
99        PurpleXfer *xfer = data;
[5ebff60]100        struct im_connection *ic = purple_ic_by_pa(xfer->account);
101        struct prpl_xfer_data *px = g_new0(struct prpl_xfer_data, 1);
[553767c]102        PurpleBuddy *buddy;
103        const char *who;
[5ebff60]104
105        buddy = purple_find_buddy(xfer->account, xfer->who);
106        who = buddy ? purple_buddy_get_name(buddy) : xfer->who;
107
[553767c]108        /* TODO(wilmer): After spreading some more const goodness in BitlBee,
109           remove the evil cast below. */
[5ebff60]110        px->ft = imcb_file_send_start(ic, (char *) who, xfer->filename, xfer->size);
[553767c]111        px->ft->data = px;
112        px->xfer = data;
113        px->xfer->ui_data = px;
[5ebff60]114
[553767c]115        px->ft->accept = prpl_xfer_accept;
116        px->ft->canceled = prpl_xfer_canceled;
117        px->ft->write_request = prpl_xfer_write_request;
[5ebff60]118
[553767c]119        return FALSE;
120}
121
[5ebff60]122static void prplcb_xfer_new(PurpleXfer *xfer)
[553767c]123{
[5ebff60]124        if (purple_xfer_get_type(xfer) == PURPLE_XFER_RECEIVE) {
[553767c]125                /* This should suppress the stupid file dialog. */
[5ebff60]126                purple_xfer_set_local_filename(xfer, "/tmp/wtf123");
127
[553767c]128                /* Sadly the xfer struct is still empty ATM so come back after
129                   the caller is done. */
[5ebff60]130                b_timeout_add(0, prplcb_xfer_new_send_cb, xfer);
131        } else {
132                struct prpl_xfer_data *px = g_new0(struct prpl_xfer_data, 1);
133
[553767c]134                px->ft = next_ft;
135                px->ft->data = px;
136                px->xfer = xfer;
137                px->xfer->ui_data = px;
[5ebff60]138
139                purple_xfer_set_filename(xfer, px->ft->file_name);
140                purple_xfer_set_size(xfer, px->ft->file_size);
141
[553767c]142                next_ft = NULL;
143        }
144}
145
[5ebff60]146static void prplcb_xfer_progress(PurpleXfer *xfer, double percent)
[553767c]147{
[5ebff60]148        fprintf(stderr, "prplcb_xfer_dbg 0x%p %f\n", xfer, percent);
[553767c]149}
150
[5ebff60]151static void prplcb_xfer_dbg(PurpleXfer *xfer)
[553767c]152{
[5ebff60]153        fprintf(stderr, "prplcb_xfer_dbg 0x%p\n", xfer);
[553767c]154}
155
[5ebff60]156static gssize prplcb_xfer_write(PurpleXfer *xfer, const guchar *buffer, gssize size)
[553767c]157{
158        struct prpl_xfer_data *px = xfer->ui_data;
159        gboolean st;
[5ebff60]160
161        fprintf(stderr, "xfer_write %d %d\n", size, px->buf_len);
162
163        b_event_remove(px->ready_timer);
[553767c]164        px->ready_timer = 0;
[5ebff60]165
166        st = px->ft->write(px->ft, (char *) buffer, size);
167
168        if (st && xfer->bytes_remaining == size) {
169                imcb_file_finished(px->ft);
170        }
171
[553767c]172        return st ? size : 0;
173}
174
[5ebff60]175gssize prplcb_xfer_read(PurpleXfer *xfer, guchar **buffer, gssize size)
[553767c]176{
177        struct prpl_xfer_data *px = xfer->ui_data;
178
[5ebff60]179        fprintf(stderr, "xfer_read %d %d\n", size, px->buf_len);
180
181        if (px->buf) {
[553767c]182                *buffer = px->buf;
183                px->buf = NULL;
[5ebff60]184
185                px->ft->write_request(px->ft);
186
[553767c]187                return px->buf_len;
188        }
[5ebff60]189
[553767c]190        return 0;
191}
192
193PurpleXferUiOps bee_xfer_uiops =
194{
[98d46d5]195        prplcb_xfer_new,           /* new_xfer */
196        prplcb_xfer_dbg,           /* destroy */
197        prplcb_xfer_dbg,           /* add_xfer */
198        prplcb_xfer_progress,      /* update_progress */
199        prplcb_xfer_dbg,           /* cancel_local */
200        prplcb_xfer_dbg,           /* cancel_remote */
201        prplcb_xfer_write,         /* ui_write */
202        prplcb_xfer_read,          /* ui_read */
203        prplcb_xfer_dbg,           /* data_not_sent */
[553767c]204};
205
[5ebff60]206static gboolean prplcb_xfer_send_cb(gpointer data, gint fd, b_input_condition cond);
[553767c]207
[5ebff60]208void purple_transfer_request(struct im_connection *ic, file_transfer_t *ft, char *handle)
[553767c]209{
[d93c8beb]210        struct purple_data *pd = ic->proto_data;
[553767c]211        struct prpl_xfer_data *px;
[5ebff60]212
[553767c]213        /* xfer_new() will pick up this variable. It's a hack but we're not
214           multi-threaded anyway. */
215        next_ft = ft;
[d93c8beb]216        serv_send_file(purple_account_get_connection(pd->account), handle,
217                       ft->file_name);
[5ebff60]218
[553767c]219        ft->write = prpl_xfer_write;
[5ebff60]220
[553767c]221        px = ft->data;
[5ebff60]222        imcb_file_recv_start(ft);
223
224        px->ready_timer = b_timeout_add(100, prplcb_xfer_send_cb, px);
[553767c]225}
226
[5ebff60]227static gboolean prplcb_xfer_send_cb(gpointer data, gint fd, b_input_condition cond)
[553767c]228{
229        struct prpl_xfer_data *px = data;
[5ebff60]230
231        if (px->ft->status & FT_STATUS_TRANSFERRING) {
232                fprintf(stderr, "The ft, it is ready...\n");
233                px->ft->write_request(px->ft);
234
[553767c]235                return FALSE;
236        }
[5ebff60]237
[553767c]238        return TRUE;
239}
Note: See TracBrowser for help on using the repository browser.