Sat, 21 Feb 2004 20:59:07 +0000
[gaim-migrate @ 9035]
I don't remember this being here before
| 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" | |
| 7234 | 22 | #include "debug.h" |
| 7170 | 23 | #include "ft.h" |
| 24 | #include "util.h" | |
| 25 | ||
| 26 | #include "jabber.h" | |
| 27 | #include "iq.h" | |
| 28 | ||
| 29 | typedef struct _JabberOOBXfer { | |
| 30 | char *address; | |
| 31 | int port; | |
| 32 | char *page; | |
| 33 | ||
| 34 | GString *headers; | |
| 35 | gboolean newline; | |
| 36 | ||
| 37 | char *iq_id; | |
| 38 | ||
| 39 | JabberStream *js; | |
| 40 | ||
| 41 | } JabberOOBXfer; | |
| 42 | ||
| 43 | static void jabber_oob_xfer_init(GaimXfer *xfer) | |
| 44 | { | |
| 45 | JabberOOBXfer *jox = xfer->data; | |
| 46 | gaim_xfer_start(xfer, -1, jox->address, jox->port); | |
| 47 | } | |
| 48 | ||
| 49 | static void jabber_oob_xfer_free(GaimXfer *xfer) | |
| 50 | { | |
| 51 | JabberOOBXfer *jox = xfer->data; | |
| 7395 | 52 | jox->js->oob_file_transfers = g_list_remove(jox->js->oob_file_transfers, |
| 53 | xfer); | |
| 7170 | 54 | |
| 55 | g_string_free(jox->headers, TRUE); | |
| 56 | g_free(jox->address); | |
| 57 | g_free(jox->page); | |
| 58 | g_free(jox->iq_id); | |
| 59 | g_free(jox); | |
| 60 | ||
| 61 | xfer->data = NULL; | |
| 62 | } | |
| 63 | ||
| 64 | static void jabber_oob_xfer_end(GaimXfer *xfer) | |
| 65 | { | |
| 66 | JabberOOBXfer *jox = xfer->data; | |
| 67 | JabberIq *iq; | |
| 68 | ||
| 69 | iq = jabber_iq_new(jox->js, JABBER_IQ_RESULT); | |
| 70 | xmlnode_set_attrib(iq->node, "to", xfer->who); | |
| 71 | jabber_iq_set_id(iq, jox->iq_id); | |
| 72 | ||
| 73 | jabber_iq_send(iq); | |
| 74 | ||
| 75 | jabber_oob_xfer_free(xfer); | |
| 76 | } | |
| 77 | ||
| 78 | static void jabber_oob_xfer_start(GaimXfer *xfer) | |
| 79 | { | |
| 80 | JabberOOBXfer *jox = xfer->data; | |
| 81 | ||
| 82 | char *buf = g_strdup_printf("GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n", | |
| 83 | jox->page, jox->address); | |
| 84 | write(xfer->fd, buf, strlen(buf)); | |
| 85 | g_free(buf); | |
| 86 | } | |
| 87 | ||
| 8231 | 88 | static ssize_t jabber_oob_xfer_read(char **buffer, GaimXfer *xfer) { |
| 7170 | 89 | JabberOOBXfer *jox = xfer->data; |
| 90 | char test; | |
| 91 | int size; | |
| 92 | ||
| 93 | if(read(xfer->fd, &test, sizeof(test)) > 0) { | |
| 94 | jox->headers = g_string_append_c(jox->headers, test); | |
| 95 | if(test == '\r') | |
| 96 | return 0; | |
| 97 | if(test == '\n') { | |
| 98 | if(jox->newline) { | |
| 99 | gchar *lenstr = strstr(jox->headers->str, "Content-Length: "); | |
| 100 | if(lenstr) { | |
| 101 | sscanf(lenstr, "Content-Length: %d", &size); | |
| 102 | gaim_xfer_set_size(xfer, size); | |
| 103 | } | |
| 104 | gaim_xfer_set_read_fnc(xfer, NULL); | |
| 105 | return 0; | |
| 106 | } else | |
| 107 | jox->newline = TRUE; | |
| 108 | return 0; | |
| 109 | } | |
| 110 | jox->newline = FALSE; | |
| 111 | return 0; | |
| 7234 | 112 | } else { |
| 113 | gaim_debug(GAIM_DEBUG_ERROR, "jabber", "Read error on oob xfer!\n"); | |
| 114 | gaim_xfer_cancel_local(xfer); | |
| 7170 | 115 | } |
| 7234 | 116 | |
| 7170 | 117 | return 0; |
| 118 | } | |
| 119 | ||
| 120 | static void jabber_oob_xfer_cancel_recv(GaimXfer *xfer) { | |
| 121 | JabberOOBXfer *jox = xfer->data; | |
| 122 | JabberIq *iq; | |
| 123 | xmlnode *y; | |
| 124 | ||
| 125 | iq = jabber_iq_new(jox->js, JABBER_IQ_ERROR); | |
| 126 | xmlnode_set_attrib(iq->node, "to", xfer->who); | |
| 127 | jabber_iq_set_id(iq, jox->iq_id); | |
| 128 | y = xmlnode_new_child(iq->node, "error"); | |
| 129 | /* FIXME: need to handle other kinds of errors here */ | |
| 130 | xmlnode_set_attrib(y, "code", "406"); | |
| 131 | xmlnode_insert_data(y, "File Transfer Refused", -1); | |
| 132 | ||
| 133 | jabber_iq_send(iq); | |
| 134 | ||
| 135 | jabber_oob_xfer_free(xfer); | |
| 136 | } | |
| 137 | ||
| 138 | void jabber_oob_parse(JabberStream *js, xmlnode *packet) { | |
| 139 | JabberOOBXfer *jox; | |
| 140 | GaimXfer *xfer; | |
| 141 | char *filename; | |
| 142 | char *url; | |
| 143 | xmlnode *querynode, *urlnode; | |
| 144 | ||
| 145 | if(!(querynode = xmlnode_get_child(packet, "query"))) | |
| 146 | return; | |
| 147 | ||
| 148 | if(!(urlnode = xmlnode_get_child(querynode, "url"))) | |
| 149 | return; | |
| 150 | ||
| 151 | url = xmlnode_get_data(urlnode); | |
| 152 | ||
| 153 | jox = g_new0(JabberOOBXfer, 1); | |
| 154 | gaim_url_parse(url, &jox->address, &jox->port, &jox->page); | |
| 155 | g_free(url); | |
| 156 | jox->js = js; | |
| 157 | jox->headers = g_string_new(""); | |
| 158 | jox->iq_id = g_strdup(xmlnode_get_attrib(packet, "id")); | |
| 159 | ||
| 160 | xfer = gaim_xfer_new(js->gc->account, GAIM_XFER_RECEIVE, | |
| 161 | xmlnode_get_attrib(packet, "from")); | |
| 162 | xfer->data = jox; | |
| 163 | ||
| 164 | if(!(filename = g_strdup(g_strrstr(jox->page, "/")))) | |
| 165 | filename = g_strdup(jox->page); | |
| 166 | ||
| 167 | gaim_xfer_set_filename(xfer, filename); | |
| 168 | ||
| 169 | g_free(filename); | |
| 170 | ||
| 171 | gaim_xfer_set_init_fnc(xfer, jabber_oob_xfer_init); | |
| 172 | gaim_xfer_set_end_fnc(xfer, jabber_oob_xfer_end); | |
| 8311 | 173 | gaim_xfer_set_request_denied_fnc(xfer, jabber_oob_xfer_cancel_recv); /* XXX */ |
| 7170 | 174 | gaim_xfer_set_cancel_recv_fnc(xfer, jabber_oob_xfer_cancel_recv); |
| 175 | gaim_xfer_set_read_fnc(xfer, jabber_oob_xfer_read); | |
| 176 | gaim_xfer_set_start_fnc(xfer, jabber_oob_xfer_start); | |
| 177 | ||
| 7395 | 178 | js->oob_file_transfers = g_list_append(js->oob_file_transfers, xfer); |
| 7170 | 179 | |
| 180 | gaim_xfer_request(xfer); | |
| 181 | } | |
| 182 | ||
| 183 |