Mon, 06 Oct 2003 02:04:55 +0000
[gaim-migrate @ 7737]
jabber gets file receive support again. jabber should now be able to do
everything the old plugin did, and more.
| 7170 | 1 | /* |
| 2 | * gaim - Jabber Protocol Plugin | |
| 3 | * | |
| 4 | * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com> | |
| 5 | * | |
| 6 | * This program is free software; you can redistribute it and/or modify | |
| 7 | * it under the terms of the GNU General Public License as published by | |
| 8 | * the Free Software Foundation; either version 2 of the License, or | |
| 9 | * (at your option) any later version. | |
| 10 | * | |
| 11 | * This program is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | * GNU General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU General Public License | |
| 17 | * along with this program; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 | * | |
| 20 | */ | |
| 21 | #include "internal.h" | |
| 22 | #include "ft.h" | |
| 23 | #include "util.h" | |
| 24 | ||
| 25 | #include "jabber.h" | |
| 26 | #include "iq.h" | |
| 27 | ||
| 28 | typedef struct _JabberOOBXfer { | |
| 29 | char *address; | |
| 30 | int port; | |
| 31 | char *page; | |
| 32 | ||
| 33 | GString *headers; | |
| 34 | gboolean newline; | |
| 35 | ||
| 36 | char *iq_id; | |
| 37 | ||
| 38 | JabberStream *js; | |
| 39 | ||
| 40 | } JabberOOBXfer; | |
| 41 | ||
| 42 | static void jabber_oob_xfer_init(GaimXfer *xfer) | |
| 43 | { | |
| 44 | JabberOOBXfer *jox = xfer->data; | |
| 45 | gaim_xfer_start(xfer, -1, jox->address, jox->port); | |
| 46 | } | |
| 47 | ||
| 48 | static void jabber_oob_xfer_free(GaimXfer *xfer) | |
| 49 | { | |
| 50 | JabberOOBXfer *jox = xfer->data; | |
| 51 | jox->js->file_transfers = g_list_remove(jox->js->file_transfers, xfer); | |
| 52 | ||
| 53 | g_string_free(jox->headers, TRUE); | |
| 54 | g_free(jox->address); | |
| 55 | g_free(jox->page); | |
| 56 | g_free(jox->iq_id); | |
| 57 | g_free(jox); | |
| 58 | ||
| 59 | xfer->data = NULL; | |
| 60 | } | |
| 61 | ||
| 62 | static void jabber_oob_xfer_end(GaimXfer *xfer) | |
| 63 | { | |
| 64 | JabberOOBXfer *jox = xfer->data; | |
| 65 | JabberIq *iq; | |
| 66 | ||
| 67 | iq = jabber_iq_new(jox->js, JABBER_IQ_RESULT); | |
| 68 | xmlnode_set_attrib(iq->node, "to", xfer->who); | |
| 69 | jabber_iq_set_id(iq, jox->iq_id); | |
| 70 | ||
| 71 | jabber_iq_send(iq); | |
| 72 | ||
| 73 | jabber_oob_xfer_free(xfer); | |
| 74 | } | |
| 75 | ||
| 76 | static void jabber_oob_xfer_start(GaimXfer *xfer) | |
| 77 | { | |
| 78 | JabberOOBXfer *jox = xfer->data; | |
| 79 | ||
| 80 | char *buf = g_strdup_printf("GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n", | |
| 81 | jox->page, jox->address); | |
| 82 | write(xfer->fd, buf, strlen(buf)); | |
| 83 | g_free(buf); | |
| 84 | } | |
| 85 | ||
| 86 | static size_t jabber_oob_xfer_read(char **buffer, GaimXfer *xfer) { | |
| 87 | JabberOOBXfer *jox = xfer->data; | |
| 88 | char test; | |
| 89 | int size; | |
| 90 | ||
| 91 | if(read(xfer->fd, &test, sizeof(test)) > 0) { | |
| 92 | jox->headers = g_string_append_c(jox->headers, test); | |
| 93 | if(test == '\r') | |
| 94 | return 0; | |
| 95 | if(test == '\n') { | |
| 96 | if(jox->newline) { | |
| 97 | gchar *lenstr = strstr(jox->headers->str, "Content-Length: "); | |
| 98 | if(lenstr) { | |
| 99 | sscanf(lenstr, "Content-Length: %d", &size); | |
| 100 | gaim_xfer_set_size(xfer, size); | |
| 101 | } | |
| 102 | gaim_xfer_set_read_fnc(xfer, NULL); | |
| 103 | return 0; | |
| 104 | } else | |
| 105 | jox->newline = TRUE; | |
| 106 | return 0; | |
| 107 | } | |
| 108 | jox->newline = FALSE; | |
| 109 | return 0; | |
| 110 | } | |
| 111 | return 0; | |
| 112 | } | |
| 113 | ||
| 114 | static void jabber_oob_xfer_cancel_send(GaimXfer *xfer) { | |
| 115 | } | |
| 116 | ||
| 117 | static void jabber_oob_xfer_cancel_recv(GaimXfer *xfer) { | |
| 118 | JabberOOBXfer *jox = xfer->data; | |
| 119 | JabberIq *iq; | |
| 120 | xmlnode *y; | |
| 121 | ||
| 122 | iq = jabber_iq_new(jox->js, JABBER_IQ_ERROR); | |
| 123 | xmlnode_set_attrib(iq->node, "to", xfer->who); | |
| 124 | jabber_iq_set_id(iq, jox->iq_id); | |
| 125 | y = xmlnode_new_child(iq->node, "error"); | |
| 126 | /* FIXME: need to handle other kinds of errors here */ | |
| 127 | xmlnode_set_attrib(y, "code", "406"); | |
| 128 | xmlnode_insert_data(y, "File Transfer Refused", -1); | |
| 129 | ||
| 130 | jabber_iq_send(iq); | |
| 131 | ||
| 132 | jabber_oob_xfer_free(xfer); | |
| 133 | } | |
| 134 | ||
| 135 | void jabber_oob_parse(JabberStream *js, xmlnode *packet) { | |
| 136 | JabberOOBXfer *jox; | |
| 137 | GaimXfer *xfer; | |
| 138 | char *filename; | |
| 139 | char *url; | |
| 140 | xmlnode *querynode, *urlnode; | |
| 141 | ||
| 142 | if(!(querynode = xmlnode_get_child(packet, "query"))) | |
| 143 | return; | |
| 144 | ||
| 145 | if(!(urlnode = xmlnode_get_child(querynode, "url"))) | |
| 146 | return; | |
| 147 | ||
| 148 | url = xmlnode_get_data(urlnode); | |
| 149 | ||
| 150 | jox = g_new0(JabberOOBXfer, 1); | |
| 151 | gaim_url_parse(url, &jox->address, &jox->port, &jox->page); | |
| 152 | g_free(url); | |
| 153 | jox->js = js; | |
| 154 | jox->headers = g_string_new(""); | |
| 155 | jox->iq_id = g_strdup(xmlnode_get_attrib(packet, "id")); | |
| 156 | ||
| 157 | xfer = gaim_xfer_new(js->gc->account, GAIM_XFER_RECEIVE, | |
| 158 | xmlnode_get_attrib(packet, "from")); | |
| 159 | xfer->data = jox; | |
| 160 | ||
| 161 | if(!(filename = g_strdup(g_strrstr(jox->page, "/")))) | |
| 162 | filename = g_strdup(jox->page); | |
| 163 | ||
| 164 | gaim_xfer_set_filename(xfer, filename); | |
| 165 | ||
| 166 | g_free(filename); | |
| 167 | ||
| 168 | gaim_xfer_set_init_fnc(xfer, jabber_oob_xfer_init); | |
| 169 | gaim_xfer_set_end_fnc(xfer, jabber_oob_xfer_end); | |
| 170 | gaim_xfer_set_cancel_send_fnc(xfer, jabber_oob_xfer_cancel_send); | |
| 171 | gaim_xfer_set_cancel_recv_fnc(xfer, jabber_oob_xfer_cancel_recv); | |
| 172 | gaim_xfer_set_read_fnc(xfer, jabber_oob_xfer_read); | |
| 173 | gaim_xfer_set_start_fnc(xfer, jabber_oob_xfer_start); | |
| 174 | ||
| 175 | js->file_transfers = g_list_append(js->file_transfers, xfer); | |
| 176 | ||
| 177 | gaim_xfer_request(xfer); | |
| 178 | } | |
| 179 | ||
| 180 |