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