Sun, 05 Jul 2009 02:23:43 +0000
Convert the yahoo prpl to using the debug convenience functions instead of
purple_debug().
| 9376 | 1 | /** |
| 2 | * @file ycht.c The Yahoo! protocol plugin, YCHT protocol stuff. | |
| 3 | * | |
| 15884 | 4 | * purple |
| 9376 | 5 | * |
| 6 | * Copyright (C) 2004 Timothy Ringenbach <omarvo@hotmail.com> | |
| 7 | * Liberal amounts of code borrowed from the rest of the Yahoo! prpl. | |
| 8 | * | |
| 15884 | 9 | * Purple is the legal property of its developers, whose names are too numerous |
| 9376 | 10 | * to list here. Please refer to the COPYRIGHT file distributed with this |
| 11 | * source distribution. | |
| 12 | * | |
| 13 | * This program is free software; you can redistribute it and/or modify | |
| 14 | * it under the terms of the GNU General Public License as published by | |
| 15 | * the Free Software Foundation; either version 2 of the License, or | |
| 16 | * (at your option) any later version. | |
| 17 | * | |
| 18 | * This program is distributed in the hope that it will be useful, | |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 21 | * GNU General Public License for more details. | |
| 22 | * | |
| 23 | * You should have received a copy of the GNU General Public License | |
| 24 | * along with this program; if not, write to the Free Software | |
|
19859
71d37b57eff2
The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
15884
diff
changeset
|
25 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
| 9376 | 26 | */ |
| 27 | ||
| 28 | #include <string.h> | |
| 29 | ||
| 30 | #include "internal.h" | |
| 31 | #include "prpl.h" | |
| 32 | #include "notify.h" | |
| 33 | #include "account.h" | |
| 34 | #include "proxy.h" | |
| 35 | #include "debug.h" | |
| 36 | #include "conversation.h" | |
| 37 | #include "util.h" | |
| 38 | ||
| 39 | #include "yahoo.h" | |
| 10392 | 40 | #include "yahoo_packet.h" |
| 9376 | 41 | #include "ycht.h" |
| 42 | #include "yahoochat.h" | |
| 43 | ||
| 44 | /* | |
| 45 | * dword: YCHT | |
| 46 | * dword: 0x000000AE | |
| 47 | * dword: service | |
| 48 | * word: status | |
| 49 | * word: size | |
| 50 | */ | |
| 51 | #define YAHOO_CHAT_ID (1) | |
| 52 | /************************************************************************************ | |
| 53 | * Functions to process various kinds of packets. | |
| 54 | ************************************************************************************/ | |
| 55 | static void ycht_process_login(YchtConn *ycht, YchtPkt *pkt) | |
| 56 | { | |
| 15884 | 57 | PurpleConnection *gc = ycht->gc; |
| 9376 | 58 | struct yahoo_data *yd = gc->proto_data; |
| 59 | ||
| 60 | if (ycht->logged_in) | |
| 61 | return; | |
| 62 | ||
| 63 | yd->chat_online = TRUE; | |
| 64 | ycht->logged_in = TRUE; | |
| 65 | ||
| 66 | if (ycht->room) | |
| 67 | ycht_chat_join(ycht, ycht->room); | |
| 68 | } | |
| 69 | ||
| 70 | static void ycht_process_logout(YchtConn *ycht, YchtPkt *pkt) | |
| 71 | { | |
| 15884 | 72 | PurpleConnection *gc = ycht->gc; |
| 9376 | 73 | struct yahoo_data *yd = gc->proto_data; |
| 74 | ||
| 75 | yd->chat_online = FALSE; | |
| 76 | ycht->logged_in = FALSE; | |
| 77 | } | |
| 78 | ||
| 79 | static void ycht_process_chatjoin(YchtConn *ycht, YchtPkt *pkt) | |
| 80 | { | |
| 81 | char *room, *topic; | |
| 15884 | 82 | PurpleConnection *gc = ycht->gc; |
| 83 | PurpleConversation *c = NULL; | |
| 9376 | 84 | gboolean new_room = FALSE; |
| 85 | char **members; | |
| 86 | int i; | |
| 87 | ||
| 88 | room = g_list_nth_data(pkt->data, 0); | |
| 89 | topic = g_list_nth_data(pkt->data, 1); | |
| 90 | if (!g_list_nth_data(pkt->data, 4)) | |
| 91 | return; | |
| 92 | if (!room) | |
| 93 | return; | |
| 94 | ||
| 95 | members = g_strsplit(g_list_nth_data(pkt->data, 4), "\001", 0); | |
| 96 | for (i = 0; members[i]; i++) { | |
| 97 | char *tmp = strchr(members[i], '\002'); | |
| 98 | if (tmp) | |
| 99 | *tmp = '\0'; | |
| 100 | } | |
| 101 | ||
| 102 | if (g_list_length(pkt->data) > 5) | |
| 103 | new_room = TRUE; | |
| 104 | ||
| 105 | if (new_room && ycht->changing_rooms) { | |
| 106 | serv_got_chat_left(gc, YAHOO_CHAT_ID); | |
| 107 | ycht->changing_rooms = FALSE; | |
| 108 | c = serv_got_joined_chat(gc, YAHOO_CHAT_ID, room); | |
| 109 | } else { | |
| 15884 | 110 | c = purple_find_chat(gc, YAHOO_CHAT_ID); |
| 9376 | 111 | } |
| 112 | ||
| 113 | if (topic) | |
| 15884 | 114 | purple_conv_chat_set_topic(PURPLE_CONV_CHAT(c), NULL, topic); |
| 9376 | 115 | |
| 116 | for (i = 0; members[i]; i++) { | |
| 117 | if (new_room) { | |
| 15884 | 118 | /*if (!strcmp(members[i], purple_connection_get_display_name(ycht->gc))) |
| 9376 | 119 | continue;*/ |
| 15884 | 120 | purple_conv_chat_add_user(PURPLE_CONV_CHAT(c), members[i], NULL, PURPLE_CBFLAGS_NONE, TRUE); |
| 9376 | 121 | } else { |
| 15884 | 122 | yahoo_chat_add_user(PURPLE_CONV_CHAT(c), members[i], NULL); |
| 9376 | 123 | } |
| 124 | } | |
| 125 | ||
| 126 | g_strfreev(members); | |
| 127 | } | |
| 128 | ||
| 129 | static void ycht_process_chatpart(YchtConn *ycht, YchtPkt *pkt) | |
| 130 | { | |
| 131 | char *room, *who; | |
| 132 | ||
| 133 | room = g_list_nth_data(pkt->data, 0); | |
| 134 | who = g_list_nth_data(pkt->data, 1); | |
| 135 | ||
| 136 | if (who && room) { | |
| 15884 | 137 | PurpleConversation *c = purple_find_chat(ycht->gc, YAHOO_CHAT_ID); |
| 138 | if (c && !purple_utf8_strcasecmp(purple_conversation_get_name(c), room)) | |
| 139 | purple_conv_chat_remove_user(PURPLE_CONV_CHAT(c), who, NULL); | |
| 9376 | 140 | |
| 141 | } | |
| 142 | } | |
| 143 | ||
| 144 | static void ycht_progress_chatmsg(YchtConn *ycht, YchtPkt *pkt) | |
| 145 | { | |
| 146 | char *who, *what, *msg; | |
| 15884 | 147 | PurpleConversation *c; |
| 148 | PurpleConnection *gc = ycht->gc; | |
| 9376 | 149 | |
| 150 | who = g_list_nth_data(pkt->data, 1); | |
| 151 | what = g_list_nth_data(pkt->data, 2); | |
| 152 | ||
| 153 | if (!who || !what) | |
| 154 | return; | |
| 155 | ||
| 15884 | 156 | c = purple_find_chat(gc, YAHOO_CHAT_ID); |
| 9376 | 157 | if (!c) |
| 158 | return; | |
| 159 | ||
| 160 | msg = yahoo_string_decode(gc, what, 1); | |
| 161 | what = yahoo_codes_to_html(msg); | |
| 162 | g_free(msg); | |
| 163 | ||
| 164 | if (pkt->service == YCHT_SERVICE_CHATMSG_EMOTE) { | |
| 165 | char *tmp = g_strdup_printf("/me %s", what); | |
| 166 | g_free(what); | |
| 167 | what = tmp; | |
| 168 | } | |
| 169 | ||
| 170 | serv_got_chat_in(gc, YAHOO_CHAT_ID, who, 0, what, time(NULL)); | |
| 171 | g_free(what); | |
| 172 | } | |
| 173 | ||
| 174 | static void ycht_progress_online_friends(YchtConn *ycht, YchtPkt *pkt) | |
| 175 | { | |
| 176 | #if 0 | |
| 15884 | 177 | PurpleConnection *gc = ycht->gc; |
| 9376 | 178 | struct yahoo_data *yd = gc->proto_data; |
| 179 | ||
| 180 | if (ycht->logged_in) | |
| 181 | return; | |
| 182 | ||
| 183 | yd->chat_online = TRUE; | |
| 184 | ycht->logged_in = TRUE; | |
| 185 | ||
| 186 | if (ycht->room) | |
| 187 | ycht_chat_join(ycht, ycht->room); | |
| 188 | #endif | |
| 189 | } | |
| 190 | ||
| 191 | /***************************************************************************** | |
| 192 | * Functions dealing with YCHT packets and their contents directly. | |
| 193 | *****************************************************************************/ | |
|
11318
13fa1d5134f3
[gaim-migrate @ 13521]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10392
diff
changeset
|
194 | static void ycht_packet_dump(const guchar *data, int len) |
| 9376 | 195 | { |
| 196 | #ifdef YAHOO_YCHT_DEBUG | |
| 197 | int i; | |
| 198 | ||
|
27547
c136491361eb
Convert the yahoo prpl to using the debug convenience functions instead of
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
27343
diff
changeset
|
199 | purple_debug_misc("yahoo", ""); |
| 9376 | 200 | |
| 201 | for (i = 0; i + 1 < len; i += 2) { | |
| 202 | if ((i % 16 == 0) && i) { | |
|
27547
c136491361eb
Convert the yahoo prpl to using the debug convenience functions instead of
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
27343
diff
changeset
|
203 | purple_debug_misc(NULL, "\n"); |
|
c136491361eb
Convert the yahoo prpl to using the debug convenience functions instead of
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
27343
diff
changeset
|
204 | purple_debug_misc("yahoo", ""); |
| 9376 | 205 | } |
| 206 | ||
|
27547
c136491361eb
Convert the yahoo prpl to using the debug convenience functions instead of
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
27343
diff
changeset
|
207 | purple_debug_misc(NULL, "%02hhx%02hhx ", data[i], data[i + 1]); |
| 9376 | 208 | } |
| 209 | if (i < len) | |
|
27547
c136491361eb
Convert the yahoo prpl to using the debug convenience functions instead of
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
27343
diff
changeset
|
210 | purple_debug_misc(NULL, "%02hhx", data[i]); |
| 9376 | 211 | |
|
27547
c136491361eb
Convert the yahoo prpl to using the debug convenience functions instead of
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
27343
diff
changeset
|
212 | purple_debug_misc(NULL, "\n"); |
|
c136491361eb
Convert the yahoo prpl to using the debug convenience functions instead of
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
27343
diff
changeset
|
213 | purple_debug_misc("yahoo", ""); |
| 9376 | 214 | |
| 215 | for (i = 0; i < len; i++) { | |
| 216 | if ((i % 16 == 0) && i) { | |
|
27547
c136491361eb
Convert the yahoo prpl to using the debug convenience functions instead of
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
27343
diff
changeset
|
217 | purple_debug_misc(NULL, "\n"); |
|
c136491361eb
Convert the yahoo prpl to using the debug convenience functions instead of
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
27343
diff
changeset
|
218 | purple_debug_misc("yahoo", ""); |
| 9376 | 219 | } |
| 220 | ||
| 221 | if (g_ascii_isprint(data[i])) | |
|
27547
c136491361eb
Convert the yahoo prpl to using the debug convenience functions instead of
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
27343
diff
changeset
|
222 | purple_debug_misc(NULL, "%c ", data[i]); |
| 9376 | 223 | else |
|
27547
c136491361eb
Convert the yahoo prpl to using the debug convenience functions instead of
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
27343
diff
changeset
|
224 | purple_debug_misc(NULL, ". "); |
| 9376 | 225 | } |
| 226 | ||
|
27547
c136491361eb
Convert the yahoo prpl to using the debug convenience functions instead of
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
27343
diff
changeset
|
227 | purple_debug_misc(NULL, "\n"); |
|
27335
06a805d4e690
Strip trailing whitespace and comment #endif marcos that close #ifdef macros
Mark Doliner <markdoliner@pidgin.im>
parents:
21503
diff
changeset
|
228 | #endif /* YAHOO_YCHT_DEBUG */ |
| 9376 | 229 | } |
| 230 | ||
| 231 | static YchtPkt *ycht_packet_new(guint version, guint service, int status) | |
| 232 | { | |
| 233 | YchtPkt *ret; | |
| 234 | ||
| 235 | ret = g_new0(YchtPkt, 1); | |
| 236 | ||
| 237 | ret->version = version; | |
| 238 | ret->service = service; | |
| 239 | ret->status = status; | |
| 240 | ||
| 241 | return ret; | |
| 242 | } | |
| 243 | ||
| 244 | static void ycht_packet_append(YchtPkt *pkt, const char *str) | |
| 245 | { | |
| 246 | g_return_if_fail(pkt != NULL); | |
| 247 | g_return_if_fail(str != NULL); | |
| 248 | ||
| 249 | pkt->data = g_list_append(pkt->data, g_strdup(str)); | |
| 250 | } | |
| 251 | ||
| 252 | static int ycht_packet_length(YchtPkt *pkt) | |
| 253 | { | |
| 254 | int ret; | |
| 255 | GList *l; | |
| 256 | ||
| 257 | ret = YCHT_HEADER_LEN; | |
| 258 | ||
| 259 | for (l = pkt->data; l; l = l->next) { | |
| 260 | ret += strlen(l->data); | |
| 261 | if (l->next) | |
| 262 | ret += strlen(YCHT_SEP); | |
| 263 | } | |
| 264 | ||
| 265 | return ret; | |
| 266 | } | |
| 267 | ||
| 15884 | 268 | static void ycht_packet_send_write_cb(gpointer data, gint source, PurpleInputCondition cond) |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
269 | { |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
270 | YchtConn *ycht = data; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
271 | int ret, writelen; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
272 | |
| 15884 | 273 | writelen = purple_circ_buffer_get_max_read(ycht->txbuf); |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
274 | |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
275 | if (writelen == 0) { |
| 15884 | 276 | purple_input_remove(ycht->tx_handler); |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
277 | ycht->tx_handler = 0; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
278 | return; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
279 | } |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
280 | |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
281 | ret = write(ycht->fd, ycht->txbuf->outptr, writelen); |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
282 | |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
283 | if (ret < 0 && errno == EAGAIN) |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
284 | return; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
285 | else if (ret <= 0) { |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
286 | /* TODO: error handling */ |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
287 | /* |
|
20854
c844b5545119
Add reasons to a couple of new errors (and to a commented-out one, for
Will Thompson <resiak@pidgin.im>
parents:
19859
diff
changeset
|
288 | purple_connection_error_reason(purple_account_get_connection(irc->account), |
| 21279 | 289 | PURPLE_CONNECTION_ERROR_NETWORK_ERROR, |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
290 | _("Server has disconnected")); |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
291 | */ |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
292 | return; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
293 | } |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
294 | |
| 15884 | 295 | purple_circ_buffer_mark_read(ycht->txbuf, ret); |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
296 | |
|
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 | |
| 9376 | 299 | static void ycht_packet_send(YchtConn *ycht, YchtPkt *pkt) |
| 300 | { | |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
301 | int len, pos, written; |
| 9376 | 302 | char *buf; |
| 303 | GList *l; | |
| 304 | ||
| 305 | g_return_if_fail(ycht != NULL); | |
| 306 | g_return_if_fail(pkt != NULL); | |
| 307 | g_return_if_fail(ycht->fd != -1); | |
| 308 | ||
| 309 | pos = 0; | |
| 310 | len = ycht_packet_length(pkt); | |
| 311 | buf = g_malloc(len); | |
| 312 | ||
| 313 | memcpy(buf + pos, "YCHT", 4); pos += 4; | |
| 314 | pos += yahoo_put32(buf + pos, pkt->version); | |
| 315 | pos += yahoo_put32(buf + pos, pkt->service); | |
| 316 | pos += yahoo_put16(buf + pos, pkt->status); | |
| 317 | pos += yahoo_put16(buf + pos, len - YCHT_HEADER_LEN); | |
| 318 | ||
| 319 | for (l = pkt->data; l; l = l->next) { | |
| 320 | int slen = strlen(l->data); | |
| 321 | memcpy(buf + pos, l->data, slen); pos += slen; | |
| 322 | ||
| 323 | if (l->next) { | |
| 324 | memcpy(buf + pos, YCHT_SEP, strlen(YCHT_SEP)); | |
| 325 | pos += strlen(YCHT_SEP); | |
| 326 | } | |
| 327 | } | |
| 328 | ||
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
329 | if (!ycht->tx_handler) |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
330 | written = write(ycht->fd, buf, len); |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
331 | else { |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
332 | written = -1; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
333 | errno = EAGAIN; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
334 | } |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
335 | |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
336 | if (written < 0 && errno == EAGAIN) |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
337 | written = 0; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
338 | else if (written <= 0) { |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
339 | /* TODO: Error handling (was none before NBIO changes) */ |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
340 | written = 0; |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
341 | } |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
342 | |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
343 | if (written < len) { |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
344 | if (!ycht->tx_handler) |
| 15884 | 345 | ycht->tx_handler = purple_input_add(ycht->fd, |
| 346 | PURPLE_INPUT_WRITE, ycht_packet_send_write_cb, | |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
347 | ycht); |
| 15884 | 348 | purple_circ_buffer_append(ycht->txbuf, buf + written, |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
349 | len - written); |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
350 | } |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
351 | |
| 9376 | 352 | g_free(buf); |
| 353 | } | |
| 354 | ||
| 355 | static void ycht_packet_read(YchtPkt *pkt, const char *buf, int len) | |
| 356 | { | |
| 357 | const char *pos = buf; | |
| 358 | const char *needle; | |
| 359 | char *tmp, *tmp2; | |
| 360 | int i = 0; | |
| 361 | ||
| 362 | while (len > 0 && (needle = g_strstr_len(pos, len, YCHT_SEP))) { | |
| 363 | tmp = g_strndup(pos, needle - pos); | |
| 364 | pkt->data = g_list_append(pkt->data, tmp); | |
| 365 | len -= needle - pos + strlen(YCHT_SEP); | |
| 366 | pos = needle + strlen(YCHT_SEP); | |
| 367 | tmp2 = g_strescape(tmp, NULL); | |
| 15884 | 368 | purple_debug_misc("yahoo", "Data[%d]:\t%s\n", i++, tmp2); |
| 9376 | 369 | g_free(tmp2); |
| 370 | } | |
| 371 | ||
| 372 | if (len) { | |
| 373 | tmp = g_strndup(pos, len); | |
| 374 | pkt->data = g_list_append(pkt->data, tmp); | |
| 375 | tmp2 = g_strescape(tmp, NULL); | |
| 15884 | 376 | purple_debug_misc("yahoo", "Data[%d]:\t%s\n", i, tmp2); |
| 9376 | 377 | g_free(tmp2); |
| 378 | }; | |
| 379 | ||
| 15884 | 380 | purple_debug_misc("yahoo", "--==End of incoming YCHT packet==--\n"); |
| 9376 | 381 | } |
| 382 | ||
| 383 | static void ycht_packet_process(YchtConn *ycht, YchtPkt *pkt) | |
| 384 | { | |
| 385 | if (pkt->data && !strncmp(pkt->data->data, "*** Danger Will Robinson!!!", strlen("*** Danger Will Robinson!!!"))) | |
| 386 | return; | |
| 387 | ||
| 388 | switch (pkt->service) { | |
| 389 | case YCHT_SERVICE_LOGIN: | |
| 390 | ycht_process_login(ycht, pkt); | |
| 391 | break; | |
| 392 | case YCHT_SERVICE_LOGOUT: | |
| 393 | ycht_process_logout(ycht, pkt); | |
| 394 | break; | |
| 395 | case YCHT_SERVICE_CHATJOIN: | |
| 396 | ycht_process_chatjoin(ycht, pkt); | |
| 397 | break; | |
| 398 | case YCHT_SERVICE_CHATPART: | |
| 399 | ycht_process_chatpart(ycht, pkt); | |
| 400 | break; | |
| 401 | case YCHT_SERVICE_CHATMSG: | |
| 402 | case YCHT_SERVICE_CHATMSG_EMOTE: | |
| 403 | ycht_progress_chatmsg(ycht, pkt); | |
| 404 | break; | |
| 405 | case YCHT_SERVICE_ONLINE_FRIENDS: | |
| 406 | ycht_progress_online_friends(ycht, pkt); | |
| 407 | break; | |
| 408 | default: | |
| 15884 | 409 | purple_debug_warning("yahoo", "YCHT: warning, unhandled service 0x%02x\n", pkt->service); |
| 9376 | 410 | } |
| 411 | } | |
| 412 | ||
| 413 | static void ycht_packet_free(YchtPkt *pkt) | |
| 414 | { | |
| 415 | GList *l; | |
| 416 | ||
| 417 | g_return_if_fail(pkt != NULL); | |
| 418 | ||
| 419 | for (l = pkt->data; l; l = l->next) | |
| 420 | g_free(l->data); | |
| 421 | g_list_free(pkt->data); | |
| 422 | g_free(pkt); | |
| 423 | } | |
| 424 | ||
| 425 | /************************************************************************************ | |
| 426 | * Functions dealing with connecting and disconnecting and reading data into YchtPkt | |
| 427 | * structs, and all that stuff. | |
| 428 | ************************************************************************************/ | |
| 429 | ||
| 430 | void ycht_connection_close(YchtConn *ycht) | |
| 431 | { | |
| 432 | struct yahoo_data *yd = ycht->gc->proto_data; | |
| 433 | ||
| 434 | if (yd) { | |
| 435 | yd->ycht = NULL; | |
| 436 | yd->chat_online = FALSE; | |
| 437 | } | |
| 438 | ||
| 439 | if (ycht->fd > 0) | |
| 440 | close(ycht->fd); | |
| 441 | if (ycht->inpa) | |
| 15884 | 442 | purple_input_remove(ycht->inpa); |
| 9376 | 443 | |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
444 | if (ycht->tx_handler) |
| 15884 | 445 | purple_input_remove(ycht->tx_handler); |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
446 | |
| 15884 | 447 | purple_circ_buffer_destroy(ycht->txbuf); |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
448 | |
|
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
449 | g_free(ycht->rxqueue); |
| 9376 | 450 | |
| 451 | g_free(ycht); | |
| 452 | } | |
| 453 | ||
| 454 | static void ycht_connection_error(YchtConn *ycht, const gchar *error) | |
| 455 | { | |
| 9397 | 456 | |
| 15884 | 457 | purple_notify_info(ycht->gc, NULL, _("Connection problem with the YCHT server."), error); |
| 9376 | 458 | ycht_connection_close(ycht); |
| 459 | } | |
| 460 | ||
| 15884 | 461 | static void ycht_pending(gpointer data, gint source, PurpleInputCondition cond) |
| 9376 | 462 | { |
| 463 | YchtConn *ycht = data; | |
| 464 | char buf[1024]; | |
| 465 | int len; | |
| 466 | ||
| 467 | len = read(ycht->fd, buf, sizeof(buf)); | |
| 468 | ||
|
14488
17cf9193cc88
[gaim-migrate @ 17136]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
469 | if (len < 0) { |
|
17cf9193cc88
[gaim-migrate @ 17136]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
470 | gchar *tmp; |
|
17cf9193cc88
[gaim-migrate @ 17136]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
471 | |
|
17cf9193cc88
[gaim-migrate @ 17136]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
472 | if (errno == EAGAIN) |
|
17cf9193cc88
[gaim-migrate @ 17136]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
473 | /* No worries */ |
|
17cf9193cc88
[gaim-migrate @ 17136]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
474 | return; |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
475 | |
|
14488
17cf9193cc88
[gaim-migrate @ 17136]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
476 | tmp = g_strdup_printf(_("Lost connection with server\n%s"), |
|
21389
e1dd8142bb87
replace most calls to strerror with calls to g_strerror. strerror will return
Nathan Walp <nwalp@pidgin.im>
parents:
19859
diff
changeset
|
477 | g_strerror(errno)); |
|
14488
17cf9193cc88
[gaim-migrate @ 17136]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
478 | ycht_connection_error(ycht, tmp); |
|
17cf9193cc88
[gaim-migrate @ 17136]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
479 | g_free(tmp); |
|
17cf9193cc88
[gaim-migrate @ 17136]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
480 | return; |
|
17cf9193cc88
[gaim-migrate @ 17136]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
481 | } else if (len == 0) { |
|
17cf9193cc88
[gaim-migrate @ 17136]
Mark Doliner <markdoliner@pidgin.im>
parents:
14254
diff
changeset
|
482 | ycht_connection_error(ycht, _("Server closed the connection.")); |
| 9376 | 483 | return; |
| 484 | } | |
| 485 | ||
| 486 | ycht->rxqueue = g_realloc(ycht->rxqueue, len + ycht->rxlen); | |
| 487 | memcpy(ycht->rxqueue + ycht->rxlen, buf, len); | |
| 488 | ycht->rxlen += len; | |
| 489 | ||
| 490 | while (1) { | |
| 491 | YchtPkt *pkt; | |
| 492 | int pos = 0; | |
| 493 | int pktlen; | |
| 494 | guint service; | |
| 495 | guint version; | |
| 496 | gint status; | |
| 497 | ||
| 498 | if (ycht->rxlen < YCHT_HEADER_LEN) | |
| 499 | return; | |
| 500 | ||
|
11318
13fa1d5134f3
[gaim-migrate @ 13521]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10392
diff
changeset
|
501 | if (strncmp("YCHT", (char *)ycht->rxqueue, 4) != 0) |
| 15884 | 502 | purple_debug_error("yahoo", "YCHT: protocol error.\n"); |
| 9376 | 503 | |
| 504 | pos += 4; /* YCHT */ | |
| 505 | ||
| 506 | version = yahoo_get32(ycht->rxqueue + pos); pos += 4; | |
| 507 | service = yahoo_get32(ycht->rxqueue + pos); pos += 4; | |
| 508 | status = yahoo_get16(ycht->rxqueue + pos); pos += 2; | |
| 509 | pktlen = yahoo_get16(ycht->rxqueue + pos); pos += 2; | |
|
27547
c136491361eb
Convert the yahoo prpl to using the debug convenience functions instead of
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
27343
diff
changeset
|
510 | purple_debug_misc("yahoo", "ycht: %d bytes to read, rxlen is %d\n", |
|
c136491361eb
Convert the yahoo prpl to using the debug convenience functions instead of
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
27343
diff
changeset
|
511 | pktlen, ycht->rxlen); |
| 9376 | 512 | |
| 513 | if (ycht->rxlen < (YCHT_HEADER_LEN + pktlen)) | |
| 514 | return; | |
| 515 | ||
| 15884 | 516 | purple_debug_misc("yahoo", "--==Incoming YCHT packet==--\n"); |
|
27547
c136491361eb
Convert the yahoo prpl to using the debug convenience functions instead of
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
27343
diff
changeset
|
517 | purple_debug_misc("yahoo", "YCHT Service: 0x%02x Version: 0x%02x Status: 0x%02x\n", |
|
c136491361eb
Convert the yahoo prpl to using the debug convenience functions instead of
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
27343
diff
changeset
|
518 | service, version, status); |
| 9376 | 519 | ycht_packet_dump(ycht->rxqueue, YCHT_HEADER_LEN + pktlen); |
| 520 | ||
| 521 | pkt = ycht_packet_new(version, service, status); | |
|
11318
13fa1d5134f3
[gaim-migrate @ 13521]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10392
diff
changeset
|
522 | ycht_packet_read(pkt, (char *)ycht->rxqueue + pos, pktlen); |
| 9376 | 523 | |
| 524 | ycht->rxlen -= YCHT_HEADER_LEN + pktlen; | |
| 525 | if (ycht->rxlen) { | |
|
11318
13fa1d5134f3
[gaim-migrate @ 13521]
Stu Tomlinson <nosnilmot@pidgin.im>
parents:
10392
diff
changeset
|
526 | guchar *tmp = g_memdup(ycht->rxqueue + YCHT_HEADER_LEN + pktlen, ycht->rxlen); |
| 9376 | 527 | g_free(ycht->rxqueue); |
| 528 | ycht->rxqueue = tmp; | |
| 529 | } else { | |
| 530 | g_free(ycht->rxqueue); | |
| 531 | ycht->rxqueue = NULL; | |
| 532 | } | |
| 533 | ||
| 534 | ycht_packet_process(ycht, pkt); | |
| 535 | ||
| 536 | ycht_packet_free(pkt); | |
| 537 | } | |
| 538 | } | |
| 539 | ||
|
14182
517f4531b8a0
[gaim-migrate @ 16754]
Mark Doliner <markdoliner@pidgin.im>
parents:
14170
diff
changeset
|
540 | static void ycht_got_connected(gpointer data, gint source, const gchar *error_message) |
| 9376 | 541 | { |
| 542 | YchtConn *ycht = data; | |
| 15884 | 543 | PurpleConnection *gc = ycht->gc; |
| 9376 | 544 | struct yahoo_data *yd = gc->proto_data; |
| 545 | YchtPkt *pkt; | |
| 546 | char *buf; | |
| 547 | ||
| 548 | if (source < 0) { | |
| 9397 | 549 | ycht_connection_error(ycht, _("Unable to connect.")); |
| 9376 | 550 | return; |
| 551 | } | |
| 552 | ||
| 553 | ycht->fd = source; | |
| 554 | ||
| 555 | pkt = ycht_packet_new(YCHT_VERSION, YCHT_SERVICE_LOGIN, 0); | |
| 556 | ||
| 15884 | 557 | buf = g_strdup_printf("%s\001Y=%s; T=%s", purple_connection_get_display_name(gc), yd->cookie_y, yd->cookie_t); |
| 9376 | 558 | ycht_packet_append(pkt, buf); |
| 559 | g_free(buf); | |
| 560 | ||
| 561 | ycht_packet_send(ycht, pkt); | |
| 562 | ||
| 563 | ycht_packet_free(pkt); | |
| 564 | ||
| 15884 | 565 | ycht->inpa = purple_input_add(ycht->fd, PURPLE_INPUT_READ, ycht_pending, ycht); |
| 9376 | 566 | } |
| 567 | ||
| 15884 | 568 | void ycht_connection_open(PurpleConnection *gc) |
| 9376 | 569 | { |
| 570 | YchtConn *ycht; | |
| 571 | struct yahoo_data *yd = gc->proto_data; | |
| 15884 | 572 | PurpleAccount *account = purple_connection_get_account(gc); |
| 9376 | 573 | |
| 574 | ycht = g_new0(YchtConn, 1); | |
| 575 | ycht->gc = gc; | |
| 576 | ycht->fd = -1; | |
| 577 | ||
| 578 | yd->ycht = ycht; | |
| 579 | ||
|
27343
8a367ee70456
Pass the gc as the handle in a bunch of calls to purple_proxy_connect
Mark Doliner <markdoliner@pidgin.im>
parents:
27335
diff
changeset
|
580 | if (purple_proxy_connect(gc, account, |
| 15884 | 581 | purple_account_get_string(account, "ycht-server", YAHOO_YCHT_HOST), |
| 582 | purple_account_get_int(account, "ycht-port", YAHOO_YCHT_PORT), | |
|
14170
f611621bc8a0
[gaim-migrate @ 16742]
Mark Doliner <markdoliner@pidgin.im>
parents:
14151
diff
changeset
|
583 | ycht_got_connected, ycht) == NULL) |
| 9376 | 584 | { |
| 9397 | 585 | ycht_connection_error(ycht, _("Connection problem")); |
| 9376 | 586 | return; |
| 587 | } | |
| 588 | } | |
| 589 | ||
| 590 | /******************************************************************************************* | |
| 591 | * These are functions called because the user did something. | |
| 592 | *******************************************************************************************/ | |
| 593 | ||
| 594 | void ycht_chat_join(YchtConn *ycht, const char *room) | |
| 595 | { | |
| 596 | YchtPkt *pkt; | |
| 597 | char *tmp; | |
| 598 | ||
| 599 | tmp = g_strdup(room); | |
|
13201
8c224ef70efa
[gaim-migrate @ 15563]
Daniel Atallah <datallah@pidgin.im>
parents:
11644
diff
changeset
|
600 | g_free(ycht->room); |
| 9376 | 601 | ycht->room = tmp; |
| 602 | ||
| 603 | if (!ycht->logged_in) | |
| 604 | return; | |
| 605 | ||
| 606 | ycht->changing_rooms = TRUE; | |
| 607 | pkt = ycht_packet_new(YCHT_VERSION, YCHT_SERVICE_CHATJOIN, 0); | |
| 608 | ycht_packet_append(pkt, ycht->room); | |
| 609 | ycht_packet_send(ycht, pkt); | |
| 610 | ycht_packet_free(pkt); | |
| 611 | } | |
| 612 | ||
| 613 | int ycht_chat_send(YchtConn *ycht, const char *room, const char *what) | |
| 614 | { | |
| 615 | YchtPkt *pkt; | |
| 616 | char *msg1, *msg2, *buf; | |
| 617 | ||
| 618 | if (strcmp(room, ycht->room)) | |
| 15884 | 619 | purple_debug_warning("yahoo", "uhoh, sending to the wrong room!\n"); |
| 9376 | 620 | |
| 621 | pkt = ycht_packet_new(YCHT_VERSION, YCHT_SERVICE_CHATMSG, 0); | |
| 622 | ||
| 623 | msg1 = yahoo_html_to_codes(what); | |
| 624 | msg2 = yahoo_string_encode(ycht->gc, msg1, NULL); | |
| 625 | g_free(msg1); | |
| 626 | ||
| 627 | buf = g_strdup_printf("%s\001%s", ycht->room, msg2); | |
| 628 | ycht_packet_append(pkt, buf); | |
| 629 | g_free(msg2); | |
| 630 | g_free(buf); | |
| 631 | ||
| 632 | ycht_packet_send(ycht, pkt); | |
| 633 | ycht_packet_free(pkt); | |
| 634 | return 1; | |
| 635 | } | |
| 636 | ||
| 637 | void ycht_chat_leave(YchtConn *ycht, const char *room, gboolean logout) | |
| 638 | { | |
| 639 | if (logout) | |
| 640 | ycht_connection_close(ycht); | |
| 641 | } | |
| 642 | ||
| 643 | void ycht_chat_send_invite(YchtConn *ycht, const char *room, const char *buddy, const char *msg) | |
| 644 | { | |
| 645 | } | |
| 646 | ||
| 647 | void ycht_chat_goto_user(YchtConn *ycht, const char *name) | |
| 648 | { | |
| 649 | } | |
| 650 | ||
| 651 | void ycht_chat_send_keepalive(YchtConn *ycht) | |
| 652 | { | |
| 653 | YchtPkt *pkt; | |
| 654 | ||
| 655 | pkt = ycht_packet_new(YCHT_VERSION, YCHT_SERVICE_PING, 0); | |
| 656 | ycht_packet_send(ycht, pkt); | |
| 657 | ycht_packet_free(pkt); | |
| 658 | } |