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 | /* 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 | |
---|
29 | #include "bitlbee.h" |
---|
30 | |
---|
31 | #include <stdarg.h> |
---|
32 | |
---|
33 | #include <glib.h> |
---|
34 | #include <purple.h> |
---|
35 | |
---|
36 | struct prpl_xfer_data |
---|
37 | { |
---|
38 | PurpleXfer *xfer; |
---|
39 | file_transfer_t *ft; |
---|
40 | gint ready_timer; |
---|
41 | char *buf; |
---|
42 | int buf_len; |
---|
43 | }; |
---|
44 | |
---|
45 | static file_transfer_t *next_ft; |
---|
46 | |
---|
47 | struct im_connection *purple_ic_by_pa( PurpleAccount *pa ); |
---|
48 | |
---|
49 | static gboolean prpl_xfer_write_request( struct file_transfer *ft ) |
---|
50 | { |
---|
51 | return FALSE; |
---|
52 | } |
---|
53 | |
---|
54 | static gboolean prpl_xfer_write( struct file_transfer *ft, char *buffer, unsigned int len ) |
---|
55 | { |
---|
56 | struct prpl_xfer_data *px = ft->data; |
---|
57 | |
---|
58 | return FALSE; |
---|
59 | } |
---|
60 | |
---|
61 | static void prpl_xfer_accept( struct file_transfer *ft ) |
---|
62 | { |
---|
63 | struct prpl_xfer_data *px = ft->data; |
---|
64 | purple_xfer_request_accepted( px->xfer, NULL ); |
---|
65 | prpl_xfer_write_request( ft ); |
---|
66 | } |
---|
67 | |
---|
68 | static void prpl_xfer_canceled( struct file_transfer *ft, char *reason ) |
---|
69 | { |
---|
70 | struct prpl_xfer_data *px = ft->data; |
---|
71 | purple_xfer_request_denied( px->xfer ); |
---|
72 | } |
---|
73 | |
---|
74 | static gboolean prplcb_xfer_new_send_cb( gpointer data, gint fd, b_input_condition cond ); |
---|
75 | |
---|
76 | static void prplcb_xfer_new( PurpleXfer *xfer ) |
---|
77 | { |
---|
78 | if( purple_xfer_get_type( xfer ) == PURPLE_XFER_RECEIVE ) |
---|
79 | { |
---|
80 | /* This should suppress the stupid file dialog. */ |
---|
81 | purple_xfer_set_local_filename( xfer, "/tmp/wtf123" ); |
---|
82 | |
---|
83 | /* Sadly the xfer struct is still empty ATM so come back after |
---|
84 | the caller is done. */ |
---|
85 | b_timeout_add( 0, prplcb_xfer_new_send_cb, xfer ); |
---|
86 | } |
---|
87 | else |
---|
88 | { |
---|
89 | /* |
---|
90 | struct prpl_xfer_data *px = g_new0( struct prpl_xfer_data, 1 ); |
---|
91 | |
---|
92 | px->ft = next_ft; |
---|
93 | px->ft->data = px; |
---|
94 | px->xfer = xfer; |
---|
95 | px->xfer->ui_data = px; |
---|
96 | |
---|
97 | purple_xfer_set_filename( xfer, px->ft->file_name ); |
---|
98 | purple_xfer_set_size( xfer, px->ft->file_size ); |
---|
99 | |
---|
100 | next_ft = NULL; |
---|
101 | */ |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | static gboolean prplcb_xfer_new_send_cb( gpointer data, gint fd, b_input_condition cond ) |
---|
106 | { |
---|
107 | PurpleXfer *xfer = data; |
---|
108 | struct im_connection *ic = purple_ic_by_pa( xfer->account ); |
---|
109 | struct prpl_xfer_data *px = g_new0( struct prpl_xfer_data, 1 ); |
---|
110 | PurpleBuddy *buddy; |
---|
111 | const char *who; |
---|
112 | |
---|
113 | buddy = purple_find_buddy( xfer->account, xfer->who ); |
---|
114 | who = buddy ? purple_buddy_get_name( buddy ) : xfer->who; |
---|
115 | |
---|
116 | /* TODO(wilmer): After spreading some more const goodness in BitlBee, |
---|
117 | remove the evil cast below. */ |
---|
118 | px->ft = imcb_file_send_start( ic, (char*) who, xfer->filename, xfer->size ); |
---|
119 | px->ft->data = px; |
---|
120 | px->xfer = data; |
---|
121 | px->xfer->ui_data = px; |
---|
122 | |
---|
123 | px->ft->accept = prpl_xfer_accept; |
---|
124 | px->ft->canceled = prpl_xfer_canceled; |
---|
125 | px->ft->write_request = prpl_xfer_write_request; |
---|
126 | |
---|
127 | return FALSE; |
---|
128 | } |
---|
129 | |
---|
130 | static void prplcb_xfer_progress( PurpleXfer *xfer, double percent ) |
---|
131 | { |
---|
132 | fprintf( stderr, "prplcb_xfer_dbg 0x%p %f\n", xfer, percent ); |
---|
133 | } |
---|
134 | |
---|
135 | static void prplcb_xfer_dbg( PurpleXfer *xfer ) |
---|
136 | { |
---|
137 | fprintf( stderr, "prplcb_xfer_dbg 0x%p\n", xfer ); |
---|
138 | } |
---|
139 | |
---|
140 | PurpleXferUiOps bee_xfer_uiops = |
---|
141 | { |
---|
142 | prplcb_xfer_new, |
---|
143 | prplcb_xfer_dbg, |
---|
144 | prplcb_xfer_dbg, |
---|
145 | prplcb_xfer_progress, |
---|
146 | prplcb_xfer_dbg, |
---|
147 | prplcb_xfer_dbg, |
---|
148 | NULL, |
---|
149 | NULL, |
---|
150 | prplcb_xfer_dbg, |
---|
151 | }; |
---|
152 | |
---|
153 | static gboolean prplcb_xfer_send_cb( gpointer data, gint fd, b_input_condition cond ); |
---|
154 | |
---|
155 | void purple_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *handle ) |
---|
156 | { |
---|
157 | PurpleAccount *pa = ic->proto_data; |
---|
158 | struct prpl_xfer_data *px; |
---|
159 | |
---|
160 | /* xfer_new() will pick up this variable. It's a hack but we're not |
---|
161 | multi-threaded anyway. */ |
---|
162 | next_ft = ft; |
---|
163 | serv_send_file( purple_account_get_connection( pa ), handle, ft->file_name ); |
---|
164 | |
---|
165 | ft->write = prpl_xfer_write; |
---|
166 | |
---|
167 | px = ft->data; |
---|
168 | imcb_file_recv_start( ft ); |
---|
169 | } |
---|