Mon, 04 Jun 2007 06:57:25 +0000
Christopher Layne tells me that a null in a Yahoo packet where we
were expecting a 0xc0 80 delimiter means that we've reached the
end of the packet. I left a ridiciously long comment in the code.
| 10392 | 1 | /* |
| 15884 | 2 | * purple |
| 10392 | 3 | * |
| 15884 | 4 | * Purple is the legal property of its developers, whose names are too numerous |
| 10392 | 5 | * to list here. Please refer to the COPYRIGHT file distributed with this |
| 6 | * source distribution. | |
| 7 | * | |
| 8 | * This program is free software; you can redistribute it and/or modify | |
| 9 | * it under the terms of the GNU General Public License as published by | |
| 10 | * the Free Software Foundation; either version 2 of the License, or | |
| 11 | * (at your option) any later version. | |
| 12 | * | |
| 13 | * This program is distributed in the hope that it will be useful, | |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 16 | * GNU General Public License for more details. | |
| 17 | * | |
| 18 | * You should have received a copy of the GNU General Public License | |
| 19 | * along with this program; if not, write to the Free Software | |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 21 | * | |
| 22 | */ | |
| 23 | ||
| 24 | #include "internal.h" | |
| 25 | #include "debug.h" | |
| 26 | ||
| 27 | #include "yahoo.h" | |
| 28 | #include "yahoo_packet.h" | |
| 29 | ||
| 30 | struct yahoo_packet *yahoo_packet_new(enum yahoo_service service, enum yahoo_status status, int id) | |
| 31 | { | |
| 32 | struct yahoo_packet *pkt = g_new0(struct yahoo_packet, 1); | |
| 33 | ||
| 34 | pkt->service = service; | |
| 35 | pkt->status = status; | |
| 36 | pkt->id = id; | |
| 37 | ||
| 38 | return pkt; | |
| 39 | } | |
| 40 | ||
| 10394 | 41 | void yahoo_packet_hash_str(struct yahoo_packet *pkt, int key, const char *value) |
| 10392 | 42 | { |
|
14204
6ee6b82a7c49
[gaim-migrate @ 16784]
Mark Doliner <markdoliner@pidgin.im>
parents:
14095
diff
changeset
|
43 | struct yahoo_pair *pair; |
|
6ee6b82a7c49
[gaim-migrate @ 16784]
Mark Doliner <markdoliner@pidgin.im>
parents:
14095
diff
changeset
|
44 | |
|
6ee6b82a7c49
[gaim-migrate @ 16784]
Mark Doliner <markdoliner@pidgin.im>
parents:
14095
diff
changeset
|
45 | g_return_if_fail(value != NULL); |
|
6ee6b82a7c49
[gaim-migrate @ 16784]
Mark Doliner <markdoliner@pidgin.im>
parents:
14095
diff
changeset
|
46 | |
|
6ee6b82a7c49
[gaim-migrate @ 16784]
Mark Doliner <markdoliner@pidgin.im>
parents:
14095
diff
changeset
|
47 | pair = g_new0(struct yahoo_pair, 1); |
| 10392 | 48 | pair->key = key; |
| 49 | pair->value = g_strdup(value); | |
|
14095
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
50 | pkt->hash = g_slist_prepend(pkt->hash, pair); |
| 10392 | 51 | } |
| 52 | ||
| 10394 | 53 | void yahoo_packet_hash_int(struct yahoo_packet *pkt, int key, int value) |
| 54 | { | |
|
14204
6ee6b82a7c49
[gaim-migrate @ 16784]
Mark Doliner <markdoliner@pidgin.im>
parents:
14095
diff
changeset
|
55 | struct yahoo_pair *pair; |
| 10394 | 56 | |
|
14204
6ee6b82a7c49
[gaim-migrate @ 16784]
Mark Doliner <markdoliner@pidgin.im>
parents:
14095
diff
changeset
|
57 | pair = g_new0(struct yahoo_pair, 1); |
| 10394 | 58 | pair->key = key; |
| 59 | pair->value = g_strdup_printf("%d", value); | |
|
14095
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
60 | pkt->hash = g_slist_prepend(pkt->hash, pair); |
| 10394 | 61 | } |
| 62 | ||
| 63 | void yahoo_packet_hash(struct yahoo_packet *pkt, const char *fmt, ...) | |
| 64 | { | |
| 65 | char *strval; | |
| 66 | int key, intval; | |
| 67 | const char *cur; | |
| 68 | va_list ap; | |
| 69 | ||
| 70 | va_start(ap, fmt); | |
| 71 | for (cur = fmt; *cur; cur++) { | |
| 72 | key = va_arg(ap, int); | |
| 73 | switch (*cur) { | |
| 74 | case 'i': | |
| 75 | intval = va_arg(ap, int); | |
| 76 | yahoo_packet_hash_int(pkt, key, intval); | |
| 77 | break; | |
| 78 | case 's': | |
| 79 | strval = va_arg(ap, char *); | |
| 80 | yahoo_packet_hash_str(pkt, key, strval); | |
| 81 | break; | |
| 82 | default: | |
| 15884 | 83 | purple_debug_error("yahoo", "Invalid format character '%c'\n", *cur); |
| 10394 | 84 | break; |
| 85 | } | |
| 86 | } | |
| 87 | va_end(ap); | |
| 88 | } | |
| 89 | ||
|
13277
c8a85dd74704
[gaim-migrate @ 15642]
Richard Laager <rlaager@pidgin.im>
parents:
13276
diff
changeset
|
90 | size_t yahoo_packet_length(struct yahoo_packet *pkt) |
| 10392 | 91 | { |
| 92 | GSList *l; | |
| 93 | ||
|
13277
c8a85dd74704
[gaim-migrate @ 15642]
Richard Laager <rlaager@pidgin.im>
parents:
13276
diff
changeset
|
94 | size_t len = 0; |
| 10392 | 95 | |
| 96 | l = pkt->hash; | |
| 97 | while (l) { | |
| 98 | struct yahoo_pair *pair = l->data; | |
| 99 | int tmp = pair->key; | |
| 100 | do { | |
| 101 | tmp /= 10; | |
| 102 | len++; | |
| 103 | } while (tmp); | |
| 104 | len += 2; | |
| 105 | len += strlen(pair->value); | |
| 106 | len += 2; | |
| 107 | l = l->next; | |
| 108 | } | |
| 109 | ||
| 110 | return len; | |
| 111 | } | |
| 112 | ||
|
17577
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
113 | /* |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
114 | * 'len' is the value given to us by the server that is supposed to |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
115 | * be the length of 'data'. But apparently there's a time when this |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
116 | * length is incorrect. Christopher Layne thinks it might be a bug |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
117 | * in their server code. |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
118 | * |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
119 | * The following information is from Christopher: |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
120 | * |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
121 | * It sometimes happens when Yahoo! sends a packet continuation within |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
122 | * chat. Sometimes when joining a large chatroom the initial |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
123 | * SERVICE_CHATJOIN packet will be so large that it will need to be |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
124 | * split into multiple packets. That's fine, except that the length |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
125 | * of the second packet is wrong. The packet has the same length as |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
126 | * the first packet, and the length given in the header is the same, |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
127 | * however the actual data in the packet is shorter than this length. |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
128 | * So half of the packet contains good, valid data, and then the rest |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
129 | * of the packet is junk. Luckily there is a null terminator after |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
130 | * the valid data and before the invalid data. |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
131 | * |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
132 | * What does all this mean? It means that we parse through the data |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
133 | * pulling out key/value pairs until we've parsed 'len' bytes, or until |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
134 | * we run into a null terminator, whichever comes first. |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
135 | */ |
|
14095
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
136 | void yahoo_packet_read(struct yahoo_packet *pkt, const guchar *data, int len) |
| 10392 | 137 | { |
| 138 | int pos = 0; | |
|
14322
662704dffc4d
[gaim-migrate @ 16942]
Mark Doliner <markdoliner@pidgin.im>
parents:
14321
diff
changeset
|
139 | char key[64]; |
|
662704dffc4d
[gaim-migrate @ 16942]
Mark Doliner <markdoliner@pidgin.im>
parents:
14321
diff
changeset
|
140 | const guchar *delimiter; |
|
14095
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
141 | gboolean accept; |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
142 | int x; |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
143 | struct yahoo_pair *pair; |
| 10392 | 144 | |
|
14095
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
145 | while (pos + 1 < len) |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
146 | { |
|
17577
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
147 | if (data[pos] == '\0') |
|
c98f95792cbc
Christopher Layne tells me that a null in a Yahoo packet where we
Mark Doliner <markdoliner@pidgin.im>
parents:
15982
diff
changeset
|
148 | break; |
| 10392 | 149 | |
|
14095
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
150 | pair = g_new0(struct yahoo_pair, 1); |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
151 | |
| 10392 | 152 | x = 0; |
| 153 | while (pos + 1 < len) { | |
| 154 | if (data[pos] == 0xc0 && data[pos + 1] == 0x80) | |
| 155 | break; | |
| 156 | if (x >= sizeof(key)-1) { | |
| 157 | x++; | |
| 158 | pos++; | |
| 159 | continue; | |
| 160 | } | |
| 161 | key[x++] = data[pos++]; | |
| 162 | } | |
| 163 | if (x >= sizeof(key)-1) { | |
| 164 | x = 0; | |
| 165 | } | |
| 166 | key[x] = 0; | |
| 167 | pos += 2; | |
| 168 | pair->key = strtol(key, NULL, 10); | |
| 169 | accept = x; /* if x is 0 there was no key, so don't accept it */ | |
| 170 | ||
|
15283
efefd3bf8b81
[gaim-migrate @ 18011]
Mark Doliner <markdoliner@pidgin.im>
parents:
15276
diff
changeset
|
171 | if (pos + 1 > len) { |
|
efefd3bf8b81
[gaim-migrate @ 18011]
Mark Doliner <markdoliner@pidgin.im>
parents:
15276
diff
changeset
|
172 | /* Malformed packet! (Truncated--garbage or something) */ |
|
14095
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
173 | accept = FALSE; |
| 10392 | 174 | } |
| 175 | ||
| 176 | if (accept) { | |
|
15982
31ae61be5792
Fix a small tooltip-related memleak
Mark Doliner <markdoliner@pidgin.im>
parents:
15884
diff
changeset
|
177 | /* TODO: strstr() should not be used here because data isn't NULL terminated */ |
|
14322
662704dffc4d
[gaim-migrate @ 16942]
Mark Doliner <markdoliner@pidgin.im>
parents:
14321
diff
changeset
|
178 | delimiter = (const guchar *)strstr((char *)&data[pos], "\xc0\x80"); |
|
14095
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
179 | if (delimiter == NULL) |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
180 | { |
|
15283
efefd3bf8b81
[gaim-migrate @ 18011]
Mark Doliner <markdoliner@pidgin.im>
parents:
15276
diff
changeset
|
181 | /* Malformed packet! (It doesn't end in 0xc0 0x80) */ |
|
14095
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
182 | g_free(pair); |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
183 | pos = len; |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
184 | continue; |
| 10392 | 185 | } |
|
14322
662704dffc4d
[gaim-migrate @ 16942]
Mark Doliner <markdoliner@pidgin.im>
parents:
14321
diff
changeset
|
186 | x = delimiter - data; |
|
14095
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
187 | pair->value = g_strndup((const gchar *)&data[pos], x - pos); |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
188 | pos = x; |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
189 | pkt->hash = g_slist_prepend(pkt->hash, pair); |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
190 | |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
191 | #ifdef DEBUG |
| 14321 | 192 | { |
| 193 | char *esc; | |
| 194 | esc = g_strescape(pair->value, NULL); | |
| 15884 | 195 | purple_debug(PURPLE_DEBUG_MISC, "yahoo", |
| 14321 | 196 | "Key: %d \tValue: %s\n", pair->key, esc); |
| 197 | g_free(esc); | |
| 198 | } | |
|
14095
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
199 | #endif |
| 10392 | 200 | } else { |
| 201 | g_free(pair); | |
| 202 | } | |
| 203 | pos += 2; | |
| 204 | ||
| 205 | /* Skip over garbage we've noticed in the mail notifications */ | |
| 206 | if (data[0] == '9' && data[pos] == 0x01) | |
| 207 | pos++; | |
| 208 | } | |
|
14095
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
209 | |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
210 | /* |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
211 | * Originally this function used g_slist_append(). I changed |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
212 | * it to use g_slist_prepend() for improved performance. |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
213 | * Ideally the Yahoo! PRPL code would be indifferent to the |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
214 | * order of the key/value pairs, but I don't know if this is |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
215 | * the case for all incoming messages. To be on the safe side |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
216 | * we reverse the list. |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
217 | */ |
|
125eacb85b5f
[gaim-migrate @ 16635]
Mark Doliner <markdoliner@pidgin.im>
parents:
13829
diff
changeset
|
218 | pkt->hash = g_slist_reverse(pkt->hash); |
| 10392 | 219 | } |
| 220 | ||
| 221 | void yahoo_packet_write(struct yahoo_packet *pkt, guchar *data) | |
| 222 | { | |
| 223 | GSList *l = pkt->hash; | |
| 224 | int pos = 0; | |
| 225 | ||
| 226 | while (l) { | |
| 227 | struct yahoo_pair *pair = l->data; | |
|
11161
f9272f277000
[gaim-migrate @ 13249]
Mark Doliner <markdoliner@pidgin.im>
parents:
10394
diff
changeset
|
228 | gchar buf[100]; |
| 10392 | 229 | |
| 230 | g_snprintf(buf, sizeof(buf), "%d", pair->key); | |
|
11161
f9272f277000
[gaim-migrate @ 13249]
Mark Doliner <markdoliner@pidgin.im>
parents:
10394
diff
changeset
|
231 | strcpy((char *)&data[pos], buf); |
| 10392 | 232 | pos += strlen(buf); |
| 233 | data[pos++] = 0xc0; | |
| 234 | data[pos++] = 0x80; | |
| 235 | ||
|
11161
f9272f277000
[gaim-migrate @ 13249]
Mark Doliner <markdoliner@pidgin.im>
parents:
10394
diff
changeset
|
236 | strcpy((char *)&data[pos], pair->value); |
| 10392 | 237 | pos += strlen(pair->value); |
| 238 | data[pos++] = 0xc0; | |
| 239 | data[pos++] = 0x80; | |
| 240 | ||
| 241 | l = l->next; | |
| 242 | } | |
| 243 | } | |
| 244 | ||
| 245 | void yahoo_packet_dump(guchar *data, int len) | |
| 246 | { | |
| 247 | #ifdef YAHOO_DEBUG | |
| 248 | int i; | |
| 249 | ||
| 15884 | 250 | purple_debug(PURPLE_DEBUG_MISC, "yahoo", ""); |
| 10392 | 251 | |
| 252 | for (i = 0; i + 1 < len; i += 2) { | |
| 253 | if ((i % 16 == 0) && i) { | |
| 15884 | 254 | purple_debug(PURPLE_DEBUG_MISC, NULL, "\n"); |
| 255 | purple_debug(PURPLE_DEBUG_MISC, "yahoo", ""); | |
| 10392 | 256 | } |
| 257 | ||
| 15884 | 258 | purple_debug(PURPLE_DEBUG_MISC, NULL, "%02x%02x ", data[i], data[i + 1]); |
| 10392 | 259 | } |
| 260 | if (i < len) | |
| 15884 | 261 | purple_debug(PURPLE_DEBUG_MISC, NULL, "%02x", data[i]); |
| 10392 | 262 | |
| 15884 | 263 | purple_debug(PURPLE_DEBUG_MISC, NULL, "\n"); |
| 264 | purple_debug(PURPLE_DEBUG_MISC, "yahoo", ""); | |
| 10392 | 265 | |
| 266 | for (i = 0; i < len; i++) { | |
| 267 | if ((i % 16 == 0) && i) { | |
| 15884 | 268 | purple_debug(PURPLE_DEBUG_MISC, NULL, "\n"); |
| 269 | purple_debug(PURPLE_DEBUG_MISC, "yahoo", ""); | |
| 10392 | 270 | } |
| 271 | ||
| 272 | if (g_ascii_isprint(data[i])) | |
| 15884 | 273 | purple_debug(PURPLE_DEBUG_MISC, NULL, "%c ", data[i]); |
| 10392 | 274 | else |
| 15884 | 275 | purple_debug(PURPLE_DEBUG_MISC, NULL, ". "); |
| 10392 | 276 | } |
| 277 | ||
| 15884 | 278 | purple_debug(PURPLE_DEBUG_MISC, NULL, "\n"); |
| 10392 | 279 | #endif |
| 280 | } | |
| 281 | ||
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
282 | static void |
| 15884 | 283 | yahoo_packet_send_can_write(gpointer data, gint source, PurpleInputCondition cond) |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
284 | { |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
285 | struct yahoo_data *yd = data; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
286 | int ret, writelen; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
287 | |
| 15884 | 288 | writelen = purple_circ_buffer_get_max_read(yd->txbuf); |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
289 | |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
290 | if (writelen == 0) { |
| 15884 | 291 | purple_input_remove(yd->txhandler); |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
292 | yd->txhandler = -1; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
293 | return; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
294 | } |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
295 | |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
296 | ret = write(yd->fd, yd->txbuf->outptr, writelen); |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
297 | |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
298 | if (ret < 0 && errno == EAGAIN) |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
299 | return; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
300 | else if (ret < 0) { |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
301 | /* TODO: what to do here - do we really have to disconnect? */ |
| 15884 | 302 | purple_connection_error(yd->gc, _("Write Error")); |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
303 | return; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
304 | } |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
305 | |
| 15884 | 306 | purple_circ_buffer_mark_read(yd->txbuf, ret); |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
307 | } |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
308 | |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
309 | |
|
13277
c8a85dd74704
[gaim-migrate @ 15642]
Richard Laager <rlaager@pidgin.im>
parents:
13276
diff
changeset
|
310 | size_t yahoo_packet_build(struct yahoo_packet *pkt, int pad, gboolean wm, |
|
14508
02d80a119d80
[gaim-migrate @ 17160]
Evan Schoenberg <evands@pidgin.im>
parents:
14322
diff
changeset
|
311 | gboolean jp, guchar **buf) |
| 10392 | 312 | { |
|
13277
c8a85dd74704
[gaim-migrate @ 15642]
Richard Laager <rlaager@pidgin.im>
parents:
13276
diff
changeset
|
313 | size_t pktlen = yahoo_packet_length(pkt); |
|
c8a85dd74704
[gaim-migrate @ 15642]
Richard Laager <rlaager@pidgin.im>
parents:
13276
diff
changeset
|
314 | size_t len = YAHOO_PACKET_HDRLEN + pktlen; |
| 10392 | 315 | guchar *data; |
| 316 | int pos = 0; | |
| 317 | ||
| 318 | data = g_malloc0(len + 1); | |
| 319 | ||
| 320 | memcpy(data + pos, "YMSG", 4); pos += 4; | |
| 321 | ||
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
322 | if (wm) |
| 10392 | 323 | pos += yahoo_put16(data + pos, YAHOO_WEBMESSENGER_PROTO_VER); |
|
14508
02d80a119d80
[gaim-migrate @ 17160]
Evan Schoenberg <evands@pidgin.im>
parents:
14322
diff
changeset
|
324 | else if (jp) |
|
02d80a119d80
[gaim-migrate @ 17160]
Evan Schoenberg <evands@pidgin.im>
parents:
14322
diff
changeset
|
325 | pos += yahoo_put16(data + pos, YAHOO_PROTO_VER_JAPAN); |
| 10392 | 326 | else |
| 327 | pos += yahoo_put16(data + pos, YAHOO_PROTO_VER); | |
| 328 | pos += yahoo_put16(data + pos, 0x0000); | |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
329 | pos += yahoo_put16(data + pos, pktlen + pad); |
| 10392 | 330 | pos += yahoo_put16(data + pos, pkt->service); |
| 331 | pos += yahoo_put32(data + pos, pkt->status); | |
| 332 | pos += yahoo_put32(data + pos, pkt->id); | |
| 333 | ||
| 334 | yahoo_packet_write(pkt, data + pos); | |
| 335 | ||
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
336 | *buf = data; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
337 | |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
338 | return len; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
339 | } |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
340 | |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
341 | int yahoo_packet_send(struct yahoo_packet *pkt, struct yahoo_data *yd) |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
342 | { |
|
13277
c8a85dd74704
[gaim-migrate @ 15642]
Richard Laager <rlaager@pidgin.im>
parents:
13276
diff
changeset
|
343 | size_t len; |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
344 | int ret; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
345 | guchar *data; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
346 | |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
347 | if (yd->fd < 0) |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
348 | return -1; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
349 | |
|
14508
02d80a119d80
[gaim-migrate @ 17160]
Evan Schoenberg <evands@pidgin.im>
parents:
14322
diff
changeset
|
350 | len = yahoo_packet_build(pkt, 0, yd->wm, yd->jp, &data); |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
351 | |
| 10392 | 352 | yahoo_packet_dump(data, len); |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
353 | if (yd->txhandler == -1) |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
354 | ret = write(yd->fd, data, len); |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
355 | else { |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
356 | ret = -1; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
357 | errno = EAGAIN; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
358 | } |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
359 | |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
360 | if (ret < 0 && errno == EAGAIN) |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
361 | ret = 0; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
362 | else if (ret <= 0) { |
| 15884 | 363 | purple_debug_warning("yahoo", "Only wrote %d of %d bytes!", ret, len); |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
364 | g_free(data); |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
365 | return ret; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
366 | } |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
367 | |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
368 | if (ret < len) { |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
369 | if (yd->txhandler == -1) |
| 15884 | 370 | yd->txhandler = purple_input_add(yd->fd, PURPLE_INPUT_WRITE, |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
371 | yahoo_packet_send_can_write, yd); |
| 15884 | 372 | purple_circ_buffer_append(yd->txbuf, data + ret, len - ret); |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
373 | } |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
374 | |
| 10392 | 375 | g_free(data); |
| 376 | ||
| 377 | return ret; | |
| 378 | } | |
| 379 | ||
| 380 | int yahoo_packet_send_and_free(struct yahoo_packet *pkt, struct yahoo_data *yd) | |
| 381 | { | |
| 382 | int ret; | |
|
11644
939411169d01
[gaim-migrate @ 13922]
Peter Lawler <pidgin@bleeter.id.au>
parents:
11161
diff
changeset
|
383 | |
| 10392 | 384 | ret = yahoo_packet_send(pkt, yd); |
| 385 | yahoo_packet_free(pkt); | |
| 386 | return ret; | |
| 387 | } | |
| 388 | ||
| 389 | void yahoo_packet_free(struct yahoo_packet *pkt) | |
| 390 | { | |
| 391 | while (pkt->hash) { | |
| 392 | struct yahoo_pair *pair = pkt->hash->data; | |
| 393 | g_free(pair->value); | |
| 394 | g_free(pair); | |
| 395 | pkt->hash = g_slist_remove(pkt->hash, pair); | |
| 396 | } | |
| 397 | g_free(pkt); | |
| 398 | } |