src/protocols/yahoo/ycht.c

branch
gaim
changeset 20470
77693555855f
parent 13071
b98e72d4089a
parent 20469
b2836a24d81e
child 20471
1966704b3e42
equal deleted inserted replaced
13071:b98e72d4089a 20470:77693555855f
1 /**
2 * @file ycht.c The Yahoo! protocol plugin, YCHT protocol stuff.
3 *
4 * gaim
5 *
6 * Copyright (C) 2004 Timothy Ringenbach <omarvo@hotmail.com>
7 * Liberal amounts of code borrowed from the rest of the Yahoo! prpl.
8 *
9 * Gaim is the legal property of its developers, whose names are too numerous
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
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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"
40 #include "yahoo_packet.h"
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 {
57 GaimConnection *gc = ycht->gc;
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 {
72 GaimConnection *gc = ycht->gc;
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;
82 GaimConnection *gc = ycht->gc;
83 GaimConversation *c = NULL;
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 {
110 c = gaim_find_chat(gc, YAHOO_CHAT_ID);
111 }
112
113 if (topic)
114 gaim_conv_chat_set_topic(GAIM_CONV_CHAT(c), NULL, topic);
115
116 for (i = 0; members[i]; i++) {
117 if (new_room) {
118 /*if (!strcmp(members[i], gaim_connection_get_display_name(ycht->gc)))
119 continue;*/
120 gaim_conv_chat_add_user(GAIM_CONV_CHAT(c), members[i], NULL, GAIM_CBFLAGS_NONE, TRUE);
121 } else {
122 yahoo_chat_add_user(GAIM_CONV_CHAT(c), members[i], NULL);
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) {
137 GaimConversation *c = gaim_find_chat(ycht->gc, YAHOO_CHAT_ID);
138 if (c && !gaim_utf8_strcasecmp(gaim_conversation_get_name(c), room))
139 gaim_conv_chat_remove_user(GAIM_CONV_CHAT(c), who, NULL);
140
141 }
142 }
143
144 static void ycht_progress_chatmsg(YchtConn *ycht, YchtPkt *pkt)
145 {
146 char *who, *what, *msg;
147 GaimConversation *c;
148 GaimConnection *gc = ycht->gc;
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
156 c = gaim_find_chat(gc, YAHOO_CHAT_ID);
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
177 GaimConnection *gc = ycht->gc;
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 *****************************************************************************/
194 static void ycht_packet_dump(const guchar *data, int len)
195 {
196 #ifdef YAHOO_YCHT_DEBUG
197 int i;
198
199 gaim_debug(GAIM_DEBUG_MISC, "yahoo", "");
200
201 for (i = 0; i + 1 < len; i += 2) {
202 if ((i % 16 == 0) && i) {
203 gaim_debug(GAIM_DEBUG_MISC, NULL, "\n");
204 gaim_debug(GAIM_DEBUG_MISC, "yahoo", "");
205 }
206
207 gaim_debug(GAIM_DEBUG_MISC, NULL, "%02hhx%02hhx ", data[i], data[i + 1]);
208 }
209 if (i < len)
210 gaim_debug(GAIM_DEBUG_MISC, NULL, "%02hhx", data[i]);
211
212 gaim_debug(GAIM_DEBUG_MISC, NULL, "\n");
213 gaim_debug(GAIM_DEBUG_MISC, "yahoo", "");
214
215 for (i = 0; i < len; i++) {
216 if ((i % 16 == 0) && i) {
217 gaim_debug(GAIM_DEBUG_MISC, NULL, "\n");
218 gaim_debug(GAIM_DEBUG_MISC, "yahoo", "");
219 }
220
221 if (g_ascii_isprint(data[i]))
222 gaim_debug(GAIM_DEBUG_MISC, NULL, "%c ", data[i]);
223 else
224 gaim_debug(GAIM_DEBUG_MISC, NULL, ". ");
225 }
226
227 gaim_debug(GAIM_DEBUG_MISC, NULL, "\n");
228 #endif
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
268 static void ycht_packet_send(YchtConn *ycht, YchtPkt *pkt)
269 {
270 int len, pos;
271 char *buf;
272 GList *l;
273
274 g_return_if_fail(ycht != NULL);
275 g_return_if_fail(pkt != NULL);
276 g_return_if_fail(ycht->fd != -1);
277
278 pos = 0;
279 len = ycht_packet_length(pkt);
280 buf = g_malloc(len);
281
282 memcpy(buf + pos, "YCHT", 4); pos += 4;
283 pos += yahoo_put32(buf + pos, pkt->version);
284 pos += yahoo_put32(buf + pos, pkt->service);
285 pos += yahoo_put16(buf + pos, pkt->status);
286 pos += yahoo_put16(buf + pos, len - YCHT_HEADER_LEN);
287
288 for (l = pkt->data; l; l = l->next) {
289 int slen = strlen(l->data);
290 memcpy(buf + pos, l->data, slen); pos += slen;
291
292 if (l->next) {
293 memcpy(buf + pos, YCHT_SEP, strlen(YCHT_SEP));
294 pos += strlen(YCHT_SEP);
295 }
296 }
297
298 write(ycht->fd, buf, len);
299 g_free(buf);
300 }
301
302 static void ycht_packet_read(YchtPkt *pkt, const char *buf, int len)
303 {
304 const char *pos = buf;
305 const char *needle;
306 char *tmp, *tmp2;
307 int i = 0;
308
309 while (len > 0 && (needle = g_strstr_len(pos, len, YCHT_SEP))) {
310 tmp = g_strndup(pos, needle - pos);
311 pkt->data = g_list_append(pkt->data, tmp);
312 len -= needle - pos + strlen(YCHT_SEP);
313 pos = needle + strlen(YCHT_SEP);
314 tmp2 = g_strescape(tmp, NULL);
315 gaim_debug_misc("yahoo", "Data[%d]:\t%s\n", i++, tmp2);
316 g_free(tmp2);
317 }
318
319 if (len) {
320 tmp = g_strndup(pos, len);
321 pkt->data = g_list_append(pkt->data, tmp);
322 tmp2 = g_strescape(tmp, NULL);
323 gaim_debug_misc("yahoo", "Data[%d]:\t%s\n", i, tmp2);
324 g_free(tmp2);
325 };
326
327 gaim_debug_misc("yahoo", "--==End of incoming YCHT packet==--\n");
328 }
329
330 static void ycht_packet_process(YchtConn *ycht, YchtPkt *pkt)
331 {
332 if (pkt->data && !strncmp(pkt->data->data, "*** Danger Will Robinson!!!", strlen("*** Danger Will Robinson!!!")))
333 return;
334
335 switch (pkt->service) {
336 case YCHT_SERVICE_LOGIN:
337 ycht_process_login(ycht, pkt);
338 break;
339 case YCHT_SERVICE_LOGOUT:
340 ycht_process_logout(ycht, pkt);
341 break;
342 case YCHT_SERVICE_CHATJOIN:
343 ycht_process_chatjoin(ycht, pkt);
344 break;
345 case YCHT_SERVICE_CHATPART:
346 ycht_process_chatpart(ycht, pkt);
347 break;
348 case YCHT_SERVICE_CHATMSG:
349 case YCHT_SERVICE_CHATMSG_EMOTE:
350 ycht_progress_chatmsg(ycht, pkt);
351 break;
352 case YCHT_SERVICE_ONLINE_FRIENDS:
353 ycht_progress_online_friends(ycht, pkt);
354 break;
355 default:
356 gaim_debug_warning("yahoo", "YCHT: warning, unhandled service 0x%02x\n", pkt->service);
357 }
358 }
359
360 static void ycht_packet_free(YchtPkt *pkt)
361 {
362 GList *l;
363
364 g_return_if_fail(pkt != NULL);
365
366 for (l = pkt->data; l; l = l->next)
367 g_free(l->data);
368 g_list_free(pkt->data);
369 g_free(pkt);
370 }
371
372 /************************************************************************************
373 * Functions dealing with connecting and disconnecting and reading data into YchtPkt
374 * structs, and all that stuff.
375 ************************************************************************************/
376
377 void ycht_connection_close(YchtConn *ycht)
378 {
379 struct yahoo_data *yd = ycht->gc->proto_data;
380
381 if (yd) {
382 yd->ycht = NULL;
383 yd->chat_online = FALSE;
384 }
385
386 if (ycht->fd > 0)
387 close(ycht->fd);
388 if (ycht->inpa)
389 gaim_input_remove(ycht->inpa);
390
391 if (ycht->rxqueue)
392 g_free(ycht->rxqueue);
393
394 g_free(ycht);
395 }
396
397 static void ycht_connection_error(YchtConn *ycht, const gchar *error)
398 {
399
400 gaim_notify_info(ycht->gc, NULL, _("Connection problem with the YCHT server."), error);
401 ycht_connection_close(ycht);
402 }
403
404 static void ycht_pending(gpointer data, gint source, GaimInputCondition cond)
405 {
406 YchtConn *ycht = data;
407 char buf[1024];
408 int len;
409
410 len = read(ycht->fd, buf, sizeof(buf));
411
412 if (len <= 0) {
413 ycht_connection_error(ycht, _("Unable to read"));
414 return;
415 }
416
417 ycht->rxqueue = g_realloc(ycht->rxqueue, len + ycht->rxlen);
418 memcpy(ycht->rxqueue + ycht->rxlen, buf, len);
419 ycht->rxlen += len;
420
421 while (1) {
422 YchtPkt *pkt;
423 int pos = 0;
424 int pktlen;
425 guint service;
426 guint version;
427 gint status;
428
429 if (ycht->rxlen < YCHT_HEADER_LEN)
430 return;
431
432 if (strncmp("YCHT", (char *)ycht->rxqueue, 4) != 0)
433 gaim_debug_error("yahoo", "YCHT: protocol error.\n");
434
435 pos += 4; /* YCHT */
436
437 version = yahoo_get32(ycht->rxqueue + pos); pos += 4;
438 service = yahoo_get32(ycht->rxqueue + pos); pos += 4;
439 status = yahoo_get16(ycht->rxqueue + pos); pos += 2;
440 pktlen = yahoo_get16(ycht->rxqueue + pos); pos += 2;
441 gaim_debug(GAIM_DEBUG_MISC, "yahoo",
442 "ycht: %d bytes to read, rxlen is %d\n", pktlen, ycht->rxlen);
443
444 if (ycht->rxlen < (YCHT_HEADER_LEN + pktlen))
445 return;
446
447 gaim_debug_misc("yahoo", "--==Incoming YCHT packet==--\n");
448 gaim_debug(GAIM_DEBUG_MISC, "yahoo",
449 "YCHT Service: 0x%02x Version: 0x%02x Status: 0x%02x\n",
450 service, version, status);
451 ycht_packet_dump(ycht->rxqueue, YCHT_HEADER_LEN + pktlen);
452
453 pkt = ycht_packet_new(version, service, status);
454 ycht_packet_read(pkt, (char *)ycht->rxqueue + pos, pktlen);
455
456 ycht->rxlen -= YCHT_HEADER_LEN + pktlen;
457 if (ycht->rxlen) {
458 guchar *tmp = g_memdup(ycht->rxqueue + YCHT_HEADER_LEN + pktlen, ycht->rxlen);
459 g_free(ycht->rxqueue);
460 ycht->rxqueue = tmp;
461 } else {
462 g_free(ycht->rxqueue);
463 ycht->rxqueue = NULL;
464 }
465
466 ycht_packet_process(ycht, pkt);
467
468 ycht_packet_free(pkt);
469 }
470 }
471
472 static void ycht_got_connected(gpointer data, gint source, GaimInputCondition cond)
473 {
474 YchtConn *ycht = data;
475 GaimConnection *gc = ycht->gc;
476 struct yahoo_data *yd = gc->proto_data;
477 YchtPkt *pkt;
478 char *buf;
479
480 if (source < 0) {
481 ycht_connection_error(ycht, _("Unable to connect."));
482 return;
483 }
484
485 ycht->fd = source;
486
487 pkt = ycht_packet_new(YCHT_VERSION, YCHT_SERVICE_LOGIN, 0);
488
489 buf = g_strdup_printf("%s\001Y=%s; T=%s", gaim_connection_get_display_name(gc), yd->cookie_y, yd->cookie_t);
490 ycht_packet_append(pkt, buf);
491 g_free(buf);
492
493 ycht_packet_send(ycht, pkt);
494
495 ycht_packet_free(pkt);
496
497 ycht->inpa = gaim_input_add(ycht->fd, GAIM_INPUT_READ, ycht_pending, ycht);
498 }
499
500 void ycht_connection_open(GaimConnection *gc)
501 {
502 YchtConn *ycht;
503 struct yahoo_data *yd = gc->proto_data;
504 GaimAccount *account = gaim_connection_get_account(gc);
505
506 ycht = g_new0(YchtConn, 1);
507 ycht->gc = gc;
508 ycht->fd = -1;
509
510 yd->ycht = ycht;
511
512 if (gaim_proxy_connect(account,
513 gaim_account_get_string(account, "ycht-server", YAHOO_YCHT_HOST),
514 gaim_account_get_int(account, "ycht-port", YAHOO_YCHT_PORT),
515 ycht_got_connected, ycht) != 0)
516 {
517 ycht_connection_error(ycht, _("Connection problem"));
518 return;
519 }
520 }
521
522 /*******************************************************************************************
523 * These are functions called because the user did something.
524 *******************************************************************************************/
525
526 void ycht_chat_join(YchtConn *ycht, const char *room)
527 {
528 YchtPkt *pkt;
529 char *tmp;
530
531 tmp = g_strdup(room);
532 if (ycht->room)
533 g_free(ycht->room);
534 ycht->room = tmp;
535
536 if (!ycht->logged_in)
537 return;
538
539 ycht->changing_rooms = TRUE;
540 pkt = ycht_packet_new(YCHT_VERSION, YCHT_SERVICE_CHATJOIN, 0);
541 ycht_packet_append(pkt, ycht->room);
542 ycht_packet_send(ycht, pkt);
543 ycht_packet_free(pkt);
544 }
545
546 int ycht_chat_send(YchtConn *ycht, const char *room, const char *what)
547 {
548 YchtPkt *pkt;
549 char *msg1, *msg2, *buf;
550
551 if (strcmp(room, ycht->room))
552 gaim_debug_warning("yahoo", "uhoh, sending to the wrong room!\n");
553
554 pkt = ycht_packet_new(YCHT_VERSION, YCHT_SERVICE_CHATMSG, 0);
555
556 msg1 = yahoo_html_to_codes(what);
557 msg2 = yahoo_string_encode(ycht->gc, msg1, NULL);
558 g_free(msg1);
559
560 buf = g_strdup_printf("%s\001%s", ycht->room, msg2);
561 ycht_packet_append(pkt, buf);
562 g_free(msg2);
563 g_free(buf);
564
565 ycht_packet_send(ycht, pkt);
566 ycht_packet_free(pkt);
567 return 1;
568 }
569
570 void ycht_chat_leave(YchtConn *ycht, const char *room, gboolean logout)
571 {
572 if (logout)
573 ycht_connection_close(ycht);
574 }
575
576 void ycht_chat_send_invite(YchtConn *ycht, const char *room, const char *buddy, const char *msg)
577 {
578 }
579
580 void ycht_chat_goto_user(YchtConn *ycht, const char *name)
581 {
582 }
583
584 void ycht_chat_send_keepalive(YchtConn *ycht)
585 {
586 YchtPkt *pkt;
587
588 pkt = ycht_packet_new(YCHT_VERSION, YCHT_SERVICE_PING, 0);
589 ycht_packet_send(ycht, pkt);
590 ycht_packet_free(pkt);
591 }

mercurial