[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" |
---|
| 30 | |
---|
| 31 | #include <stdarg.h> |
---|
| 32 | |
---|
| 33 | #include <glib.h> |
---|
| 34 | #include <purple.h> |
---|
| 35 | |
---|
[5ebff60] | 36 | struct prpl_xfer_data { |
---|
[2309152] | 37 | PurpleXfer *xfer; |
---|
| 38 | file_transfer_t *ft; |
---|
[e7dc02a] | 39 | struct im_connection *ic; |
---|
[8822d23] | 40 | int fd; |
---|
[75c3ff7] | 41 | char *fn, *handle; |
---|
[8822d23] | 42 | gboolean ui_wants_data; |
---|
[2309152] | 43 | }; |
---|
| 44 | |
---|
| 45 | static file_transfer_t *next_ft; |
---|
| 46 | |
---|
[5ebff60] | 47 | struct im_connection *purple_ic_by_pa(PurpleAccount *pa); |
---|
| 48 | static gboolean prplcb_xfer_new_send_cb(gpointer data, gint fd, b_input_condition cond); |
---|
| 49 | static gboolean prpl_xfer_write_request(struct file_transfer *ft); |
---|
[2309152] | 50 | |
---|
| 51 | |
---|
[c96c72f] | 52 | /* Receiving files (IM->UI): */ |
---|
[5ebff60] | 53 | static void prpl_xfer_accept(struct file_transfer *ft) |
---|
[2309152] | 54 | { |
---|
| 55 | struct prpl_xfer_data *px = ft->data; |
---|
[5ebff60] | 56 | |
---|
| 57 | purple_xfer_request_accepted(px->xfer, NULL); |
---|
| 58 | prpl_xfer_write_request(ft); |
---|
[2309152] | 59 | } |
---|
| 60 | |
---|
[5ebff60] | 61 | static void prpl_xfer_canceled(struct file_transfer *ft, char *reason) |
---|
[2309152] | 62 | { |
---|
| 63 | struct prpl_xfer_data *px = ft->data; |
---|
[5ebff60] | 64 | |
---|
| 65 | purple_xfer_request_denied(px->xfer); |
---|
[2309152] | 66 | } |
---|
| 67 | |
---|
[5ebff60] | 68 | static void prplcb_xfer_new(PurpleXfer *xfer) |
---|
[2309152] | 69 | { |
---|
[5ebff60] | 70 | if (purple_xfer_get_type(xfer) == PURPLE_XFER_RECEIVE) { |
---|
| 71 | struct prpl_xfer_data *px = g_new0(struct prpl_xfer_data, 1); |
---|
| 72 | |
---|
[8822d23] | 73 | xfer->ui_data = px; |
---|
| 74 | px->xfer = xfer; |
---|
[5ebff60] | 75 | px->fn = mktemp(g_strdup("/tmp/bitlbee-purple-ft.XXXXXX")); |
---|
[8822d23] | 76 | px->fd = -1; |
---|
[5ebff60] | 77 | px->ic = purple_ic_by_pa(xfer->account); |
---|
| 78 | |
---|
| 79 | purple_xfer_set_local_filename(xfer, px->fn); |
---|
| 80 | |
---|
[2309152] | 81 | /* Sadly the xfer struct is still empty ATM so come back after |
---|
| 82 | the caller is done. */ |
---|
[5ebff60] | 83 | b_timeout_add(0, prplcb_xfer_new_send_cb, xfer); |
---|
| 84 | } else { |
---|
[e7dc02a] | 85 | struct file_transfer *ft = next_ft; |
---|
| 86 | struct prpl_xfer_data *px = ft->data; |
---|
[5ebff60] | 87 | |
---|
[e7dc02a] | 88 | xfer->ui_data = px; |
---|
[2309152] | 89 | px->xfer = xfer; |
---|
[5ebff60] | 90 | |
---|
[2309152] | 91 | next_ft = NULL; |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
[5ebff60] | 95 | static gboolean prplcb_xfer_new_send_cb(gpointer data, gint fd, b_input_condition cond) |
---|
[2309152] | 96 | { |
---|
[553767c] | 97 | PurpleXfer *xfer = data; |
---|
[5ebff60] | 98 | struct im_connection *ic = purple_ic_by_pa(xfer->account); |
---|
[8822d23] | 99 | struct prpl_xfer_data *px = xfer->ui_data; |
---|
[553767c] | 100 | PurpleBuddy *buddy; |
---|
| 101 | const char *who; |
---|
[5ebff60] | 102 | |
---|
| 103 | buddy = purple_find_buddy(xfer->account, xfer->who); |
---|
| 104 | who = buddy ? purple_buddy_get_name(buddy) : xfer->who; |
---|
| 105 | |
---|
[553767c] | 106 | /* TODO(wilmer): After spreading some more const goodness in BitlBee, |
---|
| 107 | remove the evil cast below. */ |
---|
[5ebff60] | 108 | px->ft = imcb_file_send_start(ic, (char *) who, xfer->filename, xfer->size); |
---|
[553767c] | 109 | px->ft->data = px; |
---|
[5ebff60] | 110 | |
---|
[553767c] | 111 | px->ft->accept = prpl_xfer_accept; |
---|
| 112 | px->ft->canceled = prpl_xfer_canceled; |
---|
| 113 | px->ft->write_request = prpl_xfer_write_request; |
---|
[5ebff60] | 114 | |
---|
[553767c] | 115 | return FALSE; |
---|
[2309152] | 116 | } |
---|
| 117 | |
---|
[5ebff60] | 118 | gboolean try_write_to_ui(gpointer data, gint fd, b_input_condition cond) |
---|
[c96c72f] | 119 | { |
---|
| 120 | struct file_transfer *ft = data; |
---|
| 121 | struct prpl_xfer_data *px = ft->data; |
---|
| 122 | struct stat fs; |
---|
| 123 | off_t tx_bytes; |
---|
[5ebff60] | 124 | |
---|
[c96c72f] | 125 | /* If we don't have the file opened yet, there's no data so wait. */ |
---|
[5ebff60] | 126 | if (px->fd < 0 || !px->ui_wants_data) { |
---|
[c96c72f] | 127 | return FALSE; |
---|
[5ebff60] | 128 | } |
---|
| 129 | |
---|
| 130 | tx_bytes = lseek(px->fd, 0, SEEK_CUR); |
---|
| 131 | fstat(px->fd, &fs); |
---|
| 132 | |
---|
| 133 | if (fs.st_size > tx_bytes) { |
---|
[c96c72f] | 134 | char buf[1024]; |
---|
[5ebff60] | 135 | size_t n = MIN(fs.st_size - tx_bytes, sizeof(buf)); |
---|
| 136 | |
---|
| 137 | if (read(px->fd, buf, n) == n && ft->write(ft, buf, n)) { |
---|
[c96c72f] | 138 | px->ui_wants_data = FALSE; |
---|
[5ebff60] | 139 | } else { |
---|
| 140 | purple_xfer_cancel_local(px->xfer); |
---|
| 141 | imcb_file_canceled(px->ic, ft, "Read error"); |
---|
[c96c72f] | 142 | } |
---|
| 143 | } |
---|
[5ebff60] | 144 | |
---|
| 145 | if (lseek(px->fd, 0, SEEK_CUR) == px->xfer->size) { |
---|
[75c3ff7] | 146 | /*purple_xfer_end( px->xfer );*/ |
---|
[5ebff60] | 147 | imcb_file_finished(px->ic, ft); |
---|
[c96c72f] | 148 | } |
---|
[5ebff60] | 149 | |
---|
[c96c72f] | 150 | return FALSE; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | /* UI calls this when its buffer is empty and wants more data to send to the user. */ |
---|
[5ebff60] | 154 | static gboolean prpl_xfer_write_request(struct file_transfer *ft) |
---|
[c96c72f] | 155 | { |
---|
| 156 | struct prpl_xfer_data *px = ft->data; |
---|
[5ebff60] | 157 | |
---|
[c96c72f] | 158 | px->ui_wants_data = TRUE; |
---|
[5ebff60] | 159 | try_write_to_ui(ft, 0, 0); |
---|
| 160 | |
---|
[c96c72f] | 161 | return FALSE; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | /* Generic (IM<>UI): */ |
---|
[5ebff60] | 166 | static void prplcb_xfer_destroy(PurpleXfer *xfer) |
---|
[8822d23] | 167 | { |
---|
| 168 | struct prpl_xfer_data *px = xfer->ui_data; |
---|
[5ebff60] | 169 | |
---|
| 170 | g_free(px->fn); |
---|
| 171 | g_free(px->handle); |
---|
| 172 | if (px->fd >= 0) { |
---|
| 173 | close(px->fd); |
---|
| 174 | } |
---|
| 175 | g_free(px); |
---|
[8822d23] | 176 | } |
---|
| 177 | |
---|
[5ebff60] | 178 | static void prplcb_xfer_progress(PurpleXfer *xfer, double percent) |
---|
[2309152] | 179 | { |
---|
[8822d23] | 180 | struct prpl_xfer_data *px = xfer->ui_data; |
---|
[5ebff60] | 181 | |
---|
| 182 | if (px == NULL) { |
---|
[e7dc02a] | 183 | return; |
---|
[5ebff60] | 184 | } |
---|
| 185 | |
---|
| 186 | if (purple_xfer_get_type(xfer) == PURPLE_XFER_SEND) { |
---|
| 187 | if (*px->fn) { |
---|
[75c3ff7] | 188 | char *slash; |
---|
[5ebff60] | 189 | |
---|
| 190 | unlink(px->fn); |
---|
| 191 | if ((slash = strrchr(px->fn, '/'))) { |
---|
[75c3ff7] | 192 | *slash = '\0'; |
---|
[5ebff60] | 193 | rmdir(px->fn); |
---|
[75c3ff7] | 194 | } |
---|
[e7dc02a] | 195 | *px->fn = '\0'; |
---|
| 196 | } |
---|
[5ebff60] | 197 | |
---|
[e7dc02a] | 198 | return; |
---|
| 199 | } |
---|
[5ebff60] | 200 | |
---|
| 201 | if (px->fd == -1 && percent > 0) { |
---|
[8822d23] | 202 | /* Weeeeeeeee, we're getting data! That means the file exists |
---|
| 203 | by now so open it and start sending to the UI. */ |
---|
[5ebff60] | 204 | px->fd = open(px->fn, O_RDONLY); |
---|
| 205 | |
---|
[8822d23] | 206 | /* Unlink it now, because we don't need it after this. */ |
---|
[5ebff60] | 207 | unlink(px->fn); |
---|
[8822d23] | 208 | } |
---|
[5ebff60] | 209 | |
---|
| 210 | if (percent < 1) { |
---|
| 211 | try_write_to_ui(px->ft, 0, 0); |
---|
| 212 | } else { |
---|
[c96c72f] | 213 | /* Another nice problem: If we have the whole file, it only |
---|
| 214 | gets closed when we return. Problem: There may still be |
---|
| 215 | stuff buffered and not written, we'll only see it after |
---|
| 216 | the caller close()s the file. So poll the file after that. */ |
---|
[5ebff60] | 217 | b_timeout_add(0, try_write_to_ui, px->ft); |
---|
| 218 | } |
---|
[8822d23] | 219 | } |
---|
| 220 | |
---|
[5ebff60] | 221 | static void prplcb_xfer_cancel_remote(PurpleXfer *xfer) |
---|
[8822d23] | 222 | { |
---|
| 223 | struct prpl_xfer_data *px = xfer->ui_data; |
---|
[5ebff60] | 224 | |
---|
| 225 | if (px->ft) { |
---|
| 226 | imcb_file_canceled(px->ic, px->ft, "Canceled by remote end"); |
---|
| 227 | } else { |
---|
[75c3ff7] | 228 | /* px->ft == NULL for sends, because of the two stages. :-/ */ |
---|
[5ebff60] | 229 | imcb_error(px->ic, "File transfer cancelled by remote end"); |
---|
| 230 | } |
---|
[e7dc02a] | 231 | } |
---|
| 232 | |
---|
[5ebff60] | 233 | static void prplcb_xfer_dbg(PurpleXfer *xfer) |
---|
[553767c] | 234 | { |
---|
[5ebff60] | 235 | fprintf(stderr, "prplcb_xfer_dbg 0x%p\n", xfer); |
---|
[2309152] | 236 | } |
---|
| 237 | |
---|
[c96c72f] | 238 | |
---|
| 239 | /* Sending files (UI->IM): */ |
---|
[5ebff60] | 240 | static gboolean prpl_xfer_write(struct file_transfer *ft, char *buffer, unsigned int len); |
---|
| 241 | static gboolean purple_transfer_request_cb(gpointer data, gint fd, b_input_condition cond); |
---|
[e7dc02a] | 242 | |
---|
[5ebff60] | 243 | void purple_transfer_request(struct im_connection *ic, file_transfer_t *ft, char *handle) |
---|
[2309152] | 244 | { |
---|
[5ebff60] | 245 | struct prpl_xfer_data *px = g_new0(struct prpl_xfer_data, 1); |
---|
[75c3ff7] | 246 | char *dir, *basename; |
---|
[5ebff60] | 247 | |
---|
[e7dc02a] | 248 | ft->data = px; |
---|
| 249 | px->ft = ft; |
---|
[5ebff60] | 250 | |
---|
| 251 | dir = g_strdup("/tmp/bitlbee-purple-ft.XXXXXX"); |
---|
| 252 | if (!mkdtemp(dir)) { |
---|
| 253 | imcb_error(ic, "Could not create temporary file for file transfer"); |
---|
| 254 | g_free(px); |
---|
| 255 | g_free(dir); |
---|
[75c3ff7] | 256 | return; |
---|
| 257 | } |
---|
[5ebff60] | 258 | |
---|
| 259 | if ((basename = strrchr(ft->file_name, '/'))) { |
---|
[75c3ff7] | 260 | basename++; |
---|
[5ebff60] | 261 | } else { |
---|
[75c3ff7] | 262 | basename = ft->file_name; |
---|
[5ebff60] | 263 | } |
---|
| 264 | px->fn = g_strdup_printf("%s/%s", dir, basename); |
---|
| 265 | px->fd = open(px->fn, O_WRONLY | O_CREAT, 0600); |
---|
| 266 | g_free(dir); |
---|
| 267 | |
---|
| 268 | if (px->fd < 0) { |
---|
| 269 | imcb_error(ic, "Could not create temporary file for file transfer"); |
---|
| 270 | g_free(px); |
---|
| 271 | g_free(px->fn); |
---|
[75c3ff7] | 272 | return; |
---|
| 273 | } |
---|
[5ebff60] | 274 | |
---|
[e7dc02a] | 275 | px->ic = ic; |
---|
[5ebff60] | 276 | px->handle = g_strdup(handle); |
---|
| 277 | |
---|
| 278 | imcb_log(ic, |
---|
| 279 | "Due to libpurple limitations, the file has to be cached locally before proceeding with the actual file transfer. Please wait..."); |
---|
| 280 | |
---|
| 281 | b_timeout_add(0, purple_transfer_request_cb, ft); |
---|
[c96c72f] | 282 | } |
---|
[2309152] | 283 | |
---|
[5ebff60] | 284 | static void purple_transfer_forward(struct file_transfer *ft) |
---|
[2309152] | 285 | { |
---|
[e7dc02a] | 286 | struct prpl_xfer_data *px = ft->data; |
---|
| 287 | PurpleAccount *pa = px->ic->proto_data; |
---|
[5ebff60] | 288 | |
---|
[2309152] | 289 | /* xfer_new() will pick up this variable. It's a hack but we're not |
---|
| 290 | multi-threaded anyway. */ |
---|
| 291 | next_ft = ft; |
---|
[5ebff60] | 292 | serv_send_file(purple_account_get_connection(pa), px->handle, px->fn); |
---|
[e7dc02a] | 293 | } |
---|
| 294 | |
---|
[5ebff60] | 295 | static gboolean purple_transfer_request_cb(gpointer data, gint fd, b_input_condition cond) |
---|
[e7dc02a] | 296 | { |
---|
| 297 | file_transfer_t *ft = data; |
---|
[4aa0f6b] | 298 | struct prpl_xfer_data *px = ft->data; |
---|
[5ebff60] | 299 | |
---|
| 300 | if (ft->write == NULL) { |
---|
[e7dc02a] | 301 | ft->write = prpl_xfer_write; |
---|
[5ebff60] | 302 | imcb_file_recv_start(px->ic, ft); |
---|
[e7dc02a] | 303 | } |
---|
[5ebff60] | 304 | |
---|
| 305 | ft->write_request(ft); |
---|
| 306 | |
---|
[e7dc02a] | 307 | return FALSE; |
---|
[2309152] | 308 | } |
---|
[c96c72f] | 309 | |
---|
[5ebff60] | 310 | static gboolean prpl_xfer_write(struct file_transfer *ft, char *buffer, unsigned int len) |
---|
[e7dc02a] | 311 | { |
---|
| 312 | struct prpl_xfer_data *px = ft->data; |
---|
[5ebff60] | 313 | |
---|
| 314 | if (write(px->fd, buffer, len) != len) { |
---|
| 315 | imcb_file_canceled(px->ic, ft, "Error while writing temporary file"); |
---|
[e7dc02a] | 316 | return FALSE; |
---|
| 317 | } |
---|
[5ebff60] | 318 | |
---|
| 319 | if (lseek(px->fd, 0, SEEK_CUR) >= ft->file_size) { |
---|
| 320 | close(px->fd); |
---|
[e7dc02a] | 321 | px->fd = -1; |
---|
[5ebff60] | 322 | |
---|
| 323 | purple_transfer_forward(ft); |
---|
| 324 | imcb_file_finished(px->ic, ft); |
---|
[e7dc02a] | 325 | px->ft = NULL; |
---|
[5ebff60] | 326 | } else { |
---|
| 327 | b_timeout_add(0, purple_transfer_request_cb, ft); |
---|
[e7dc02a] | 328 | } |
---|
[5ebff60] | 329 | |
---|
[e7dc02a] | 330 | return TRUE; |
---|
| 331 | } |
---|
[c96c72f] | 332 | |
---|
| 333 | |
---|
| 334 | |
---|
| 335 | PurpleXferUiOps bee_xfer_uiops = |
---|
| 336 | { |
---|
| 337 | prplcb_xfer_new, |
---|
| 338 | prplcb_xfer_destroy, |
---|
[75c3ff7] | 339 | NULL, /* prplcb_xfer_add, */ |
---|
[c96c72f] | 340 | prplcb_xfer_progress, |
---|
| 341 | prplcb_xfer_dbg, |
---|
| 342 | prplcb_xfer_cancel_remote, |
---|
| 343 | NULL, |
---|
| 344 | NULL, |
---|
| 345 | prplcb_xfer_dbg, |
---|
| 346 | }; |
---|