Debian Codegraph @

src/bnetd/message.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1998 Mark Baysinger ([email protected])
3 * Copyright (C) 1998,1999,2001,2002 Ross Combs ([email protected])
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 #include "common/setup_before.h"
20 #define MESSAGE_INTERNAL_ACCESS
21 #include "message.h"
22
23 #include <cstring>
24 #include <cerrno>
25 #include <string>
26
27 #include "compat/gethostname.h"
28 #include "common/xalloc.h"
29 #include "common/eventlog.h"
30 #include "common/addr.h"
31 #include "common/tag.h"
32 #include "common/packet.h"
33 #include "common/bot_protocol.h"
34 #include "common/bn_type.h"
35 #include "common/list.h"
36 #include "common/util.h"
37 #include "common/xstring.h"
38
39 #include "account.h"
40 #include "account_wrap.h"
41 #include "channel.h"
42 #include "channel_conv.h"
43 #include "game.h"
44 #include "mail.h"
45 #include "prefs.h"
46 #include "connection.h"
47 #include "irc.h"
48 #include "command.h"
49 #include "i18n.h"
50 #include "common/setup_after.h"
51
52 namespace pvpgn
53 {
54
55 namespace bnetd
56 {
57
58 static int message_telnet_format(t_packet * packet, t_message_type type, t_connection * me, t_connection * dst, char const * text, unsigned int dstflags);
59 static int message_bot_format(t_packet * packet, t_message_type type, t_connection * me, t_connection * dst, char const * text, unsigned int dstflags);
60 static int message_bnet_format(t_packet * packet, t_message_type type, t_connection * me, t_connection * dst, char const * text, unsigned int dstflags);
61 static t_packet * message_cache_lookup(t_message * message, t_connection *dst, unsigned int flags);
62
63 static char const * message_type_get_str(t_message_type type)
64 {
65 switch (type)
66 {
67 case message_type_adduser:
68 return "adduser";
69 case message_type_join:
70 return "join";
71 case message_type_part:
72 return "part";
73 case message_type_whisper:
74 return "whisper";
75 case message_type_talk:
76 return "talk";
77 case message_type_broadcast:
78 return "broadcast";
79 case message_type_channel:
80 return "channel";
81 case message_type_userflags:
82 return "userflags";
83 case message_type_whisperack:
84 return "whisperack";
85 case message_type_friendwhisperack: //[zap-zero] 20020518
86 return "friendwhisperack";
87 case message_type_channelfull:
88 return "channelfull";
89 case message_type_channeldoesnotexist:
90 return "channeldoesnotexist";
91 case message_type_channelrestricted:
92 return "channelrestricted";
93 case message_type_info:
94 return "info";
95 case message_type_error:
96 return "error";
97 case message_type_emote:
98 return "emote";
99 case message_type_uniqueid:
100 return "uniqueid";
101 case message_type_notice:
102 return "notice";
103 case message_type_nick:
104 return "nick";
105 case message_type_namreply:
106 return "namreply";
107 case message_type_topic:
108 return "topic";
109 case message_type_mode:
110 return "mode";
111 case message_type_host:
112 return "host";
113 case message_type_invmsg:
114 return "invmsg";
115 case message_type_page:
116 return "page";
117 case message_type_kick:
118 return "kick";
119 case message_type_quit:
120 return "quit";
121 case message_wol_joingame:
122 return "wol_joingame";
123 case message_type_gameopt_talk:
124 return "gameopt_talk";
125 case message_type_gameopt_whisper:
126 return "gameopt_whisper";
127 case message_wol_start_game:
128 return "wol_start_game";
129 case message_wol_advertr:
130 return "wol_advertr";
131 case message_wol_chanchk:
132 return "wol_chanchk";
133 case message_wol_userip:
134 return "wol_userip";
135 case message_type_null:
136 return "null";
137 default:
138 return "UNKNOWN";
139 }
140 }
141
142
143 /* make sure none of the expanded format symbols is longer than this (with null) */
144 #define MAX_INC 64
145
146 extern char * message_format_line(t_connection const * c, char const * in)
147 {
148 char * out;
149 unsigned int inpos;
150 unsigned int outpos;
151 unsigned int outlen = MAX_INC;
152 unsigned int inlen;
153 char clienttag_str[5];
154
155 out = (char*)xmalloc(outlen + 1);
156
157 inlen = std::strlen(in);
158 out[0] = 'I';
159 for (inpos = 0, outpos = 1; inpos < inlen; inpos++)
160 {
161 if (in[inpos] != '%')
162 {
163 out[outpos] = in[inpos];
164 outpos += 1;
165 }
166 else
167 switch (in[++inpos])
168 {
169 case '%':
170 out[outpos++] = '%';
171 break;
172
173 case 'a':
174 std::sprintf(&out[outpos], "%u", accountlist_get_length());
175 outpos += std::strlen(&out[outpos]);
176 break;
177
178 case 'c':
179 std::sprintf(&out[outpos], "%d", channellist_get_length());
180 outpos += std::strlen(&out[outpos]);
181 break;
182
183 case 'g':
184 std::sprintf(&out[outpos], "%d", gamelist_get_length());
185 outpos += std::strlen(&out[outpos]);
186 break;
187
188 case 'h':
189 if (gethostname(&out[outpos], MAX_INC) < 0)
190 {
191 eventlog(eventlog_level_error, __FUNCTION__, "could not get hostname (gethostname: {})", std::strerror(errno));
192 std::strcpy(&out[outpos], "localhost"); /* not much else you can do */
193 }
194 outpos += std::strlen(&out[outpos]);
195 break;
196
197 case 'i':
198 std::sprintf(&out[outpos], UID_FORMATF, conn_get_userid(c));
199 outpos += std::strlen(&out[outpos]);
200 break;
201
202 case 'l':
203 {
204 char const * tname;
205
206 std::strncpy(&out[outpos], (tname = (conn_get_chatname(c) ? conn_get_chatname(c) : conn_get_loggeduser(c))), MAX_USERNAME_LEN - 1);
207 conn_unget_chatname(c, tname);
208 }
209 out[outpos + MAX_USERNAME_LEN - 1] = '\0';
210 outpos += std::strlen(&out[outpos]);
211 break;
212
213 case 'm':
214 {
215 unsigned mails = check_mail(c);
216 if (mails > 0)
217 std::sprintf(&out[outpos], "You have %u message(s) in your mailbox.", mails);
218 else std::strcpy(&out[outpos], "You have no mail.");
219 outpos += std::strlen(&out[outpos]);
220 break;
221 }
222
223 case 'r':
224 std::strncpy(&out[outpos], addr_num_to_ip_str(conn_get_addr(c)), MAX_INC - 1);
225 out[outpos + MAX_INC - 1] = '\0';
226 outpos += std::strlen(&out[outpos]);
227 break;
228
229 case 's':
230 std::sprintf(&out[outpos], "%s", prefs_get_servername());
231 outpos += std::strlen(&out[outpos]);
232 break;
233
234 case 't':
235 std::sprintf(&out[outpos], "%s", tag_uint_to_str(clienttag_str, conn_get_clienttag(c)));
236 outpos += std::strlen(&out[outpos]);
237 break;
238
239 case 'u':
240 std::sprintf(&out[outpos], "%d", connlist_login_get_length());
241 outpos += std::strlen(&out[outpos]);
242 break;
243
244 case 'v':
245 std::strcpy(&out[outpos], PVPGN_SOFTWARE " " PVPGN_VERSION);
246 outpos += std::strlen(&out[outpos]);
247 break;
248
249 case 'C': /* simulated command */
250 out[0] = 'C';
251 break;
252
253 case 'B': /* BROADCAST */
254 out[0] = 'B';
255 break;
256
257 case 'E': /* ERROR */
258 out[0] = 'E';
259 break;
260
261 case 'G':
262 std::sprintf(&out[outpos], "%d", game_get_count_by_clienttag(conn_get_clienttag(c)));
263 outpos += std::strlen(&out[outpos]);
264 break;
265
266 case 'H':
267 std::strcpy(&out[outpos], prefs_get_contact_name());
268 outpos += std::strlen(&out[outpos]);
269 break;
270
271 case 'I': /* INFO */
272 out[0] = 'I';
273 break;
274
275 case 'M': /* MESSAGE */
276 out[0] = 'M';
277 break;
278
279 case 'N':
280 std::strcpy(&out[outpos], clienttag_get_title(conn_get_clienttag(c)));
281 outpos += std::strlen(&out[outpos]);
282 break;
283
284 case 'T': /* EMOTE */
285 out[0] = 'T';
286 break;
287
288 case 'U':
289 std::sprintf(&out[outpos], "%d", conn_get_user_count_by_clienttag(conn_get_clienttag(c)));
290 outpos += std::strlen(&out[outpos]);
291 break;
292
293 case 'W': /* INFO */
294 out[0] = 'W';
295 break;
296
297 default:
298 eventlog(eventlog_level_warn, __FUNCTION__, "bad formatter \"%{}\"", in[inpos - 1]);
299 }
300
301 if ((outpos + MAX_INC) >= outlen)
302 {
303 char * newout;
304
305 outlen += MAX_INC;
306 newout = (char*)xrealloc(out, outlen);
307 out = newout;
308 }
309 }
310 out[outpos] = '\0';
311
312 return out;
313 }
314
315
316 static int message_telnet_format(t_packet * packet, t_message_type type, t_connection * me, t_connection * dst, char const * text, unsigned int dstflags)
317 {
318 char * msgtemp;
319
320 if (!packet)
321 {
322 eventlog(eventlog_level_error, __FUNCTION__, "got NULL packet");
323 return -1;
324 }
325
326 switch (type)
327 {
328 case message_type_uniqueid:
329 if (!text)
330 {
331 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
332 return -1;
333 }
334 msgtemp = (char*)xmalloc(std::strlen(text) + 32);
335 std::sprintf(msgtemp, "Your unique name: %s\r\n", text);
336 break;
337 case message_type_adduser:
338 if (!me)
339 {
340 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
341 return -1;
342 }
343 {
344 char const * tname;
345
346 tname = conn_get_chatcharname(me, dst);
347 msgtemp = (char*)xmalloc(std::strlen(tname) + 32);
348 std::sprintf(msgtemp, "[%s is here]\r\n", tname);
349 conn_unget_chatcharname(me, tname);
350 }
351 break;
352 case message_type_join:
353 if (!me)
354 {
355 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
356 return -1;
357 }
358 if (me == dst)
359 return -1;
360 else
361 {
362 char const * tname;
363
364 tname = conn_get_chatcharname(me, dst);
365 msgtemp = (char*)xmalloc(std::strlen(tname) + 32);
366 std::sprintf(msgtemp, "[%s enters]\r\n", tname);
367 conn_unget_chatcharname(me, tname);
368 }
369 break;
370 case message_type_part:
371 if (!me)
372 {
373 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
374 return -1;
375 }
376 {
377 char const * tname;
378
379 tname = conn_get_chatcharname(me, dst);
380 msgtemp = (char*)xmalloc(std::strlen(tname) + 32);
381 std::sprintf(msgtemp, "[%s leaves]\r\n", tname);
382 conn_unget_chatcharname(me, tname);
383 }
384 break;
385 case message_type_kick:
386 if (!me)
387 {
388 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
389 return -1;
390 }
391 {
392 char const * tname;
393
394 tname = conn_get_chatcharname(me, dst);
395 msgtemp = (char*)xmalloc(std::strlen(tname) + 32);
396 std::sprintf(msgtemp, "[%s has been kicked]\r\n", tname);
397 conn_unget_chatcharname(me, tname);
398 }
399 break;
400 case message_type_quit:
401 if (!me)
402 {
403 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
404 return -1;
405 }
406 {
407 char const * tname;
408
409 tname = conn_get_chatcharname(me, dst);
410 msgtemp = (char*)xmalloc(std::strlen(tname) + 32);
411 std::sprintf(msgtemp, "[%s quit]\r\n", tname);
412 conn_unget_chatcharname(me, tname);
413 }
414 break;
415 case message_type_whisper:
416 case message_type_notice:
417 case message_type_page:
418 if (!text)
419 {
420 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
421 return -1;
422 }
423 if (dstflags&MF_X)
424 return -1; /* player is ignored */
425 {
426 char const * tname;
427 char const * newtext;
428
429 if (me)
430 tname = conn_get_chatcharname(me, dst);
431 else
432 tname = prefs_get_servername();
433
434 if ((newtext = escape_chars(text, std::strlen(text))))
435 {
436 msgtemp = (char*)xmalloc(std::strlen(tname) + 8 + std::strlen(newtext) + 4);
437 std::sprintf(msgtemp, "<from %s> %s\r\n", tname, newtext);
438 xfree((void *)newtext); /* avoid warning */
439 }
440 else
441 {
442 msgtemp = (char*)xmalloc(16 + std::strlen(tname));
443 std::sprintf(msgtemp, "<from %s> \r\n", tname);
444 }
445 if (me)
446 conn_unget_chatcharname(me, tname);
447 }
448 break;
449 case message_type_talk:
450 if (!text)
451 {
452 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
453 return -1;
454 }
455 if (dstflags&MF_X)
456 return -1; /* player is ignored */
457 {
458 char const * tname;
459 char const * newtext;
460
461 if (me)
462 tname = conn_get_chatcharname(me, dst);
463 else
464 tname = prefs_get_servername();
465
466 if ((newtext = escape_chars(text, std::strlen(text))))
467 {
468 msgtemp = (char*)xmalloc(std::strlen(tname) + 4 + std::strlen(newtext) + 4);
469 std::sprintf(msgtemp, "<%s> %s\r\n", tname, newtext);
470 xfree((void *)newtext); /* avoid warning */
471 }
472 else
473 {
474 msgtemp = (char*)xmalloc(std::strlen(tname) + 8);
475 std::sprintf(msgtemp, "<%s> \r\n", tname);
476 }
477 if (me)
478 conn_unget_chatcharname(me, tname);
479 }
480 break;
481 case message_type_broadcast:
482 if (!text)
483 {
484 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
485 return -1;
486 }
487 if (dstflags&MF_X)
488 return -1; /* player is ignored */
489 {
490 char const * newtext;
491
492 if ((newtext = escape_chars(text, std::strlen(text))))
493 {
494 msgtemp = (char*)xmalloc(16 + std::strlen(newtext) + 4);
495 std::sprintf(msgtemp, "Broadcast: %s\r\n", newtext); /* FIXME: show source? */
496 xfree((void *)newtext); /* avoid warning */
497 }
498 else
499 {
500 msgtemp = (char*)xmalloc(16);
501 std::sprintf(msgtemp, "Broadcast: \r\n"); /* FIXME: show source? */
502 }
503 }
504 break;
505 case message_type_channel:
506 if (!text)
507 {
508 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
509 return -1;
510 }
511 msgtemp = (char*)xmalloc(std::strlen(text) + 32);
512 std::sprintf(msgtemp, "Joining channel: \"%s\"\r\n", text);
513 break;
514 case message_type_userflags:
515 if (!me)
516 {
517 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
518 return -1;
519 }
520 msgtemp = xstrdup("");
521 break;
522 case message_type_whisperack:
523 if (!me)
524 {
525 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
526 return -1;
527 }
528 if (!text)
529 {
530 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
531 return -1;
532 }
533 {
534 char const * tname;
535 char const * newtext;
536
537 tname = conn_get_chatcharname(me, dst);
538 if ((newtext = escape_chars(text, std::strlen(text))))
539 {
540 msgtemp = (char*)xmalloc(std::strlen(tname) + 8 + std::strlen(newtext) + 4);
541 std::sprintf(msgtemp, "<to %s> %s\r\n", tname, newtext);
542 xfree((void *)newtext); /* avoid warning */
543 }
544 else
545 {
546 msgtemp = (char*)xmalloc(std::strlen(tname) + 8 + std::strlen(text) + 4);
547 std::sprintf(msgtemp, "<to %s> %s\r\n", tname, text);
548 }
549 conn_unget_chatcharname(me, tname);
550 }
551 break;
552 case message_type_friendwhisperack: // [zap-zero] 20020518
553 if (!me)
554 {
555 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
556 return -1;
557 }
558 if (!text)
559 {
560 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
561 return -1;
562 }
563 {
564 char const * newtext;
565
566 if ((newtext = escape_chars(text, std::strlen(text))))
567 {
568 msgtemp = (char*)xmalloc(14 + 8 + std::strlen(newtext) + 4);
569 std::sprintf(msgtemp, "<to your friends> %s\r\n", newtext);
570 xfree((void *)newtext); /* avoid warning */
571 }
572 else
573 {
574 msgtemp = (char*)xmalloc(14 + 8 + std::strlen(text) + 4);
575 std::sprintf(msgtemp, "<to your friends> %s\r\n", text);
576 }
577 }
578 break;
579
580 case message_type_channelfull:
581 /* FIXME */
582 msgtemp = xstrdup("");
583 break;
584 case message_type_channeldoesnotexist:
585 /* FIXME */
586 msgtemp = xstrdup("");
587 break;
588 case message_type_channelrestricted:
589 /* FIXME */
590 msgtemp = xstrdup("");
591 break;
592 case message_type_info:
593 if (!text)
594 {
595 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
596 return -1;
597 }
598 {
599 char const * newtext;
600
601 if ((newtext = escape_chars(text, std::strlen(text))))
602 {
603 msgtemp = (char*)xmalloc(std::strlen(newtext) + 4);
604 std::sprintf(msgtemp, "%s\r\n", newtext);
605 xfree((void *)newtext); /* avoid warning */
606 }
607 else
608 {
609 msgtemp = (char*)xmalloc(std::strlen(text) + 4);
610 std::sprintf(msgtemp, "%s\r\n", text);
611 }
612 }
613 break;
614 case message_type_error:
615 if (!text)
616 {
617 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
618 return -1;
619 }
620 {
621 char const * newtext;
622
623 if ((newtext = escape_chars(text, std::strlen(text))))
624 {
625 msgtemp = (char*)xmalloc(8 + std::strlen(newtext) + 4);
626 std::sprintf(msgtemp, "ERROR: %s\r\n", newtext);
627 xfree((void *)newtext); /* avoid warning */
628 }
629 else
630 {
631 msgtemp = (char*)xmalloc(8 + std::strlen(text) + 4);
632 std::sprintf(msgtemp, "ERROR: %s\r\n", text);
633 }
634 }
635 break;
636 case message_type_emote:
637 if (!me)
638 {
639 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
640 return -1;
641 }
642 if (!text)
643 {
644 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
645 return -1;
646 }
647 if (dstflags&MF_X)
648 return -1; /* player is ignored */
649 {
650 char const * tname;
651 char const * newtext;
652
653 tname = conn_get_chatcharname(me, dst);
654 if ((newtext = escape_chars(text, std::strlen(text))))
655 {
656 msgtemp = (char*)xmalloc(std::strlen(tname) + 4 + std::strlen(newtext) + 4);
657 std::sprintf(msgtemp, "<%s %s>\r\n", tname, newtext);
658 xfree((void *)newtext); /* avoid warning */
659 }
660 else
661 {
662 msgtemp = (char*)xmalloc(std::strlen(tname) + 4 + std::strlen(text) + 4);
663 std::sprintf(msgtemp, "<%s %s>\r\n", tname, text);
664 }
665 conn_unget_chatcharname(me, tname);
666 }
667 break;
668 case message_type_mode:
669 if (!me)
670 {
671 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
672 return -1;
673 }
674 {
675 char const * tname;
676
677 tname = conn_get_chatcharname(me, dst);
678 msgtemp = (char*)xmalloc(std::strlen(tname) + 32);
679 std::sprintf(msgtemp, "%s change mode: %s\r\n", tname, text);
680 conn_unget_chatcharname(me, tname);
681 }
682 break;
683 default:
684 eventlog(eventlog_level_error, __FUNCTION__, "got bad message type {}", (int)type);
685 return -1;
686 }
687
688 {
689 int retval;
690
691 retval = packet_append_ntstring(packet, msgtemp);
692 xfree(msgtemp);
693 return retval;
694 }
695 }
696
697
698 static int message_bot_format(t_packet * packet, t_message_type type, t_connection * me, t_connection * dst, char const * text, unsigned int dstflags)
699 {
700 char * msgtemp;
701 char clienttag_str[5];
702
703 if (!packet)
704 {
705 eventlog(eventlog_level_error, __FUNCTION__, "got NULL packet");
706 return -1;
707 }
708
709 /* special-case the login banner so it doesn't have numbers
710 * at the start of each line
711 */
712 if (me &&
713 conn_get_state(me) != conn_state_loggedin &&
714 conn_get_state(me) != conn_state_destroy &&
715 type != message_type_null) /* this does not apply for NULL messages */
716 {
717 if (!text)
718 {
719 #if 0
720 /* battle.net actually sends them during login */
721 if (type == message_type_null)
722 return 0; /* don't display null messages during the login */
723 #endif
724 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for non-loggedin state");
725 return -1;
726 }
727 msgtemp = (char*)xmalloc(std::strlen(text) + 4);
728 std::sprintf(msgtemp, "%s\r\n", text);
729 }
730 else
731 switch (type)
732 {
733 case message_type_null:
734 msgtemp = (char*)xmalloc(32);
735 std::sprintf(msgtemp, "%u %s\r\n", EID_NULL, "NULL");
736 break;
737 case message_type_uniqueid: /* FIXME: need to send this for some bots, also needed to support guest accounts */
738 if (!text)
739 {
740 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
741 return -1;
742 }
743 msgtemp = (char*)xmalloc(std::strlen(text) + 32);
744 std::sprintf(msgtemp, "%u %s %s\r\n", EID_UNIQUENAME, "NAME", text);
745 break;
746 case message_type_adduser:
747 if (!me)
748 {
749 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
750 return -1;
751 }
752 {
753 char const * tname;
754
755 tname = conn_get_chatcharname(me, dst);
756 msgtemp = (char*)xmalloc(32 + std::strlen(tname) + 32);
757 std::sprintf(msgtemp, "%u %s %s %04x [%s]\r\n", EID_SHOWUSER, "USER", tname, conn_get_flags(me) | dstflags, tag_uint_to_str(clienttag_str, conn_get_fake_clienttag(me)));
758 conn_unget_chatcharname(me, tname);
759 }
760 break;
761 case message_type_join:
762 if (!me)
763 {
764 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
765 return -1;
766 }
767 if (me == dst)
768 return -1;
769 else
770 {
771 char const * tname;
772
773 tname = conn_get_chatcharname(me, dst);
774 msgtemp = (char*)xmalloc(32 + std::strlen(tname) + 32);
775 std::sprintf(msgtemp, "%u %s %s %04x [%s]\r\n", EID_JOIN, "JOIN", tname, conn_get_flags(me) | dstflags, tag_uint_to_str(clienttag_str, conn_get_fake_clienttag(me)));
776 conn_unget_chatcharname(me, tname);
777 }
778 break;
779 case message_type_part:
780 case message_type_kick:
781 case message_type_quit:
782 if (!me)
783 {
784 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
785 return -1;
786 }
787 {
788 char const * tname;
789
790 tname = conn_get_chatcharname(me, dst);
791 msgtemp = (char*)xmalloc(32 + std::strlen(tname) + 32);
792 std::sprintf(msgtemp, "%u %s %s %04x\r\n", EID_LEAVE, "LEAVE", tname, conn_get_flags(me) | dstflags);
793 conn_unget_chatcharname(me, tname);
794 }
795 break;
796 case message_type_whisper:
797 case message_type_notice:
798 case message_type_page:
799 if (!text)
800 {
801 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
802 return -1;
803 }
804 if (dstflags&MF_X)
805 return -1; /* player is ignored */
806 {
807 char const * tname;
808
809 if (me)
810 tname = conn_get_chatcharname(me, dst);
811 else
812 tname = prefs_get_servername();
813
814 msgtemp = (char*)xmalloc(32 + std::strlen(tname) + 32 + std::strlen(text));
815 std::sprintf(msgtemp, "%u %s %s %04x \"%s\"\r\n", EID_WHISPER, "WHISPER", tname, me ? conn_get_flags(me) | dstflags : dstflags, text);
816 if (me)
817 conn_unget_chatcharname(me, tname);
818 }
819 break;
820 case message_type_talk:
821 if (!me)
822 {
823 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
824 return -1;
825 }
826 if (!text)
827 {
828 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
829 return -1;
830 }
831 if (dstflags&MF_X)
832 return -1; /* player is ignored */
833 {
834 char const * tname;
835
836 tname = conn_get_chatcharname(me, dst);
837 msgtemp = (char*)xmalloc(32 + std::strlen(tname) + 32 + std::strlen(text));
838 std::sprintf(msgtemp, "%u %s %s %04x \"%s\"\r\n", EID_TALK, "TALK", tname, conn_get_flags(me) | dstflags, text);
839 conn_unget_chatcharname(me, tname);
840 }
841 break;
842 case message_type_broadcast:
843 if (!text)
844 {
845 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
846 return -1;
847 }
848 if (dstflags&MF_X)
849 return -1; /* player is ignored */
850 msgtemp = (char*)xmalloc(32 + 32 + std::strlen(text));
851 std::sprintf(msgtemp, "%u %s \"%s\"\r\n", EID_BROADCAST, "_", text); /* FIXME: what does this look like on Battle.net? */
852 break;
853 case message_type_channel:
854 if (!text)
855 {
856 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
857 return -1;
858 }
859 msgtemp = (char*)xmalloc(32 + std::strlen(text));
860 std::sprintf(msgtemp, "%u %s \"%s\"\r\n", EID_CHANNEL, "CHANNEL", text);
861 break;
862 case message_type_userflags:
863 if (!me)
864 {
865 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
866 return -1;
867 }
868 {
869 char const * tname;
870
871 tname = conn_get_chatcharname(me, dst);
872 msgtemp = (char*)xmalloc(32 + std::strlen(tname) + 16);
873 std::sprintf(msgtemp, "%u %s %s %04x\r\n", EID_USERFLAGS, "USER", tname, conn_get_flags(me) | dstflags);
874 conn_unget_chatcharname(me, tname);
875 }
876 break;
877 case message_type_whisperack:
878 if (!me)
879 {
880 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
881 return -1;
882 }
883 if (!text)
884 {
885 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
886 return -1;
887 }
888 {
889 char const * tname;
890
891 tname = conn_get_chatcharname(me, dst);
892 msgtemp = (char*)xmalloc(32 + std::strlen(tname) + 32 + std::strlen(text));
893 std::sprintf(msgtemp, "%u %s %s %04x \"%s\"\r\n", EID_WHISPERSENT, "WHISPER", tname, conn_get_flags(me) | dstflags, text);
894 conn_unget_chatcharname(me, tname);
895 }
896 break;
897 case message_type_friendwhisperack: // [zap-zero] 20020518
898 if (!me)
899 {
900 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
901 return -1;
902 }
903 if (!text)
904 {
905 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
906 return -1;
907 }
908 {
909 msgtemp = (char*)xmalloc(32 + 16 + 32 + std::strlen(text));
910 std::sprintf(msgtemp, "%u %s \"your friends\" %04x \"%s\"\r\n", EID_WHISPERSENT, "WHISPER", conn_get_flags(me) | dstflags, text);
911 }
912 break;
913
914 case message_type_channelfull:
915 msgtemp = (char*)xmalloc(32);
916 std::sprintf(msgtemp, "%u \r\n", EID_CHANNELFULL); /* FIXME */
917 break;
918 case message_type_channeldoesnotexist:
919 msgtemp = (char*)xmalloc(32);
920 std::sprintf(msgtemp, "%u \r\n", EID_CHANNELDOESNOTEXIST); /* FIXME */
921 break;
922 case message_type_channelrestricted:
923 msgtemp = (char*)xmalloc(32);
924 std::sprintf(msgtemp, "%u \r\n", EID_CHANNELRESTRICTED); /* FIXME */
925 break;
926 case message_type_info:
927 if (!text)
928 {
929 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
930 return -1;
931 }
932 msgtemp = (char*)xmalloc(32 + 16 + std::strlen(text));
933 std::sprintf(msgtemp, "%u %s \"%s\"\r\n", EID_INFO, "INFO", text);
934 break;
935 case message_type_error:
936 if (!text)
937 {
938 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
939 return -1;
940 }
941 msgtemp = (char*)xmalloc(32 + 16 + std::strlen(text));
942 std::sprintf(msgtemp, "%u %s \"%s\"\r\n", EID_ERROR, "ERROR", text);
943 break;
944 case message_type_emote:
945 if (!me)
946 {
947 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
948 return -1;
949 }
950 if (!text)
951 {
952 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
953 return -1;
954 }
955 if (dstflags&MF_X)
956 return -1; /* player is ignored */
957 {
958 char const * tname;
959
960 tname = conn_get_chatcharname(me, dst);
961 msgtemp = (char*)xmalloc(32 + std::strlen(tname) + 32 + std::strlen(text));
962 std::sprintf(msgtemp, "%u %s %s %04x \"%s\"\r\n", EID_EMOTE, "EMOTE", tname, conn_get_flags(me) | dstflags, text);
963 conn_unget_chatcharname(me, tname);
964 }
965 break;
966 default:
967 eventlog(eventlog_level_error, __FUNCTION__, "got bad message type {}", (int)type);
968 return -1;
969 }
970
971 if (std::strlen(msgtemp) > MAX_MESSAGE_LEN)
972 msgtemp[MAX_MESSAGE_LEN] = '\0'; /* now truncate to max size */
973
974 {
975 int retval;
976
977 retval = packet_append_ntstring(packet, msgtemp);
978 xfree(msgtemp);
979 return retval;
980 }
981 }
982
983
984 static int message_bnet_format(t_packet * packet, t_message_type type, t_connection * me, t_connection * dst, char const * text, unsigned int dstflags)
985 {
986 if (!packet)
987 {
988 eventlog(eventlog_level_error, __FUNCTION__, "got NULL packet");
989 return -1;
990 }
991
992 // empty messages are needed to separate text (or example, in bnhelp output)
993 if (text && text[0] == '\0')
994 text = " "; /* empty messages crash some clients, just send whitespace */
995
996 std::string temp;
997 if (text && (std::strlen(text) > MAX_MESSAGE_LEN)) {
998 /* PELISH: We are trying to support MAX_IRC_MESSAGE_LEN for IRC and also
999 MAX_MESSAGE_LEN for bnet */
1000 temp = std::string(text, text + MAX_MESSAGE_LEN);
1001 text = temp.c_str();
1002 }
1003
1004 packet_set_size(packet, sizeof(t_server_message));
1005 packet_set_type(packet, SERVER_MESSAGE);
1006 bn_int_set(&packet->u.server_message.player_ip, SERVER_MESSAGE_PLAYER_IP_DUMMY);
1007 bn_int_nset(&packet->u.server_message.account_num, SERVER_MESSAGE_ACCOUNT_NUM);
1008 bn_int_set(&packet->u.server_message.reg_auth, SERVER_MESSAGE_REG_AUTH);
1009
1010 switch (type)
1011 {
1012 case message_type_adduser:
1013 if (!me)
1014 {
1015 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
1016 return -1;
1017 }
1018 bn_int_set(&packet->u.server_message.type, SERVER_MESSAGE_TYPE_ADDUSER);
1019 bn_int_set(&packet->u.server_message.flags, conn_get_flags(me) | dstflags);
1020 bn_int_set(&packet->u.server_message.latency, conn_get_latency(me));
1021 {
1022 char const * tname;
1023 char const * playerinfo;
1024
1025 tname = conn_get_chatcharname(me, dst);
1026 packet_append_string(packet, tname);
1027 conn_unget_chatcharname(me, tname);
1028 if ((conn_get_clienttag(me) == CLIENTTAG_WARCRAFT3_UINT) || (conn_get_clienttag(me) == CLIENTTAG_WAR3XP_UINT))
1029 playerinfo = conn_get_w3_playerinfo(me);
1030 else playerinfo = conn_get_playerinfo(me);
1031
1032 if (playerinfo == NULL) { playerinfo = ""; }
1033 packet_append_string(packet, playerinfo);
1034 }
1035 break;
1036 case message_type_join:
1037 if (!me)
1038 {
1039 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
1040 return -1;
1041 }
1042 bn_int_set(&packet->u.server_message.type, SERVER_MESSAGE_TYPE_JOIN);
1043 bn_int_set(&packet->u.server_message.flags, conn_get_flags(me) | dstflags);
1044 bn_int_set(&packet->u.server_message.latency, conn_get_latency(me));
1045 if (me == dst)
1046 return -1;
1047 else
1048 {
1049 char const * tname;
1050 char const * playerinfo;
1051
1052 tname = conn_get_chatcharname(me, dst);
1053 packet_append_string(packet, tname);
1054 conn_unget_chatcharname(me, tname);
1055
1056 if ((conn_get_clienttag(me) == CLIENTTAG_WARCRAFT3_UINT) || (conn_get_clienttag(me) == CLIENTTAG_WAR3XP_UINT))
1057 playerinfo = conn_get_w3_playerinfo(me);
1058 else playerinfo = conn_get_playerinfo(me);
1059
1060 if (playerinfo == NULL) { playerinfo = ""; }
1061 packet_append_string(packet, playerinfo);
1062 }
1063 break;
1064 case message_type_part:
1065 case message_type_kick:
1066 case message_type_quit:
1067 if (!me)
1068 {
1069 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
1070 return -1;
1071 }
1072 bn_int_set(&packet->u.server_message.type, SERVER_MESSAGE_TYPE_PART);
1073 bn_int_set(&packet->u.server_message.flags, conn_get_flags(me) | dstflags);
1074 bn_int_set(&packet->u.server_message.latency, conn_get_latency(me));
1075 {
1076 char const * tname;
1077
1078 tname = conn_get_chatcharname(me, dst);
1079 packet_append_string(packet, tname);
1080 conn_unget_chatcharname(me, tname);
1081 packet_append_string(packet, "");
1082 }
1083 break;
1084 case message_type_whisper:
1085 case message_type_notice:
1086 case message_type_page:
1087 if (!text)
1088 {
1089 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
1090 return -1;
1091 }
1092 if (dstflags&MF_X)
1093 return -1; /* player is ignored */
1094 bn_int_set(&packet->u.server_message.type, SERVER_MESSAGE_TYPE_WHISPER);
1095 bn_int_set(&packet->u.server_message.flags, me ? conn_get_flags(me) | dstflags : dstflags);
1096 bn_int_set(&packet->u.server_message.latency, me ? conn_get_latency(me) : 0);
1097
1098 if (me)
1099 {
1100 char const * tname;
1101
1102 tname = conn_get_chatcharname(me, dst);
1103 packet_append_string(packet, tname);
1104 conn_unget_chatcharname(me, tname);
1105 }
1106 else
1107 packet_append_string(packet, prefs_get_servername());
1108
1109 packet_append_string(packet, text);
1110
1111 break;
1112 case message_type_talk:
1113 if (!me)
1114 {
1115 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
1116 return -1;
1117 }
1118 if (!text)
1119 {
1120 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
1121 return -1;
1122 }
1123 if (dstflags&MF_X)
1124 return -1; /* player is ignored */
1125 bn_int_set(&packet->u.server_message.type, SERVER_MESSAGE_TYPE_TALK);
1126 bn_int_set(&packet->u.server_message.flags, conn_get_flags(me) | dstflags);
1127 bn_int_set(&packet->u.server_message.latency, conn_get_latency(me));
1128 {
1129 char const * tname;
1130
1131 tname = conn_get_chatcharname(me, dst);
1132 packet_append_string(packet, tname);
1133 conn_unget_chatcharname(me, tname);
1134 packet_append_string(packet, text);
1135 }
1136 break;
1137 case message_type_broadcast:
1138 if (!text)
1139 {
1140 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
1141 return -1;
1142 }
1143 if (dstflags&MF_X)
1144 return -1; /* player is ignored */
1145 bn_int_set(&packet->u.server_message.type, SERVER_MESSAGE_TYPE_BROADCAST);
1146 bn_int_set(&packet->u.server_message.flags, conn_get_flags(me) | dstflags);
1147 bn_int_set(&packet->u.server_message.latency, conn_get_latency(me));
1148 {
1149 char const * tname;
1150
1151 tname = conn_get_chatcharname(me, dst);
1152 packet_append_string(packet, tname);
1153 conn_unget_chatcharname(me, tname);
1154 packet_append_string(packet, text);
1155 }
1156 break;
1157 case message_type_channel:
1158 if (!me)
1159 {
1160 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
1161 return -1;
1162 }
1163 if (!text)
1164 {
1165 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
1166 return -1;
1167 }
1168 bn_int_set(&packet->u.server_message.type, SERVER_MESSAGE_TYPE_CHANNEL);
1169 {
1170 t_channel const * channel;
1171
1172 if (!(channel = conn_get_channel(me)))
1173 bn_int_set(&packet->u.server_message.flags, 0);
1174 else
1175 bn_int_set(&packet->u.server_message.flags, cflags_to_bncflags(channel_get_flags(channel)));
1176 }
1177 bn_int_set(&packet->u.server_message.latency, conn_get_latency(me));
1178 {
1179 char const * tname;
1180
1181 tname = conn_get_chatname(me);
1182 packet_append_string(packet, tname);
1183 conn_unget_chatname(me, tname);
1184 packet_append_string(packet, text);
1185 }
1186 break;
1187 case message_type_userflags:
1188 if (!me)
1189 {
1190 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
1191 return -1;
1192 }
1193 bn_int_set(&packet->u.server_message.type, SERVER_MESSAGE_TYPE_USERFLAGS);
1194 bn_int_set(&packet->u.server_message.flags, conn_get_flags(me) | dstflags);
1195 bn_int_set(&packet->u.server_message.latency, conn_get_latency(me));
1196 {
1197 char const * tname;
1198 char const * playerinfo;
1199
1200 tname = conn_get_chatcharname(me, dst);
1201 packet_append_string(packet, tname);
1202 conn_unget_chatcharname(me, tname);
1203 if ((conn_get_clienttag(me) == CLIENTTAG_WARCRAFT3_UINT) || (conn_get_clienttag(me) == CLIENTTAG_WAR3XP_UINT))
1204 playerinfo = conn_get_w3_playerinfo(me);
1205 else playerinfo = conn_get_playerinfo(me);
1206
1207 if (playerinfo == NULL) { playerinfo = ""; }
1208
1209 packet_append_string(packet, playerinfo);
1210 }
1211 break;
1212 case message_type_whisperack:
1213 if (!me)
1214 {
1215 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
1216 return -1;
1217 }
1218 if (!text)
1219 {
1220 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
1221 return -1;
1222 }
1223 bn_int_set(&packet->u.server_message.type, SERVER_MESSAGE_TYPE_WHISPERACK);
1224 bn_int_set(&packet->u.server_message.flags, conn_get_flags(me) | dstflags);
1225 bn_int_set(&packet->u.server_message.latency, conn_get_latency(me));
1226 {
1227 char const * tname;
1228
1229 tname = conn_get_chatcharname(me, dst);
1230 packet_append_string(packet, tname);
1231 conn_unget_chatcharname(me, tname);
1232 packet_append_string(packet, text);
1233 }
1234 break;
1235 case message_type_friendwhisperack: // [zap-zero] 20020518
1236 if (!me)
1237 {
1238 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
1239 return -1;
1240 }
1241 if (!text)
1242 {
1243 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
1244 return -1;
1245 }
1246 bn_int_set(&packet->u.server_message.type, SERVER_MESSAGE_TYPE_WHISPERACK);
1247 bn_int_set(&packet->u.server_message.flags, conn_get_flags(me) | dstflags);
1248 bn_int_set(&packet->u.server_message.latency, conn_get_latency(me));
1249 {
1250
1251 packet_append_string(packet, "your friends");
1252 packet_append_string(packet, text);
1253 }
1254 break;
1255
1256 case message_type_channelfull: /* FIXME */
1257 bn_int_set(&packet->u.server_message.type, SERVER_MESSAGE_TYPE_CHANNELFULL);
1258 bn_int_set(&packet->u.server_message.flags, 0);
1259 bn_int_set(&packet->u.server_message.latency, 0);
1260 packet_append_string(packet, "");
1261 packet_append_string(packet, "");
1262 break;
1263 case message_type_channeldoesnotexist:
1264 if (!me)
1265 {
1266 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
1267 return -1;
1268 }
1269 if (!text)
1270 {
1271 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
1272 return -1;
1273 }
1274 bn_int_set(&packet->u.server_message.type, SERVER_MESSAGE_TYPE_CHANNELDOESNOTEXIST);
1275 bn_int_set(&packet->u.server_message.flags, conn_get_flags(me) | dstflags);
1276 bn_int_set(&packet->u.server_message.latency, conn_get_latency(me));
1277 {
1278 char const * tname;
1279
1280 tname = conn_get_chatname(me);
1281 packet_append_string(packet, tname);
1282 conn_unget_chatname(me, tname);
1283 packet_append_string(packet, text);
1284 }
1285 break;
1286 case message_type_channelrestricted: /* FIXME */
1287 bn_int_set(&packet->u.server_message.type, SERVER_MESSAGE_TYPE_CHANNELRESTRICTED);
1288 bn_int_set(&packet->u.server_message.flags, 0);
1289 bn_int_set(&packet->u.server_message.latency, 0);
1290 packet_append_string(packet, "");
1291 packet_append_string(packet, "");
1292 break;
1293 case message_type_info:
1294 if (!text)
1295 {
1296 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
1297 return -1;
1298 }
1299 bn_int_set(&packet->u.server_message.type, SERVER_MESSAGE_TYPE_INFO);
1300 bn_int_set(&packet->u.server_message.flags, 0);
1301 bn_int_set(&packet->u.server_message.latency, 0);
1302 packet_append_string(packet, "");
1303 packet_append_string(packet, text);
1304 break;
1305 case message_type_error:
1306 if (!text)
1307 {
1308 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
1309 return -1;
1310 }
1311 bn_int_set(&packet->u.server_message.type, SERVER_MESSAGE_TYPE_ERROR);
1312 bn_int_set(&packet->u.server_message.flags, 0);
1313 bn_int_set(&packet->u.server_message.latency, 0);
1314 packet_append_string(packet, "");
1315 packet_append_string(packet, text);
1316 break;
1317 case message_type_emote:
1318 if (!me)
1319 {
1320 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection for {}", message_type_get_str(type));
1321 return -1;
1322 }
1323 if (!text)
1324 {
1325 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text for {}", message_type_get_str(type));
1326 return -1;
1327 }
1328 if (dstflags&MF_X)
1329 return -1; /* player is ignored */
1330 bn_int_set(&packet->u.server_message.type, SERVER_MESSAGE_TYPE_EMOTE);
1331 bn_int_set(&packet->u.server_message.flags, conn_get_flags(me) | dstflags);
1332 bn_int_set(&packet->u.server_message.latency, conn_get_latency(me));
1333 {
1334 char const * tname;
1335
1336 tname = conn_get_chatcharname(me, dst);
1337 packet_append_string(packet, tname);
1338 conn_unget_chatcharname(me, tname);
1339 packet_append_string(packet, text);
1340 }
1341 break;
1342 default:
1343 eventlog(eventlog_level_error, __FUNCTION__, "got bad message type {}", (int)type);
1344 return -1;
1345 }
1346
1347 return 0;
1348 }
1349
1350
1351 extern t_message * message_create(t_message_type type, t_connection * src, char const * text)
1352 {
1353 t_message * message;
1354
1355 message = (t_message*)xmalloc(sizeof(t_message));
1356 message->num_cached = 0;
1357 message->packets = NULL;
1358 message->classes = NULL;
1359 message->dstflags = NULL;
1360 message->mclasses = NULL;
1361 message->type = type;
1362 message->src = src;
1363 message->text = text;
1364
1365 return message;
1366 }
1367
1368
1369 extern int message_destroy(t_message * message)
1370 {
1371 unsigned int i;
1372
1373 if (!message)
1374 {
1375 eventlog(eventlog_level_error, __FUNCTION__, "got NULL message");
1376 return -1;
1377 }
1378
1379 if (message->packets)
1380 {
1381 for (i = 0; i < message->num_cached; i++)
1382 {
1383 if (message->packets[i])
1384 packet_del_ref(message->packets[i]);
1385 }
1386 xfree(message->packets);
1387 }
1388 if (message->classes)
1389 xfree(message->classes);
1390 if (message->dstflags)
1391 xfree(message->dstflags);
1392 if (message->mclasses)
1393 xfree(message->mclasses);
1394 xfree(message);
1395
1396 return 0;
1397 }
1398
1399
1400 static t_packet * message_cache_lookup(t_message * message, t_connection *dst, unsigned int dstflags)
1401 {
1402 unsigned int i = 0;
1403 t_packet * packet;
1404 t_message_class mclass;
1405 t_conn_class cclass;
1406
1407 if (!message)
1408 {
1409 eventlog(eventlog_level_error, __FUNCTION__, "got NULL message");
1410 return NULL;
1411 }
1412
1413 cclass = conn_get_class(dst);
1414 mclass = conn_get_message_class(message->src, dst);
1415
1416 if (message->classes && message->mclasses && message->packets && message->dstflags)
1417 {
1418 for (i = 0; i < message->num_cached; i++)
1419 {
1420 if (message->classes[i] == cclass && message->dstflags[i] == dstflags
1421 && message->mclasses[i] == mclass)
1422 return message->packets[i];
1423 }
1424 }
1425 {
1426 t_packet * * temp_packets;
1427 t_conn_class * temp_classes;
1428 unsigned int * temp_dstflags;
1429 t_message_class *temp_mclasses;
1430
1431 if (!message->packets)
1432 temp_packets = (t_packet**)xmalloc(sizeof(t_packet *)*(message->num_cached + 1));
1433 else
1434 temp_packets = (t_packet**)xrealloc(message->packets, sizeof(t_packet *)*(message->num_cached + 1));
1435
1436 if (!message->classes)
1437 temp_classes = (t_conn_class*)xmalloc(sizeof(t_conn_class)*(message->num_cached + 1));
1438 else
1439 temp_classes = (t_conn_class*)xrealloc(message->classes, sizeof(t_conn_class)*(message->num_cached + 1));
1440
1441 if (!message->dstflags)
1442 temp_dstflags = (unsigned int *)xmalloc(sizeof(unsigned int)*(message->num_cached + 1));
1443 else
1444 temp_dstflags = (unsigned int *)xrealloc(message->dstflags, sizeof(unsigned int)*(message->num_cached + 1));
1445
1446 if (!message->mclasses)
1447 temp_mclasses = (t_message_class*)xmalloc(sizeof(t_message_class)*(message->num_cached + 1));
1448 else
1449 temp_mclasses = (t_message_class*)xrealloc(message->mclasses, sizeof(t_message_class)*(message->num_cached + 1));
1450
1451 message->packets = temp_packets;
1452 message->classes = temp_classes;
1453 message->dstflags = temp_dstflags;
1454 message->mclasses = temp_mclasses;
1455 }
1456
1457 switch (cclass)
1458 {
1459 case conn_class_telnet:
1460 if (!(packet = packet_create(packet_class_raw)))
1461 {
1462 eventlog(eventlog_level_error, __FUNCTION__, "could not create packet");
1463 return NULL;
1464 }
1465 if (message_telnet_format(packet, message->type, message->src, dst, message->text, dstflags) < 0)
1466 {
1467 packet_del_ref(packet);
1468 packet = NULL; /* we can cache the NULL too */
1469 }
1470 break;
1471 case conn_class_bot:
1472 if (!(packet = packet_create(packet_class_raw)))
1473 {
1474 eventlog(eventlog_level_error, __FUNCTION__, "could not create packet");
1475 return NULL;
1476 }
1477 if (message_bot_format(packet, message->type, message->src, dst, message->text, dstflags) < 0)
1478 {
1479 packet_del_ref(packet);
1480 packet = NULL; /* we can cache the NULL too */
1481 }
1482 break;
1483 case conn_class_bnet:
1484 if (!(packet = packet_create(packet_class_bnet)))
1485 {
1486 eventlog(eventlog_level_error, __FUNCTION__, "could not create packet");
1487 return NULL;
1488 }
1489 if (message_bnet_format(packet, message->type, message->src, dst, message->text, dstflags) < 0)
1490 {
1491 packet_del_ref(packet);
1492 packet = NULL; /* we can cache the NULL too */
1493 }
1494 break;
1495 case conn_class_irc:
1496 case conn_class_wol:
1497 case conn_class_wserv:
1498 case conn_class_wgameres:
1499 if (!(packet = packet_create(packet_class_raw)))
1500 {
1501 eventlog(eventlog_level_error, __FUNCTION__, "could not create packet");
1502 return NULL;
1503 }
1504 /* irc_message_format() is in irc.c */
1505 if (irc_message_format(packet, message->type, message->src, dst, message->text, dstflags) < 0)
1506 {
1507 packet_del_ref(packet);
1508 packet = NULL; /* we can cache the NULL too */
1509 }
1510 break;
1511 case conn_class_init:
1512 case conn_class_file:
1513 case conn_class_d2cs_bnetd:
1514 case conn_class_w3route:
1515 packet = NULL;
1516 break; /* cache the NULL but dont send any error,
1517 * this are normal connections */
1518 default:
1519 eventlog(eventlog_level_error, __FUNCTION__, "unsupported connection class {}", (int)cclass);
1520 packet = NULL; /* we can cache the NULL too */
1521 }
1522
1523 message->num_cached++;
1524 message->packets[i] = packet;
1525 message->classes[i] = cclass;
1526 message->dstflags[i] = dstflags;
1527 message->mclasses[i] = mclass;
1528
1529 return packet;
1530 }
1531
1532
1533 extern int message_send(t_message * message, t_connection * dst)
1534 {
1535 t_packet * packet;
1536 unsigned int dstflags;
1537
1538 if (!message)
1539 {
1540 eventlog(eventlog_level_error, __FUNCTION__, "got NULL message");
1541 return -1;
1542 }
1543 if (!dst)
1544 {
1545 eventlog(eventlog_level_error, __FUNCTION__, "got NULL dst connection");
1546 return -1;
1547 }
1548
1549 dstflags = 0;
1550 if (message->src)
1551 {
1552 char const * tname;
1553
1554 if ((tname = conn_get_chatname(message->src)) && conn_check_ignoring(dst, tname) == 1)
1555 {
1556 conn_unget_chatname(message->src, tname);
1557 dstflags |= MF_X;
1558 }
1559 if (tname)
1560 conn_unget_chatname(message->src, tname);
1561 }
1562
1563 if (!(packet = message_cache_lookup(message, dst, dstflags)))
1564 return -1;
1565
1566 /* FIXME: this is not needed now, message has dst */
1567 if ((conn_get_class(dst) == conn_class_irc) || (conn_get_class(dst) == conn_class_wol) || (conn_get_class(dst) == conn_class_wserv) || (conn_get_class(dst) == conn_class_wgameres)) {
1568 /* HACK: IRC message always need the recipient and are therefore bad to cache. */
1569 /* So we only cache a pseudo packet and convert it to a real packet later ... */
1570 packet = packet_duplicate(packet); /* we want to modify packet so we have to create a copy ... */
1571 if (irc_message_postformat(packet, dst) < 0) {
1572 packet_del_ref(packet); /* we don't need the previously created copy anymore ... */
1573 return -1;
1574 }
1575 }
1576
1577 conn_push_outqueue(dst, packet);
1578
1579 if ((conn_get_class(dst) == conn_class_irc) || (conn_get_class(dst) == conn_class_wol) || (conn_get_class(dst) == conn_class_wserv) || (conn_get_class(dst) == conn_class_wgameres))
1580 packet_del_ref(packet); /* we don't need the previously created copy anymore ... */
1581
1582 return 0;
1583 }
1584
1585
1586 extern int message_send_all(t_message * message)
1587 {
1588 t_connection * c;
1589 t_elem const * curr;
1590 int rez;
1591
1592 if (!message)
1593 {
1594 eventlog(eventlog_level_error, __FUNCTION__, "got NULL message");
1595 return -1;
1596 }
1597
1598 rez = -1;
1599 LIST_TRAVERSE_CONST(connlist(), curr)
1600 {
1601 c = (t_connection*)elem_get_data(curr);
1602 if (message_send(message, c) == 0)
1603 rez = 0;
1604 }
1605
1606 return rez;
1607 }
1608
1609 extern int message_send_text(t_connection * dst, t_message_type type, t_connection * src, std::string text)
1610 {
1611 return message_send_text(dst, type, src, text.c_str());
1612 }
1613
1614 extern int message_send_text(t_connection * dst, t_message_type type, t_connection * src, char const * text)
1615 {
1616 t_message * message;
1617 int rez;
1618
1619 if (!dst)
1620 {
1621 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1622 return -1;
1623 }
1624
1625 if (!(message = message_create(type, src, text)))
1626 return -1;
1627 rez = message_send(message, dst);
1628 message_destroy(message);
1629
1630 return rez;
1631 }
1632
1633
1634 extern int message_send_admins(t_connection * src, t_message_type type, char const * text)
1635 {
1636 t_elem const * curr;
1637 t_connection * tc;
1638 int counter = 0;
1639
1640 LIST_TRAVERSE_CONST(connlist(), curr)
1641 {
1642 tc = (t_connection*)elem_get_data(curr);
1643 if (!tc)
1644 continue;
1645 if (account_get_auth_admin(conn_get_account(tc), NULL) == 1 && tc != src)
1646 {
1647 message_send_text(tc, type, src, text);
1648 counter++;
1649 }
1650 }
1651
1652 return counter;
1653 }
1654
1655
1656 extern int message_send_formatted(t_connection * dst, char const * text)
1657 {
1658 char * line;
1659
1660 if (!dst)
1661 {
1662 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1663 return -1;
1664 }
1665
1666 if (!(line = message_format_line(dst, text)))
1667 {
1668 eventlog(eventlog_level_error, __FUNCTION__, "could not format input text \"{}\"", text);
1669 return -1;
1670 }
1671
1672 i18n_convert(dst, line);
1673
1674 /* caller beware: empty messages can crash Blizzard clients */
1675 switch (line[0])
1676 {
1677 case 'C':
1678 if (line[1] == '/')
1679 handle_command(dst, &line[1]);
1680 else
1681 if (conn_get_channel(dst) && !conn_quota_exceeded(dst, &line[1]))
1682 channel_message_send(conn_get_channel(dst), message_type_talk, dst, &line[1]);
1683 break;
1684 case 'B':
1685 message_send_text(dst, message_type_broadcast, dst, &line[1]);
1686 break;
1687 case 'E':
1688 message_send_text(dst, message_type_error, dst, &line[1]);
1689 break;
1690 case 'M':
1691 message_send_text(dst, message_type_talk, dst, &line[1]);
1692 break;
1693 case 'T':
1694 message_send_text(dst, message_type_emote, dst, &line[1]);
1695 break;
1696 case 'I':
1697 case 'W':
1698 message_send_text(dst, message_type_info, dst, &line[1]);
1699 break;
1700 default:
1701 eventlog(eventlog_level_error, __FUNCTION__, "unknown message type '{}'", line[0]);
1702 xfree(line);
1703 return -1;
1704 }
1705
1706 xfree(line);
1707 return 0;
1708 }
1709
1710
1711 extern int message_send_file(t_connection * dst, std::FILE * fd)
1712 {
1713 char * buff;
1714
1715 if (!dst)
1716 {
1717 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
1718 return -1;
1719 }
1720 if (!fd)
1721 {
1722 eventlog(eventlog_level_error, __FUNCTION__, "got NULL fd");
1723 return -1;
1724 }
1725
1726 while ((buff = file_get_line(fd)))
1727 {
1728 message_send_formatted(dst, buff);
1729 }
1730 file_get_line(NULL); // clear file_get_line buffer
1731
1732 return 0;
1733 }
1734
1735 /* Show message box on a client side (https://github.com/pvpgn/pvpgn-server/issues/15) */
1736 extern int messagebox_show(t_connection * dst, char const * text, char const * caption, int type)
1737 {
1738 t_packet *rpacket;
1739
1740 std::string newtext = str_replace_nl(text);
1741
1742 if ((rpacket = packet_create(packet_class_bnet)))
1743 {
1744 packet_set_size(rpacket, sizeof(t_server_messagebox));
1745 packet_set_type(rpacket, SERVER_MESSAGEBOX);
1746 bn_int_set(&rpacket->u.server_messagebox.style, type);
1747 packet_append_string(rpacket, newtext.c_str());
1748 packet_append_string(rpacket, caption);
1749 conn_push_outqueue(dst, rpacket);
1750 packet_del_ref(rpacket);
1751 }
1752 return 0;
1753 }
1754
1755 }
1756
1757 }