comparison libpurple/protocols/irc/dcc_send.c @ 15374:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children 21bc8d84974f
comparison
equal deleted inserted replaced
15373:f79e0f4df793 15374:5fe8042783c1
1 /**
2 * @file dcc_send.c Functions used in sending files with DCC SEND
3 *
4 * gaim
5 *
6 * Copyright (C) 2004, Timothy T Ringenbach <omarvo@hotmail.com>
7 * Copyright (C) 2003, Robbert Haarman <gaim@inglorion.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #include "internal.h"
25 #include "irc.h"
26 #include "debug.h"
27 #include "ft.h"
28 #include "notify.h"
29 #include "network.h"
30
31 /***************************************************************************
32 * Functions related to receiving files via DCC SEND
33 ***************************************************************************/
34
35 struct irc_xfer_rx_data {
36 gchar *ip;
37 };
38
39 static void irc_dccsend_recv_destroy(GaimXfer *xfer)
40 {
41 struct irc_xfer_rx_data *xd = xfer->data;
42
43 if (xd->ip != NULL)
44 g_free(xd->ip);
45
46 g_free(xd);
47 }
48
49 /*
50 * This function is called whenever data is received.
51 * It sends the acknowledgement (in the form of a total byte count as an
52 * unsigned 4 byte integer in network byte order)
53 */
54 static void irc_dccsend_recv_ack(GaimXfer *xfer, const guchar *data, size_t size) {
55 unsigned long l;
56
57 l = htonl(xfer->bytes_sent);
58 write(xfer->fd, &l, sizeof(l));
59 }
60
61 static void irc_dccsend_recv_init(GaimXfer *xfer) {
62 struct irc_xfer_rx_data *xd = xfer->data;
63
64 gaim_xfer_start(xfer, -1, xd->ip, xfer->remote_port);
65 g_free(xd->ip);
66 xd->ip = NULL;
67 }
68
69 /* This function makes the necessary arrangements for receiving files */
70 void irc_dccsend_recv(struct irc_conn *irc, const char *from, const char *msg) {
71 GaimXfer *xfer;
72 struct irc_xfer_rx_data *xd;
73 gchar **token;
74 struct in_addr addr;
75 GString *filename;
76 int i = 0;
77 guint32 nip;
78
79 token = g_strsplit(msg, " ", 0);
80 if (!token[0] || !token[1] || !token[2]) {
81 g_strfreev(token);
82 return;
83 }
84
85 filename = g_string_new("");
86 if (token[0][0] == '"') {
87 if (!strchr(&(token[0][1]), '"')) {
88 g_string_append(filename, &(token[0][1]));
89 for (i = 1; token[i]; i++)
90 if (!strchr(token[i], '"')) {
91 g_string_append_printf(filename, " %s", token[i]);
92 } else {
93 g_string_append_len(filename, token[i], strlen(token[i]) - 1);
94 break;
95 }
96 } else {
97 g_string_append_len(filename, &(token[0][1]), strlen(&(token[0][1])) - 1);
98 }
99 } else {
100 g_string_append(filename, token[0]);
101 }
102
103 if (!token[i] || !token[i+1] || !token[i+2]) {
104 g_strfreev(token);
105 g_string_free(filename, TRUE);
106 return;
107 }
108 i++;
109
110 xfer = gaim_xfer_new(irc->account, GAIM_XFER_RECEIVE, from);
111 if (xfer)
112 {
113 xd = g_new0(struct irc_xfer_rx_data, 1);
114 xfer->data = xd;
115
116 gaim_xfer_set_filename(xfer, filename->str);
117 xfer->remote_port = atoi(token[i+1]);
118
119 nip = strtoul(token[i], NULL, 10);
120 if (nip) {
121 addr.s_addr = htonl(nip);
122 xd->ip = g_strdup(inet_ntoa(addr));
123 } else {
124 xd->ip = g_strdup(token[i]);
125 }
126 gaim_debug(GAIM_DEBUG_INFO, "irc", "Receiving file from %s\n",
127 xd->ip);
128 gaim_xfer_set_size(xfer, token[i+2] ? atoi(token[i+2]) : 0);
129
130 gaim_xfer_set_init_fnc(xfer, irc_dccsend_recv_init);
131 gaim_xfer_set_ack_fnc(xfer, irc_dccsend_recv_ack);
132
133 gaim_xfer_set_end_fnc(xfer, irc_dccsend_recv_destroy);
134 gaim_xfer_set_request_denied_fnc(xfer, irc_dccsend_recv_destroy);
135 gaim_xfer_set_cancel_send_fnc(xfer, irc_dccsend_recv_destroy);
136
137 gaim_xfer_request(xfer);
138 }
139 g_strfreev(token);
140 g_string_free(filename, TRUE);
141 }
142
143 /*******************************************************************
144 * Functions related to sending files via DCC SEND
145 *******************************************************************/
146
147 struct irc_xfer_send_data {
148 GaimNetworkListenData *listen_data;
149 gint inpa;
150 int fd;
151 guchar *rxqueue;
152 guint rxlen;
153 };
154
155 static void irc_dccsend_send_destroy(GaimXfer *xfer)
156 {
157 struct irc_xfer_send_data *xd = xfer->data;
158
159 if (xd == NULL)
160 return;
161
162 if (xd->listen_data != NULL)
163 gaim_network_listen_cancel(xd->listen_data);
164 if (xd->inpa > 0)
165 gaim_input_remove(xd->inpa);
166 if (xd->fd != -1)
167 close(xd->fd);
168
169 if (xd->rxqueue)
170 g_free(xd->rxqueue);
171
172 g_free(xd);
173 }
174
175 /* just in case you were wondering, this is why DCC is gay */
176 static void irc_dccsend_send_read(gpointer data, int source, GaimInputCondition cond)
177 {
178 GaimXfer *xfer = data;
179 struct irc_xfer_send_data *xd = xfer->data;
180 char *buffer[16];
181 int len;
182
183 len = read(source, buffer, sizeof(buffer));
184
185 if (len < 0 && errno == EAGAIN)
186 return;
187 else if (len <= 0) {
188 /* XXX: Shouldn't this be canceling the transfer? */
189 gaim_input_remove(xd->inpa);
190 xd->inpa = 0;
191 return;
192 }
193
194 xd->rxqueue = g_realloc(xd->rxqueue, len + xd->rxlen);
195 memcpy(xd->rxqueue + xd->rxlen, buffer, len);
196 xd->rxlen += len;
197
198 while (1) {
199 size_t acked;
200
201 if (xd->rxlen < 4)
202 break;
203
204 acked = ntohl(*((gint32 *)xd->rxqueue));
205
206 xd->rxlen -= 4;
207 if (xd->rxlen) {
208 unsigned char *tmp = g_memdup(xd->rxqueue + 4, xd->rxlen);
209 g_free(xd->rxqueue);
210 xd->rxqueue = tmp;
211 } else {
212 g_free(xd->rxqueue);
213 xd->rxqueue = NULL;
214 }
215
216 if (acked >= gaim_xfer_get_size(xfer)) {
217 gaim_input_remove(xd->inpa);
218 xd->inpa = 0;
219 gaim_xfer_set_completed(xfer, TRUE);
220 gaim_xfer_end(xfer);
221 return;
222 }
223 }
224 }
225
226 static gssize irc_dccsend_send_write(const guchar *buffer, size_t size, GaimXfer *xfer)
227 {
228 gssize s;
229 int ret;
230
231 s = MIN(gaim_xfer_get_bytes_remaining(xfer), size);
232 if (!s)
233 return 0;
234
235 ret = write(xfer->fd, buffer, s);
236
237 if (ret < 0 && errno == EAGAIN)
238 ret = 0;
239
240 return ret;
241 }
242
243 static void irc_dccsend_send_connected(gpointer data, int source, GaimInputCondition cond) {
244 GaimXfer *xfer = (GaimXfer *) data;
245 struct irc_xfer_send_data *xd = xfer->data;
246 int conn;
247
248 conn = accept(xd->fd, NULL, 0);
249 if (conn == -1) {
250 /* Accepting the connection failed. This could just be related
251 * to the nonblocking nature of the listening socket, so we'll
252 * just try again next time */
253 /* Let's print an error message anyway */
254 gaim_debug_warning("irc", "accept: %s\n", strerror(errno));
255 return;
256 }
257
258 gaim_input_remove(xfer->watcher);
259 xfer->watcher = 0;
260 close(xd->fd);
261 xd->fd = -1;
262
263 xd->inpa = gaim_input_add(conn, GAIM_INPUT_READ, irc_dccsend_send_read, xfer);
264 /* Start the transfer */
265 gaim_xfer_start(xfer, conn, NULL, 0);
266 }
267
268 static void
269 irc_dccsend_network_listen_cb(int sock, gpointer data)
270 {
271 GaimXfer *xfer = data;
272 struct irc_xfer_send_data *xd;
273 GaimConnection *gc;
274 struct irc_conn *irc;
275 const char *arg[2];
276 char *tmp;
277 struct in_addr addr;
278 unsigned short int port;
279
280 xd = xfer->data;
281 xd->listen_data = NULL;
282
283 if (gaim_xfer_get_status(xfer) == GAIM_XFER_STATUS_CANCEL_LOCAL
284 || gaim_xfer_get_status(xfer) == GAIM_XFER_STATUS_CANCEL_REMOTE) {
285 gaim_xfer_unref(xfer);
286 return;
287 }
288
289 xd = xfer->data;
290 gc = gaim_account_get_connection(gaim_xfer_get_account(xfer));
291 irc = gc->proto_data;
292
293 gaim_xfer_unref(xfer);
294
295 if (sock < 0) {
296 gaim_notify_error(gc, NULL, _("File Transfer Failed"),
297 _("Gaim could not open a listening port."));
298 gaim_xfer_cancel_local(xfer);
299 return;
300 }
301
302 xd->fd = sock;
303
304 port = gaim_network_get_port_from_fd(sock);
305 gaim_debug_misc("irc", "port is %hu\n", port);
306 /* Monitor the listening socket */
307 xfer->watcher = gaim_input_add(sock, GAIM_INPUT_READ,
308 irc_dccsend_send_connected, xfer);
309
310 /* Send the intended recipient the DCC request */
311 arg[0] = xfer->who;
312 inet_aton(gaim_network_get_my_ip(irc->fd), &addr);
313 arg[1] = tmp = g_strdup_printf("\001DCC SEND \"%s\" %u %hu %" G_GSIZE_FORMAT "\001",
314 xfer->filename, ntohl(addr.s_addr),
315 port, xfer->size);
316
317 irc_cmd_privmsg(gc->proto_data, "msg", NULL, arg);
318 g_free(tmp);
319 }
320
321 /*
322 * This function is called after the user has selected a file to send.
323 */
324 static void irc_dccsend_send_init(GaimXfer *xfer) {
325 GaimConnection *gc = gaim_account_get_connection(gaim_xfer_get_account(xfer));
326 struct irc_xfer_send_data *xd = xfer->data;
327
328 xfer->filename = g_path_get_basename(xfer->local_filename);
329
330 gaim_xfer_ref(xfer);
331
332 /* Create a listening socket */
333 xd->listen_data = gaim_network_listen_range(0, 0, SOCK_STREAM,
334 irc_dccsend_network_listen_cb, xfer);
335 if (xd->listen_data == NULL) {
336 gaim_xfer_unref(xfer);
337 gaim_notify_error(gc, NULL, _("File Transfer Failed"),
338 _("Gaim could not open a listening port."));
339 gaim_xfer_cancel_local(xfer);
340 }
341
342 }
343
344 GaimXfer *irc_dccsend_new_xfer(GaimConnection *gc, const char *who) {
345 GaimXfer *xfer;
346 struct irc_xfer_send_data *xd;
347
348 /* Build the file transfer handle */
349 xfer = gaim_xfer_new(gaim_connection_get_account(gc), GAIM_XFER_SEND, who);
350 if (xfer)
351 {
352 xd = g_new0(struct irc_xfer_send_data, 1);
353 xd->fd = -1;
354 xfer->data = xd;
355
356 /* Setup our I/O op functions */
357 gaim_xfer_set_init_fnc(xfer, irc_dccsend_send_init);
358 gaim_xfer_set_write_fnc(xfer, irc_dccsend_send_write);
359 gaim_xfer_set_end_fnc(xfer, irc_dccsend_send_destroy);
360 gaim_xfer_set_request_denied_fnc(xfer, irc_dccsend_send_destroy);
361 gaim_xfer_set_cancel_send_fnc(xfer, irc_dccsend_send_destroy);
362 }
363
364 return xfer;
365 }
366
367 /**
368 * Gaim calls this function when the user selects Send File from the
369 * buddy menu
370 * It sets up the GaimXfer struct and tells Gaim to go ahead
371 */
372 void irc_dccsend_send_file(GaimConnection *gc, const char *who, const char *file) {
373 GaimXfer *xfer = irc_dccsend_new_xfer(gc, who);
374
375 /* Perform the request */
376 if (file)
377 gaim_xfer_request_accepted(xfer, file);
378 else
379 gaim_xfer_request(xfer);
380 }