-
Notifications
You must be signed in to change notification settings - Fork 2
/
protocol.c
2888 lines (2586 loc) · 102 KB
/
protocol.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
Protocol snippet by KaVir. Released into the public domain in February 2011.
In case this is not legally possible:
The copyright holder grants any entity the right to use this work for any
purpose, without any conditions, unless such conditions are required by law.
******************************************************************************/
/******************************************************************************
This snippet was originally designed to be codebase independent, but has been
modified slightly so that it runs out-of-the-box on Merc derivatives. To use
it for other codebases, just change the code in the "Diku/Merc" section below.
******************************************************************************/
/******************************************************************************
Header files.
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/telnet.h>
#include <time.h>
#include <malloc.h>
#include <alloca.h>
#include <ctype.h>
#include "protocol.h"
/******************************************************************************
The following section is for Diku/Merc derivatives. Replace as needed.
******************************************************************************/
#include "merc.h"
static void Write( descriptor_t *apDescriptor, const char *apData )
{
if ( apDescriptor != NULL && !apDescriptor->fcommand )
{
if ( apDescriptor->pProtocol->WriteOOB > 0 ||
apDescriptor->outtop == 0 )
{
apDescriptor->pProtocol->WriteOOB = 2;
}
}
write_to_buffer( apDescriptor, apData, 0 );
}
static void ReportBug( const char *apText )
{
bug( apText, 0 );
}
static void InfoMessage( descriptor_t *apDescriptor, const char *apData )
{
Write( apDescriptor, "\t[F210][\toINFO\t[F210]]\tn " );
Write( apDescriptor, apData );
}
static void CompressStart( descriptor_t *apDescriptor )
{
/* If your mud uses MCCP (Mud Client Compression Protocol), you need to
* call whatever function normally starts compression from here - the
* ReportBug() call should then be deleted.
*
* Otherwise you can just ignore this function.
*/
ReportBug( "CompressStart() in protocol.c is being called, but it doesn't do anything!\n" );
}
static void CompressEnd( descriptor_t *apDescriptor )
{
/* If your mud uses MCCP (Mud Client Compression Protocol), you need to
* call whatever function normally starts compression from here - the
* ReportBug() call should then be deleted.
*
* Otherwise you can just ignore this function.
*/
ReportBug( "CompressEnd() in protocol.c is being called, but it doesn't do anything!\n" );
}
/******************************************************************************
MSDP file-scope variables.
******************************************************************************/
/* These are for the GUI_VARIABLES, my unofficial extension of MSDP. They're
* intended for clients that wish to offer a generic GUI - not as nice as a
* custom GUI, admittedly, but still better than a plain terminal window.
*
* These are per-player so that they can be customised for different characters
* (eg changing 'mana' to 'blood' for vampires). You could even allow players
* to customise the buttons and gauges themselves if you wish.
*/
static const char s_Button1[] = "\005\002Help\002help\006";
static const char s_Button2[] = "\005\002Look\002look\006";
static const char s_Button3[] = "\005\002Score\002help\006";
static const char s_Button4[] = "\005\002Equipment\002equipment\006";
static const char s_Button5[] = "\005\002Inventory\002inventory\006";
static const char s_Gauge1[] = "\005\002Health\002red\002HEALTH\002HEALTH_MAX\006";
static const char s_Gauge2[] = "\005\002Mana\002blue\002MANA\002MANA_MAX\006";
static const char s_Gauge3[] = "\005\002Movement\002green\002MOVEMENT\002MOVEMENT_MAX\006";
static const char s_Gauge4[] = "\005\002Exp TNL\002yellow\002EXPERIENCE\002EXPERIENCE_MAX\006";
static const char s_Gauge5[] = "\005\002Opponent\002darkred\002OPPONENT_HEALTH\002OPPONENT_HEALTH_MAX\006";
/******************************************************************************
MSDP variable table.
******************************************************************************/
/* Macros for readability, but you can remove them if you don't like them */
#define NUMBER_READ_ONLY false, false, false, false, -1, -1, 0, NULL
#define NUMBER_READ_ONLY_SET_TO(x) false, false, false, false, -1, -1, x, NULL
#define STRING_READ_ONLY true, false, false, false, -1, -1, 0, NULL
#define NUMBER_IN_THE_RANGE(x,y) false, true, false, false, x, y, 0, NULL
#define BOOLEAN_SET_TO(x) false, true, false, false, 0, 1, x, NULL
#define STRING_WITH_LENGTH_OF(x,y) true, true, false, false, x, y, 0, NULL
#define STRING_WRITE_ONCE(x,y) true, true, true, false, -1, -1, 0, NULL
#define STRING_GUI(x) true, false, false, true, -1, -1, 0, x
static variable_name_t VariableNameTable[eMSDP_MAX+1] =
{
/* General */
{ eMSDP_CHARACTER_NAME, "CHARACTER_NAME", STRING_READ_ONLY },
{ eMSDP_SERVER_ID, "SERVER_ID", STRING_READ_ONLY },
{ eMSDP_SERVER_TIME, "SERVER_TIME", NUMBER_READ_ONLY },
{ eMSDP_SNIPPET_VERSION, "SNIPPET_VERSION", NUMBER_READ_ONLY_SET_TO(SNIPPET_VERSION) },
/* Character */
{ eMSDP_AFFECTS, "AFFECTS", STRING_READ_ONLY },
{ eMSDP_ALIGNMENT, "ALIGNMENT", NUMBER_READ_ONLY },
{ eMSDP_EXPERIENCE, "EXPERIENCE", NUMBER_READ_ONLY },
{ eMSDP_EXPERIENCE_MAX, "EXPERIENCE_MAX", NUMBER_READ_ONLY },
{ eMSDP_EXPERIENCE_TNL, "EXPERIENCE_TNL", NUMBER_READ_ONLY },
{ eMSDP_HEALTH, "HEALTH", NUMBER_READ_ONLY },
{ eMSDP_HEALTH_MAX, "HEALTH_MAX", NUMBER_READ_ONLY },
{ eMSDP_LEVEL, "LEVEL", NUMBER_READ_ONLY },
{ eMSDP_RACE, "RACE", STRING_READ_ONLY },
{ eMSDP_CLASS, "CLASS", STRING_READ_ONLY },
{ eMSDP_MANA, "MANA", NUMBER_READ_ONLY },
{ eMSDP_MANA_MAX, "MANA_MAX", NUMBER_READ_ONLY },
{ eMSDP_WIMPY, "WIMPY", NUMBER_READ_ONLY },
{ eMSDP_PRACTICE, "PRACTICE", NUMBER_READ_ONLY },
{ eMSDP_MONEY, "MONEY", NUMBER_READ_ONLY },
{ eMSDP_MOVEMENT, "MOVEMENT", NUMBER_READ_ONLY },
{ eMSDP_MOVEMENT_MAX, "MOVEMENT_MAX", NUMBER_READ_ONLY },
{ eMSDP_HITROLL, "HITROLL", NUMBER_READ_ONLY },
{ eMSDP_DAMROLL, "DAMROLL", NUMBER_READ_ONLY },
{ eMSDP_AC, "AC", NUMBER_READ_ONLY },
{ eMSDP_STR, "STR", NUMBER_READ_ONLY },
{ eMSDP_INT, "INT", NUMBER_READ_ONLY },
{ eMSDP_WIS, "WIS", NUMBER_READ_ONLY },
{ eMSDP_DEX, "DEX", NUMBER_READ_ONLY },
{ eMSDP_CON, "CON", NUMBER_READ_ONLY },
{ eMSDP_STR_PERM, "STR_PERM", NUMBER_READ_ONLY },
{ eMSDP_INT_PERM, "INT_PERM", NUMBER_READ_ONLY },
{ eMSDP_WIS_PERM, "WIS_PERM", NUMBER_READ_ONLY },
{ eMSDP_DEX_PERM, "DEX_PERM", NUMBER_READ_ONLY },
{ eMSDP_CON_PERM, "CON_PERM", NUMBER_READ_ONLY },
/* Combat */
{ eMSDP_OPPONENT_HEALTH, "OPPONENT_HEALTH", NUMBER_READ_ONLY },
{ eMSDP_OPPONENT_HEALTH_MAX,"OPPONENT_HEALTH_MAX",NUMBER_READ_ONLY },
{ eMSDP_OPPONENT_LEVEL, "OPPONENT_LEVEL", NUMBER_READ_ONLY },
{ eMSDP_OPPONENT_NAME, "OPPONENT_NAME", STRING_READ_ONLY },
/* World */
{ eMSDP_AREA_NAME, "AREA_NAME", STRING_READ_ONLY },
{ eMSDP_ROOM_EXITS, "ROOM_EXITS", STRING_READ_ONLY },
{ eMSDP_ROOM_NAME, "ROOM_NAME", STRING_READ_ONLY },
{ eMSDP_ROOM_VNUM, "ROOM_VNUM", NUMBER_READ_ONLY },
{ eMSDP_WORLD_TIME, "WORLD_TIME", NUMBER_READ_ONLY },
/* Configurable variables */
{ eMSDP_CLIENT_ID, "CLIENT_ID", STRING_WRITE_ONCE(1,40) },
{ eMSDP_CLIENT_VERSION, "CLIENT_VERSION", STRING_WRITE_ONCE(1,40) },
{ eMSDP_PLUGIN_ID, "PLUGIN_ID", STRING_WITH_LENGTH_OF(1,40) },
{ eMSDP_ANSI_COLORS, "ANSI_COLORS", BOOLEAN_SET_TO(1) },
{ eMSDP_XTERM_256_COLORS, "XTERM_256_COLORS", BOOLEAN_SET_TO(0) },
{ eMSDP_UTF_8, "UTF_8", BOOLEAN_SET_TO(0) },
{ eMSDP_SOUND, "SOUND", BOOLEAN_SET_TO(0) },
{ eMSDP_MXP, "MXP", BOOLEAN_SET_TO(0) },
/* GUI variables */
{ eMSDP_BUTTON_1, "BUTTON_1", STRING_GUI(s_Button1) },
{ eMSDP_BUTTON_2, "BUTTON_2", STRING_GUI(s_Button2) },
{ eMSDP_BUTTON_3, "BUTTON_3", STRING_GUI(s_Button3) },
{ eMSDP_BUTTON_4, "BUTTON_4", STRING_GUI(s_Button4) },
{ eMSDP_BUTTON_5, "BUTTON_5", STRING_GUI(s_Button5) },
{ eMSDP_GAUGE_1, "GAUGE_1", STRING_GUI(s_Gauge1) },
{ eMSDP_GAUGE_2, "GAUGE_2", STRING_GUI(s_Gauge2) },
{ eMSDP_GAUGE_3, "GAUGE_3", STRING_GUI(s_Gauge3) },
{ eMSDP_GAUGE_4, "GAUGE_4", STRING_GUI(s_Gauge4) },
{ eMSDP_GAUGE_5, "GAUGE_5", STRING_GUI(s_Gauge5) },
{ eMSDP_MAX, "", 0 } /* This must always be last. */
};
/******************************************************************************
MSSP file-scope variables.
******************************************************************************/
static int s_Players = 0;
static time_t s_Uptime = 0;
/******************************************************************************
Local function prototypes.
******************************************************************************/
static void Negotiate ( descriptor_t *apDescriptor );
static void PerformHandshake ( descriptor_t *apDescriptor, char aCmd, char aProtocol );
static void PerformSubnegotiation ( descriptor_t *apDescriptor, char aCmd, char *apData, int aSize );
static void SendNegotiationSequence ( descriptor_t *apDescriptor, char aCmd, char aProtocol );
static bool_t ConfirmNegotiation ( descriptor_t *apDescriptor, negotiated_t aProtocol, bool_t abWillDo, bool_t abSendReply );
static void ParseMSDP ( descriptor_t *apDescriptor, const char *apData );
static void ExecuteMSDPPair ( descriptor_t *apDescriptor, const char *apVariable, const char *apValue );
static void ParseATCP ( descriptor_t *apDescriptor, const char *apData );
#ifdef MUDLET_PACKAGE
static void SendATCP ( descriptor_t *apDescriptor, const char *apVariable, const char *apValue );
#endif /* MUDLET_PACKAGE */
static void SendMSSP ( descriptor_t *apDescriptor );
static char *GetMxpTag ( const char *apTag, const char *apText );
static const char *GetAnsiColour ( bool_t abBackground, int aRed, int aGreen, int aBlue );
static const char *GetRGBColour ( bool_t abBackground, int aRed, int aGreen, int aBlue );
static bool_t IsValidColour ( const char *apArgument );
static bool_t MatchString ( const char *apFirst, const char *apSecond );
static bool_t PrefixString ( const char *apPart, const char *apWhole );
static bool_t IsNumber ( const char *apString );
static char *AllocString ( const char *apString );
/******************************************************************************
ANSI colour codes.
******************************************************************************/
static const char s_Clean [] = "\033[0;00m"; /* Remove colour */
static const char s_DarkBlack [] = "\033[0;30m"; /* Black foreground */
static const char s_DarkRed [] = "\033[0;31m"; /* Red foreground */
static const char s_DarkGreen [] = "\033[0;32m"; /* Green foreground */
static const char s_DarkYellow [] = "\033[0;33m"; /* Yellow foreground */
static const char s_DarkBlue [] = "\033[0;34m"; /* Blue foreground */
static const char s_DarkMagenta [] = "\033[0;35m"; /* Magenta foreground */
static const char s_DarkCyan [] = "\033[0;36m"; /* Cyan foreground */
static const char s_DarkWhite [] = "\033[0;37m"; /* White foreground */
static const char s_BoldBlack [] = "\033[1;30m"; /* Grey foreground */
static const char s_BoldRed [] = "\033[1;31m"; /* Bright red foreground */
static const char s_BoldGreen [] = "\033[1;32m"; /* Bright green foreground */
static const char s_BoldYellow [] = "\033[1;33m"; /* Bright yellow foreground */
static const char s_BoldBlue [] = "\033[1;34m"; /* Bright blue foreground */
static const char s_BoldMagenta [] = "\033[1;35m"; /* Bright magenta foreground */
static const char s_BoldCyan [] = "\033[1;36m"; /* Bright cyan foreground */
static const char s_BoldWhite [] = "\033[1;37m"; /* Bright white foreground */
static const char s_BackBlack [] = "\033[1;40m"; /* Black background */
static const char s_BackRed [] = "\033[1;41m"; /* Red background */
static const char s_BackGreen [] = "\033[1;42m"; /* Green background */
static const char s_BackYellow [] = "\033[1;43m"; /* Yellow background */
static const char s_BackBlue [] = "\033[1;44m"; /* Blue background */
static const char s_BackMagenta [] = "\033[1;45m"; /* Magenta background */
static const char s_BackCyan [] = "\033[1;46m"; /* Cyan background */
static const char s_BackWhite [] = "\033[1;47m"; /* White background */
/******************************************************************************
Protocol global functions.
******************************************************************************/
protocol_t *ProtocolCreate( void )
{
int i; /* Loop counter */
protocol_t *pProtocol;
/* Called the first time we enter - make sure the table is correct */
static bool_t bInit = false;
if ( !bInit )
{
bInit = true;
for ( i = eMSDP_NONE+1; i < eMSDP_MAX; ++i )
{
if ( VariableNameTable[i].Variable != i )
{
ReportBug( "MSDP: Variable table does not match the enums in the header.\n" );
break;
}
}
}
pProtocol = malloc(sizeof(protocol_t));
pProtocol->WriteOOB = 0;
for ( i = eNEGOTIATED_TTYPE; i < eNEGOTIATED_MAX; ++i )
pProtocol->Negotiated[i] = false;
pProtocol->bIACMode = false;
pProtocol->bNegotiated = false;
pProtocol->bRenegotiate = false;
pProtocol->bNeedMXPVersion = false;
pProtocol->bBlockMXP = false;
pProtocol->bTTYPE = false;
pProtocol->bECHO = false;
pProtocol->bNAWS = false;
pProtocol->bCHARSET = false;
pProtocol->bMSDP = false;
pProtocol->bMSSP = false;
pProtocol->bATCP = false;
pProtocol->bMSP = false;
pProtocol->bMXP = false;
pProtocol->bMCCP = false;
pProtocol->b256Support = eUNKNOWN;
pProtocol->ScreenWidth = 0;
pProtocol->ScreenHeight = 0;
pProtocol->pMXPVersion = AllocString("Unknown");
pProtocol->pLastTTYPE = NULL;
pProtocol->pVariables = malloc(sizeof(MSDP_t*)*eMSDP_MAX);
for ( i = eMSDP_NONE+1; i < eMSDP_MAX; ++i )
{
pProtocol->pVariables[i] = malloc(sizeof(MSDP_t));
pProtocol->pVariables[i]->bReport = false;
pProtocol->pVariables[i]->bDirty = false;
pProtocol->pVariables[i]->ValueInt = 0;
pProtocol->pVariables[i]->pValueString = NULL;
if ( VariableNameTable[i].bString )
{
if ( VariableNameTable[i].pDefault != NULL )
pProtocol->pVariables[i]->pValueString = AllocString(VariableNameTable[i].pDefault);
else if ( VariableNameTable[i].bConfigurable )
pProtocol->pVariables[i]->pValueString = AllocString("Unknown");
else /* Use an empty string */
pProtocol->pVariables[i]->pValueString = AllocString("");
}
else if ( VariableNameTable[i].Default != 0 )
{
pProtocol->pVariables[i]->ValueInt = VariableNameTable[i].Default;
}
}
return pProtocol;
}
void ProtocolDestroy( protocol_t *apProtocol )
{
int i; /* Loop counter */
for ( i = eMSDP_NONE+1; i < eMSDP_MAX; ++i )
{
free(apProtocol->pVariables[i]->pValueString);
free(apProtocol->pVariables[i]);
}
free(apProtocol->pVariables);
free(apProtocol->pLastTTYPE);
free(apProtocol->pMXPVersion);
free(apProtocol);
}
void ProtocolInput( descriptor_t *apDescriptor, char *apData, int aSize, char *apOut )
{
static char CmdBuf[MAX_PROTOCOL_BUFFER+1];
static char IacBuf[MAX_PROTOCOL_BUFFER+1];
int CmdIndex = 0;
int IacIndex = 0;
int Index;
protocol_t *pProtocol = apDescriptor ? apDescriptor->pProtocol : NULL;
for ( Index = 0; Index < aSize; ++Index )
{
/* If we'd overflow the buffer, we just ignore the input */
if ( CmdIndex >= MAX_PROTOCOL_BUFFER || IacIndex >= MAX_PROTOCOL_BUFFER )
{
ReportBug("ProtocolInput: Too much incoming data to store in the buffer.\n");
return;
}
/* IAC IAC is treated as a single value of 255 */
if ( apData[Index] == (char)IAC && apData[Index+1] == (char)IAC )
{
if ( pProtocol->bIACMode )
IacBuf[IacIndex++] = (char)IAC;
else /* In-band command */
CmdBuf[CmdIndex++] = (char)IAC;
Index++;
}
else if ( pProtocol->bIACMode )
{
/* End subnegotiation. */
if ( apData[Index] == (char)IAC && apData[Index+1] == (char)SE )
{
Index++;
pProtocol->bIACMode = false;
IacBuf[IacIndex] = '\0';
if ( IacIndex >= 2 )
PerformSubnegotiation( apDescriptor, IacBuf[0], &IacBuf[1], IacIndex-1 );
IacIndex = 0;
}
else
IacBuf[IacIndex++] = apData[Index];
}
else if ( apData[Index] == (char)27 && apData[Index+1] == '[' &&
isdigit(apData[Index+2]) && apData[Index+3] == 'z' )
{
char MXPBuffer [1024];
char *pMXPTag = NULL;
int i = 0; /* Loop counter */
Index += 4; /* Skip to the start of the MXP sequence. */
while ( Index < aSize && apData[Index] != '>' && i < 1000 )
{
MXPBuffer[i++] = apData[Index++];
}
MXPBuffer[i++] = '>';
MXPBuffer[i] = '\0';
if ( ( pMXPTag = GetMxpTag( "CLIENT=", MXPBuffer ) ) != NULL )
{
/* Overwrite the previous client name - this is harder to fake */
free(pProtocol->pVariables[eMSDP_CLIENT_ID]->pValueString);
pProtocol->pVariables[eMSDP_CLIENT_ID]->pValueString = AllocString(pMXPTag);
}
if ( ( pMXPTag = GetMxpTag( "VERSION=", MXPBuffer ) ) != NULL )
{
const char *pClientName = pProtocol->pVariables[eMSDP_CLIENT_ID]->pValueString;
free(pProtocol->pVariables[eMSDP_CLIENT_VERSION]->pValueString);
pProtocol->pVariables[eMSDP_CLIENT_VERSION]->pValueString = AllocString(pMXPTag);
if ( MatchString( "MUSHCLIENT", pClientName ) )
{
/* MUSHclient 4.02 and later supports 256 colours. */
if ( strcmp(pMXPTag, "4.02") >= 0 )
{
pProtocol->pVariables[eMSDP_XTERM_256_COLORS]->ValueInt = 1;
pProtocol->b256Support = eYES;
}
else /* We know for sure that 256 colours are not supported */
pProtocol->b256Support = eNO;
}
else if ( MatchString( "CMUD", pClientName ) )
{
/* CMUD 3.04 and later supports 256 colours. */
if ( strcmp(pMXPTag, "3.04") >= 0 )
{
pProtocol->pVariables[eMSDP_XTERM_256_COLORS]->ValueInt = 1;
pProtocol->b256Support = eYES;
}
else /* We know for sure that 256 colours are not supported */
pProtocol->b256Support = eNO;
}
else if ( MatchString( "ATLANTIS", pClientName ) )
{
/* Atlantis 0.9.9.0 supports XTerm 256 colours, but it doesn't
* yet have MXP. However MXP is planned, so once it responds
* to a <VERSION> tag we'll know we can use 256 colours.
*/
pProtocol->pVariables[eMSDP_XTERM_256_COLORS]->ValueInt = 1;
pProtocol->b256Support = eYES;
}
}
if ( ( pMXPTag = GetMxpTag( "MXP=", MXPBuffer ) ) != NULL )
{
free(pProtocol->pMXPVersion);
pProtocol->pMXPVersion = AllocString(pMXPTag);
}
if ( strcmp(pProtocol->pMXPVersion, "Unknown") )
{
Write( apDescriptor, "\n" );
sprintf( MXPBuffer, "MXP version %s detected and enabled.\r\n",
pProtocol->pMXPVersion );
InfoMessage( apDescriptor, MXPBuffer );
}
}
else /* In-band command */
{
if ( apData[Index] == (char)IAC )
{
switch ( apData[Index+1] )
{
case (char)SB: /* Begin subnegotiation. */
Index++;
pProtocol->bIACMode = true;
break;
case (char)DO: /* Handshake. */
case (char)DONT:
case (char)WILL:
case (char)WONT:
PerformHandshake( apDescriptor, apData[Index+1], apData[Index+2] );
Index += 2;
break;
case (char)IAC: /* Two IACs count as one. */
CmdBuf[CmdIndex++] = (char)IAC;
Index++;
break;
default: /* Skip it. */
Index++;
break;
}
}
else
CmdBuf[CmdIndex++] = apData[Index];
}
}
/* Terminate the two buffers */
IacBuf[IacIndex] = '\0';
CmdBuf[CmdIndex] = '\0';
/* Copy the input buffer back to the player. */
strcat( apOut, CmdBuf );
}
const char *ProtocolOutput( descriptor_t *apDescriptor, const char *apData, int *apLength )
{
static char Result[MAX_OUTPUT_BUFFER+1];
const char Tab[] = "\t";
const char MSP[] = "!!";
const char MXPStart[] = "\033[1z<";
const char MXPStop[] = ">\033[7z";
const char LinkStart[] = "\033[1z<send>\033[7z";
const char LinkStop[] = "\033[1z</send>\033[7z";
bool_t bTerminate = false, bUseMXP = false, bUseMSP = false;
#ifdef COLOUR_CHAR
bool_t bColourOn = COLOUR_ON_BY_DEFAULT;
#endif /* COLOUR_CHAR */
int i = 0, j = 0; /* Index values */
protocol_t *pProtocol = apDescriptor ? apDescriptor->pProtocol : NULL;
if ( pProtocol == NULL || apData == NULL )
return apData;
/* Strip !!SOUND() triggers if they support MSP or are using sound */
if ( pProtocol->bMSP || pProtocol->pVariables[eMSDP_SOUND]->ValueInt )
bUseMSP = true;
for ( ; i < MAX_OUTPUT_BUFFER && apData[j] != '\0' && !bTerminate &&
(*apLength <= 0 || j < *apLength); ++j )
{
if ( apData[j] == '\t' )
{
const char *pCopyFrom = NULL;
switch ( apData[++j] )
{
case '\t': /* Two tabs in a row will display an actual tab */
pCopyFrom = Tab;
break;
case 'n':
pCopyFrom = s_Clean;
break;
case 'r': /* dark red */
pCopyFrom = ColourRGB(apDescriptor, "F200");
break;
case 'R': /* light red */
pCopyFrom = ColourRGB(apDescriptor, "F500");
break;
case 'g': /* dark green */
pCopyFrom = ColourRGB(apDescriptor, "F020");
break;
case 'G': /* light green */
pCopyFrom = ColourRGB(apDescriptor, "F050");
break;
case 'y': /* dark yellow */
pCopyFrom = ColourRGB(apDescriptor, "F220");
break;
case 'Y': /* light yellow */
pCopyFrom = ColourRGB(apDescriptor, "F550");
break;
case 'b': /* dark blue */
pCopyFrom = ColourRGB(apDescriptor, "F002");
break;
case 'B': /* light blue */
pCopyFrom = ColourRGB(apDescriptor, "F005");
break;
case 'm': /* dark magenta */
pCopyFrom = ColourRGB(apDescriptor, "F202");
break;
case 'M': /* light magenta */
pCopyFrom = ColourRGB(apDescriptor, "F505");
break;
case 'c': /* dark cyan */
pCopyFrom = ColourRGB(apDescriptor, "F022");
break;
case 'C': /* light cyan */
pCopyFrom = ColourRGB(apDescriptor, "F055");
break;
case 'w': /* dark white */
pCopyFrom = ColourRGB(apDescriptor, "F222");
break;
case 'W': /* light white */
pCopyFrom = ColourRGB(apDescriptor, "F555");
break;
case 'a': /* dark azure */
pCopyFrom = ColourRGB(apDescriptor, "F014");
break;
case 'A': /* light azure */
pCopyFrom = ColourRGB(apDescriptor, "F025");
break;
case 'j': /* dark jade */
pCopyFrom = ColourRGB(apDescriptor, "F031");
break;
case 'J': /* light jade */
pCopyFrom = ColourRGB(apDescriptor, "F052");
break;
case 'l': /* dark lime */
pCopyFrom = ColourRGB(apDescriptor, "F140");
break;
case 'L': /* light lime */
pCopyFrom = ColourRGB(apDescriptor, "F250");
break;
case 'o': /* dark orange */
pCopyFrom = ColourRGB(apDescriptor, "F520");
break;
case 'O': /* light orange */
pCopyFrom = ColourRGB(apDescriptor, "F530");
break;
case 'p': /* dark pink */
pCopyFrom = ColourRGB(apDescriptor, "F301");
break;
case 'P': /* light pink */
pCopyFrom = ColourRGB(apDescriptor, "F502");
break;
case 't': /* dark tan */
pCopyFrom = ColourRGB(apDescriptor, "F210");
break;
case 'T': /* light tan */
pCopyFrom = ColourRGB(apDescriptor, "F321");
break;
case 'v': /* dark violet */
pCopyFrom = ColourRGB(apDescriptor, "F104");
break;
case 'V': /* light violet */
pCopyFrom = ColourRGB(apDescriptor, "F205");
break;
case '(': /* MXP link */
if ( !pProtocol->bBlockMXP && pProtocol->pVariables[eMSDP_MXP]->ValueInt )
pCopyFrom = LinkStart;
break;
case ')': /* MXP link */
if ( !pProtocol->bBlockMXP && pProtocol->pVariables[eMSDP_MXP]->ValueInt )
pCopyFrom = LinkStop;
pProtocol->bBlockMXP = false;
break;
case '<':
if ( !pProtocol->bBlockMXP && pProtocol->pVariables[eMSDP_MXP]->ValueInt )
{
pCopyFrom = MXPStart;
bUseMXP = true;
}
else /* No MXP support, so just strip it out */
{
while ( apData[j] != '\0' && apData[j] != '>' )
++j;
}
pProtocol->bBlockMXP = false;
break;
case '[':
if ( tolower(apData[++j]) == 'u' )
{
char Buffer[8] = {'\0'}, BugString[256];
int Index = 0;
int Number = 0;
bool_t bDone = false, bValid = true;
while ( isdigit(apData[++j]) )
{
Number *= 10;
Number += (apData[j])-'0';
}
if ( apData[j] == '/' )
++j;
while ( apData[j] != '\0' && !bDone )
{
if ( apData[j] == ']' )
bDone = true;
else if ( Index < 7 )
Buffer[Index++] = apData[j++];
else /* It's too long, so ignore the rest and note the problem */
{
j++;
bValid = false;
}
}
if ( !bDone )
{
sprintf( BugString, "BUG: Unicode substitute '%s' wasn't terminated with ']'.\n", Buffer );
ReportBug( BugString );
}
else if ( !bValid )
{
sprintf( BugString, "BUG: Unicode substitute '%s' truncated. Missing ']'?\n", Buffer );
ReportBug( BugString );
}
else if ( pProtocol->pVariables[eMSDP_UTF_8]->ValueInt )
{
pCopyFrom = UnicodeGet(Number);
}
else /* Display the substitute string */
{
pCopyFrom = Buffer;
}
/* Terminate if we've reached the end of the string */
bTerminate = !bDone;
}
else if ( tolower(apData[j]) == 'f' || tolower(apData[j]) == 'b' )
{
char Buffer[8] = {'\0'}, BugString[256];
int Index = 0;
bool_t bDone = false, bValid = true;
/* Copy the 'f' (foreground) or 'b' (background) */
Buffer[Index++] = apData[j++];
while ( apData[j] != '\0' && !bDone && bValid )
{
if ( apData[j] == ']' )
bDone = true;
else if ( Index < 4 )
Buffer[Index++] = apData[j++];
else /* It's too long, so drop out - the colour code may still be valid */
bValid = false;
}
if ( !bDone || !bValid )
{
sprintf( BugString, "BUG: RGB %sground colour '%s' wasn't terminated with ']'.\n",
(tolower(Buffer[0]) == 'f') ? "fore" : "back", &Buffer[1] );
ReportBug( BugString );
}
else if ( !IsValidColour(Buffer) )
{
sprintf( BugString, "BUG: RGB %sground colour '%s' invalid (each digit must be in the range 0-5).\n",
(tolower(Buffer[0]) == 'f') ? "fore" : "back", &Buffer[1] );
ReportBug( BugString );
}
else /* Success */
{
pCopyFrom = ColourRGB(apDescriptor, Buffer);
}
}
else if ( tolower(apData[j]) == 'x' )
{
char Buffer[8] = {'\0'}, BugString[256];
int Index = 0;
bool_t bDone = false, bValid = true;
++j; /* Skip the 'x' */
while ( apData[j] != '\0' && !bDone )
{
if ( apData[j] == ']' )
bDone = true;
else if ( Index < 7 )
Buffer[Index++] = apData[j++];
else /* It's too long, so ignore the rest and note the problem */
{
j++;
bValid = false;
}
}
if ( !bDone )
{
sprintf( BugString, "BUG: Required MXP version '%s' wasn't terminated with ']'.\n", Buffer );
ReportBug( BugString );
}
else if ( !bValid )
{
sprintf( BugString, "BUG: Required MXP version '%s' too long. Missing ']'?\n", Buffer );
ReportBug( BugString );
}
else if ( !strcmp(pProtocol->pMXPVersion, "Unknown") ||
strcmp(pProtocol->pMXPVersion, Buffer) < 0 )
{
/* Their version of MXP isn't high enough */
pProtocol->bBlockMXP = true;
}
else /* MXP is sufficient for this tag */
{
pProtocol->bBlockMXP = false;
}
/* Terminate if we've reached the end of the string */
bTerminate = !bDone;
}
break;
case '!': /* Used for in-band MSP sound triggers */
pCopyFrom = MSP;
break;
#ifdef COLOUR_CHAR
case '+':
bColourOn = true;
break;
case '-':
bColourOn = false;
break;
#endif /* COLOUR_CHAR */
case '\0':
bTerminate = true;
break;
default:
break;
}
/* Copy the colour code, if any. */
if ( pCopyFrom != NULL )
{
while ( *pCopyFrom != '\0' && i < MAX_OUTPUT_BUFFER )
Result[i++] = *pCopyFrom++;
}
}
#ifdef COLOUR_CHAR
else if ( bColourOn && apData[j] == COLOUR_CHAR )
{
const char ColourChar[] = { COLOUR_CHAR, '\0' };
const char *pCopyFrom = NULL;
switch ( apData[++j] )
{
case COLOUR_CHAR: /* Two in a row display the actual character */
pCopyFrom = ColourChar;
break;
case 'n':
pCopyFrom = s_Clean;
break;
case 'r': /* dark red */
pCopyFrom = ColourRGB(apDescriptor, "F200");
break;
case 'R': /* light red */
pCopyFrom = ColourRGB(apDescriptor, "F500");
break;
case 'g': /* dark green */
pCopyFrom = ColourRGB(apDescriptor, "F020");
break;
case 'G': /* light green */
pCopyFrom = ColourRGB(apDescriptor, "F050");
break;
case 'y': /* dark yellow */
pCopyFrom = ColourRGB(apDescriptor, "F220");
break;
case 'Y': /* light yellow */
pCopyFrom = ColourRGB(apDescriptor, "F550");
break;
case 'b': /* dark blue */
pCopyFrom = ColourRGB(apDescriptor, "F002");
break;
case 'B': /* light blue */
pCopyFrom = ColourRGB(apDescriptor, "F005");
break;
case 'm': /* dark magenta */
pCopyFrom = ColourRGB(apDescriptor, "F202");
break;
case 'M': /* light magenta */
pCopyFrom = ColourRGB(apDescriptor, "F505");
break;
case 'c': /* dark cyan */
pCopyFrom = ColourRGB(apDescriptor, "F022");
break;
case 'C': /* light cyan */
pCopyFrom = ColourRGB(apDescriptor, "F055");
break;
case 'w': /* dark white */
pCopyFrom = ColourRGB(apDescriptor, "F222");
break;
case 'W': /* light white */
pCopyFrom = ColourRGB(apDescriptor, "F555");
break;
case 'a': /* dark azure */
pCopyFrom = ColourRGB(apDescriptor, "F014");
break;
case 'A': /* light azure */
pCopyFrom = ColourRGB(apDescriptor, "F025");
break;
case 'j': /* dark jade */
pCopyFrom = ColourRGB(apDescriptor, "F031");
break;
case 'J': /* light jade */
pCopyFrom = ColourRGB(apDescriptor, "F052");
break;
case 'l': /* dark lime */
pCopyFrom = ColourRGB(apDescriptor, "F140");
break;
case 'L': /* light lime */
pCopyFrom = ColourRGB(apDescriptor, "F250");
break;
case 'o': /* dark orange */
pCopyFrom = ColourRGB(apDescriptor, "F520");
break;
case 'O': /* light orange */
pCopyFrom = ColourRGB(apDescriptor, "F530");
break;
case 'p': /* dark pink */
pCopyFrom = ColourRGB(apDescriptor, "F301");
break;
case 'P': /* light pink */
pCopyFrom = ColourRGB(apDescriptor, "F502");
break;
case 't': /* dark tan */
pCopyFrom = ColourRGB(apDescriptor, "F210");
break;
case 'T': /* light tan */
pCopyFrom = ColourRGB(apDescriptor, "F321");
break;
case 'v': /* dark violet */
pCopyFrom = ColourRGB(apDescriptor, "F104");
break;
case 'V': /* light violet */
pCopyFrom = ColourRGB(apDescriptor, "F205");
break;
case '\0':
bTerminate = true;
break;
#ifdef EXTENDED_COLOUR
case '[':
if ( tolower(apData[++j]) == 'f' || tolower(apData[j]) == 'b' )
{
char Buffer[8] = {'\0'};
int Index = 0;
bool_t bDone = false, bValid = true;
/* Copy the 'f' (foreground) or 'b' (background) */
Buffer[Index++] = apData[j++];
while ( apData[j] != '\0' && !bDone && bValid )
{
if ( apData[j] == ']' )
bDone = true;
else if ( Index < 4 )
Buffer[Index++] = apData[j++];
else /* It's too long, so drop out - the colour code may still be valid */
bValid = false;
}
if ( bDone && bValid && IsValidColour(Buffer) )
pCopyFrom = ColourRGB(apDescriptor, Buffer);
}
break;
#endif /* EXTENDED_COLOUR */
default:
#ifdef DISPLAY_INVALID_COLOUR_CODES
Result[i++] = COLOUR_CHAR;
Result[i++] = apData[j];
#endif /* DISPLAY_INVALID_COLOUR_CODES */
break;
}
/* Copy the colour code, if any. */
if ( pCopyFrom != NULL )
{
while ( *pCopyFrom != '\0' && i < MAX_OUTPUT_BUFFER )
Result[i++] = *pCopyFrom++;
}
}
#endif /* COLOUR_CHAR */
else if ( bUseMXP && apData[j] == '>' )
{
const char *pCopyFrom = MXPStop;
while ( *pCopyFrom != '\0' && i < MAX_OUTPUT_BUFFER)
Result[i++] = *pCopyFrom++;
bUseMXP = false;
}
else if ( bUseMSP && j > 0 && apData[j-1] == '!' && apData[j] == '!' &&
PrefixString("SOUND(", &apData[j+1]) )
{
/* Avoid accidental triggering of old-style MSP triggers */
Result[i++] = '?';
}
else /* Just copy the character normally */
{
Result[i++] = apData[j];
}
}
/* If we'd overflow the buffer, we don't send any output */
if ( i >= MAX_OUTPUT_BUFFER )
{
i = 0;
ReportBug("ProtocolOutput: Too much outgoing data to store in the buffer.\n");
}
/* Terminate the string */
Result[i] = '\0';
/* Store the length */
if ( apLength )