-
Notifications
You must be signed in to change notification settings - Fork 76
/
evaluate.cpp
1468 lines (1143 loc) · 46.9 KB
/
evaluate.cpp
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
#include "stdafx.h"
#include "MUSHclient.h"
#include "doc.h"
// command evaluation
// For lengthy explanation see: http://www.gammon.com.au/forum/?id=6572
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
BOOL CMUSHclientDoc::EvaluateCommand (const CString & full_input,
const bool bCountThem,
bool & bOmitFromLog,
const bool bTest)
{
CString str;
POSITION pos;
CString input = full_input;
CAliasList AliasList;
// get rid of any carriage returns
input.Replace ("\r", "");
// ignore blank lines ?? is this wise?
if (input.IsEmpty ())
return false;
// ------------------------- SPEED WALKING ------------------------------
// see if they are doing speed walking
if (m_enable_speed_walk &&
input.Left (m_speed_walk_prefix.GetLength ()) == m_speed_walk_prefix)
{
CString strEvaluatedSpeedwalk = DoEvaluateSpeedwalk (input.Mid (m_speed_walk_prefix.GetLength ()));
if (!strEvaluatedSpeedwalk.IsEmpty ())
{
if (strEvaluatedSpeedwalk [0] == '*') // error in speedwalk string?
{
::UMessageBox (strEvaluatedSpeedwalk.Mid (1));
return true;
}
// let them know if they are foolishly trying to send to a closed connection
if (CheckConnected ())
return true;
SendMsg (strEvaluatedSpeedwalk, m_display_my_input,
true, // queue it
LoggingInput ());
}
return false;
}
// end of having a speed-walk string
// here if not speed walking
// --------------------------- ALIASES ------------------------------
bool bEchoAlias = m_display_my_input;
OneShotItemMap mapOneShotItems;
if (m_enable_aliases)
{
PluginListIterator pit;
// Do plugins (stop if one stops trigger evaluation).
// Do only negative sequence number plugins at this point
// Suggested by Fiendish. Added in version 4.97.
for (pit = m_PluginList.begin ();
pit != m_PluginList.end () &&
(*pit)->m_iSequence < 0;
++pit)
{
m_CurrentPlugin = *pit;
if (m_CurrentPlugin->m_bEnabled)
if (ProcessOneAliasSequence (input,
bCountThem,
bOmitFromLog,
bEchoAlias,
AliasList,
mapOneShotItems))
{
m_CurrentPlugin = NULL;
return true;
}
} // end of doing each plugin
m_CurrentPlugin = NULL;
if (ProcessOneAliasSequence (input,
bCountThem,
bOmitFromLog,
bEchoAlias,
AliasList,
mapOneShotItems))
return true;
// do plugins (stop if one stops alias evaluation)
for (pit = m_PluginList.begin ();
pit != m_PluginList.end ();
++pit)
{
// skip past negative sequence numbers
if ((*pit)->m_iSequence < 0)
continue;
m_CurrentPlugin = *pit;
if (m_CurrentPlugin->m_bEnabled)
if (ProcessOneAliasSequence (input,
bCountThem,
bOmitFromLog,
bEchoAlias,
AliasList,
mapOneShotItems))
{
m_CurrentPlugin = NULL;
return true;
}
} // end of doing each plugin
m_CurrentPlugin = NULL; // not in a plugin any more
} // end of aliases enabled
// if no alias matched at all, just send the raw command
if (AliasList.IsEmpty ())
{
// let them know if they are foolishly trying to send to a closed connection
if (CheckConnected ())
return true;
// don't reconnect on a deliberate QUIT
if (input.CompareNoCase (m_macros [MAC_QUIT]) == 0)
m_bDisconnectOK = true; // don't want reconnect on quit
SendMsg (input, m_display_my_input, false, LoggingInput ()); // send now
return FALSE;
}
// execute any scripts associated with aliases we found
bool bFoundIt;
CAlias * existing_alias_item;
CAlias * alias_item;
CString strAliasName;
for (pos = AliasList.GetHeadPosition (); pos; )
{
alias_item = AliasList.GetNext (pos);
bFoundIt = false;
// check that alias still exists, in case a script deleted it - and also
// to work out which plugin it is in
m_CurrentPlugin = NULL;
// main aliases - if main scripting active
for (POSITION pos = GetAliasMap ().GetStartPosition (); !bFoundIt && pos; )
{
GetAliasMap ().GetNextAssoc (pos, strAliasName, existing_alias_item);
if (existing_alias_item == alias_item)
{
bFoundIt = true;
// execute Alias script
ExecuteAliasScript (alias_item, input);
}
} // end of scanning main aliases
// do plugins
for (PluginListIterator pit = m_PluginList.begin ();
!bFoundIt && pit != m_PluginList.end ();
++pit)
{
m_CurrentPlugin = *pit;
if (m_CurrentPlugin->m_bEnabled)
for (POSITION pos = GetAliasMap ().GetStartPosition (); !bFoundIt && pos; )
{
GetAliasMap ().GetNextAssoc (pos, strAliasName, existing_alias_item);
if (existing_alias_item == alias_item)
{
bFoundIt = true;
// execute Alias script
ExecuteAliasScript (alias_item, input);
}
} // end of scanning plugin aliases
} // end of doing plugins list
} // end of list of aliass that fired
// now that we have run all scripts etc., delete one-shot aliases
int iDeletedCount = 0;
int iDeletedNonTemporaryCount = 0;
set<CPlugin *> pluginsWithDeletions;
for (OneShotItemMap::const_iterator one_shot_it = mapOneShotItems.begin ();
one_shot_it != mapOneShotItems.end ();
one_shot_it++)
{
CAlias * alias_item;
CString strAliasName = one_shot_it->sItemKey.c_str ();
m_CurrentPlugin = one_shot_it->pWhichPlugin; // set back to correct plugin
if (!GetAliasMap ().Lookup (strAliasName, alias_item))
continue;
// can't if executing a script
if (alias_item->bExecutingScript)
continue;
if (!m_CurrentPlugin && !alias_item->bTemporary)
iDeletedNonTemporaryCount++;
iDeletedCount++;
// the alias seems to exist - delete its pointer
delete alias_item;
// now delete its entry
GetAliasMap ().RemoveKey (strAliasName);
pluginsWithDeletions.insert (m_CurrentPlugin);
} // end of deleting one-shot items
if (iDeletedCount > 0)
{
// make sure we sort the correct plugin(s)
for ( set<CPlugin *>::iterator i = pluginsWithDeletions.begin (); i != pluginsWithDeletions.end (); i++)
{
m_CurrentPlugin = *i;
SortAliases ();
}
if (iDeletedNonTemporaryCount > 0) // plugin mods don't really count
SetModifiedFlag (TRUE); // document has changed
}
m_CurrentPlugin = NULL;
return FALSE;
} // end of CMUSHclientDoc::EvaluateCommand
// wildcard fixer
string FixWildcard (const string sWildcard, // the wildcard
const bool bMakeLowerCase, // true to make lower case
const int iSendTo, // where it is going to
const CString strLanguage) // what script language
{
string sResult = sWildcard;
// force to lower-case if that is what they want
if (bMakeLowerCase)
sResult = tolower (sResult);
// escape out strings if we are sending to script
if (iSendTo == eSendToScript || iSendTo == eSendToScriptAfterOmit)
{
if (strLanguage.CompareNoCase ("vbscript") == 0)
// " becomes ""
sResult = FindAndReplace (sResult, "\"", "\"\"");
else
{ // not VBscript
// escape out backslashes first (ie. \ becomes \\ )
sResult = FindAndReplace (sResult, "\\", "\\\\");
// now turn " to \"
sResult = FindAndReplace (sResult, "\"", "\\\"");
// finally better escape out the $ signs
if (strLanguage.CompareNoCase ("perlscript") == 0)
sResult = FindAndReplace (sResult, "$", "\\$");
} // end of not VBscript
} // end of sending to script
return sResult;
} // end of FixWildcard
CTrigger * CMUSHclientDoc::EvaluateTrigger (const CString & input,
CString & output,
int & iItem, // which one to start with
int & iStartCol,
int & iEndCol)
{
// timer t ("EvaluateTrigger");
bool matched = false;
CTrigger * trigger_item = NULL;
int iCount = GetTriggerArray ().GetSize (); // how many there are
output.Empty ();
// matching start/end col defaults to whole line
iStartCol = 0;
iEndCol = input.GetLength ();
// if triggers not enabled, return empty response
if (!m_enable_triggers)
return NULL; // error return
for ( ; iItem < iCount; iItem++)
{
trigger_item = GetTriggerArray () [iItem];
if (!trigger_item->bEnabled)
continue; // ignore non-enabled triggers
m_iTriggersEvaluatedCount++; // count evaluations
// do regular expression, if available
if (trigger_item->regexp)
{
CString strTarget;
if (trigger_item->bMultiLine)
{
// timer t ("Assembling text");
string s;
// can't do it if not enough lines received (hmm, maybe not)
// if (m_sRecentLines.size () < trigger_item->iLinesToMatch)
// continue;
// assemble multi-line match text
int iPos = m_sRecentLines.size () - trigger_item->iLinesToMatch;
if (iPos < 0)
iPos = 0;
for (int iCount = 0;
iCount < trigger_item->iLinesToMatch &&
iPos != m_sRecentLines.size ()
; iPos++, iCount++
)
{
s += m_sRecentLines [iPos];
s += '\n'; // multi-line triggers always end in newlines (new in version 3.50)
} // end of assembling text
strTarget = s.c_str ();
}
else
strTarget = input;
/*
New feature in 3.18 - trigger match strings can incorporate variables in
the "trigger" portion. Do a quick scan to see if this is the case, and if
so, recompile the regexp with substituted variables.
Note, non-existent and empty variables will be silently dropped.
*/
if (trigger_item->bExpandVariables &&
trigger_item->trigger.Find ('@') != -1)
{
CString strOutput = FixSendText (trigger_item->trigger,
trigger_item->iSendTo,
NULL, // regexp
GetLanguage (),
false, // lower-case wildcards
true, // expand variables
false, // expand wildcards
true, // convert regexps
trigger_item->bRegexp, // is it regexp or normal?
false, // don't throw exceptions
NULL); // no name substitution in match text
LONGLONG iOldTimeTaken = 0;
long iOldMatchAttempts = 0;
// remember time taken to execute them
if (trigger_item->regexp)
{
iOldTimeTaken = trigger_item->regexp->iTimeTaken;
iOldMatchAttempts = trigger_item->regexp->m_iMatchAttempts;
}
delete trigger_item->regexp; // get rid of earlier regular expression
trigger_item->regexp = NULL;
// all triggers are now regular expressions
CString strRegexp;
if (trigger_item->bRegexp)
strRegexp = strOutput;
else
strRegexp = ConvertToRegularExpression (strOutput);
try
{
trigger_item->regexp = regcomp (strRegexp,
(trigger_item->ignore_case ? PCRE_CASELESS : 0) |
(trigger_item->bMultiLine ? PCRE_MULTILINE : 0) |
(m_bUTF_8 ? PCRE_UTF8 : 0)
);
} // end of try
catch(CException* e)
{
e->ReportError ();
e->Delete ();
continue;
} // end of catch
// add back execution time
if (trigger_item->regexp)
{
trigger_item->regexp->iTimeTaken += iOldTimeTaken;
trigger_item->regexp->m_iMatchAttempts += iOldMatchAttempts;
}
} // end of variable substitution
try
{
// timer t ("Evaluating regular expression");
if (!regexec (trigger_item->regexp, strTarget))
continue;
} // end of try
catch(CException* e)
{
e->ReportError ();
e->Delete ();
continue;
} // end of catch
iStartCol = trigger_item->regexp->m_vOffsets [0];
iEndCol = trigger_item->regexp->m_vOffsets [1];
trigger_item->wildcards.clear ();
for (int iWildcard = 0;
iWildcard < MAX_WILDCARDS;
iWildcard++)
trigger_item->wildcards.push_back
(
FixWildcard (trigger_item->regexp->GetWildcard (iWildcard),
trigger_item->bLowercaseWildcard,
trigger_item->iSendTo,
m_strLanguage)
);
}
else
continue; // no regexp, ignore trigger
matched = true;
trigger_item->tWhenMatched = CTime::GetCurrentTime(); // when it matched
// copy contents to output area, replacing %1, %2 etc. with appropriate contents
// get unlabelled trigger's internal name
const char * pLabel = trigger_item->strLabel;
if (pLabel [0] == 0)
pLabel = GetTriggerRevMap () [trigger_item].c_str ();
output += FixSendText (::FixupEscapeSequences (trigger_item->contents),
trigger_item->iSendTo, // where it is going
trigger_item->regexp, // regexp
GetLanguage (), // eg. vbscript
trigger_item->bLowercaseWildcard, // lower-case wildcards
trigger_item->bExpandVariables, // expand variables
true, // expand wildcards
false, // convert regexps
false, // is it regexp or normal?
false, // don't throw exceptions
pLabel);
break; // break out of loop, we have a trigger match
} // end of search each trigger item
if (!matched)
return NULL;
return trigger_item;
} // end of CMUSHclientDoc::EvaluateTrigger
BOOL Set_Up_Set_Strings (const int set_type,
CString & suggested_name,
CString & filter,
CString & title,
CString & suggested_extension)
{
switch (set_type)
{
case TRIGGER: suggested_extension = "mct";
filter = "MUSHclient triggers (*.mct)|*.mct||";
title = "Trigger file name";
suggested_name += " triggers";
break;
case ALIAS: suggested_extension = "mca";
filter = "MUSHclient aliases (*.mca)|*.mca||";
title = "Alias file name";
suggested_name += " aliases";
break;
case COLOUR: suggested_extension = "mcc";
filter = "MUSHclient colours (*.mcc)|*.mcc||";
title = "Colour file name";
suggested_name += " colours";
break;
case MACRO: suggested_extension = "mcm";
filter = "MUSHclient macros (*.mcm)|*.mcm||";
title = "Macro file name";
suggested_name += " macros";
break;
case STRING: suggested_extension = "mcs";
filter = "MUSHclient strings (*.mcs)|*.mcs||";
title = "Strings file name";
suggested_name += " strings";
break;
case TIMER: suggested_extension = "mci";
filter = "MUSHclient timers (*.mci)|*.mci||";
title = "Timers file name";
suggested_name += " timers";
break;
default: return TRUE; // error return
} // end of switch
return FALSE; // OK return
} // end of CMUSHclientDoc::Set_Up_Set_Strings
BOOL CMUSHclientDoc::Load_Set (const int set_type,
CString strFileName,
CWnd * parent_window)
{
BOOL replace = TRUE;
if (strFileName.IsEmpty ())
{
CString suggested_name = m_mush_name,
filter,
title,
suggested_extension;
CString filename;
if (Set_Up_Set_Strings (set_type,
suggested_name,
filter,
title,
suggested_extension))
return TRUE; // bad set_type
CFileDialog filedlg (TRUE, // loading the file
suggested_extension, // default extension
"", // suggested name
OFN_HIDEREADONLY | OFN_FILEMUSTEXIST,
filter, // filter
parent_window); // parent window
filedlg.m_ofn.lpstrTitle = title;
filedlg.m_ofn.lpstrFile = filename.GetBuffer (_MAX_PATH); // needed!! (for Win32s)
if (App.platform == VER_PLATFORM_WIN32s)
strcpy (filedlg.m_ofn.lpstrFile, "");
else
strcpy (filedlg.m_ofn.lpstrFile, suggested_name);
ChangeToFileBrowsingDirectory ();
int nResult = filedlg.DoModal();
ChangeToStartupDirectory ();
if (nResult!= IDOK)
return TRUE; // cancelled dialog
// since they can have any number of triggers, aliases and timers, ask them
// whether they want to add this file to an existing list (if any)
if (set_type == TRIGGER && !m_TriggerMap.IsEmpty ())
{
if (::TMessageBox ("Replace existing triggers?\n"
"If you reply \"No\", then triggers from the file"
" will be added to existing triggers",
MB_YESNO | MB_ICONQUESTION) == IDNO)
replace = FALSE;
}
else
if (set_type == ALIAS && !m_AliasMap.IsEmpty ())
{
if (::TMessageBox ("Replace existing aliases?\n"
"If you reply \"No\", then aliases from the file"
" will be added to existing aliases",
MB_YESNO | MB_ICONQUESTION) == IDNO)
replace = FALSE;
}
else
if (set_type == TIMER && !m_TimerMap.IsEmpty ())
{
if (::TMessageBox ("Replace existing timers?\n"
"If you reply \"No\", then timers from the file"
" will be added to existing timers",
MB_YESNO | MB_ICONQUESTION) == IDNO)
replace = FALSE;
}
strFileName = filedlg.GetPathName ();
} // end of no filename suppliedl
CFile * f = NULL;
CArchive * ar = NULL;
try
{
f = new CFile (strFileName, CFile::modeRead | CFile::shareDenyWrite);
ar = new CArchive(f, CArchive::load);
if (IsArchiveXML (*ar))
{
switch (set_type)
{
case TRIGGER:
if (replace)
DELETE_MAP (m_TriggerMap, CTrigger);
Load_World_XML (*ar, XML_TRIGGERS | XML_NO_PLUGINS | XML_IMPORT_MAIN_FILE_ONLY);
break;
case ALIAS:
if (replace)
DELETE_MAP (m_AliasMap, CAlias);
Load_World_XML (*ar, XML_ALIASES | XML_NO_PLUGINS | XML_IMPORT_MAIN_FILE_ONLY);
break;
case COLOUR:
Load_World_XML (*ar, XML_COLOURS | XML_NO_PLUGINS | XML_IMPORT_MAIN_FILE_ONLY);
break;
case MACRO:
Load_World_XML (*ar, XML_MACROS | XML_NO_PLUGINS | XML_IMPORT_MAIN_FILE_ONLY);
break;
case TIMER:
if (replace)
DELETE_MAP (m_TimerMap, CTimer);
Load_World_XML (*ar, XML_TIMERS | XML_NO_PLUGINS | XML_IMPORT_MAIN_FILE_ONLY);
break;
} // end of switch
} // end of XML load
else
{
::TMessageBox ("File does not have a valid MUSHclient XML signature.",
MB_ICONSTOP);
AfxThrowArchiveException (CArchiveException::badSchema);
} // end of not XML
} // end of try block
// even on an exception we will return a "good" status, because the triggers etc.
// may well have been deleted by now, so we need to redraw the lists
catch (CFileException * e)
{
::UMessageBox (TFormat ("Unable to open or read %s",
(LPCTSTR) strFileName), MB_ICONEXCLAMATION);
e->Delete ();
} // end of catching a file exception
catch (CMemoryException * e)
{
::TMessageBox ("Insufficient memory to do this operation", MB_ICONEXCLAMATION);
e->Delete ();
} // end of catching a memory exception
catch (CArchiveException * e)
{
::UMessageBox (TFormat ("The file %s is not in the correct format",
(LPCTSTR) strFileName), MB_ICONEXCLAMATION);
e->Delete ();
} // end of catching an archive exception
delete ar; // delete archive
delete f; // delete file
SetModifiedFlag (TRUE); // document has now changed
return false; // OK return
} // end of CMUSHclientDoc::load_set
BOOL CMUSHclientDoc::Save_Set (const int set_type,
CWnd * parent_window)
{
CString suggested_name = m_mush_name,
filter,
title,
suggested_extension;
CFile * f = NULL;
CArchive * ar = NULL;
BOOL error = TRUE;
CString sig;
CString filename;
if (Set_Up_Set_Strings (set_type,
suggested_name,
filter,
title,
suggested_extension))
return TRUE; // bad set_type
CFileDialog filedlg (FALSE, // saving the file
suggested_extension, // default extension
"", // suggested name
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
filter, // filter
parent_window); // parent window
// fix up name to remove characters that are invalid
int i;
while ((i = suggested_name.FindOneOf ("<>\"|?:#%;/\\")) != -1)
suggested_name = suggested_name.Left (i) + suggested_name.Mid (i + 1);
filedlg.m_ofn.lpstrTitle = title;
filedlg.m_ofn.lpstrFile = filename.GetBuffer (_MAX_PATH); // needed!! (for Win32s)
if (App.platform == VER_PLATFORM_WIN32s)
strcpy (filedlg.m_ofn.lpstrFile, "");
else
strcpy (filedlg.m_ofn.lpstrFile, suggested_name);
ChangeToFileBrowsingDirectory ();
int nResult = filedlg.DoModal();
ChangeToStartupDirectory ();
if (nResult != IDOK)
return TRUE; // cancelled dialog
CPlugin * pSavedPlugin = m_CurrentPlugin;
m_CurrentPlugin = NULL; // make sure we save main triggers etc.
try
{
f = new CFile (filedlg.GetPathName (),
CFile::modeCreate | CFile::modeReadWrite);
ar = new CArchive(f, CArchive::store);
switch (set_type)
{
case TRIGGER: Save_World_XML (*ar, XML_TRIGGERS); break;
case ALIAS: Save_World_XML (*ar, XML_ALIASES); break;
case COLOUR: Save_World_XML (*ar, XML_COLOURS); break;
case MACRO: Save_World_XML (*ar, XML_MACROS); break;
case TIMER: Save_World_XML (*ar, XML_TIMERS); break;
} // end of switch
error = FALSE;
} // end of try block
catch (CFileException * e)
{
::TMessageBox ("Unable to create the requested file", MB_ICONEXCLAMATION);
e->Delete ();
} // end of catching a file exception
catch (CMemoryException * e)
{
::TMessageBox ("Insufficient memory to do this operation", MB_ICONEXCLAMATION);
e->Delete ();
} // end of catching a memory exception
catch (CArchiveException * e)
{
::TMessageBox ("There was a problem in the data format", MB_ICONEXCLAMATION);
e->Delete ();
} // end of catching an archive exception
m_CurrentPlugin = pSavedPlugin;
delete ar; // delete archive
delete f; // delete file
return error; // OK return
} // end of CMUSHclientDoc::save_set
bool CMUSHclientDoc::ProcessOneAliasSequence (const CString strCurrentLine,
const bool bCountThem,
bool & bOmitFromLog,
bool & bEchoAlias,
CAliasList & AliasList,
OneShotItemMap & mapOneShotItems)
{
for (int iAlias = 0; iAlias < GetAliasArray ().GetSize (); iAlias++)
{
CAlias * alias_item = GetAliasArray () [iAlias];
// ignore non-enabled aliases
if (!alias_item->bEnabled)
continue;
m_iAliasesEvaluatedCount++;
BOOL bMatched;
// empty wildcards now
for (int i = 0; i < MAX_WILDCARDS; i++)
alias_item->wildcards [i] = "";
CString strTarget = strCurrentLine;
try
{
bMatched = regexec (alias_item->regexp, strTarget);
}
catch(CException* e)
{
e->ReportError ();
e->Delete ();
bMatched = false;
}
if (!bMatched) // no match, try next one
continue;
m_iAliasesMatchedCount++;
m_iAliasesMatchedThisSessionCount++;
if (alias_item->bOneShot)
mapOneShotItems.push_back (
OneShotItem (m_CurrentPlugin,
(const char *) alias_item->strInternalName));
// if alias wants it, omit entire typed line from command history
if (alias_item->bOmitFromCommandHistory)
m_bOmitFromCommandHistory = true;
alias_item->wildcards.clear ();
for (int iWildcard = 0;
iWildcard < MAX_WILDCARDS;
iWildcard++)
alias_item->wildcards.push_back
(
FixWildcard (alias_item->regexp->GetWildcard (iWildcard),
false,
alias_item->iSendTo,
m_strLanguage)
);
// If current line is not a note line, force a line change (by displaying
// an empty string), so that the style change is on the note line and not
// the back of the previous line. This was added to stop an alias, which calls
// a Lua script which does outputting, failing because during outputting it
// terminated the previous line in the middle of a script.
if (m_pCurrentLine && (m_pCurrentLine->flags & NOTE_OR_COMMAND) != COMMENT)
DisplayMsg ("", 0, COMMENT);
// echo the alias they typed, unless command echo off, or previously displayed
// (if wanted - v3.38)
if (bEchoAlias && // not already done
alias_item->bEchoAlias) // alias wants to be echoed
{
DisplayMsg (strCurrentLine + ENDLINE,
strCurrentLine.GetLength () + strlen (ENDLINE),
USER_INPUT | (LoggingInput () ? LOG_LINE : 0));
bEchoAlias = false; // don't echo the same line twice
// and log the command the actually typed
if (LoggingInput ())
LogCommand (strCurrentLine);
}
if (bCountThem)
alias_item->nMatched++; // count alias matches
bOmitFromLog = alias_item->bOmitFromLog;
alias_item->tWhenMatched = CTime::GetCurrentTime(); // when it matched
if (alias_item->strLabel.IsEmpty ())
Trace ("Matched alias \"%s\"", (LPCTSTR) alias_item->name);
else
Trace ("Matched alias %s", (LPCTSTR) alias_item->strLabel);
// get unlabelled alias's internal name
const char * pLabel = alias_item->strLabel;
if (pLabel [0] == 0)
pLabel = GetAliasRevMap () [alias_item].c_str ();
// if we have to do parameter substitution on the alias, do it now
CString strSendText;
// copy contents to strSendText area, replacing %1, %2 etc. with appropriate contents
try
{
strSendText = FixSendText (::FixupEscapeSequences (alias_item->contents),
alias_item->iSendTo, // where it is going
alias_item->regexp, // regexp
GetLanguage (), // eg. vbscript
false, // lower-case wildcards
alias_item->bExpandVariables, // expand variables
true, // expand wildcards
false, // convert regexps
false, // is it regexp or normal?
true, // throw exceptions
pLabel);
}
catch (CException* e)
{
e->ReportError();
e->Delete();
return true;
}
AliasList.AddTail (alias_item); // add to list of aliases
CString strExtraOutput;
// let them know if they are foolishly trying to send to a closed connection
// - only applies to commands that actually send to the world
if (!strSendText.IsEmpty ())
switch (alias_item->iSendTo)
{
case eSendToWorld:
case eSendToCommandQueue:
case eSendToSpeedwalk:
case eSendImmediate:
if (CheckConnected ())
return true;
break;
}
alias_item->bExecutingScript = true; // cannot be deleted now
SendTo (alias_item->iSendTo,
strSendText,
alias_item->bOmitFromOutput,
alias_item->bOmitFromLog,
TFormat ("Alias: %s", (LPCTSTR) alias_item->strLabel),
alias_item->strVariable,
strExtraOutput);
alias_item->bExecutingScript = false; // can be deleted now
// display any stuff sent to output window
if (!strExtraOutput.IsEmpty ())
DisplayMsg (strExtraOutput, strExtraOutput.GetLength (), COMMENT);
// only re-match if they want multiple matches
if (!alias_item->bKeepEvaluating)
break;
} // end of looping, checking each alias
return FALSE;
} // end of CMUSHclientDoc::ProcessOneAliasSequence
bool CMUSHclientDoc::ExecuteAliasScript (CAlias * alias_item,
const CString strCurrentLine)
{
if (CheckScriptingAvailable ("Alias", alias_item->dispid, alias_item->strProcedure))
return false;
if (alias_item->dispid != DISPID_UNKNOWN) // if we have a dispatch id
{