forked from Mudlet/Mudlet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHost.cpp
More file actions
1133 lines (1037 loc) · 37.1 KB
/
Host.cpp
File metadata and controls
1133 lines (1037 loc) · 37.1 KB
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
/***************************************************************************
* Copyright (C) 2008-2013 by Heiko Koehn - KoehnHeiko@googlemail.com *
* Copyright (C) 2014 by Ahmed Charles - acharles@outlook.com *
* Copyright (C) 2015-2017 by Stephen Lyons - slysven@virginmedia.com *
* Copyright (C) 2016 by Ian Adkins - ieadkins@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "Host.h"
#include "LuaInterface.h"
#include "TConsole.h"
#include "TEvent.h"
#include "TMap.h"
#include "TRoomDB.h"
#include "TScript.h"
#include "XMLexport.h"
#include "XMLimport.h"
#include "dlgTriggerEditor.h"
#include "mudlet.h"
#include "pre_guard.h"
#include <QtUiTools>
#include <QApplication>
#include <QDir>
#include <QMessageBox>
#include <QStringBuilder>
#include "post_guard.h"
#include <errno.h>
#include <zip.h>
Host::Host(int port, const QString& hostname, const QString& login, const QString& pass, int id)
: mTelnet(this)
, mpConsole(0)
, mLuaInterpreter(this, id)
, mTriggerUnit(this)
, mTimerUnit(this)
, mScriptUnit(this)
, mAliasUnit(this)
, mActionUnit(this)
, mKeyUnit(this)
, commandLineMinimumHeight(30)
, mAlertOnNewData(true)
, mAllowToSendCommand(true)
, mAutoClearCommandLineAfterSend(false)
, mBlockScriptCompile(true)
, mEchoLuaErrors(false)
, mBorderBottomHeight(0)
, mBorderLeftWidth(0)
, mBorderRightWidth(0)
, mBorderTopHeight(0)
, mCodeCompletion(true)
, mCommandLineFont(QFont("Bitstream Vera Sans Mono", 10, QFont::Normal))
, mCommandSeparator(QString(";"))
, mDisableAutoCompletion(false)
, mDisplayFont(QFont("Bitstream Vera Sans Mono", 10, QFont::Normal))
, mEnableGMCP(true)
, mEnableMSDP(false)
, mFORCE_GA_OFF(false)
, mFORCE_NO_COMPRESSION(false)
, mFORCE_SAVE_ON_EXIT(false)
, mHostID(id)
, mHostName(hostname)
, mInsertedMissingLF(false)
, mIsGoingDown(false)
, mLF_ON_GA(true)
, mLogin(login)
, mMainIconSize(3)
, mNoAntiAlias(false)
, mPass(pass)
, mpEditorDialog(0)
, mpMap(new TMap(this))
, mpNotePad(0)
, mPort(port)
, mPrintCommand(true)
, mIsCurrentLogFileInHtmlFormat(false)
, mIsLoggingTimestamps(false)
, mResetProfile(false)
, mRetries(5)
, mSaveProfileOnExit(false)
, mScreenHeight(25)
, mScreenWidth(90)
, mTEFolderIconSize(3)
, mTimeout(60)
, mUSE_FORCE_LF_AFTER_PROMPT(false)
, mUSE_IRE_DRIVER_BUGFIX(true)
, mUSE_UNIX_EOL(false)
, mWrapAt(100)
, mWrapIndentCount(0)
, mBlack(Qt::black)
, mLightBlack(Qt::darkGray)
, mRed(Qt::darkRed)
, mLightRed(Qt::red)
, mLightGreen(Qt::green)
, mGreen(Qt::darkGreen)
, mLightBlue(Qt::blue)
, mBlue(Qt::darkBlue)
, mLightYellow(Qt::yellow)
, mYellow(Qt::darkYellow)
, mLightCyan(Qt::cyan)
, mCyan(Qt::darkCyan)
, mLightMagenta(Qt::magenta)
, mMagenta(Qt::darkMagenta)
, mLightWhite(Qt::white)
, mWhite(Qt::lightGray)
, mFgColor(Qt::lightGray)
, mBgColor(Qt::black)
, mCommandBgColor(Qt::black)
, mCommandFgColor(QColor(113, 113, 0))
, mBlack_2(Qt::black)
, mLightBlack_2(Qt::darkGray)
, mRed_2(Qt::darkRed)
, mLightRed_2(Qt::red)
, mLightGreen_2(Qt::green)
, mGreen_2(Qt::darkGreen)
, mLightBlue_2(Qt::blue)
, mBlue_2(Qt::darkBlue)
, mLightYellow_2(Qt::yellow)
, mYellow_2(Qt::darkYellow)
, mLightCyan_2(Qt::cyan)
, mCyan_2(Qt::darkCyan)
, mLightMagenta_2(Qt::magenta)
, mMagenta_2(Qt::darkMagenta)
, mLightWhite_2(Qt::white)
, mWhite_2(Qt::lightGray)
, mFgColor_2(Qt::lightGray)
, mBgColor_2(Qt::black)
, mSpellDic("en_US")
, mLogStatus(false)
, mEnableSpellCheck(true)
, mModuleSaveBlock(false)
, mLineSize(10.0)
, mRoomSize(0.5)
, mBubbleMode(false)
, mShowRoomID(false)
, mMapperUseAntiAlias(true)
, mServerGUI_Package_version(-1)
, mServerGUI_Package_name("nothing")
, mAcceptServerGUI(true)
, mCommandLineFgColor(Qt::darkGray)
, mCommandLineBgColor(Qt::black)
, mFORCE_MXP_NEGOTIATION_OFF(false)
, mpDockableMapWidget()
, mHaveMapperScript(false)
, mEditorTheme("Mudlet")
, mEditorThemeFile("Mudlet.tmTheme")
, mThemePreviewItemID(-1)
, mThemePreviewType(QString())
{
// mLogStatus = mudlet::self()->mAutolog;
mLuaInterface.reset(new LuaInterface(this));
QString directoryLogFile = QDir::homePath() + "/.config/mudlet/profiles/";
directoryLogFile.append(mHostName);
directoryLogFile.append("/log");
QString logFileName = directoryLogFile + "/errors.txt";
QDir dirLogFile;
if (!dirLogFile.exists(directoryLogFile)) {
dirLogFile.mkpath(directoryLogFile);
}
mErrorLogFile.setFileName(logFileName);
mErrorLogFile.open(QIODevice::Append);
// This is NOW used (for map
// file auditing and other issues)
mErrorLogStream.setDevice(&mErrorLogFile);
QTimer::singleShot(0, [this]() {
if (mpMap->restore(QString(), false)) {
mpMap->audit();
}
});
mMapStrongHighlight = false;
mGMCP_merge_table_keys.append("Char.Status");
mDoubleClickIgnore.insert('"');
mDoubleClickIgnore.insert('\'');
}
Host::~Host()
{
if (mpDockableMapWidget) {
mpDockableMapWidget->deleteLater();
}
mIsGoingDown = true;
mIsClosingDown = true;
mTelnet.disconnect();
mErrorLogStream.flush();
mErrorLogFile.close();
}
void Host::saveModules(int sync)
{
if (mModuleSaveBlock) {
//FIXME: This should generate an error to the user
return;
}
QMapIterator<QString, QStringList> it(modulesToWrite);
QStringList modulesToSync;
QString dirName = QDir::homePath() + "/.config/mudlet/moduleBackups/";
QDir savePath = QDir(dirName);
if (!savePath.exists()) {
savePath.mkpath(dirName);
}
while (it.hasNext()) {
it.next();
QStringList entry = it.value();
QString filename_xml = entry[0];
QString time = QDateTime::currentDateTime().toString("dd-MM-yyyy#hh-mm-ss");
QString moduleName = it.key();
QString tempDir;
QString zipName;
zip* zipFile = 0;
// Filename extension tests should be case insensitive to work on MacOS Platforms...! - Slysven
if (filename_xml.endsWith(QStringLiteral("mpackage"), Qt::CaseInsensitive) || filename_xml.endsWith(QStringLiteral("zip"), Qt::CaseInsensitive)) {
tempDir = QDir::homePath() + "/.config/mudlet/profiles/" + mHostName + "/" + moduleName;
filename_xml = tempDir + "/" + moduleName + ".xml";
int err;
zipFile = zip_open(entry[0].toStdString().c_str(), 0, &err);
zipName = filename_xml;
QDir packageDir = QDir(tempDir);
if (!packageDir.exists()) {
packageDir.mkpath(tempDir);
}
} else {
savePath.rename(filename_xml, dirName + moduleName + time); //move the old file, use the key (module name) as the file
}
QFile file_xml(filename_xml);
if (file_xml.open(QIODevice::WriteOnly)) {
XMLexport writer(this);
writer.writeModuleXML(&file_xml, it.key());
file_xml.close();
if (entry[1].toInt()) {
modulesToSync << it.key();
}
} else {
file_xml.close();
//FIXME: Should have an error reported to user
//qDebug()<<"failed to write xml for module:"<<entry[0]<<", check permissions?";
mModuleSaveBlock = true;
return;
}
if (!zipName.isEmpty()) {
struct zip_source* s = zip_source_file(zipFile, filename_xml.toStdString().c_str(), 0, 0);
QTime t;
t.start();
// int err = zip_file_add( zipFile, QString(moduleName+".xml").toStdString().c_str(), s, ZIP_FL_OVERWRITE );
int err = zip_add(zipFile, QString(moduleName + ".xml").toStdString().c_str(), s);
//FIXME: error checking
if (zipFile) {
err = zip_close(zipFile);
}
//FIXME: error checking
}
}
modulesToWrite.clear();
if (sync) {
//synchronize modules across sessions
QMap<Host*, TConsole*> activeSessions = mudlet::self()->mConsoleMap;
QMapIterator<Host*, TConsole*> it2(activeSessions);
while (it2.hasNext()) {
it2.next();
Host* host = it2.key();
if (host->mHostName == mHostName) {
continue;
}
QMap<QString, QStringList> installedModules = host->mInstalledModules;
QMap<QString, int> modulePri = host->mModulePriorities;
QMapIterator<QString, int> it3(modulePri);
QMap<int, QStringList> moduleOrder;
while (it3.hasNext()) {
it3.next();
//QStringList moduleEntry = moduleOrder[it3.value()];
//moduleEntry.append(it3.key());
moduleOrder[it3.value()].append(it3.key()); // = moduleEntry;
}
QMapIterator<int, QStringList> it4(moduleOrder);
while (it4.hasNext()) {
it4.next();
QStringList moduleList = it4.value();
for (int i = 0; i < moduleList.size(); i++) {
QString moduleName = moduleList[i];
if (modulesToSync.contains(moduleName)) {
host->reloadModule(moduleName);
}
}
}
}
}
}
void Host::reloadModule(const QString& moduleName)
{
QMap<QString, QStringList> installedModules = mInstalledModules;
QMapIterator<QString, QStringList> it(installedModules);
while (it.hasNext()) {
it.next();
QStringList entry = it.value();
if (it.key() == moduleName) {
uninstallPackage(it.key(), 2);
installPackage(entry[0], 2);
}
}
//iterate through mInstalledModules again and reset the entry flag to be correct.
//both the installedModules and mInstalled should be in the same order now as well
QMapIterator<QString, QStringList> it2(mInstalledModules);
while (it2.hasNext()) {
it2.next();
QStringList entry = installedModules[it2.key()];
mInstalledModules[it2.key()] = entry;
}
}
void Host::resetProfile()
{
getTimerUnit()->stopAllTriggers();
mudlet::self()->mTimerMap.clear();
getTimerUnit()->removeAllTempTimers();
getTriggerUnit()->removeAllTempTriggers();
mTimerUnit.doCleanup();
mTriggerUnit.doCleanup();
mpConsole->resetMainConsole();
mEventHandlerMap.clear();
mEventMap.clear();
mLuaInterpreter.initLuaGlobals();
mLuaInterpreter.loadGlobal();
mBlockScriptCompile = false;
getTriggerUnit()->compileAll();
getAliasUnit()->compileAll();
getActionUnit()->compileAll();
getKeyUnit()->compileAll();
getScriptUnit()->compileAll();
//getTimerUnit()->compileAll();
mResetProfile = false;
mTimerUnit.reenableAllTriggers();
TEvent event;
event.mArgumentList.append(QLatin1String("sysLoadEvent"));
event.mArgumentTypeList.append(ARGUMENT_TYPE_STRING);
raiseEvent(event);
qDebug() << "resetProfile() DONE";
}
// Saves profile to disk - does not save items dirty in the editor, however.
// takes a directory to save in or an empty string for the default location
// as well as a boolean whenever to sync the modules or not
// returns true+filepath if successful or false+error message otherwise
std::tuple<bool, QString, QString> Host::saveProfile(const QString& saveLocation, bool syncModules)
{
QString directory_xml;
if (saveLocation.isEmpty()) {
directory_xml = QStringLiteral("%1/.config/mudlet/profiles/%2/current").arg(QDir::homePath(), getName());
} else {
directory_xml = saveLocation;
}
QString filename_xml = QStringLiteral("%1/%2.xml").arg(directory_xml, QDateTime::currentDateTime().toString("dd-MM-yyyy#hh-mm-ss"));
QDir dir_xml;
if (!dir_xml.exists(directory_xml)) {
dir_xml.mkpath(directory_xml);
}
QFile file_xml(filename_xml);
if (file_xml.open(QIODevice::WriteOnly)) {
XMLexport writer(this);
writer.exportHost(&file_xml);
file_xml.close();
saveModules(syncModules ? 1 : 0);
return std::make_tuple(true, filename_xml, QString());
} else {
return std::make_tuple(false, filename_xml, file_xml.errorString());
}
}
// Now returns the total weight of the path
const unsigned int Host::assemblePath()
{
unsigned int totalWeight = 0;
QStringList pathList;
for (int i : mpMap->mPathList) {
QString n = QString::number(i);
pathList.append(n);
}
QStringList directionList = mpMap->mDirList;
QStringList weightList;
for (int stepWeight : mpMap->mWeightList) {
totalWeight += stepWeight;
QString n = QString::number(stepWeight);
weightList.append(n);
}
QString tableName = QStringLiteral("speedWalkPath");
mLuaInterpreter.set_lua_table(tableName, pathList);
tableName = QStringLiteral("speedWalkDir");
mLuaInterpreter.set_lua_table(tableName, directionList);
tableName = QStringLiteral("speedWalkWeight");
mLuaInterpreter.set_lua_table(tableName, weightList);
return totalWeight;
}
const bool Host::checkForMappingScript()
{
// the mapper script reminder is only shown once
// because it is too difficult and error prone (->proper script sequence)
// to disable this message
bool ret = (mLuaInterpreter.check_for_mappingscript() || mHaveMapperScript);
mHaveMapperScript = true;
return ret;
}
void Host::startSpeedWalk()
{
int totalWeight = assemblePath();
Q_UNUSED(totalWeight);
QString f = QStringLiteral("doSpeedWalk");
QString n = QString();
mLuaInterpreter.call(f, n);
}
void Host::adjustNAWS()
{
mTelnet.setDisplayDimensions();
}
void Host::setReplacementCommand(const QString& s)
{
mReplacementCommand = s;
}
void Host::stopAllTriggers()
{
mTriggerUnit.stopAllTriggers();
mAliasUnit.stopAllTriggers();
mTimerUnit.stopAllTriggers();
}
void Host::reenableAllTriggers()
{
mTriggerUnit.reenableAllTriggers();
mAliasUnit.reenableAllTriggers();
mTimerUnit.reenableAllTriggers();
}
void Host::send(QString cmd, bool wantPrint, bool dontExpandAliases)
{
if (wantPrint && mPrintCommand) {
mInsertedMissingLF = true;
if ((cmd == "") && (mUSE_IRE_DRIVER_BUGFIX) && (!mUSE_FORCE_LF_AFTER_PROMPT)) {
;
} else {
// used to print the terminal <LF> that terminates a telnet command
// this is important to get the cursor position right
mpConsole->printCommand(cmd);
}
mpConsole->update();
}
QStringList commandList = cmd.split(QString(mCommandSeparator), QString::SkipEmptyParts);
if (!dontExpandAliases) {
if (commandList.size() == 0) {
sendRaw("\n"); //NOTE: damit leerprompt moeglich sind
return;
}
}
for (int i = 0; i < commandList.size(); i++) {
if (commandList[i].size() < 1) {
continue;
}
QString command = commandList[i];
command.remove(QChar::LineFeed);
mReplacementCommand = "";
if (dontExpandAliases) {
mTelnet.sendData(command);
continue;
}
if (!mAliasUnit.processDataStream(command)) {
if (mReplacementCommand.size() > 0) {
mTelnet.sendData(mReplacementCommand);
} else {
mTelnet.sendData(command);
}
}
}
}
void Host::sendRaw(QString command)
{
mTelnet.sendData(command);
}
int Host::createStopWatch()
{
int newWatchID = mStopWatchMap.size() + 1;
mStopWatchMap[newWatchID] = QTime(0, 0, 0, 0);
return newWatchID;
}
double Host::getStopWatchTime(int watchID)
{
if (mStopWatchMap.contains(watchID)) {
return static_cast<double>(mStopWatchMap[watchID].elapsed()) / 1000;
} else {
return -1.0;
}
}
bool Host::startStopWatch(int watchID)
{
if (mStopWatchMap.contains(watchID)) {
mStopWatchMap[watchID].start();
return true;
} else {
return false;
}
}
double Host::stopStopWatch(int watchID)
{
if (mStopWatchMap.contains(watchID)) {
return static_cast<double>(mStopWatchMap[watchID].elapsed()) / 1000;
} else {
return -1.0;
}
}
bool Host::resetStopWatch(int watchID)
{
if (mStopWatchMap.contains(watchID)) {
mStopWatchMap[watchID].setHMS(0, 0, 0, 0);
return true;
} else {
return false;
}
}
void Host::callEventHandlers()
{
}
void Host::incomingStreamProcessor(const QString& data, int line)
{
mTriggerUnit.processDataStream(data, line);
mTimerUnit.doCleanup();
if (mResetProfile) {
resetProfile();
}
}
void Host::registerEventHandler(const QString& name, TScript* pScript)
{
if (mEventHandlerMap.contains(name)) {
if (!mEventHandlerMap[name].contains(pScript)) {
mEventHandlerMap[name].append(pScript);
}
} else {
QList<TScript*> scriptList;
scriptList.append(pScript);
mEventHandlerMap.insert(name, scriptList);
}
}
void Host::registerAnonymousEventHandler(const QString& name, const QString& fun)
{
if (mAnonymousEventHandlerFunctions.contains(name)) {
if (!mAnonymousEventHandlerFunctions[name].contains(fun)) {
mAnonymousEventHandlerFunctions[name].push_back(fun);
}
} else {
QStringList newList;
newList << fun;
mAnonymousEventHandlerFunctions[name] = newList;
}
}
void Host::unregisterEventHandler(const QString& name, TScript* pScript)
{
if (mEventHandlerMap.contains(name)) {
mEventHandlerMap[name].removeAll(pScript);
}
}
void Host::raiseEvent(const TEvent& pE)
{
if (pE.mArgumentList.isEmpty()) {
return;
}
if (mEventHandlerMap.contains(pE.mArgumentList.at(0))) {
QList<TScript*> scriptList = mEventHandlerMap.value(pE.mArgumentList.at(0));
for (auto& script : scriptList) {
script->callEventHandler(pE);
}
}
if (mAnonymousEventHandlerFunctions.contains(pE.mArgumentList.at(0))) {
QStringList functionsList = mAnonymousEventHandlerFunctions.value(pE.mArgumentList.at(0));
for (int i = 0, total = functionsList.size(); i < total; ++i) {
mLuaInterpreter.callEventHandler(functionsList.at(i), pE);
}
}
}
void Host::postIrcMessage(const QString& a, const QString& b, const QString& c)
{
TEvent event;
event.mArgumentList << QLatin1String("sysIrcMessage");
event.mArgumentList << a << b << c;
event.mArgumentTypeList << ARGUMENT_TYPE_STRING << ARGUMENT_TYPE_STRING << ARGUMENT_TYPE_STRING << ARGUMENT_TYPE_STRING;
raiseEvent(event);
}
void Host::enableTimer(const QString& name)
{
mTimerUnit.enableTimer(name);
}
void Host::disableTimer(const QString& name)
{
mTimerUnit.disableTimer(name);
}
bool Host::killTimer(const QString& name)
{
return mTimerUnit.killTimer(name);
}
void Host::enableKey(const QString& name)
{
mKeyUnit.enableKey(name);
}
void Host::disableKey(const QString& name)
{
mKeyUnit.disableKey(name);
}
void Host::enableTrigger(const QString& name)
{
mTriggerUnit.enableTrigger(name);
}
void Host::disableTrigger(const QString& name)
{
mTriggerUnit.disableTrigger(name);
}
bool Host::killTrigger(const QString& name)
{
return mTriggerUnit.killTrigger(name);
}
void Host::connectToServer()
{
mTelnet.connectIt(mUrl, mPort);
}
void Host::closingDown()
{
QMutexLocker locker(&mLock);
mIsClosingDown = true;
}
bool Host::isClosingDown()
{
QMutexLocker locker(&mLock);
return mIsClosingDown;
}
bool Host::installPackage(const QString& fileName, int module)
{
// As the pointed to dialog is only used now WITHIN this method and this
// method can be re-entered, it is best to use a local rather than a class
// pointer just in case we accidently reenter this method in the future.
QDialog* pUnzipDialog = Q_NULLPTR;
// Module notes:
// For the module install, a module flag of 0 is a package, a flag
// of 1 means the module is being installed for the first time via
// the UI, a flag of 2 means the module is being synced (so it's "installed"
// already), a flag of 3 means the module is being installed from
// a script. This separation is necessary to be able to reuse code
// while avoiding infinite loops from script installations.
if (fileName.isEmpty()) {
return false;
}
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
return false;
}
QString packageName = fileName.section(QStringLiteral("/"), -1);
packageName.remove(QStringLiteral(".trigger"), Qt::CaseInsensitive);
packageName.remove(QStringLiteral(".xml"), Qt::CaseInsensitive);
packageName.remove(QStringLiteral(".zip"), Qt::CaseInsensitive);
packageName.remove(QStringLiteral(".mpackage"), Qt::CaseInsensitive);
packageName.remove(QLatin1Char('\\'));
packageName.remove(QLatin1Char('.'));
if (module) {
if ((module == 2) && (mActiveModules.contains(packageName))) {
uninstallPackage(packageName, 2);
} else if ((module == 3) && (mActiveModules.contains(packageName))) {
return false; //we're already installed
}
} else {
if (mInstalledPackages.contains(packageName)) {
return false;
}
}
//the extra module check is needed here to prevent infinite loops from script loaded modules
if (mpEditorDialog && module != 3) {
mpEditorDialog->doCleanReset();
}
QFile file2;
if (fileName.endsWith(QStringLiteral(".zip"), Qt::CaseInsensitive) || fileName.endsWith(QStringLiteral(".mpackage"), Qt::CaseInsensitive)) {
QString _home = QStringLiteral("%1/.config/mudlet/profiles/%2").arg(QDir::homePath(), getName());
QString _dest = QStringLiteral("%1/%2/").arg(_home, packageName);
QDir _tmpDir(_home); // home directory for the PROFILE
_tmpDir.mkpath(_dest);
// TODO: report failure to create destination folder for package/module in profile
QUiLoader loader(this);
QFile uiFile(QStringLiteral(":/ui/package_manager_unpack.ui"));
uiFile.open(QFile::ReadOnly);
pUnzipDialog = dynamic_cast<QDialog*>(loader.load(&uiFile, 0));
uiFile.close();
if (!pUnzipDialog) {
return false;
}
QLabel* pLabel = pUnzipDialog->findChild<QLabel*>(QStringLiteral("label"));
if (pLabel) {
if (module) {
pLabel->setText(tr("Unpacking module:\n\"%1\"\nplease wait...").arg(packageName));
} else {
pLabel->setText(tr("Unpacking package:\n\"%1\"\nplease wait...").arg(packageName));
}
}
pUnzipDialog->hide(); // Must hide to change WindowModality
pUnzipDialog->setWindowTitle(tr("Unpacking"));
pUnzipDialog->setWindowModality(Qt::ApplicationModal);
pUnzipDialog->show();
qApp->processEvents();
pUnzipDialog->raise();
pUnzipDialog->repaint(); // Force a redraw
qApp->processEvents(); // Try to ensure we are on top of any other dialogs and freshly drawn
auto successful = mudlet::unzip(fileName, _dest, _tmpDir);
pUnzipDialog->deleteLater();
pUnzipDialog = Q_NULLPTR;
if (!successful) {
return false;
}
// requirements for zip packages:
// - packages must be compressed in zip format
// - file extension should be .mpackage (though .zip is accepted)
// - there can only be a single xml file per package
// - the xml file must be located in the root directory of the zip package. example: myPack.zip contains: the folder images and the file myPack.xml
QDir _dir(_dest);
// before we start importing xmls in, see if the config.lua manifest file exists
// - if it does, update the packageName from it
if (_dir.exists(QStringLiteral("config.lua"))) {
// read in the new packageName from Lua. Should be expanded in future to whatever else config.lua will have
readPackageConfig(_dir.absoluteFilePath(QStringLiteral("config.lua")), packageName);
// now that the packageName changed, redo relevant checks to make sure it's still valid
if (module) {
if (mActiveModules.contains(packageName)) {
uninstallPackage(packageName, 2);
}
} else {
if (mInstalledPackages.contains(packageName)) {
// cleanup and quit if already installed
removeDir(_dir.absolutePath(), _dir.absolutePath());
return false;
}
}
// continuing, so update the folder name on disk
QString newpath(QStringLiteral("%1/%2/").arg(_home, packageName));
_dir.rename(_dir.absolutePath(), newpath);
_dir = QDir(newpath);
}
QStringList _filterList;
_filterList << QStringLiteral("*.xml") << QStringLiteral("*.trigger");
QFileInfoList entries = _dir.entryInfoList(_filterList, QDir::Files);
for (auto& entry : entries) {
file2.setFileName(entry.absoluteFilePath());
file2.open(QFile::ReadOnly | QFile::Text);
QString profileName = getName();
QString login = getLogin();
QString pass = getPass();
XMLimport reader(this);
if (module) {
QStringList moduleEntry;
moduleEntry << fileName;
moduleEntry << QStringLiteral("0");
mInstalledModules[packageName] = moduleEntry;
mActiveModules.append(packageName);
} else {
mInstalledPackages.append(packageName);
}
reader.importPackage(&file2, packageName, module); // TODO: Missing false return value handler
setName(profileName);
setLogin(login);
setPass(pass);
file2.close();
}
} else {
file2.setFileName(fileName);
file2.open(QFile::ReadOnly | QFile::Text);
//mInstalledPackages.append( packageName );
QString profileName = getName();
QString login = getLogin();
QString pass = getPass();
XMLimport reader(this);
if (module) {
QStringList moduleEntry;
moduleEntry << fileName;
moduleEntry << QStringLiteral("0");
mInstalledModules[packageName] = moduleEntry;
mActiveModules.append(packageName);
} else {
mInstalledPackages.append(packageName);
}
reader.importPackage(&file2, packageName, module); // TODO: Missing false return value handler
setName(profileName);
setLogin(login);
setPass(pass);
file2.close();
}
if (mpEditorDialog) {
mpEditorDialog->doCleanReset();
}
if (!module) {
saveProfile();
}
// reorder permanent and temporary triggers: perm first, temp second
mTriggerUnit.reorderTriggersAfterPackageImport();
// raise 2 events - a generic one and a more detailed one to serve both
// a simple need ("I just want the install event") and a more specific need
// ("I specifically need to know when the module was synced")
TEvent genericInstallEvent;
genericInstallEvent.mArgumentList.append(QLatin1String("sysInstall"));
genericInstallEvent.mArgumentTypeList.append(ARGUMENT_TYPE_STRING);
genericInstallEvent.mArgumentList.append(packageName);
genericInstallEvent.mArgumentTypeList.append(ARGUMENT_TYPE_STRING);
raiseEvent(genericInstallEvent);
TEvent detailedInstallEvent;
switch (module) {
case 0:
detailedInstallEvent.mArgumentList.append(QLatin1String("sysInstallPackage"));
break;
case 1:
detailedInstallEvent.mArgumentList.append(QLatin1String("sysInstallModule"));
break;
case 2:
detailedInstallEvent.mArgumentList.append(QLatin1String("sysSyncInstallModule"));
break;
case 3:
detailedInstallEvent.mArgumentList.append(QLatin1String("sysLuaInstallModule"));
break;
default:
Q_UNREACHABLE();
}
detailedInstallEvent.mArgumentTypeList.append(ARGUMENT_TYPE_STRING);
detailedInstallEvent.mArgumentList.append(packageName);
detailedInstallEvent.mArgumentTypeList.append(ARGUMENT_TYPE_STRING);
detailedInstallEvent.mArgumentList.append(fileName);
detailedInstallEvent.mArgumentTypeList.append(ARGUMENT_TYPE_STRING);
raiseEvent(detailedInstallEvent);
return true;
}
// credit: http://john.nachtimwald.com/2010/06/08/qt-remove-directory-and-its-contents/
bool Host::removeDir(const QString& dirName, const QString& originalPath)
{
bool result = true;
QDir dir(dirName);
if (dir.exists(dirName)) {
Q_FOREACH (QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
// prevent recursion outside of the original branch
if (info.isDir() && info.absoluteFilePath().startsWith(originalPath)) {
result = removeDir(info.absoluteFilePath(), originalPath);
} else {
result = QFile::remove(info.absoluteFilePath());
}
if (!result) {
return result;
}
}
result = dir.rmdir(dirName);
}
return result;
}
// This may be called by installPackage(...) in that case however it will have
// module == 2 and in THAT situation it will NOT RE-invoke installPackage(...)
// again - Slysven
bool Host::uninstallPackage(const QString& packageName, int module)
{
// As with the installPackage, the module codes are:
// 0=package, 1=uninstall from dialog, 2=uninstall due to module syncing,
// 3=uninstall from a script
if (module) {
if (!mInstalledModules.contains(packageName)) {
return false;
}
} else {
if (!mInstalledPackages.contains(packageName)) {
return false;
}
}
// raise 2 events - a generic one and a more detailed one to serve both
// a simple need ("I just want the uninstall event") and a more specific need
// ("I specifically need to know when the module was uninstalled via Lua")
TEvent genericUninstallEvent;
genericUninstallEvent.mArgumentList.append(QLatin1String("sysUninstall"));
genericUninstallEvent.mArgumentTypeList.append(ARGUMENT_TYPE_STRING);
genericUninstallEvent.mArgumentList.append(packageName);
genericUninstallEvent.mArgumentTypeList.append(ARGUMENT_TYPE_STRING);
raiseEvent(genericUninstallEvent);
TEvent detailedUninstallEvent;
switch (module) {
case 0:
detailedUninstallEvent.mArgumentList.append(QLatin1String("sysUninstallPackage"));
break;
case 1:
detailedUninstallEvent.mArgumentList.append(QLatin1String("sysUninstallModule"));
break;
case 2:
detailedUninstallEvent.mArgumentList.append(QLatin1String("sysSyncUninstallModule"));
break;
case 3:
detailedUninstallEvent.mArgumentList.append(QLatin1String("sysLuaUninstallModule"));
break;
default:
Q_UNREACHABLE();
}
detailedUninstallEvent.mArgumentTypeList.append(ARGUMENT_TYPE_STRING);
detailedUninstallEvent.mArgumentList.append(packageName);
detailedUninstallEvent.mArgumentTypeList.append(ARGUMENT_TYPE_STRING);
raiseEvent(detailedUninstallEvent);
int dualInstallations = 0;
if (mInstalledModules.contains(packageName) && mInstalledPackages.contains(packageName)) {
dualInstallations = 1;
}
//we check for the module=3 because if we reset the editor, we will re-execute the
//module uninstall, thus creating an infinite loop.
if (mpEditorDialog && module != 3) {
mpEditorDialog->doCleanReset();
}
mTriggerUnit.uninstall(packageName);
mTimerUnit.uninstall(packageName);
mAliasUnit.uninstall(packageName);
mActionUnit.uninstall(packageName);
mScriptUnit.uninstall(packageName);
mKeyUnit.uninstall(packageName);
if (module) {
//if module == 2, this is a temporary uninstall for reloading so we exit here
QStringList entry = mInstalledModules[packageName];
mInstalledModules.remove(packageName);
mActiveModules.removeAll(packageName);
if (module == 2) {
return true;
}
//if module == 1/3, we actually uninstall it.