Fri, 01 Sep 2006 08:52:23 +0000
[gaim-migrate @ 17110]
Make peer disconnection error messages a little better for oscar ft and
direct connect
| 13593 | 1 | /* |
| 2 | * Gaim's oscar protocol plugin | |
| 3 | * This file is the legal property of its developers. | |
| 4 | * Please see the AUTHORS file distributed alongside this file. | |
| 5 | * | |
| 6 | * This library is free software; you can redistribute it and/or | |
| 7 | * modify it under the terms of the GNU Lesser General Public | |
| 8 | * License as published by the Free Software Foundation; either | |
| 9 | * version 2 of the License, or (at your option) any later version. | |
| 10 | * | |
| 11 | * This library 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 GNU | |
| 14 | * Lesser General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU Lesser General Public | |
| 17 | * License along with this library; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 | */ | |
| 20 | ||
| 21 | /* From the oscar PRPL */ | |
| 22 | #include "oscar.h" | |
| 23 | #include "peer.h" | |
| 24 | ||
| 25 | /* From Gaim */ | |
| 26 | #include "conversation.h" | |
| 27 | #include "imgstore.h" | |
| 28 | #include "util.h" | |
| 29 | ||
| 30 | /** | |
| 31 | * Free any ODC related data and print a message to the conversation | |
| 32 | * window based on conn->disconnect_reason. | |
| 33 | */ | |
| 34 | void | |
| 35 | peer_odc_close(PeerConnection *conn) | |
| 36 | { | |
|
14464
b7bca43f75b3
[gaim-migrate @ 17110]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
37 | gchar *tmp; |
| 13593 | 38 | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13601
diff
changeset
|
39 | if (conn->disconnect_reason == OSCAR_DISCONNECT_REMOTE_CLOSED) |
|
14464
b7bca43f75b3
[gaim-migrate @ 17110]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
40 | tmp = g_strdup(_("The remote user has closed the connection.")); |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13601
diff
changeset
|
41 | else if (conn->disconnect_reason == OSCAR_DISCONNECT_REMOTE_REFUSED) |
|
14464
b7bca43f75b3
[gaim-migrate @ 17110]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
42 | tmp = g_strdup(_("The remote user has declined your request.")); |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13601
diff
changeset
|
43 | else if (conn->disconnect_reason == OSCAR_DISCONNECT_LOST_CONNECTION) |
|
14464
b7bca43f75b3
[gaim-migrate @ 17110]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
44 | tmp = g_strdup_printf(_("Lost connection with the remote user:<br>%s"), |
|
b7bca43f75b3
[gaim-migrate @ 17110]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
45 | conn->error_message); |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13601
diff
changeset
|
46 | else if (conn->disconnect_reason == OSCAR_DISCONNECT_INVALID_DATA) |
|
14464
b7bca43f75b3
[gaim-migrate @ 17110]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
47 | tmp = g_strdup(_("Received invalid data on connection with remote user.")); |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13601
diff
changeset
|
48 | else if (conn->disconnect_reason == OSCAR_DISCONNECT_COULD_NOT_CONNECT) |
|
14464
b7bca43f75b3
[gaim-migrate @ 17110]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
49 | tmp = g_strdup(_("Could not establish a connection with the remote user.")); |
| 13593 | 50 | else |
| 51 | /* | |
| 52 | * We shouldn't print a message for some disconnect_reasons. | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13601
diff
changeset
|
53 | * Like OSCAR_DISCONNECT_LOCAL_CLOSED. |
| 13593 | 54 | */ |
| 55 | tmp = NULL; | |
| 56 | ||
| 57 | if (tmp != NULL) | |
| 58 | { | |
| 59 | GaimAccount *account; | |
| 60 | GaimConversation *conv; | |
| 61 | ||
| 62 | account = gaim_connection_get_account(conn->od->gc); | |
| 63 | conv = gaim_conversation_new(GAIM_CONV_TYPE_IM, account, conn->sn); | |
| 64 | gaim_conversation_write(conv, NULL, tmp, GAIM_MESSAGE_SYSTEM, time(NULL)); | |
|
14464
b7bca43f75b3
[gaim-migrate @ 17110]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
65 | g_free(tmp); |
| 13593 | 66 | } |
| 67 | ||
| 68 | if (conn->frame != NULL) | |
| 69 | { | |
| 70 | OdcFrame *frame; | |
| 71 | frame = conn->frame; | |
| 72 | g_free(frame->payload.data); | |
| 73 | g_free(frame); | |
| 74 | } | |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * Write the given OdcFrame to a ByteStream and send it out | |
| 79 | * on the established PeerConnection. | |
| 80 | */ | |
| 81 | static void | |
| 82 | peer_odc_send(PeerConnection *conn, OdcFrame *frame) | |
| 83 | { | |
| 84 | GaimAccount *account; | |
| 85 | const char *username; | |
| 86 | size_t length; | |
| 87 | ByteStream bs; | |
| 88 | ||
| 89 | gaim_debug_info("oscar", "Outgoing ODC frame to %s with " | |
| 90 | "type=0x%04x, flags=0x%04x, payload length=%u\n", | |
| 91 | conn->sn, frame->type, frame->flags, frame->payload.len); | |
| 92 | ||
| 93 | account = gaim_connection_get_account(conn->od->gc); | |
| 94 | username = gaim_account_get_username(account); | |
| 95 | memcpy(frame->sn, username, strlen(username)); | |
| 96 | memcpy(frame->cookie, conn->cookie, 8); | |
| 97 | ||
| 98 | length = 76; | |
| 99 | byte_stream_init(&bs, malloc(length + frame->payload.len), | |
| 100 | length + frame->payload.len); | |
| 101 | byte_stream_putraw(&bs, conn->magic, 4); | |
| 102 | byte_stream_put16(&bs, length); | |
| 103 | byte_stream_put16(&bs, frame->type); | |
| 104 | byte_stream_put16(&bs, frame->subtype); | |
| 105 | byte_stream_put16(&bs, 0x0000); | |
| 106 | byte_stream_putraw(&bs, frame->cookie, 8); | |
| 107 | byte_stream_put16(&bs, 0x0000); | |
| 108 | byte_stream_put16(&bs, 0x0000); | |
| 109 | byte_stream_put16(&bs, 0x0000); | |
| 110 | byte_stream_put16(&bs, 0x0000); | |
| 111 | byte_stream_put32(&bs, frame->payload.len); | |
| 112 | byte_stream_put16(&bs, 0x0000); | |
| 113 | byte_stream_put16(&bs, frame->encoding); | |
| 114 | byte_stream_put16(&bs, 0x0000); | |
| 115 | byte_stream_put16(&bs, frame->flags); | |
| 116 | byte_stream_put16(&bs, 0x0000); | |
| 117 | byte_stream_put16(&bs, 0x0000); | |
| 118 | byte_stream_putraw(&bs, frame->sn, 32); | |
| 119 | byte_stream_putraw(&bs, frame->payload.data, frame->payload.len); | |
| 120 | ||
| 121 | peer_connection_send(conn, &bs); | |
| 122 | ||
| 123 | free(bs.data); | |
| 124 | } | |
| 125 | ||
| 126 | /** | |
| 127 | * Send a very basic ODC frame (which contains the cookie) so that the | |
| 128 | * remote user can verify that we are the person they were expecting. | |
| 129 | * If we made an outgoing connection to then remote user, then we send | |
| 130 | * this immediately. If the remote user connected to us, then we wait | |
| 131 | * for the other person to send this to us, then we send one to them. | |
| 132 | */ | |
| 133 | void | |
| 134 | peer_odc_send_cookie(PeerConnection *conn) | |
| 135 | { | |
| 136 | OdcFrame frame; | |
| 137 | ||
| 138 | memset(&frame, 0, sizeof(OdcFrame)); | |
| 139 | frame.type = 0x0001; | |
| 140 | frame.subtype = 0x0006; | |
| 141 | frame.flags = 0x0060; /* Maybe this means "we're sending the cookie"? */ | |
| 142 | ||
| 143 | peer_odc_send(conn, &frame); | |
| 144 | } | |
| 145 | ||
| 146 | /** | |
| 147 | * Send client-to-client typing notification over an established direct connection. | |
| 148 | */ | |
| 149 | void | |
| 150 | peer_odc_send_typing(PeerConnection *conn, GaimTypingState typing) | |
| 151 | { | |
| 152 | OdcFrame frame; | |
| 153 | ||
| 154 | memset(&frame, 0, sizeof(OdcFrame)); | |
| 155 | frame.type = 0x0001; | |
| 156 | frame.subtype = 0x0006; | |
| 157 | if (typing == GAIM_TYPING) | |
| 158 | frame.flags = 0x0002 | 0x0008; | |
| 159 | else if (typing == GAIM_TYPED) | |
| 160 | frame.flags = 0x0002 | 0x0004; | |
| 161 | else | |
| 162 | frame.flags = 0x0002; | |
| 163 | ||
| 164 | peer_odc_send(conn, &frame); | |
| 165 | } | |
| 166 | ||
| 167 | /** | |
| 168 | * Send client-to-client IM over an established direct connection. | |
| 169 | * To send a direct IM, call this just like you would aim_send_im. | |
| 170 | * | |
| 171 | * @param conn The already-connected ODC connection. | |
| 172 | * @param msg Null-terminated string to send. | |
| 173 | * @param len The length of the message to send, including binary data. | |
| 174 | * @param encoding See the AIM_CHARSET_* defines in oscar.h | |
| 175 | * @param autoreply TRUE if this is any auto-reply. | |
| 176 | */ | |
| 177 | void | |
| 178 | peer_odc_send_im(PeerConnection *conn, const char *msg, int len, int encoding, gboolean autoreply) | |
| 179 | { | |
| 180 | OdcFrame frame; | |
| 181 | ||
| 182 | g_return_if_fail(msg != NULL); | |
| 183 | g_return_if_fail(len > 0); | |
| 184 | ||
| 185 | memset(&frame, 0, sizeof(OdcFrame)); | |
| 186 | frame.type = 0x0001; | |
| 187 | frame.subtype = 0x0006; | |
| 188 | frame.payload.len = len; | |
| 189 | frame.encoding = encoding; | |
| 190 | frame.flags = autoreply; | |
| 191 | byte_stream_init(&frame.payload, malloc(len), len); | |
| 192 | byte_stream_putraw(&frame.payload, (guint8 *)msg, len); | |
| 193 | ||
| 194 | peer_odc_send(conn, &frame); | |
| 195 | ||
| 196 | g_free(frame.payload.data); | |
| 197 | } | |
| 198 | ||
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
199 | struct embedded_data |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
200 | { |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
201 | size_t size; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
202 | const guint8 *data; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
203 | }; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
204 | |
| 13593 | 205 | /** |
| 206 | * This is called after a direct IM has been received in its entirety. This | |
| 207 | * function is passed a long chunk of data which contains the IM with any | |
| 208 | * data chunks (images) appended to it. | |
| 209 | * | |
| 210 | * This function rips out all the data chunks and creates an imgstore for | |
| 211 | * each one. In order to do this, it first goes through the IM and takes | |
| 212 | * out all the IMG tags. When doing so, it rewrites the original IMG tag | |
| 213 | * with one compatible with the imgstore Gaim core code. For each one, we | |
| 214 | * then read in chunks of data from the end of the message and actually | |
| 215 | * create the img store using the given data. | |
| 216 | * | |
| 217 | * For somewhat easy reference, here's a sample message | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
218 | * (with added whitespace): |
| 13593 | 219 | * |
| 220 | * <HTML><BODY BGCOLOR="#ffffff"> | |
| 221 | * <FONT LANG="0"> | |
| 222 | * This is a really stupid picture:<BR> | |
| 223 | * <IMG SRC="Sample.jpg" ID="1" WIDTH="283" HEIGHT="212" DATASIZE="9894"><BR> | |
| 224 | * Yeah it is<BR> | |
| 225 | * Here is another one:<BR> | |
| 226 | * <IMG SRC="Soap Bubbles.bmp" ID="2" WIDTH="256" HEIGHT="256" DATASIZE="65978"> | |
| 227 | * </FONT> | |
| 228 | * </BODY></HTML> | |
| 229 | * <BINARY> | |
| 230 | * <DATA ID="1" SIZE="9894">datadatadatadata</DATA> | |
| 231 | * <DATA ID="2" SIZE="65978">datadatadatadata</DATA> | |
| 232 | * </BINARY> | |
| 233 | */ | |
| 234 | static void | |
| 235 | peer_odc_handle_payload(PeerConnection *conn, const char *msg, size_t len, int encoding, gboolean autoreply) | |
| 236 | { | |
| 237 | GaimConnection *gc; | |
| 238 | GaimAccount *account; | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
239 | const char *msgend, *binary_start, *dataend; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
240 | const char *tmp, *start, *end, *idstr, *src, *sizestr; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
241 | GData *attributes; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
242 | GHashTable *embedded_datas; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
243 | struct embedded_data *embedded_data; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
244 | GSList *images; |
| 13593 | 245 | gchar *utf8; |
| 246 | GString *newmsg; | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
247 | GaimMessageFlags imflags; |
| 13593 | 248 | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
249 | gc = conn->od->gc; |
| 13593 | 250 | account = gaim_connection_get_account(gc); |
| 251 | ||
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
252 | dataend = msg + len; |
| 13593 | 253 | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
254 | /* |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
255 | * Create a hash table containing references to each embedded |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
256 | * data chunk. The key is the "ID" and the value is an |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
257 | * embedded_data struct. |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
258 | */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
259 | embedded_datas = g_hash_table_new_full(g_direct_hash, |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
260 | g_direct_equal, NULL, g_free); |
| 13593 | 261 | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
262 | /* |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
263 | * Create an index of any binary chunks. If we run into any |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
264 | * problems while parsing the binary data section then we stop |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
265 | * parsing it, and the local user will see broken image icons. |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
266 | */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
267 | /* TODO: Use a length argument when looking for the <binary> tag! */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
268 | binary_start = gaim_strcasestr(msg, "<binary>"); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
269 | if (binary_start == NULL) |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
270 | msgend = dataend; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
271 | else |
| 13593 | 272 | { |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
273 | msgend = binary_start; |
| 13593 | 274 | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
275 | /* Move our pointer to immediately after the <binary> tag */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
276 | tmp = binary_start + 8; |
| 13593 | 277 | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
278 | /* The embedded binary markup has a mimimum length of 29 bytes */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
279 | /* TODO: Use a length argument when looking for the <data> tag! */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
280 | while ((tmp + 29 <= dataend) && |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
281 | gaim_markup_find_tag("data", tmp, &start, &tmp, &attributes)) |
| 13593 | 282 | { |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
283 | unsigned int id; |
| 13593 | 284 | size_t size; |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
285 | |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
286 | /* Move the binary pointer from ">" to the start of the data */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
287 | tmp++; |
| 13593 | 288 | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
289 | /* Get the ID */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
290 | idstr = g_datalist_get_data(&attributes, "id"); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
291 | if (idstr == NULL) |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
292 | { |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
293 | g_datalist_clear(&attributes); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
294 | break; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
295 | } |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
296 | id = atoi(idstr); |
| 13593 | 297 | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
298 | /* Get the size */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
299 | sizestr = g_datalist_get_data(&attributes, "size"); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
300 | if (sizestr == NULL) |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
301 | { |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
302 | g_datalist_clear(&attributes); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
303 | break; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
304 | } |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
305 | size = atol(sizestr); |
| 13593 | 306 | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
307 | g_datalist_clear(&attributes); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
308 | |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
309 | if ((size > 0) && (tmp + size > dataend)) |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
310 | break; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
311 | |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
312 | embedded_data = g_new(struct embedded_data, 1); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
313 | embedded_data->size = size; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
314 | embedded_data->data = (const guint8 *)tmp; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
315 | tmp += size; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
316 | |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
317 | /* Skip past the closing </data> tag */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
318 | if (strncasecmp(tmp, "</data>", 7)) |
|
13600
4f436279a21c
[gaim-migrate @ 15985]
Mark Doliner <markdoliner@pidgin.im>
parents:
13593
diff
changeset
|
319 | { |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
320 | g_free(embedded_data); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
321 | break; |
|
13600
4f436279a21c
[gaim-migrate @ 15985]
Mark Doliner <markdoliner@pidgin.im>
parents:
13593
diff
changeset
|
322 | } |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
323 | tmp += 7; |
| 13593 | 324 | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
325 | g_hash_table_insert(embedded_datas, |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
326 | GINT_TO_POINTER(id), embedded_data); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
327 | } |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
328 | } |
| 13593 | 329 | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
330 | /* |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
331 | * Loop through the message, replacing OSCAR img tags with the |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
332 | * equivalent Gaim img tag. |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
333 | */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
334 | images = NULL; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
335 | newmsg = g_string_new(""); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
336 | tmp = msg; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
337 | while (gaim_markup_find_tag("img", tmp, &start, &end, &attributes)) |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
338 | { |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
339 | int imgid = 0; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
340 | |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
341 | idstr = g_datalist_get_data(&attributes, "id"); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
342 | src = g_datalist_get_data(&attributes, "src"); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
343 | sizestr = g_datalist_get_data(&attributes, "datasize"); |
| 13593 | 344 | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
345 | if ((idstr != NULL) && (src != NULL) && (sizestr!= NULL)) |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
346 | { |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
347 | unsigned int id; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
348 | size_t size; |
| 13593 | 349 | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
350 | id = atoi(idstr); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
351 | size = atol(sizestr); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
352 | embedded_data = g_hash_table_lookup(embedded_datas, |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
353 | GINT_TO_POINTER(id)); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
354 | |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
355 | if ((embedded_data != NULL) && (embedded_data->size == size)) |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
356 | { |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
357 | imgid = gaim_imgstore_add(embedded_data->data, size, src); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
358 | |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
359 | /* Record the image number */ |
| 13593 | 360 | images = g_slist_append(images, GINT_TO_POINTER(imgid)); |
| 361 | } | |
| 362 | } | |
| 363 | ||
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
364 | /* Delete the attribute list */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
365 | g_datalist_clear(&attributes); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
366 | |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
367 | /* Append the message up to the tag */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
368 | utf8 = gaim_plugin_oscar_decode_im_part(account, conn->sn, |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
369 | encoding, 0x0000, tmp, start - tmp); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
370 | if (utf8 != NULL) { |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
371 | g_string_append(newmsg, utf8); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
372 | g_free(utf8); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
373 | } |
| 13593 | 374 | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
375 | if (imgid != 0) |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
376 | { |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
377 | /* Write the new image tag */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
378 | g_string_append_printf(newmsg, "<IMG ID=\"%d\">", imgid); |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
379 | } |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
380 | |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
381 | /* Continue from the end of the tag */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
382 | tmp = end + 1; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
383 | } |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
384 | |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
385 | /* Append any remaining message data */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
386 | if (tmp <= msgend) |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
387 | { |
| 13593 | 388 | utf8 = gaim_plugin_oscar_decode_im_part(account, conn->sn, |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
389 | encoding, 0x0000, tmp, msgend - tmp); |
| 13593 | 390 | if (utf8 != NULL) { |
| 391 | g_string_append(newmsg, utf8); | |
| 392 | g_free(utf8); | |
| 393 | } | |
| 394 | } | |
| 395 | ||
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
396 | /* Send the message */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
397 | imflags = 0; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
398 | if (images != NULL) |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
399 | imflags |= GAIM_MESSAGE_IMAGES; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
400 | if (autoreply) |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
401 | imflags |= GAIM_MESSAGE_AUTO_RESP; |
| 13593 | 402 | serv_got_im(gc, conn->sn, newmsg->str, imflags, time(NULL)); |
| 403 | g_string_free(newmsg, TRUE); | |
| 404 | ||
| 405 | /* unref any images we allocated */ | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
406 | if (images) |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
407 | { |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
408 | GSList *l; |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
409 | for (l = images; l != NULL; l = l->next) |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
410 | gaim_imgstore_unref(GPOINTER_TO_INT(l->data)); |
| 13593 | 411 | g_slist_free(images); |
| 412 | } | |
|
13601
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
413 | |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
414 | /* Delete our list of pointers to embedded images */ |
|
d22aea27e7e6
[gaim-migrate @ 15986]
Mark Doliner <markdoliner@pidgin.im>
parents:
13600
diff
changeset
|
415 | g_hash_table_destroy(embedded_datas); |
| 13593 | 416 | } |
| 417 | ||
| 418 | /** | |
| 419 | * This is a gaim_input_add() watcher callback function for reading | |
| 420 | * direct IM payload data. "Payload data" is always an IM and | |
| 421 | * maybe some embedded images or files or something. The actual | |
| 422 | * ODC frame is read using peer_connection_recv_cb(). We temporarily | |
| 423 | * switch to this watcher callback ONLY to read the payload, and we | |
| 424 | * switch back once we're done. | |
| 425 | */ | |
| 426 | static void | |
| 427 | peer_odc_recv_cb(gpointer data, gint source, GaimInputCondition cond) | |
| 428 | { | |
| 429 | PeerConnection *conn; | |
| 430 | OdcFrame *frame; | |
| 431 | ByteStream *bs; | |
| 432 | ssize_t read; | |
| 433 | ||
| 434 | conn = data; | |
| 435 | frame = conn->frame; | |
| 436 | bs = &frame->payload; | |
| 437 | ||
| 438 | /* Read data into the temporary buffer until it is complete */ | |
| 439 | read = recv(conn->fd, | |
| 440 | &bs->data[bs->offset], | |
| 441 | bs->len - bs->offset, | |
| 442 | 0); | |
| 443 | ||
| 444 | /* Check if the remote user closed the connection */ | |
| 445 | if (read == 0) | |
| 446 | { | |
|
14464
b7bca43f75b3
[gaim-migrate @ 17110]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
447 | peer_connection_destroy(conn, OSCAR_DISCONNECT_REMOTE_CLOSED, NULL); |
| 13593 | 448 | return; |
| 449 | } | |
| 450 | ||
| 451 | if (read == -1) | |
| 452 | { | |
| 453 | if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) | |
| 454 | /* No worries */ | |
| 455 | return; | |
| 456 | ||
|
14464
b7bca43f75b3
[gaim-migrate @ 17110]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
457 | peer_connection_destroy(conn, |
|
b7bca43f75b3
[gaim-migrate @ 17110]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
458 | OSCAR_DISCONNECT_LOST_CONNECTION, strerror(errno)); |
| 13593 | 459 | return; |
| 460 | } | |
| 461 | ||
| 462 | bs->offset += read; | |
| 463 | if (bs->offset < bs->len) | |
| 464 | /* Waiting for more data to arrive */ | |
| 465 | return; | |
| 466 | ||
| 467 | /* We have a complete ODC/OFT frame! Handle it and continue reading */ | |
| 468 | byte_stream_rewind(bs); | |
| 469 | peer_odc_handle_payload(conn, (const char *)bs->data, | |
| 470 | bs->len, frame->encoding, frame->flags & 0x0001); | |
| 471 | g_free(bs->data); | |
| 472 | bs->data = NULL; | |
| 473 | g_free(frame); | |
| 474 | conn->frame = NULL; | |
| 475 | ||
| 476 | gaim_input_remove(conn->watcher_incoming); | |
| 477 | conn->watcher_incoming = gaim_input_add(conn->fd, | |
| 478 | GAIM_INPUT_READ, peer_connection_recv_cb, conn); | |
| 479 | } | |
| 480 | ||
| 481 | /** | |
| 482 | * Handle an incoming OdcFrame. If there is a payload associated | |
| 483 | * with this frame, then we remove the old watcher and add the | |
| 484 | * ODC watcher to read in the payload. | |
| 485 | */ | |
| 486 | void | |
| 487 | peer_odc_recv_frame(PeerConnection *conn, ByteStream *bs) | |
| 488 | { | |
| 489 | GaimConnection *gc; | |
| 490 | OdcFrame *frame; | |
| 491 | ||
| 492 | gc = conn->od->gc; | |
| 493 | ||
| 494 | frame = g_new0(OdcFrame, 1); | |
| 495 | frame->type = byte_stream_get16(bs); | |
| 496 | frame->subtype = byte_stream_get16(bs); | |
| 497 | byte_stream_advance(bs, 2); | |
| 498 | byte_stream_getrawbuf(bs, frame->cookie, 8); | |
| 499 | byte_stream_advance(bs, 8); | |
| 500 | frame->payload.len = byte_stream_get32(bs); | |
| 501 | frame->encoding = byte_stream_get16(bs); | |
| 502 | byte_stream_advance(bs, 4); | |
| 503 | frame->flags = byte_stream_get16(bs); | |
| 504 | byte_stream_advance(bs, 4); | |
| 505 | byte_stream_getrawbuf(bs, frame->sn, 32); | |
| 506 | ||
| 507 | gaim_debug_info("oscar", "Incoming ODC frame from %s with " | |
| 508 | "type=0x%04x, flags=0x%04x, payload length=%u\n", | |
| 509 | frame->sn, frame->type, frame->flags, frame->payload.len); | |
| 510 | ||
| 511 | if (!conn->ready) | |
| 512 | { | |
| 513 | /* | |
| 514 | * We need to verify the cookie so that we know we are | |
| 515 | * connected to our friend and not a malicious middle man. | |
| 516 | */ | |
| 517 | ||
| 518 | GaimAccount *account; | |
| 519 | GaimConversation *conv; | |
| 520 | ||
| 521 | if (conn->flags & PEER_CONNECTION_FLAG_IS_INCOMING) | |
| 522 | { | |
| 523 | if (memcmp(conn->cookie, frame->cookie, 8)) | |
| 524 | { | |
| 525 | /* | |
| 526 | * Oh no! The user that connected to us did not send | |
| 527 | * the correct cookie! They are not our friend. Go try | |
| 528 | * to accept another connection? | |
| 529 | */ | |
| 530 | gaim_debug_info("oscar", "Received an incorrect cookie. " | |
| 531 | "Closing connection.\n"); | |
|
14464
b7bca43f75b3
[gaim-migrate @ 17110]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
532 | peer_connection_destroy(conn, |
|
b7bca43f75b3
[gaim-migrate @ 17110]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
533 | OSCAR_DISCONNECT_INVALID_DATA, NULL); |
| 13593 | 534 | g_free(frame); |
| 535 | return; | |
| 536 | } | |
| 537 | ||
| 538 | /* | |
| 539 | * Ok, we know they are legit. Now be courteous and | |
| 540 | * send them our cookie. Note: This doesn't seem | |
| 541 | * to be necessary, but it also doesn't seem to hurt. | |
| 542 | */ | |
| 543 | peer_odc_send_cookie(conn); | |
| 544 | } | |
| 545 | ||
| 546 | conn->ready = TRUE; | |
| 547 | ||
| 548 | /* | |
| 549 | * If they connected to us then close the listener socket | |
| 550 | * and send them our cookie. | |
| 551 | */ | |
| 552 | if (conn->listenerfd != -1) | |
| 553 | { | |
| 554 | close(conn->listenerfd); | |
| 555 | conn->listenerfd = -1; | |
| 556 | } | |
| 557 | ||
| 558 | /* Tell the local user that we are connected */ | |
| 559 | account = gaim_connection_get_account(gc); | |
| 560 | conv = gaim_conversation_new(GAIM_CONV_TYPE_IM, account, conn->sn); | |
| 561 | gaim_conversation_write(conv, NULL, _("Direct IM established"), | |
| 562 | GAIM_MESSAGE_SYSTEM, time(NULL)); | |
| 563 | } | |
| 564 | ||
| 565 | if ((frame->type != 0x0001) && (frame->subtype != 0x0006)) | |
| 566 | { | |
| 567 | gaim_debug_info("oscar", "Unknown ODC frame type 0x%04hx, " | |
| 568 | "subtype 0x%04hx.\n", frame->type, frame->subtype); | |
| 569 | return; | |
| 570 | } | |
| 571 | ||
| 572 | if (frame->flags & 0x0008) | |
| 573 | { | |
| 574 | /* I had to leave this. It's just too funny. It reminds me of my sister. */ | |
| 575 | gaim_debug_info("oscar", "ohmigod! %s has started typing " | |
| 576 | "(DirectIM). He's going to send you a message! " | |
| 577 | "*squeal*\n", conn->sn); | |
| 578 | serv_got_typing(gc, conn->sn, 0, GAIM_TYPING); | |
| 579 | } | |
| 580 | else if (frame->flags & 0x0004) | |
| 581 | { | |
| 582 | serv_got_typing(gc, conn->sn, 0, GAIM_TYPED); | |
| 583 | } | |
| 584 | else | |
| 585 | { | |
| 586 | serv_got_typing_stopped(gc, conn->sn); | |
| 587 | } | |
| 588 | ||
| 589 | if (frame->payload.len > 0) | |
| 590 | { | |
| 591 | /* We have payload data! Switch to the ODC watcher to read it. */ | |
| 592 | frame->payload.data = g_new(guint8, frame->payload.len); | |
| 593 | frame->payload.offset = 0; | |
| 594 | conn->frame = frame; | |
| 595 | gaim_input_remove(conn->watcher_incoming); | |
| 596 | conn->watcher_incoming = gaim_input_add(conn->fd, | |
| 597 | GAIM_INPUT_READ, peer_odc_recv_cb, conn); | |
| 598 | return; | |
| 599 | } | |
| 600 | ||
| 601 | g_free(frame); | |
| 602 | } |