-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcalladmin-client.cpp
More file actions
1110 lines (821 loc) · 20.2 KB
/
calladmin-client.cpp
File metadata and controls
1110 lines (821 loc) · 20.2 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
/**
* -----------------------------------------------------
* File calladmin-client.cpp
* Authors David Ordnung, Impact
* License GPLv3
* Web http://dordnung.de, http://gugyclan.eu
* -----------------------------------------------------
*
* Copyright (C) 2013-2017 David Ordnung, Impact
*
* 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 3 of the License, or
* 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, see <http://www.gnu.org/licenses/>
*/
// c++ libs
#include <string>
#include <sstream>
#include <ctime>
// Curl
#include <curl/curl.h>
// We need a xml parser
#include "tinyxml2/tinyxml2.h"
// Command line arguments
#include <wx/cmdline.h>
// TextCtrl
#include <wx/textctrl.h>
// checkBox
#include <wx/checkbox.h>
// SpinCtrl
#include <wx/spinctrl.h>
// ToolTip
#include <wx/tooltip.h>
// ListBox
#include <wx/listbox.h>
// Static Line
#include <wx/statline.h>
// Sound Notification
#include <wx/sound.h>
// Only one Instance
#include <wx/snglinst.h>
// Project
#include "calladmin-client.h"
#include "config.h"
#include "main.h"
#include "log.h"
#include "update.h"
#include "trackers.h"
#include "about.h"
#include "call.h"
#include "taskbar.h"
// Timer
Timer *timer = NULL;
// Attempts to Zero
int attempts = 0;
// Avatar Size
int avatarSize = 184;
// program ended already?
bool end = false;
// Version
wxString version = "0.48B";
std::string updateURL = "http://dordnung.de/sourcemod/calladmin/version.txt";
// We need something to print for a XML Error!
wxString XMLErrorString[20] =
{
"XML_NO_ERROR",
"XML_NO_ATTRIBUTE",
"XML_WRONG_ATTRIBUTE_TYPE",
"XML_ERROR_FILE_NOT_FOUND",
"XML_ERROR_FILE_COULD_NOT_BE_OPENED",
"XML_ERROR_FILE_READ_ERROR",
"XML_ERROR_ELEMENT_MISMATCH",
"XML_ERROR_PARSING_ELEMENT",
"XML_ERROR_PARSING_ATTRIBUTE",
"XML_ERROR_IDENTIFYING_TAG",
"XML_ERROR_PARSING_TEXT",
"XML_ERROR_PARSING_CDATA",
"XML_ERROR_PARSING_COMMENT",
"XML_ERROR_PARSING_DECLARATION",
"XML_ERROR_PARSING_UNKNOWN",
"XML_ERROR_EMPTY_DOCUMENT",
"XML_ERROR_MISMATCHED_ELEMENT",
"XML_ERROR_PARSING",
"XML_CAN_NOT_CONVERT_TEXT",
"XML_NO_TEXT_NODE"
};
// Help for the CMDLine
static const wxCmdLineEntryDesc g_cmdLineDesc [] =
{
{wxCMD_LINE_SWITCH, "taskbar", "taskbar", "Move GUI to taskbar on Start"},
{wxCMD_LINE_NONE}
};
// Timer already run?
bool timerStarted = false;
// First fetch time
time_t firstFetch;
// Implement the APP
IMPLEMENT_APP(CallAdmin)
// Default no taskbar start
bool CallAdmin::start_taskbar = false;
// App Started
bool CallAdmin::OnInit()
{
if (!wxApp::OnInit())
{
return false;
}
// Check duplicate
static wxSingleInstanceChecker checkInstance("Call Admin - " + wxGetUserId());
if (checkInstance.IsAnotherRunning())
{
wxMessageBox("Call Admin is already running.", "Call Admin", wxOK | wxCENTRE | wxICON_EXCLAMATION);
return false;
}
// Create Config
g_config = new wxConfig("Call Admin");
// Valid?
if (g_config == NULL)
{
return false;
}
int y = wxSystemSettings::GetMetric(wxSYS_SCREEN_Y);
// Set Avatar Size
if (y < 900 && y >= 700)
{
// Only use small avatars
avatarSize = 128;
}
else if (y < 700)
{
avatarSize = 96;
}
else if (y < 600)
{
avatarSize = 64;
}
// First set Steamid to not known
steamid = "";
// Add Icon if available
if (wxTaskBarIcon::IsAvailable())
{
m_taskBarIcon = new TaskBarIcon();
}
// Reset Calls
for (int i=0; i < MAXCALLS; i++)
{
call_dialogs[i] = NULL;
}
// Delete .old file
remove(wxStandardPaths::Get().GetExecutablePath() + ".old");
remove(wxStandardPaths::Get().GetExecutablePath() + ".new");
// Create main Dialog
main_dialog = new MainDialog("Call Admin Client");
// Valid?
if (main_dialog == NULL)
{
return false;
}
main_dialog->createWindow(start_taskbar);
return true;
}
// Destroy
CallAdmin::~CallAdmin()
{
exitProgramm();
}
// Set Help text
void CallAdmin::OnInitCmdLine(wxCmdLineParser& parser)
{
// Add Help
parser.SetDesc(g_cmdLineDesc);
// Start with -
parser.SetSwitchChars("-");
}
// Find -tasbar
bool CallAdmin::OnCmdLineParsed(wxCmdLineParser& parser)
{
start_taskbar = parser.Found("taskbar");
return true;
}
// Timer events
BEGIN_EVENT_TABLE(Timer, wxTimer)
EVT_TIMER(1, Timer::update)
END_EVENT_TABLE()
// Run the timer
void Timer::run(int milliSecs)
{
// Log Action
LogAction("Start the Timer");
Start(milliSecs);
}
// Timer executed
void Timer::update(wxTimerEvent& WXUNUSED(event))
{
// Check for Update
if (!timerStarted)
{
checkUpdate();
}
std::string pager;
// Page
if (!timerStarted)
{
pager = (page + "/notice.php?from=0&from_type=unixtime&key=" + key + "&sort=desc&limit=" + (wxString() << lastCalls));
}
else
{
pager = (page + "/notice.php?from=" + (wxString() << (step * 2)) + "&from_type=interval&key=" + key + "&sort=asc&handled=" + (wxString() << (time(0) - firstFetch)));
}
// Store Player
if (main_dialog != NULL && main_dialog->wantStore())
{
pager = pager + "&store=1&steamid=" + steamid;
}
// Get the Page
getPage(onNotice, pager);
}
void onNotice(char* error, wxString result, int WXUNUSED(x))
{
bool firstRun = false;
// First Run?
if (!timerStarted)
{
firstFetch = time(0);
timerStarted = true;
firstRun = true;
}
// Valid result?
if (result != "")
{
// Everything good :)
if (strcmp(error, "") == 0)
{
bool foundError = false;
bool foundNew = false;
// Proceed XML result!
tinyxml2::XMLDocument doc;
tinyxml2::XMLNode *node;
tinyxml2::XMLError parseError;
// Parse the xml data
parseError = doc.Parse(result);
// Parse Error?
if (parseError != tinyxml2::XML_SUCCESS)
{
foundError = true;
attempts++;
// Log Action
LogAction("Found a XML Error: " + (wxString)XMLErrorString[parseError]);
// Max attempts reached?
if (attempts == maxAttempts)
{
// Close Dialogs and create reconnect main dialog
createReconnect("XML Error: " + (wxString)XMLErrorString[parseError]);
}
else
{
// Create Parse Error
showError(XMLErrorString[parseError], "XML");
}
}
// No error so far, yeah!
if (!foundError)
{
// Goto xml child
node = doc.FirstChild();
// Goto CallAdmin
if (node != NULL)
{
node = node->NextSibling();
}
// New Calls?
if (node != NULL)
{
// Init. Call List
int foundRows = 0;
// Only if first run
if (firstRun)
{
for (tinyxml2::XMLNode *node2 = node->FirstChild(); node2; node2 = node2->NextSibling())
{
// Search for foundRows
if ((wxString)node2->Value() == "foundRows")
{
// Get Rows
std::stringstream rows(node2->FirstChild()->Value());
rows >> foundRows;
// Go on
break;
}
}
}
for (tinyxml2::XMLNode *node2 = node->FirstChild(); node2; node2 = node2->NextSibling())
{
// API Error?
if ((wxString)node2->Value() == "error")
{
foundError = true;
attempts++;
// Max attempts reached?
if (attempts == maxAttempts)
{
// Close Dialogs and create reconnect main dialog
createReconnect("API Error: " + (wxString)node2->FirstChild()->Value());
}
else
{
// API Errpr
showError((wxString)node2->FirstChild()->Value(), "API");
}
break;
}
// Row Count
if ((wxString)node2->Value() == "foundRows")
{
continue;
}
int dialog = -1;
// Normal Stepp
if (!firstRun)
{
// Look for a free place
for (int i=0; i < MAXCALLS; i++)
{
if (call_dialogs[i] == NULL)
{
dialog = i;
break;
}
}
// Everything is full, so clear Everything, client's problem oO, MAXCALLS is enough!
if (dialog == -1)
{
for (int i=0; i < MAXCALLS; i++)
{
if (call_dialogs[i] != NULL)
{
call_dialogs[i]->Destroy();
call_dialogs[i] = NULL;
}
}
dialog = 0;
}
}
else
{
// First run, update call list
dialog = foundRows - 1;
}
// Api is fine :)
int found = 0;
// Create the new CallDialog
CallDialog *newDialog = new CallDialog("New Incoming Call");
// Valid?
if (newDialog == NULL)
{
return;
}
// Put in ALL needed DATA
for (tinyxml2::XMLNode *node3 = node2->FirstChild(); node3; node3 = node3->NextSibling())
{
if ((wxString)node3->Value() == "callID")
{
found++;
newDialog->setCallID(node3->FirstChild()->Value());
}
if ((wxString)node3->Value() == "fullIP")
{
found++;
newDialog->setIP(node3->FirstChild()->Value());
}
if ((wxString)node3->Value() == "serverName")
{
found++;
newDialog->setName(node3->FirstChild()->Value());
}
if ((wxString)node3->Value() == "targetName")
{
found++;
newDialog->setTarget(node3->FirstChild()->Value());
}
if ((wxString)node3->Value() == "targetID")
{
found++;
newDialog->setTargetID(node3->FirstChild()->Value());
}
if ((wxString)node3->Value() == "targetReason")
{
found++;
newDialog->setReason(node3->FirstChild()->Value());
}
if ((wxString)node3->Value() == "clientName")
{
found++;
newDialog->setClient(node3->FirstChild()->Value());
}
if ((wxString)node3->Value() == "clientID")
{
found++;
newDialog->setClientID(node3->FirstChild()->Value());
}
if ((wxString)node3->Value() == "reportedAt")
{
found++;
newDialog->setTime(node3->FirstChild()->Value());
}
if ((wxString)node3->Value() == "callHandled")
{
found++;
newDialog->setHandled(strcmp(node3->FirstChild()->Value(), "1") == 0);
}
}
bool findDuplicate = false;
// Check duplicate Entries
for (int i=0; i < MAXCALLS; i++)
{
if (i != dialog && call_dialogs != NULL)
{
if (call_dialogs[i] != NULL)
{
// Operator overloading :)
if ((*call_dialogs[i]) == (*newDialog))
{
findDuplicate = true;
// Call is now handled
if (newDialog->getHandled() && !call_dialogs[i]->getHandled())
{
main_dialog->setHandled(i);
}
// That's enough
break;
}
}
}
}
// Found all necessary items?
if (found != 10 || findDuplicate)
{
// Something went wrong or duplicate
newDialog->Destroy();
}
else
{
// New call
foundNew = true;
// Add the new Call to the Call box
char buffer[80];
wxString text;
// But first we need a Time
time_t tt = (time_t)newDialog->getTime();
struct tm* dt = localtime(&tt);
strftime(buffer, sizeof(buffer), "%H:%M", dt);
newDialog->SetTitle("Call At " + (wxString)buffer);
text = (wxString)buffer + " - " + newDialog->getServer();
// Add the Text
newDialog->setBoxText(text);
// Now START IT!
newDialog->setID(dialog);
// Don't show calls on first Run
if (firstRun)
{
foundRows--;
newDialog->startCall(false);
}
else
{
// Log Action
LogAction("We have a new Call");
newDialog->startCall(main_dialog->isAvailable() && !isOtherInFullscreen());
}
newDialog->takeover->Enable(!newDialog->getHandled());
call_dialogs[dialog] = newDialog;
}
}
}
}
// Everything is good, set attempts to zero
if (!foundError && main_dialog != NULL)
{
// Reset attempts
attempts = 0;
// Updated Main Interface
main_dialog->SetTitle("Call Admin Client");
main_dialog->setEventText("Waiting for a new report...");
// Update Call List
if (foundNew)
{
// Update call list
main_dialog->updateCall();
// Play Sound
if (main_dialog->wantSound() && !firstRun && main_dialog->isAvailable())
{
wxSound* soundfile;
#if defined(__WXMSW__)
soundfile = new wxSound("calladmin_sound", true);
#else
wxLogNull nolog;
soundfile = new wxSound(getAppPath("resources/calladmin_sound.wav"), false);
#endif
if (soundfile != NULL && soundfile->IsOk())
{
soundfile->Play(wxSOUND_ASYNC);
// Clean
delete soundfile;
}
}
}
}
}
else
{
// Something went wrong ):
attempts++;
// Log Action
LogAction("New CURL Error: " + (std::string)error);
// Max attempts reached?
if (attempts == maxAttempts)
{
// Create reconnect main dialog
createReconnect("CURL Error: " + (std::string)error);
}
else
{
// Show the error to client
showError(error, "CURL");
}
}
}
}
// Curl Thread started
wxThread::ExitCode curlThread::Entry()
{
if (!TestDestroy())
{
// Event
wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, wxID_ThreadHandled);
// Response
std::ostringstream stream;
// Init Curl
CURL *curl = curl_easy_init();
if (curl != NULL)
{
// Error
char ebuf[CURL_ERROR_SIZE];
// Configurate Curl
curl_easy_setopt(curl, CURLOPT_URL, ((std::string)page).c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, ebuf);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout*2);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream);
// Perform Curl
CURLcode res = curl_easy_perform(curl);
// Everything good :)
if (res == CURLE_OK)
{
event.SetClientObject(new ThreadData(function, stream.str(), "", x));
}
else
{
// Error ):
event.SetClientObject(new ThreadData(function, stream.str(), ebuf, x));
}
// Clean Curl
curl_easy_cleanup(curl);
// Add Event Handler
if (main_dialog != NULL)
{
main_dialog->GetEventHandler()->AddPendingEvent(event);
}
return (wxThread::ExitCode)0;
}
event.SetClientObject(new ThreadData(function, "", "", x));
// Add Event Handler
if (main_dialog != NULL)
{
main_dialog->GetEventHandler()->AddPendingEvent(event);
}
}
return (wxThread::ExitCode)0;
}
// Curl receive data -> write to buffer
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
std::ostringstream *data = (std::ostringstream*)userp;
if (data != NULL)
{
size_t count = size * nmemb;
data->write((char*)buffer, count);
return count;
}
return (size_t) -1;
}
// Get Page
void getPage(callback function, wxString page, int x)
{
new curlThread(function, page, x);
}
// Create the Window as a reconnecter
void createReconnect(wxString error)
{
// Valid?
if (main_dialog == NULL || notebook == NULL)
{
return;
}
// Log Action
LogAction("Create a reconnect window");
main_dialog->SetTitle("Couldn't Connect");
main_dialog->setEventText(error);
main_dialog->setReconnectButton(true);
// Show it
main_dialog->Show(true);
main_dialog->Restore();
// Go to first page
notebook->SetSelection(0);
// Stop timer
timer->Stop();
}
// Create an new Error Dialog
void showError(wxString error, wxString type)
{
// Log Action
LogAction(type + " Error: " + error);
if (m_taskBarIcon != NULL)
{
m_taskBarIcon->ShowMessage("An error occured", type + " Error : " + error + "\nTry again... " + (wxString() << attempts) + "/" + (wxString() << maxAttempts), main_dialog);
}
}
// Close Taskbar Icon and destroy all dialogs
void exitProgramm()
{
if (!end)
{
// Mark as ended
end = true;
// First disappear Windows
if (main_dialog != NULL)
{
main_dialog->Show(false);
}
// No more Update dialog needed
if (update_dialog != NULL)
{
update_dialog->Show(false);
}
// Timer... STOP!
if (timer != NULL)
{
timer->Stop();
timer = NULL;
}
// Taskbar goodbye :)
if (m_taskBarIcon != NULL)
{
m_taskBarIcon->RemoveIcon();
m_taskBarIcon->Destroy();
m_taskBarIcon = NULL;
}
// No more Main dialog needed
if (main_dialog != NULL)
{
main_dialog->Destroy();
main_dialog = NULL;
}
// No more Update dialog needed
if (update_dialog != NULL)
{
update_dialog->Destroy();
update_dialog = NULL;
}
// Calls are unimportant
for (int i=0; i < MAXCALLS; i++)
{
if (call_dialogs[i] != NULL)
{
// Stop Avatar Timer
if (call_dialogs[i]->avatarTimer != NULL && call_dialogs[i]->avatarTimer->IsRunning())
{
call_dialogs[i]->avatarTimer->Stop();
}
call_dialogs[i]->Destroy();
call_dialogs[i] = NULL;
}
}
// We don't need Steam support
if (steamThreader != NULL)
{
steamThreader->Delete();
steamThreader = NULL;
}
// Delete Update
if (update_thread != NULL)
{
update_thread->Delete();
update_thread = NULL;
}
}
}
#if defined(__WXMSW__)
// Stupid Wchars...
std::wstring s2ws(wxString s)
{
// Convert a normal String to a wchar :)
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
#endif
// Get the Path of the App
wxString getAppPath(wxString file)
{
wxString path = wxStandardPaths::Get().GetExecutablePath();
// Windows format?
size_t start = path.find_last_of("\\");
if (start == 0 || start == wxString::npos)
{
// No... Linux Format ;)
start = path.find_last_of("/");