Mercurial > pidgin
comparison src/protocols/yahoo/yahoo_filexfer.c @ 14084:b7e4180af1db
[gaim-migrate @ 16705]
Don't use the same callback for both gaim_proxy_connect() and
gaim_input_add(). Aside from being a little confusing, it's
hindering some changes I want to make to gaim_proxy_connect().
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Fri, 11 Aug 2006 08:08:19 +0000 |
parents | 8d1c55309e3c |
children | 10e8eb6a4910 |
comparison
equal
deleted
inserted
replaced
14083:08dbb5912709 | 14084:b7e4180af1db |
---|---|
55 if (xd->tx_handler) | 55 if (xd->tx_handler) |
56 gaim_input_remove(xd->tx_handler); | 56 gaim_input_remove(xd->tx_handler); |
57 g_free(xd); | 57 g_free(xd); |
58 } | 58 } |
59 | 59 |
60 static void yahoo_receivefile_connected(gpointer data, gint source, GaimInputCondition condition) | 60 static void yahoo_receivefile_send_cb(gpointer data, gint source, GaimInputCondition condition) |
61 { | 61 { |
62 GaimXfer *xfer; | 62 GaimXfer *xfer; |
63 struct yahoo_xfer_data *xd; | 63 struct yahoo_xfer_data *xd; |
64 int total_len, written; | 64 int remaining, written; |
65 | |
66 xfer = data; | |
67 xd = xfer->data; | |
68 | |
69 remaining = xd->txbuflen - xd->txbuf_written; | |
70 written = write(xfer->fd, xd->txbuf + xd->txbuf_written, remaining); | |
71 | |
72 if (written < 0 && errno == EAGAIN) | |
73 written = 0; | |
74 else if (written <= 0) { | |
75 gaim_debug_error("yahoo", "Unable to write in order to start ft errno = %d\n", errno); | |
76 gaim_xfer_cancel_remote(xfer); | |
77 return; | |
78 } | |
79 | |
80 if (written < remaining) { | |
81 xd->txbuf_written += written; | |
82 return; | |
83 } | |
84 | |
85 gaim_input_remove(xd->tx_handler); | |
86 xd->tx_handler = 0; | |
87 g_free(xd->txbuf); | |
88 xd->txbuf = NULL; | |
89 xd->txbuflen = 0; | |
90 | |
91 gaim_xfer_start(xfer, source, NULL, 0); | |
92 | |
93 } | |
94 | |
95 static void yahoo_receivefile_connected(gpointer data, gint source, GaimInputCondition condition) | |
96 { | |
97 GaimXfer *xfer; | |
98 struct yahoo_xfer_data *xd; | |
65 | 99 |
66 gaim_debug(GAIM_DEBUG_INFO, "yahoo", | 100 gaim_debug(GAIM_DEBUG_INFO, "yahoo", |
67 "AAA - in yahoo_receivefile_connected\n"); | 101 "AAA - in yahoo_receivefile_connected\n"); |
68 if (!(xfer = data)) | 102 if (!(xfer = data)) |
69 return; | 103 return; |
73 gaim_xfer_error(GAIM_XFER_RECEIVE, gaim_xfer_get_account(xfer), | 107 gaim_xfer_error(GAIM_XFER_RECEIVE, gaim_xfer_get_account(xfer), |
74 xfer->who, _("Unable to connect.")); | 108 xfer->who, _("Unable to connect.")); |
75 gaim_xfer_cancel_remote(xfer); | 109 gaim_xfer_cancel_remote(xfer); |
76 return; | 110 return; |
77 } | 111 } |
112 | |
113 xfer->fd = source; | |
78 | 114 |
79 /* The first time we get here, assemble the tx buffer */ | 115 /* The first time we get here, assemble the tx buffer */ |
80 if (xd->txbuflen == 0) { | 116 if (xd->txbuflen == 0) { |
81 xd->txbuf = g_strdup_printf("GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n", | 117 xd->txbuf = g_strdup_printf("GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n", |
82 xd->path, xd->host); | 118 xd->path, xd->host); |
83 xd->txbuflen = strlen(xd->txbuf); | 119 xd->txbuflen = strlen(xd->txbuf); |
84 xd->txbuf_written = 0; | 120 xd->txbuf_written = 0; |
85 } | 121 } |
86 | 122 |
87 total_len = xd->txbuflen - xd->txbuf_written; | 123 if (!xd->tx_handler) |
88 | 124 { |
89 xfer->fd = source; | 125 xd->tx_handler = gaim_input_add(source, GAIM_INPUT_WRITE, |
90 | 126 yahoo_receivefile_send_cb, xfer); |
91 written = write(xfer->fd, xd->txbuf + xd->txbuf_written, total_len); | 127 yahoo_receivefile_send_cb(xfer, source, GAIM_INPUT_WRITE); |
128 } | |
129 } | |
130 | |
131 static void yahoo_sendfile_send_cb(gpointer data, gint source, GaimInputCondition condition) | |
132 { | |
133 GaimXfer *xfer; | |
134 struct yahoo_xfer_data *xd; | |
135 int written, remaining; | |
136 | |
137 xfer = data; | |
138 xd = xfer->data; | |
139 | |
140 remaining = xd->txbuflen - xd->txbuf_written; | |
141 written = write(xfer->fd, xd->txbuf + xd->txbuf_written, remaining); | |
92 | 142 |
93 if (written < 0 && errno == EAGAIN) | 143 if (written < 0 && errno == EAGAIN) |
94 written = 0; | 144 written = 0; |
95 else if (written <= 0) { | 145 else if (written <= 0) { |
96 gaim_debug_error("yahoo", "Unable to write in order to start ft errno = %d\n", errno); | 146 gaim_debug_error("yahoo", "Unable to write in order to start ft errno = %d\n", errno); |
97 gaim_xfer_cancel_remote(xfer); | 147 gaim_xfer_cancel_remote(xfer); |
98 return; | 148 return; |
99 } | 149 } |
100 | 150 |
101 if (written < total_len) { | 151 if (written < remaining) { |
102 if (!xd->tx_handler) | |
103 xd->tx_handler = gaim_input_add(source, GAIM_INPUT_WRITE, | |
104 yahoo_receivefile_connected, xfer); | |
105 xd->txbuf_written += written; | 152 xd->txbuf_written += written; |
106 return; | 153 return; |
107 } | 154 } |
108 | 155 |
109 if (xd->tx_handler) | 156 gaim_input_remove(xd->tx_handler); |
110 gaim_input_remove(xd->tx_handler); | |
111 xd->tx_handler = 0; | 157 xd->tx_handler = 0; |
112 g_free(xd->txbuf); | 158 g_free(xd->txbuf); |
113 xd->txbuf = NULL; | 159 xd->txbuf = NULL; |
114 xd->txbuflen = 0; | 160 xd->txbuflen = 0; |
115 | 161 |
116 gaim_xfer_start(xfer, source, NULL, 0); | 162 gaim_xfer_start(xfer, source, NULL, 0); |
117 | 163 } |
118 } | |
119 | |
120 | 164 |
121 static void yahoo_sendfile_connected(gpointer data, gint source, GaimInputCondition condition) | 165 static void yahoo_sendfile_connected(gpointer data, gint source, GaimInputCondition condition) |
122 { | 166 { |
123 GaimXfer *xfer; | 167 GaimXfer *xfer; |
124 struct yahoo_xfer_data *xd; | 168 struct yahoo_xfer_data *xd; |
125 int written, total_len; | 169 struct yahoo_packet *pkt; |
170 gchar *size, *filename, *encoded_filename, *header; | |
171 guchar *pkt_buf; | |
172 const char *host; | |
173 int port; | |
174 size_t content_length, header_len, pkt_buf_len; | |
175 GaimConnection *gc; | |
176 GaimAccount *account; | |
177 struct yahoo_data *yd; | |
126 | 178 |
127 gaim_debug(GAIM_DEBUG_INFO, "yahoo", | 179 gaim_debug(GAIM_DEBUG_INFO, "yahoo", |
128 "AAA - in yahoo_sendfile_connected\n"); | 180 "AAA - in yahoo_sendfile_connected\n"); |
129 if (!(xfer = data)) | 181 if (!(xfer = data)) |
130 return; | 182 return; |
131 if (!(xd = xfer->data)) | 183 if (!(xd = xfer->data)) |
132 return; | 184 return; |
133 | |
134 | 185 |
135 if (source < 0) { | 186 if (source < 0) { |
136 gaim_xfer_error(GAIM_XFER_RECEIVE, gaim_xfer_get_account(xfer), | 187 gaim_xfer_error(GAIM_XFER_RECEIVE, gaim_xfer_get_account(xfer), |
137 xfer->who, _("Unable to connect.")); | 188 xfer->who, _("Unable to connect.")); |
138 gaim_xfer_cancel_remote(xfer); | 189 gaim_xfer_cancel_remote(xfer); |
139 return; | 190 return; |
140 } | 191 } |
141 | 192 |
142 /* The first time we get here, assemble the tx buffer */ | |
143 if (xd->txbuflen == 0) { | |
144 struct yahoo_packet *pkt; | |
145 gchar *size, *filename, *encoded_filename, *header; | |
146 guchar *pkt_buf; | |
147 const char *host; | |
148 int port; | |
149 size_t content_length, header_len, pkt_buf_len; | |
150 GaimConnection *gc; | |
151 GaimAccount *account; | |
152 struct yahoo_data *yd; | |
153 | |
154 gc = xd->gc; | |
155 account = gaim_connection_get_account(gc); | |
156 yd = gc->proto_data; | |
157 | |
158 pkt = yahoo_packet_new(YAHOO_SERVICE_FILETRANSFER, | |
159 YAHOO_STATUS_AVAILABLE, yd->session_id); | |
160 | |
161 size = g_strdup_printf("%" G_GSIZE_FORMAT, gaim_xfer_get_size(xfer)); | |
162 filename = g_path_get_basename(gaim_xfer_get_local_filename(xfer)); | |
163 encoded_filename = yahoo_string_encode(gc, filename, NULL); | |
164 | |
165 yahoo_packet_hash(pkt, "sssss", 0, gaim_connection_get_display_name(gc), | |
166 5, xfer->who, 14, "", 27, encoded_filename, 28, size); | |
167 g_free(size); | |
168 g_free(encoded_filename); | |
169 g_free(filename); | |
170 | |
171 content_length = YAHOO_PACKET_HDRLEN + yahoo_packet_length(pkt); | |
172 | |
173 pkt_buf_len = yahoo_packet_build(pkt, 8, FALSE, &pkt_buf); | |
174 yahoo_packet_free(pkt); | |
175 | |
176 host = gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST); | |
177 port = gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT); | |
178 header = g_strdup_printf( | |
179 "POST http://%s:%d/notifyft HTTP/1.0\r\n" | |
180 "Content-length: %" G_GSIZE_FORMAT "\r\n" | |
181 "Host: %s:%d\r\n" | |
182 "Cookie: Y=%s; T=%s\r\n" | |
183 "\r\n", | |
184 host, port, content_length + 4 + gaim_xfer_get_size(xfer), | |
185 host, port, yd->cookie_y, yd->cookie_t); | |
186 | |
187 | |
188 header_len = strlen(header); | |
189 | |
190 xd->txbuflen = header_len + pkt_buf_len + 4; | |
191 xd->txbuf = g_malloc(xd->txbuflen); | |
192 | |
193 memcpy(xd->txbuf, header, header_len); | |
194 g_free(header); | |
195 memcpy(xd->txbuf + header_len, pkt_buf, pkt_buf_len); | |
196 g_free(pkt_buf); | |
197 memcpy(xd->txbuf + header_len + pkt_buf_len, "29\xc0\x80", 4); | |
198 | |
199 xd->txbuf_written = 0; | |
200 } | |
201 | |
202 total_len = xd->txbuflen - xd->txbuf_written; | |
203 | |
204 xfer->fd = source; | 193 xfer->fd = source; |
205 | 194 |
206 written = write(xfer->fd, xd->txbuf + xd->txbuf_written, total_len); | 195 /* Assemble the tx buffer */ |
207 | 196 gc = xd->gc; |
208 if (written < 0 && errno == EAGAIN) | 197 account = gaim_connection_get_account(gc); |
209 written = 0; | 198 yd = gc->proto_data; |
210 else if (written <= 0) { | 199 |
211 gaim_debug_error("yahoo", "Unable to write in order to start ft errno = %d\n", errno); | 200 pkt = yahoo_packet_new(YAHOO_SERVICE_FILETRANSFER, |
212 gaim_xfer_cancel_remote(xfer); | 201 YAHOO_STATUS_AVAILABLE, yd->session_id); |
213 return; | 202 |
214 } | 203 size = g_strdup_printf("%" G_GSIZE_FORMAT, gaim_xfer_get_size(xfer)); |
215 | 204 filename = g_path_get_basename(gaim_xfer_get_local_filename(xfer)); |
216 if (written < total_len) { | 205 encoded_filename = yahoo_string_encode(gc, filename, NULL); |
217 if (!xd->tx_handler) | 206 |
218 xd->tx_handler = gaim_input_add(source, GAIM_INPUT_WRITE, | 207 yahoo_packet_hash(pkt, "sssss", 0, gaim_connection_get_display_name(gc), |
219 yahoo_sendfile_connected, xfer); | 208 5, xfer->who, 14, "", 27, encoded_filename, 28, size); |
220 xd->txbuf_written += written; | 209 g_free(size); |
221 return; | 210 g_free(encoded_filename); |
222 } | 211 g_free(filename); |
223 | 212 |
224 if (xd->tx_handler) | 213 content_length = YAHOO_PACKET_HDRLEN + yahoo_packet_length(pkt); |
225 gaim_input_remove(xd->tx_handler); | 214 |
226 xd->tx_handler = 0; | 215 pkt_buf_len = yahoo_packet_build(pkt, 8, FALSE, &pkt_buf); |
227 g_free(xd->txbuf); | 216 yahoo_packet_free(pkt); |
228 xd->txbuf = NULL; | 217 |
229 xd->txbuflen = 0; | 218 host = gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST); |
230 | 219 port = gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT); |
231 gaim_xfer_start(xfer, source, NULL, 0); | 220 header = g_strdup_printf( |
221 "POST http://%s:%d/notifyft HTTP/1.0\r\n" | |
222 "Content-length: %" G_GSIZE_FORMAT "\r\n" | |
223 "Host: %s:%d\r\n" | |
224 "Cookie: Y=%s; T=%s\r\n" | |
225 "\r\n", | |
226 host, port, content_length + 4 + gaim_xfer_get_size(xfer), | |
227 host, port, yd->cookie_y, yd->cookie_t); | |
228 | |
229 header_len = strlen(header); | |
230 | |
231 xd->txbuflen = header_len + pkt_buf_len + 4; | |
232 xd->txbuf = g_malloc(xd->txbuflen); | |
233 | |
234 memcpy(xd->txbuf, header, header_len); | |
235 g_free(header); | |
236 memcpy(xd->txbuf + header_len, pkt_buf, pkt_buf_len); | |
237 g_free(pkt_buf); | |
238 memcpy(xd->txbuf + header_len + pkt_buf_len, "29\xc0\x80", 4); | |
239 | |
240 xd->txbuf_written = 0; | |
241 | |
242 if (xd->tx_handler == 0) | |
243 { | |
244 xd->tx_handler = gaim_input_add(source, GAIM_INPUT_WRITE, | |
245 yahoo_sendfile_send_cb, xfer); | |
246 yahoo_sendfile_send_cb(xfer, source, GAIM_INPUT_WRITE); | |
247 } | |
232 } | 248 } |
233 | 249 |
234 static void yahoo_xfer_init(GaimXfer *xfer) | 250 static void yahoo_xfer_init(GaimXfer *xfer) |
235 { | 251 { |
236 struct yahoo_xfer_data *xfer_data; | 252 struct yahoo_xfer_data *xfer_data; |