-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMessage.cpp
1427 lines (1261 loc) · 52.2 KB
/
Message.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 <ArduinoJson.h>
#include <WiFiClientSecure.h>
#include "Defs.h"
#include "Message.h"
#include "EServer.h"
#include "TimeF.h"
#include "User.h"
#include "Chat.h"
#include "Session.h"
extern EmlaServer emlaServer;
extern TimeFunctions timeFunc;
extern OledDisplay oledDisplay;
extern int coverState;
extern int oldCoverState;
extern ChatSet chats;
extern UserSet users;
extern Session session;
extern Verification verification;
extern Tasklist tasklist;
// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
WiFiClientSecure clientsec;
UniversalTelegramBot bot(BOT_TOKEN, clientsec);
// Checks for new messages every 1 second.
#define BOT_COMMANDS_GENERAL "/start - Start communication\n/state - Report the cover state\n/roles - List roles\n/users - List users in chat\n/play_6 - Throw dice (any number of possibilities with corresponding number)\n"
#define BOT_COMMANDS_SHOCKS "/shock_1 - Shock for 1 second (other intervals work with corresponding numbers)\n/shock_5 - Shock for 5 seconds (other intervals work with corresponding numbers)\n/shock_10 - Shock for 10 seconds (other intervals work with corresponding numbers)\n/shock_30 - Shock for 30 seconds (other intervals work with corresponding numbers)\n"
#define BOT_COMMANDS_RANDOM "/random_5 - Switch on random shock mode with 5 shocks per hour (other intervals work with corresponding numbers)\n/random_off - Switch off random shock mode\n"
#define BOT_COMMANDS_TEASING "/teasing_on - Enable teasing\n/teasing_off - Disable teasing\n/verify_1 - Enable verification mode with 1 check-in per day\n/verify_2 - Enable verification mode with 2 check-ins per day\n/verify_off - Disable verification mode\n"
#define BOT_COMMANDS_TASKS ""
#define BOT_COMMANDS_UNLOCK "/unlock - Unlock key safe\n/play4unlock - Throw dice to determine if unlocking should be possible\n"
#define BOT_COMMANDS_ROLES "/holder - Adopt holder role\n/teaser - Adopt teaser role\n/guest - Adopt guest role\n"
#define BOT_COMMANDS_WAITING "/waiting - Make wearer waiting to be captured by the holder\n/free - Make wearer free again (stops waiting for capture)\n"
#define BOT_COMMANDS_CAPTURE "/capture - Capture wearer as a sub\n/release - Release wearer as a sub\n"
#define BOT_COMMANDS_EMERGENCY "/thisisanemergency - Release the wearer in case of an emergency\n"
String botCommandsAll = BOT_COMMANDS_GENERAL BOT_COMMANDS_SHOCKS BOT_COMMANDS_RANDOM BOT_COMMANDS_TEASING BOT_COMMANDS_UNLOCK BOT_COMMANDS_ROLES BOT_COMMANDS_WAITING BOT_COMMANDS_CAPTURE ;
String botCommandsWearer = BOT_COMMANDS_GENERAL BOT_COMMANDS_UNLOCK BOT_COMMANDS_WAITING ;
String botCommandsHolder = BOT_COMMANDS_GENERAL BOT_COMMANDS_SHOCKS BOT_COMMANDS_RANDOM BOT_COMMANDS_TEASING BOT_COMMANDS_TASKS BOT_COMMANDS_UNLOCK BOT_COMMANDS_ROLES BOT_COMMANDS_CAPTURE ;
String botCommandsGuest = BOT_COMMANDS_GENERAL BOT_COMMANDS_ROLES BOT_COMMANDS_CAPTURE ;
String botCommandsTeaser = BOT_COMMANDS_GENERAL BOT_COMMANDS_SHOCKS BOT_COMMANDS_ROLES BOT_COMMANDS_CAPTURE ;
String botCommandsAllHelp = BOT_COMMANDS_GENERAL BOT_COMMANDS_SHOCKS BOT_COMMANDS_RANDOM BOT_COMMANDS_TEASING BOT_COMMANDS_UNLOCK BOT_COMMANDS_ROLES BOT_COMMANDS_WAITING BOT_COMMANDS_CAPTURE BOT_COMMANDS_EMERGENCY ;
/*
start - Start communication
roles - List roles
shock1 - Shock for 1 seconds
shock3 - Shock for 3 seconds
shock5 - Shock for 5 seconds
shock10 - Shock for 10 seconds
shock30 - Shock for 30 seconds
state - Report the cover state
unlock - Unlock key safe
users - Lists users in chat
holder - Adopt holder role
teaser - Adopt teaser role
guest - Adopt guest role
waiting - Make wearer waiting to be captured by the holder
free - Make wearer free again (stops waiting for capturing by the holder)
capture - Capture wearer as a sub
release - Release wearer as a sub
*/
#define COMMON_MSG_WAITING " The holder can capture him with the /capture command."
#define COMMON_MSG_CAPTURE " He is now securely locked and lost his permissions to open the key safe. The key safe can only be operated by the holder using the /unlock command.\nFurthermore, the wearer can now be punished with shocks."
#define COMMON_MSG_TEASING_ON "Teasing mode is activated. Users with teaser role can now support the holder with treatments like /shock_5."
#define COMMON_MSG_TEASING_OFF "Teasing mode is switched off. Only the holder has the permission to treat the wearer with shocks."
// ------------------------------------------------------------------------
Message::Message()
{
}
// ------------------------------------------------------------------------
void Message::Init()
{
Serial.println("*** Message::Init()");
// UserSet::Init() must have already be run!
// populate values with defaults...
session.SetTimeOfLastShock(timeFunc.GetTimeInSeconds());
session.SetTimeOfLastUnlock(timeFunc.GetTimeInSeconds());
session.SetTimeOfLastOpening(timeFunc.GetTimeInSeconds());
session.SetTimeOfLastClosing(timeFunc.GetTimeInSeconds());
int index;
index = users.AddUser(USER_ID_WEARER, USER_NAME_WEARER, ROLE_WEARER_FREE);
users.SetWearerIndex(index);
index = users.AddUser(USER_ID_BOT, "ShockCell", ROLE_SHOCKCELL);
users.GetUser(index)->SetBot(true);
AdoptSettings();
}
// ------------------------------------------------------------------------
void Message::MessageWearerState(String chatId)
{
User *u = users.GetWearer();
if (u)
{
if (u->GetRoleId() == ROLE_WEARER_CAPTURED)
SendMessage(SYMBOL_LOCK_CLOSED " Wearer is securely captured and has no rights to free himself.", chatId);
else if (u->GetRoleId() == ROLE_WEARER_WAITING)
SendMessage(SYMBOL_FINGER_UP " Wearer is available to be captured by a holder with command: /holder!!!", chatId);
else
SendMessage("Wearer is free.", chatId);
}
}
// ------------------------------------------------------------------------
void Message::MessageLastShock(String chatId)
{
SendMessage("Last shock was " + timeFunc.Time2String(timeFunc.GetTimeInSeconds() - session.GetTimeOfLastShock()) + " ago.", chatId);
}
// ------------------------------------------------------------------------
void Message::MessageIsClosed(String chatId)
{
SendMessage(SYMBOL_LOCK_CLOSED " Key safe is safely closed and locked since " + timeFunc.Time2String(timeFunc.GetTimeInSeconds() - session.GetTimeOfLastClosing()) + ".", chatId);
}
// ------------------------------------------------------------------------
void Message::MessageIsOpen(String chatId)
{
SendMessage(SYMBOL_LOCK_OPEN " Key safe is open since " + timeFunc.Time2String(timeFunc.GetTimeInSeconds() - session.GetTimeOfLastOpening()) + ".", chatId);
}
// ------------------------------------------------------------------------
void Message::MessageCoverStateChange(String chatId)
{
if (coverState == COVER_CLOSED)
{
session.SetTimeOfLastClosing(timeFunc.GetTimeInSeconds());
WriteCommandsAndSettings();
SendMessage(SYMBOL_LOCK_CLOSED " Key safe has just been safely closed.", chatId);
}
else
{
session.SetTimeOfLastOpening(timeFunc.GetTimeInSeconds());
WriteCommandsAndSettings();
SendMessage(SYMBOL_LOCK_OPEN " Key safe has just been opened.", chatId);
}
}
// ------------------------------------------------------------------------
void Message::MessageCoverState(String chatId)
{
coverState = digitalRead(COVER_OPEN_PIN);
if (coverState == COVER_CLOSED)
MessageIsClosed(chatId);
else
MessageIsOpen(chatId);
}
// ------------------------------------------------------------------------
void Message::MessageSendEarnedCredits(int creditsEarned, String chatId)
{
SendMessage(String(SYMBOL_CREDIT) + " Wearer " + users.GetWearer()->GetName() + " has earned " + creditsEarned + " credits. His new credit balance is " + session.GetCredits() + ".", chatId);
}
// ------------------------------------------------------------------------
void Message::MessageSendEarnedVouchers(int vouchersEarned, String chatId)
{
SendMessage(String(SYMBOL_VOUCHER) + " Wearer " + users.GetWearer()->GetName() + " has earned " + vouchersEarned + " unlock vouchers. His new voucher balance is " + session.GetVouchers() + ".", chatId);
}
// ------------------------------------------------------------------------
void Message::MessageModes(String chatId)
{
String msg;
if (session.IsTeasingMode())
msg = COMMON_MSG_TEASING_ON;
else
msg = COMMON_MSG_TEASING_OFF;
SendMessage(msg, chatId);
if (session.IsRandomMode())
msg = SYMBOL_DEVIL_SMILE " Random mode is activated. The wearer receives " + String(session.GetRandomModeShocksPerHour(), DEC) +
" random shocks per hour since " + timeFunc.Time2String(timeFunc.GetTimeInSeconds() - session.GetTimeOfRandomModeStart()) +
". The random mode will be automatically deactivated after " + timeFunc.Time2String(RANDOM_SHOCK_AUTO_OFF_SECONDS - timeFunc.GetTimeInSeconds() + session.GetTimeOfRandomModeStart()) + ".";
else
msg = "Random mode is switched off.";
SendMessage(msg, chatId);
if (verification.IsEnabled())
{
msg = SYMBOL_WATCHING_EYES " Verification mode is activated. The wearer must send " + String(verification.GetRequiredCountPerDay(), DEC) +
" verification photos per day as requested. He has provided " + String(verification.GetActualToday(), DEC) + " photos today.";
}
else
msg = "Verification mode is switched off.";
SendMessage(msg, chatId);
SendMessage(SYMBOL_CREDIT " Wearer has collected " + String(session.GetCredits(), DEC) + " credits from shock treatments.", chatId);
if (session.GetVouchers() > 0)
SendMessage(SYMBOL_VOUCHER " Wearer has collected " + String(session.GetVouchers(), DEC) + " unlock vouchers.", chatId);
if (session.GetDeviations() > 0)
SendMessage(SYMBOL_SMILE_OHOH " Wearer has missed expectations " + String(session.GetDeviations(), DEC) + " times.", chatId);
if (session.GetFailures() > 0)
SendMessage(SYMBOL_FORBIDDEN " Wearer has failed " + String(session.GetFailures(), DEC) + " times to complete his tasks.", chatId);
if (users.GetWearer()->IsSleeping())
SendMessage(SYMBOL_SLEEPING " Wearer is currently allowed to sleep and shocks are disabled during that time.", chatId);
}
// ------------------------------------------------------------------------
void Message::MessageUsers(String chatId)
{
int i = 0;
String msg = "Users in chat:\n";
while ((i < users.GetCount()) &&
(i < USER_CACHE_SIZE))
{
String name = users.GetUser(i)->GetName();
Serial.println(String("\"") + name + String("\""));
if (name.length() > 0)
msg += "- " + name + " (" + users.GetUser(i)->GetRoleStr() + ")\n";
++i;
Serial.println(msg);
}
SendMessage(msg, chatId);
// SendRichMessage(chatId, "inline_keyboard: [\n[{ text: 'Some button text 1', callback_data: '1' }],\n[{ text: 'Some button text 2', callback_data: '2' }],\n[{ text: 'Some button text 3', callback_data: '3' }]\n]");
}
// ------------------------------------------------------------------------
void Message::MessageRoles(String chatId)
{
Serial.println("*** Message::MessageRoles()");
String msg = "Assigned roles:\n";
User *user;
for (int r = 0; r < ROLE_COUNT; r++)
{
Serial.println(r);
user = users.GetUserFromRole(r);
if (user)
{
String name = user->GetName();
msg += "- " + user->GetRoleStr() + ": " + name + "\n";
Serial.println(msg);
}
}
SendMessage(msg, chatId);
}
// ------------------------------------------------------------------------
void Message::MessageChastikeyState(String chatId)
{
String msg;
if (session.IsActiveChastikeySession())
msg = "Wearer " + users.GetWearer()->GetName() + " is locked in a ChastiKey session and controlled by holder " + session.GetChastikeyHolder() + ". The key safe remains securely locked as long as this session is active.";
// else
// msg = "Wearer " + users.GetWearer()->GetName() + " is not locked in a ChastiKey session.";
SendMessage(msg, chatId);
}
// ------------------------------------------------------------------------
void Message::MessageState(String chatId)
{
MessageWearerState(chatId);
MessageLastShock(chatId);
MessageCoverState(chatId);
MessageModes(chatId);
MessageUsers(chatId);
MessageChastikeyState(chatId);
}
// ------------------------------------------------------------------------
void Message::MessageTasks(String chatId)
{
String msg = tasklist.GetTaskList();
SendMessage(msg, chatId);
}
// ------------------------------------------------------------------------
void Message::ShockAction(String durationStr, int count, String fromId, String chatId)
{
if (timeFunc.IsSleepingTime())
{
SendMessage("Wearer " + users.GetWearer()->GetName() + " is allowed to sleep and cannot be shocked now.", chatId);
}
else
{
unsigned long milliseconds;
if (durationStr.length() > 0)
milliseconds = durationStr.toInt() * 1000L;
else
milliseconds = 3000;
ShockAction(milliseconds, count, fromId, chatId);
}
}
//------------------------------------------------------------------------
void Message::ShockAction(unsigned long milliseconds, int count, String fromId, String chatId)
{
User *u = users.GetUserFromId(fromId);
String msg;
if (u)
{
if (u->IsHolder() ||
u->IsBot() ||
(u->IsTeaser() && session.IsTeasingMode()) ||
(u->IsWearer() && session.IsTeasingMode()))
{
String msg = SYMBOL_COLLISION SYMBOL_COLLISION SYMBOL_COLLISION " Shock processing for " + String((milliseconds / 1000L), DEC) + " s begins...";
SendMessage(msg, chatId);
SendMessage(msg, USER_ID_WEARER);
session.SetTimeOfLastShock(timeFunc.GetTimeInSeconds());
session.SetCreditFractions(session.GetCreditFractions() + (milliseconds / 1000), chatId);
WriteCommandsAndSettings();
session.Shock(count, milliseconds);
msg = "Shock processing completed. :-)";
SendMessage(msg, chatId);
SendMessage(msg, USER_ID_BOT);
WriteCommandsAndSettings();
}
else
{
String msg = SYMBOL_NO_ENTRY " Request denied. Only the holder";
if (session.IsTeasingMode())
msg += " and the teasers";
msg += " are allowed to execute shock punishments.";
SendMessage(msg, chatId);
}
}
}
// ------------------------------------------------------------------------
void Message::WaitingAction(String fromId, String chatId)
{
String msg;
User *u = users.GetUserFromId(fromId);
if (! u)
return;
// does request comes from free wearer?
if (u->IsFreeWearer())
{
users.GetWearer()->UpdateRoleId(ROLE_WEARER_WAITING);
msg = SYMBOL_WATCHING_EYES " Wearer " + users.GetWearerName() + " is now exposed and waiting to be captured by the holder." COMMON_MSG_WAITING;
SendMessage(msg, chatId);
WriteCommandsAndSettings();
}
// does request comes from waiting wearer?
else if (u->IsWaitingWearer())
{
msg = "Wearer " + users.GetWearerName() + " is already waiting for capture by the holder." COMMON_MSG_WAITING;
SendMessage(msg, chatId);
}
// does request comes from captured wearer?
else if (u->IsCapturedWearer())
{
msg = "Wearer " + users.GetWearerName() + " is already captured by the holder.";
SendMessage(msg, chatId);
}
else
{
msg = SYMBOL_NO_ENTRY " Only the free wearer can use the command /waiting to make himself available for capturing by a holder.";
SendMessage(msg, chatId);
}
}
// ------------------------------------------------------------------------
void Message::FreeAction(String fromId, String chatId)
{
String msg;
// does request comes from waiting wearer?
if (users.GetUserFromId(fromId)->IsWaitingWearer())
{
users.GetWearer()->UpdateRoleId(ROLE_WEARER_FREE);
msg = "Wearer " + users.GetWearerName() + " is now free and cannot be captured by the holder.";
SendMessage(msg, chatId);
WriteCommandsAndSettings();
}
// does request comes from free wearer?
else if (users.GetUserFromId(fromId)->IsFreeWearer())
{
msg = "Wearer " + users.GetWearerName() + " is already free.";
SendMessage(msg, chatId);
}
// does request comes from captured wearer?
else if (users.GetUserFromId(fromId)->IsCapturedWearer())
{
msg = SYMBOL_NO_ENTRY " Request is denied! Wearer " + users.GetWearerName() + " is captured by the holder. He must be released by the holder to get free.";
SendMessage(msg, chatId);
}
else
{
msg = SYMBOL_NO_ENTRY " Only the waiting wearer can use the command /free to be safe from capturing by the holder.";
SendMessage(msg, chatId);
}
}
// ------------------------------------------------------------------------
void Message::CaptureAction(String fromId, String chatId)
{
String msg;
if (users.GetUserFromId(fromId)->IsHolder() ||
(! users.GetHolder() && users.GetUserFromId(fromId)->MayBecomeHolder()))
{
// request is from holder
// or from other user when there is no holder present yet
if (users.GetWearer()->IsWaitingWearer())
{
users.GetWearer()->UpdateRoleId(ROLE_WEARER_CAPTURED);
msg = SYMBOL_LOCK_CLOSED " Capturing wearer " + users.GetWearerName() + " now." COMMON_MSG_CAPTURE;
// Make requestor a holder if necessary
if (! users.GetUserFromId(fromId)->IsHolder())
{
msg += " User " + users.GetUserFromId(fromId)->GetName() + " is now holder.";
users.GetUserFromId(fromId)->SetHolder();
}
WriteCommandsAndSettings();
}
else if (users.GetWearer()->IsCapturedWearer())
{
msg = "Wearer " + users.GetWearerName() + " is already captured." COMMON_MSG_CAPTURE;
}
else
{
msg = "Wearer " + users.GetWearerName() + " is currently not waiting to get captured. The wearer must send the /waiting command to await capture by the holder.";
}
SendMessage(msg, chatId);
}
else
{
if (users.GetWearer()->IsFreeWearer())
SendMessage("You currently cannot capture the wearer, because he has not made himself available. The wearer must be waiting for capture using the /waiting command.", chatId);
else
SendMessage(SYMBOL_NO_ENTRY " You currently have no permission to capture the wearer. Guests may capture the wearer only, if there is no current holder.", chatId);
}
}
// ------------------------------------------------------------------------
void Message::ReleaseAction(String fromId, String chatId)
{
String msg;
if (users.GetUserFromId(fromId)->IsHolder())
{
// request is from holder
if (users.GetWearer()->IsCapturedWearer())
{
users.GetWearer()->UpdateRoleId(ROLE_WEARER_WAITING);
msg = SYMBOL_LOCK_OPEN " Releasing wearer " + users.GetWearerName() + ". He is now released and received back his permissions to open the key safe.\n";
WriteCommandsAndSettings();
}
else
{
msg = "Wearer " + users.GetWearerName() + " is currently not captured and there is no need to release him.";
}
SendMessage(msg, chatId);
}
else
{
if (users.GetUserFromId(fromId)->IsCapturedWearer())
SendMessage(SYMBOL_NO_ENTRY " The wearer cannot free himself. Only the holder may release the wearer.", chatId);
else
SendMessage("You currently have no permission to release the wearer. Only the holder may release the wearer.", chatId);
}
}
// ------------------------------------------------------------------------
void Message::UnlockAction(String fromId, String chatId, bool force)
{
Serial.println("*** Message::UnlockAction()");
Serial.print("- free: ");
Serial.println(users.GetUserFromId(fromId)->IsFreeWearer());
Serial.print("- waiting: ");
Serial.println(users.GetUserFromId(fromId)->IsWaitingWearer());
Serial.print("- holder: ");
Serial.println(users.GetUserFromId(fromId)->IsHolder());
// from wearer & wearer is free
// from holder
session.InfoChastikey();
if (session.IsActiveChastikeySession() && ! force)
{
SendMessage(SYMBOL_NO_ENTRY " Unlock request is denied! Wearer is locked in an active ChastiKey session.", chatId);
}
else if (users.GetUserFromId(fromId)->MayUnlock() || force)
{
if (force)
SendMessageAll("An emergency release request has been granted.", chatId);
SendMessageAll(SYMBOL_LOCK_OPEN " Key safe is unlocking now for 4 seconds and may be opened during this period.", chatId);
session.Unlock();
// check state of the safe afterwards
coverState = digitalRead(COVER_OPEN_PIN);
if (coverState == COVER_OPEN)
{
SendMessageAll(SYMBOL_KEY " Key safe has been opened.", chatId);
WriteCommandsAndSettings();
}
else
SendMessageAll(SYMBOL_LOCK_CLOSED " Key safe is locked again and has not been opened.", chatId);
}
else
{
SendMessage(SYMBOL_NO_ENTRY " You have no permission to unlock. Only the free wearer or the holder may unlock the key safe.", chatId);
}
}
// ------------------------------------------------------------------------
void Message::PlayAction(String max, String fromId, String chatId)
{
int maxCount = max.toInt();
if (maxCount > 100)
maxCount = 100;
else if (maxCount < 2)
maxCount = 2;
String num[10] = { SYMBOL_DIGIT0, SYMBOL_DIGIT1, SYMBOL_DIGIT2, SYMBOL_DIGIT3, SYMBOL_DIGIT4, SYMBOL_DIGIT5, SYMBOL_DIGIT6, SYMBOL_DIGIT7, SYMBOL_DIGIT8, SYMBOL_DIGIT9 };
Serial.println("*** Message::PlayAction()");
SendMessage(String(SYMBOL_DICE) + " Playing with dice (1.." + String(maxCount, DEC) + ").", chatId);
SendMessage(SYMBOL_DIGIT0 SYMBOL_DIGIT0 " " SYMBOL_DRUM, chatId);
delay(300);
int lastMessageId = GetLastSentMessageId();
int result = (random(90000) * maxCount / 90000) + 1;
Serial.println("- value: " + String(result, DEC));
int i = 0;
String progress = SYMBOL_DRUM;
while (i <= result)
{
progress += ".";
EditMessage(num[i / 10] + num[i % 10] + " " + progress, String(lastMessageId, DEC), chatId);
if (maxCount < 10)
i++;
else
i = i + maxCount/10;
}
EditMessage(num[result / 10] + num[result % 10] + " " + SYMBOL_STAR_GLOWING, String(lastMessageId, DEC), chatId);
}
// ------------------------------------------------------------------------
void Message::Play4UnlockAction(String fromId, String chatId)
{
String num[10] = { SYMBOL_DIGIT0, SYMBOL_DIGIT1, SYMBOL_DIGIT2, SYMBOL_DIGIT3, SYMBOL_DIGIT4, SYMBOL_DIGIT5, SYMBOL_DIGIT6, SYMBOL_DIGIT7, SYMBOL_DIGIT8, SYMBOL_DIGIT9 };
Serial.println("*** Message::Play4UnlockAction()");
unsigned long duration = timeFunc.GetTimeInSeconds() - session.GetTimeOfLastClosing();
int winPoints = session.GetCredits() + timeFunc.GetNumberOfDays(duration);
SendMessage(String(SYMBOL_DICE) + " Playing for unlock. Wearer wins for values smaller than: " + num[winPoints / 10] + num[winPoints % 10] +
" (" + timeFunc.GetNumberOfDays(duration) + " days locked + " + session.GetCredits() + " credits)", chatId);
SendMessage(SYMBOL_DIGIT0 SYMBOL_DIGIT0 " " SYMBOL_DRUM, chatId);
delay(300);
int lastMessageId = GetLastSentMessageId();
int result = (random(90000) / 900);
Serial.println("- value: " + String(result, DEC));
int i = 0;
String progress = SYMBOL_DRUM;
while (i < result)
{
progress += ".";
EditMessage(num[i / 10] + num[i % 10] + " " + progress, String(lastMessageId, DEC), chatId);
i = i + 5;
}
EditMessage(num[result / 10] + num[result % 10] + " " + SYMBOL_STAR_GLOWING, String(lastMessageId, DEC), chatId);
if (result <= winPoints)
SendMessage(SYMBOL_LOCK_OPEN " Wearer has won! " SYMBOL_SMILY_SMILE SYMBOL_SMILY_SMILE SYMBOL_SMILY_SMILE, chatId);
else
SendMessage(SYMBOL_LOCK_CLOSED " Wearer has lost! " SYMBOL_DEVIL_SMILE SYMBOL_DEVIL_SMILE SYMBOL_DEVIL_SMILE, chatId);
}
// ------------------------------------------------------------------------
void Message::HolderAction(String fromId, String chatId)
{
User *u = users.GetUserFromId(fromId);
String msg;
if (u && u->MayBecomeHolder())
{
u->UpdateRoleId(ROLE_HOLDER);
msg = SYMBOL_QUEEN " User " + users.GetUserFromId(fromId)->GetName() + " has now holder permissions.";
msg += "The holder can use the following commands:\n" + botCommandsHolder;
SendMessage(msg, chatId);
WriteCommandsAndSettings();
}
else
{
if (u->IsHolder())
{
msg = "User " + users.GetUserFromId(fromId)->GetName() + " is already holder.";
msg += "The holder can use the following commands:\n" + botCommandsHolder;
}
else
msg = "User " + users.GetUserFromId(fromId)->GetName() + " has no permission to become holder.";
if (u->IsWearer())
msg += " The wearer is generally not allowed to adopt the holder role.";
SendMessage(msg, chatId);
}
}
// ------------------------------------------------------------------------
void Message::TeaserAction(String fromId, String chatId)
{
User *u = users.GetUserFromId(fromId);
String msg;
if (u && u->MayBecomeTeaser())
{
msg = "User " + users.GetUserFromId(fromId)->GetName() + " has now teaser permissions.";
if (u->IsHolder())
{
users.GetWearer()->UpdateRoleId(ROLE_WEARER_WAITING);
msg += " Since user " + users.GetUserFromId(fromId)->GetName() + " has given up the holder role, the wearer " + users.GetWearer()->GetName() + " is no longer captured. He is now released and received back his permissions to open the key safe.";
}
u->UpdateRoleId(ROLE_TEASER);
SendMessage(msg, chatId);
WriteCommandsAndSettings();
}
else
{
msg = SYMBOL_NO_ENTRY " User " + users.GetUserFromId(fromId)->GetName() + " has no permission to become teaser.";
SendMessage(msg, chatId);
}
}
// ------------------------------------------------------------------------
void Message::TeasingModeAction(bool mode, String fromId, String chatId)
{
Serial.println("*** Message::TeasingModeAction()");
if (users.GetHolder() && (users.GetHolder()->GetId() == fromId))
{
session.SetTeasingMode(mode);
if (mode)
SendMessage(COMMON_MSG_TEASING_ON, chatId);
else
SendMessage(COMMON_MSG_TEASING_OFF, chatId);
WriteCommandsAndSettings();
}
else
{
SendMessage(SYMBOL_NO_ENTRY " Request denied. Only the holder has the permission to control the rights for users with the teaser role.", chatId);
}
}
// ------------------------------------------------------------------------
void Message::GuestAction(String fromId, String chatId)
{
User *u = users.GetUserFromId(fromId);
String msg;
if (u)
{
msg = "User " + users.GetUserFromId(fromId)->GetName() + " has now guest permissions.";
if (u->IsHolder())
{
users.GetWearer()->UpdateRoleId(ROLE_WEARER_WAITING);
msg += " Since user " + users.GetUserFromId(fromId)->GetName() + " has given up the holder role, the wearer " + users.GetWearer()->GetName() + " is no longer captured. He is now released and received back his permissions to open the key safe.";
}
u->UpdateRoleId(ROLE_GUEST);
SendMessage(msg, chatId);
WriteCommandsAndSettings();
}
}
// ------------------------------------------------------------------------
void Message::RestrictUserAction(String fromId, String chatId)
{
User *u = users.GetUserFromId(fromId);
String msg;
if (u)
{
if (u->IsHolder())
{
long now = timeFunc.GetTimeInSeconds();
String untilDate = timeFunc.Time2String(now + 3600);
msg = "Restricting user " + users.GetWearer()->GetName() + " in chat (" + (!mutedWearer ? "unmuted" : "muted") + ") until " + untilDate + ".";
bot.restrictChatMember(chatId, users.GetWearer()->GetId(), ! mutedWearer, untilDate);
mutedWearer = ! mutedWearer;
}
else
{
msg = "Only the holder may apply restrictions.";
}
SendMessage(msg, chatId);
}
}
// ------------------------------------------------------------------------
void Message::VerificationModeAction(String commandParameter, String fromId, String chatId, bool force)
{
User *u = users.GetUserFromId(fromId);
String msg;
bool wearerMayUseCommand = false;
int minimumVerificationCount = 1;
if (verification.IsEnabled())
minimumVerificationCount = verification.GetRequiredCountPerDay() + 1;
int verificationsPerDay = commandParameter.toInt();
if (u->IsWearer() && (verificationsPerDay >= minimumVerificationCount))
wearerMayUseCommand = true;
if (u)
{
if (force ||
(u->IsHolder()) ||
(u->IsTeaser() && session.IsTeasingMode()) ||
wearerMayUseCommand)
{
if (commandParameter == "off")
{
verification.SetVerificationMode(false, 0);
SendMessage("Switching off verification mode.", chatId);
}
else
{
if (verificationsPerDay > MAX_VERIFICATIONS)
{
msg += "Maximum setting of required verifications reached.";
verificationsPerDay = MAX_VERIFICATIONS;
}
msg += "Switching on verification mode with " + commandParameter + " photos per day. This regulation remains active until it is switched off by a privileged user.";
verification.SetVerificationMode(true, verificationsPerDay);
SendMessage(msg, chatId);
}
WriteCommandsAndSettings();
}
else
{
if (u->IsWearer() && ! wearerMayUseCommand)
SendMessage("User " + u->GetName() + " has no rights to switch off the verification mode or set levels below " + verificationsPerDay + " verifications per day. Only the holder and teasers are allowed to do this.", chatId);
else
SendMessage("User " + u->GetName() + " has no rights to switch on the verification mode. Only the holder, teasers and the wearer are allowed to do this.", chatId);
}
}
}
// ------------------------------------------------------------------------
void Message::RandomShockModeAction(String commandParameter, String fromId, String chatId, bool force)
{
User *u = users.GetUserFromId(fromId);
String msg;
bool wearerMayUseCommand = false;
int wearerMinimumShockRate = 5;
if (session.IsRandomMode())
wearerMinimumShockRate = session.GetRandomModeShocksPerHour() + 1;
int shocksPerHour = commandParameter.toInt();
if (u->IsWearer() && (shocksPerHour >= wearerMinimumShockRate))
wearerMayUseCommand = true;
if (u)
{
if (force ||
(u->IsHolder()) ||
(u->IsTeaser() && session.IsTeasingMode()) ||
wearerMayUseCommand)
{
if (commandParameter == "off")
{
int creditsEarned = session.SetRandomMode(false);
SendMessage("Switching off random mode.", chatId);
MessageSendEarnedCredits(creditsEarned, chatId);
WriteCommandsAndSettings();
}
else
{
if (shocksPerHour > 0)
{
if (shocksPerHour > RANDOM_SHOCKS_PER_HOUR_MAX)
{
msg += "Maximum setting of random shocks per hour reached.";
shocksPerHour = RANDOM_SHOCKS_PER_HOUR_MAX;
}
msg += SYMBOL_COLLISION " Switching on random punishment mode with " + commandParameter + " shocks per hour on average. "
"This punishment remains active until it is switched off by a privileged user, " + String(RANDOM_SHOCK_AUTO_OFF_SECONDS/3600, DEC) +
" hours have passed or sleeping time of the wearer " + users.GetWearer()->GetName() + " begins.";
session.SetRandomMode(true, shocksPerHour);
SendMessage(msg, chatId);
WriteCommandsAndSettings();
}
else
{
SendMessage("Error: invalid number of shocks per hour provided: " + commandParameter, chatId);
}
}
}
else
{
if (u->IsWearer() && ! wearerMayUseCommand)
SendMessage("User " + u->GetName() + " has no rights to switch off the random mode or set levels below " + wearerMinimumShockRate + " shocks per hour. Only the holder and teasers are allowed to do this.", chatId);
else
SendMessage("User " + u->GetName() + " has no rights to switch on the random mode. Only the holder, teasers and the wearer are allowed to do this.", chatId);
}
}
}
// ------------------------------------------------------------------------
void Message::CheckVerificationAction(String caption, String fromId, String chatId)
{
if ((fromId == users.GetWearer()->GetId()) &&
((caption.indexOf("Verification") != -1) ||
(caption.indexOf("verification") != -1) ||
(caption.indexOf("verify") != -1) ||
(caption.indexOf("proof") != -1)))
{
verification.CheckIn(chatId);
}
}
// ------------------------------------------------------------------------
void Message::EmergencyAction(String fromId, String chatId)
{
if (users.GetWearer()->GetId() == fromId)
{
session.SetEmergencyReleaseCounterRequest(true);
SendMessageAll("An emergency release request has been received and will be processed within 5 minutes.", chatId);
}
else
SendMessage("Only the wearer may request for emergency release.", chatId);
}
// -------------------------------------------------
void Message::RestartRequest(String fromId, String chatId)
{
requestRestart = true;
requestChatId = chatId;
}
// -------------------------------------------------
void Message::RestartAction(String fromId, String chatId)
{
Serial.println("******************************************************************************************************");
Serial.println("*** RestartAction() ");
SendMessage("Restarting key safe...", chatId);
delay(3000);
ESP.restart();
}
// -------------------------------------------------
void Message::UnknownCommand(String chatId)
{
bot.sendMessage(chatId, "Error: unknown command.");
}
// -------------------------------------------------
void Message::ResetRequests()
{
requestRestart = false;
requestChatId = "";
}
// -------------------------------------------------
void Message::ProcessPendingRequests()
{
if (requestRestart)
RestartAction(USER_ID_BOT, requestChatId);
ResetRequests();
}
// -------------------------------------------------
void Message::ProcessChatMessage(String msg, String fromId, String chatId)
{
// This method is meant to look into the general communication beyond the direct commands for the bot.
Serial.println("*** ProcessChatMessage()");
Serial.println(msg);
}
// -------------------------------------------------
void Message::ProcessNewMessages()
{
int newMessageCount = bot.getUpdates(bot.last_message_received + 1);
// Serial.print("*** Message::ProcessNewMessages(");
// Serial.print(String(newMessageCount));
// Serial.println(")");
while (newMessageCount)
{
Serial.println("Message received.");
for (int i = 0; i < newMessageCount; i++)
{
// clear previous requests
ResetRequests();
// Chat id of the requester
String chat_id = String(bot.messages[i].chat_id);
Serial.print("Chat ID: ");
Serial.println(chat_id);
// check for photo
bool hasPhoto = bot.messages[i].hasPhoto;
String caption = bot.messages[i].file_caption;
Serial.print("- hasPhoto: ");
Serial.println(hasPhoto);
Serial.print("- caption: ");
Serial.println(caption);
// Print the received message
String from_name = bot.messages[i].from_name;
String from_id = bot.messages[i].from_id;
Serial.print(from_name);
Serial.print(": ");
String text = bot.messages[i].text;
Serial.println(text);
if (text.substring(text.length() - 14) == "@shockcell_bot")
text = text.substring(0, text.length() - 14);
Serial.println(text);
users.AddUser(from_id, from_name);
// execute commands
if (text == "/start")
{
String welcome = "Welcome, " + from_name + ", use the following commands to control " + users.GetWearer()->GetName() + "'s ShockCell.\n\n";
welcome += "Commands for the ";
switch (users.GetUserFromId(from_id)->GetRoleId())
{
case ROLE_WEARER_CAPTURED:
case ROLE_WEARER_WAITING:
case ROLE_WEARER_FREE:
welcome += "wearer:\n" + botCommandsWearer;
break;
case ROLE_HOLDER:
welcome += "holder:\n" + botCommandsHolder;
break;
case ROLE_TEASER:
welcome += "teaser:\n" + botCommandsTeaser;
break;
case ROLE_GUEST:
welcome += "guest:\n" + botCommandsGuest;
break;
}
SendMessage(welcome, chat_id);
}
else if (text.substring(0, 6) == "/shock")
{
if (text[6] == '_')
ShockAction(text.substring(7), 1, from_id, chat_id);
else
ShockAction(text.substring(6), 1, from_id, chat_id);
}
else if (text == "/state")
{
session.InfoChastikey();
MessageState(chat_id);
}
else if (text == "/unlock")
UnlockAction(from_id, chat_id);
else if (text == "/play4unlock")
Play4UnlockAction(from_id, chat_id);
else if (text.substring(0, 5) == "/play")
PlayAction(text.substring(6), from_id, chat_id);
else if (text == "/users")