forked from randomouscrap98/SmileBASICSourceChat
/
chat.cs
991 lines (861 loc) · 38.4 KB
/
chat.cs
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
using System;
using System.Net;
using System.Timers;
using System.Linq;
using System.Collections.Generic;
using System.Threading;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using MyExtensions;
using ChatEssentials;
using ModuleSystem;
using MyWebSocket;
using MyExtensions.Logging;
namespace ChatServer
{
public delegate void ChatEventHandler(object source, EventArgs e);
//This is the thing that will get passed as the... controller to Websockets?
public class Chat : WebSocketUser
{
public const int HeaderSize = 64;
private int uid = -1;
private long sessionID = 0;
public readonly DateTime Startup = DateTime.Now;
private readonly System.Timers.Timer userUpdateTimer = new System.Timers.Timer();
private readonly ChatServer manager;
private readonly Queue<string> backlog = new Queue<string>();
private readonly Object backlock = new Object();
private readonly Object sendlock = new Object();
private DateTime lastPing = DateTime.Now;
//private SimpleIRCRelay relay = SimpleIRCRelay.DefaultRelay;
//public event ChatEventHandler OnUserListChange;
//Set up the logger when building the chat provider. Logging will go out to a file and the console
//if possible. Otherwise, log to the default logger (which is like throwing them away)
public Chat(int userUpdateInterval, ChatServer manager)
{
userUpdateTimer.Interval = userUpdateInterval * 1000;
userUpdateTimer.Elapsed += UpdateActiveUserList;
userUpdateTimer.Start();
//Assume the manager that was given was good
this.manager = manager;
foreach (Module module in manager.GetModuleListCopy())
module.OnExtraCommandOutput += DefaultOutputMessages;
}
public void Log(string message, LogLevel level = LogLevel.Normal)
{
string tag = "Chatuser";
if (UID > 0)
tag += UID;
else
tag += "X";
manager.ChatSettings.LogProvider.LogGeneral(message, level, tag);
// if (manager != null)
// return manager.Logger;
// else
// return new MyExtensions.Logging.Logger(10);
}
//This should be the ONLY place where the active state changes
private void UpdateActiveUserList(object source, System.Timers.ElapsedEventArgs e)
{
if (ThisUser.StatusChanged)
{
ThisUser.SaveActiveState();
manager.BroadcastUserList();
Log(UserLogString + " became " + (ThisUser.Active ? "active" : "inactive"), MyExtensions.Logging.LogLevel.Debug);
}
}
//The UID for this session
public int UID
{
get { return uid; }
}
public TimeSpan TimeSincePing
{
get
{
return DateTime.Now - lastPing;
}
}
//The user attached to this session
public User ThisUser
{
get
{
return manager.GetUser(UID);
}
}
/*public string IrcUsername
{
get { return relay.Username; }
}*/
public string UserLogString
{
get
{
return ThisUser.Username + " (" + ThisUser.UID + ")";
}
}
public void MySend(string message, bool forceSend = false)
{
if (string.IsNullOrEmpty(message))
return;
//Do nothing until user has accepted policy
if (!ThisUser.AcceptedPolicy && !forceSend)
{
Log("Cannot send message because user has not accepted policy yet. ForceSend: " + forceSend,
MyExtensions.Logging.LogLevel.SuperDebug);
return;
}
manager.Bandwidth.AddOutgoing(message.Length + HeaderSize);
//All sends just enqueue their message no matter what
lock (backlock)
{
if (backlog.Count >= 30)
Log(ThisUser.Username + "'s backlog is too big (30). Message thrown away.", LogLevel.Warning);
else
backlog.Enqueue(message);
}
//Only try to send if we're not currently sending
if (Monitor.TryEnter(sendlock))
{
try
{
//Loop forever because I refuse to lock on the loop
while (true)
{
string nextMessage = null;
//NOW we lock so we can pull the next message to send.
lock (backlock)
{
if (backlog.Count > 0)
nextMessage = backlog.Dequeue();
}
//oops, no more messages. gtfo
if (nextMessage == null)
break;
//OK, now we can send it off
try
{
Log("Sending message: " + nextMessage.Truncate(100), MyExtensions.Logging.LogLevel.SuperDebug);
Send(nextMessage);
Log("Send success!", MyExtensions.Logging.LogLevel.SuperDebug);
}
catch (Exception e)
{
Log("Cannot send message: " + nextMessage + " to user: " + UserLogString + " because: " + e, LogLevel.Warning);
}
}
}
finally
{
Monitor.Exit(sendlock);
}
}
else
{
Log("Queueing message for " + ThisUser.Username, MyExtensions.Logging.LogLevel.SuperDebug);
}
}
public void MySend(LanguageTagParameters parameters, JSONObject container)
{
string message = manager.ConvertTag(parameters);
if (container is WarningJSONObject)
{
((WarningJSONObject)container).message = message;
}
else if (container is SystemMessageJSONObject)
{
((SystemMessageJSONObject)container).message = message;
}
else
{
Log("Didn't get a proper container for a language tag. Using system message as default", LogLevel.Warning);
((SystemMessageJSONObject)container).message = message;
}
MySend(container.ToString());
}
public WarningJSONObject NewWarningFromTag(LanguageTagParameters parameters)
{
return new WarningJSONObject(manager.ConvertTag(parameters));
}
public SystemMessageJSONObject NewSystemMessageFromTag(LanguageTagParameters parameters)
{
return new SystemMessageJSONObject(manager.ConvertTag(parameters));
}
public LanguageTagParameters QuickParams(ChatTags tag)
{
return new LanguageTagParameters(tag, ThisUser);
}
// public void FillContainer(LanguageTagParameters parameters, JSONObject container)
// {
//
// }
// protected override void OnOpen()
// {
// //I don't know what we're doing for OnOpen yet.
// }
//On closure of the websocket, remove ourselves from the list of active
//chatters.
public override void ClosedConnection()
{
Log ("Session disconnect: " + uid);
//Only perform special leaving messages and processing if the user was real
if (UID > 0)
{
ThisUser.PerformOnChatLeave(sessionID);
// if (!ThisUser.PerformOnChatLeave())
// Log("User session timer was in an invalid state!", LogLevel.Warning);
if (ThisUser.ShowMessages)
manager.Broadcast(new LanguageTagParameters(ChatTags.Leave, ThisUser), new SystemMessageJSONObject());
}
//Now "technically" remove the user from lists
int tempID = uid;
uid = -1;
manager.UpdateAuthUserlist();
manager.BroadcastUserList();
//Uh now put it back?
uid = tempID;
//Now get rid of events
userUpdateTimer.Elapsed -= UpdateActiveUserList;
foreach (Module module in manager.GetModuleListCopy())
module.OnExtraCommandOutput -= DefaultOutputMessages;
// manager.LeaveChat(this);
//
// foreach (Module module in manager.GetModuleListCopy())
// module.OnExtraCommandOutput -= DefaultOutputMessages;
//relay.Disconnect();
//relay.Dispose();
}
// protected override void OnError(ErrorEventArgs e)
// {
// if(e.Message != null)
// Logger.Error("UID: " + uid + " - " + e.Message, "WebSocket");
// if(e.Exception != null)
// Logger.Error(e.Exception.ToString(), "WebSocket");
//
// //OK let's see what happens here
// //OnClose(null);
// //base.OnError(e);
// }
//I guess this is WHENEVER it receives a message?
public override void ReceivedMessage(string rawMessage)
{
//Log("Got message: " + rawMessage, MyExtensions.Logging.LogLevel.Debug);
ResponseJSONObject response = new ResponseJSONObject();
response.result = false;
dynamic json = new Object();
string type = "";
//You HAVE to do this, even though it seems pointless. Users need to show up as banned immediately.
ThisUser.PullInfoFromQueryPage();
//Before anything else, log the amount of incoming data
if (!string.IsNullOrEmpty(rawMessage))
{
manager.Bandwidth.AddIncoming(rawMessage.Length + HeaderSize);
}
//First, just try to parse the JSON they gave us. If it's absolute
//garbage (or just not JSON), let them know and quit immediately.
try
{
json = JsonConvert.DeserializeObject(rawMessage);
type = json.type;
response.from = type;
}
catch
{
response.errors.Add("Could not parse JSON");
}
//If we got a bind message, let's try to authorize this channel.
if (type == "bind")
{
if (uid > 0)
{
response.errors.Add("Received another bind message, but you've already been authenticated.");
}
else
{
try
{
//First, gather information from the JSON. This is so that if
//the json is invalid, it will fail as soon as possible
string key = (string)json.key;
int newUser = (int)json.uid;
//Oops, username was invalid
if (newUser <= 0)
{
Log("Tried to bind a bad UID: " + newUser);
response.errors.Add("UID was invalid");
}
else
{
//List<Chat> removals;
string error;
if (!manager.CheckAuthentication(newUser, key, out error)) //newUser, key, out removals, out error))
{
response.errors.Add(error);
}
else
{
//Before we do anything, remove other chatting sessions
foreach (Chat removeChat in GetAllUsers().Select(x => (Chat)x).Where(x => x.UID == newUser && x != this))
removeChat.CloseSelf();
//Sessions.CloseSession(removeChat.ID);
uid = newUser;
//BEFORE adding, broadcast the "whatever has entered the chat" message
if (ThisUser.ShowMessages)
{
manager.Broadcast(QuickParams(ChatTags.Join), new SystemMessageJSONObject(),
new List<Chat> { this });
}
MySend(NewSystemMessageFromTag(QuickParams(ChatTags.Welcome)).ToString());
ChatTags enterSpamWarning = ThisUser.JoinSpam();
if (enterSpamWarning != ChatTags.None)
MySend(NewWarningFromTag(QuickParams(enterSpamWarning)).ToString());
//BEFORE sending out the user list, we need to perform onPing so that it looks like this user is active
sessionID = ThisUser.PerformOnChatEnter();
// if (!ThisUser.PerformOnChatEnter())
// Log("Invalid session entry. Sessions may be broken", LogLevel.Warning);
manager.BroadcastUserList();
Log("Authentication complete: UID " + uid + " maps to username " + ThisUser.Username +
(ThisUser.CanStaffChat ? "(staff)" : ""));
response.result = true;
List<JSONObject> outputs = new List<JSONObject>();
Dictionary<int, UserInfo> currentUsers = manager.UsersForModules();
//Also do some other crap
foreach (Module module in manager.GetModuleListCopy())
{
if (Monitor.TryEnter(module.Lock, manager.ChatSettings.MaxModuleWait))
{
try
{
outputs.AddRange(module.OnUserJoin(currentUsers[ThisUser.UID], currentUsers));
}
finally
{
Monitor.Exit(module.Lock);
}
}
else
{
Log("Skipped " + module.ModuleName + " join processing",
MyExtensions.Logging.LogLevel.Warning);
}
}
OutputMessages(outputs, ThisUser.UID);
//Finally, output the "Yo accept dis" thing if they haven't already.
if(!ThisUser.AcceptedPolicy)
{
MessageListJSONObject emptyMessages = new MessageListJSONObject();
UserListJSONObject emptyUsers = new UserListJSONObject();
ModuleJSONObject policy = new ModuleJSONObject(ChatServer.Policy);
ModuleJSONObject accept = new ModuleJSONObject("\nYou must accept this chat policy before " +
"using the chat. Type /accept if you accept the chat policy\n");
MySend(emptyMessages.ToString(), true);
MySend(emptyUsers.ToString(), true);
MySend(policy.ToString(), true);
MySend(accept.ToString(), true);
}
else if(ThisUser.ShouldPolicyRemind)
{
ModuleJSONObject policy = new ModuleJSONObject(ChatServer.Policy);
MySend(policy.ToString());
ThisUser.PerformOnReminder();
}
//Now set up the IRC relay. Oh boy, let's hope this works!
/*relay = new SimpleIRCRelay(manager.IrcServer, manager.IrcChannel, ThisUser.Username, Logger);
relay.ConnectAsync();*/
//relay.IRCRelayMessageEvent += OnIrcRelayMessage;
}
}
}
catch
{
response.errors.Add("BIND message was missing fields");
}
}
}
else if (type == "ping")
{
lastPing = DateTime.Now;
bool active = true;
try
{
active = (bool)json.active;
//relay.Ping();
}
catch (Exception messageError)
{
response.errors.Add("Internal server error: " + messageError/*.Message*/);
}
ThisUser.PerformOnPing(active);
UpdateActiveUserList(null, null);
}
else if (type == "message")
{
try
{
//First, gather information from the JSON. This is so that if
//the json is invalid, it will fail as soon as possible
string key = json.key;
string message = System.Security.SecurityElement.Escape((string)json.text);
string tag = json.tag;
//These first things don't increase spam score in any way
if (string.IsNullOrWhiteSpace(message))
{
response.errors.Add("No empty messages please");
}
else if (!manager.CheckKey(uid, key))
{
Log("Got invalid key " + key + " from " + UserLogString);
response.errors.Add("Your key is invalid");
}
else if (!ThisUser.AcceptedPolicy)
{
if(message != "/accept")
{
response.errors.Add("The only command available right now is /accept");
}
else
{
ModuleJSONObject acceptSuccess = new ModuleJSONObject("You have accepted the SmileBASIC Source " +
"chat policy. Please use the appropriate chat tab for discussion about SmileBASIC or off-topic " +
"subjects!");
MySend(acceptSuccess.ToString(), true);
Thread.Sleep(2000);
ThisUser.AcceptPolicy();
ThisUser.PerformOnReminder();
response.result = true;
MySend(manager.ChatUserList(UID));
MySend(manager.ChatMessageList(UID));
}
}
else if (ThisUser.Blocked)
{
response.errors.Add(NewWarningFromTag(QuickParams(ChatTags.Blocked)).message);
}
else if (ThisUser.Banned)
{
response.errors.Add("You are banned from chat for " +
StringExtensions.LargestTime(ThisUser.BannedUntil - DateTime.Now));
}
else if (tag == "admin" && !ThisUser.CanStaffChat ||
tag == manager.ChatSettings.GlobalTag && !ThisUser.CanGlobalChat)
{
response.errors.Add("You can't post messages here. I'm sorry.");
}
else if (!manager.ValidTagForUser(UID, tag))
{
response.errors.Add("Your post has an unrecognized tag. Cannot display");
}
else
{
Dictionary<int, UserInfo> currentUsers = manager.UsersForModules();
List<JSONObject> outputs = new List<JSONObject>();
UserMessageJSONObject userMessage = new UserMessageJSONObject(ThisUser, message, tag);
UserCommand userCommand;
Module commandModule;
string commandError = "";
//Step 1: parse a possible command. If no command is parsed, no module will be written.
if (TryCommandParse(userMessage, out commandModule, out userCommand, out commandError))
{
Log("Trying to use module " + commandModule.ModuleName + " to process command " +
userCommand.message + " from " + ThisUser.Username, MyExtensions.Logging.LogLevel.SuperDebug);
//We found a command. Send it off to the proper module and get the output
if (Monitor.TryEnter(commandModule.Lock, manager.ChatSettings.MaxModuleWait))
{
try
{
outputs.AddRange(commandModule.ProcessCommand(userCommand, currentUsers[ThisUser.UID], currentUsers));
}
finally
{
Monitor.Exit(commandModule.Lock);
}
}
else
{
response.errors.Add("The chat server is busy and can't process your command right now");
userMessage.SetHidden();
userMessage.SetUnspammable();
}
//do not update spam score if command module doesn't want it
if (!userCommand.MatchedCommand.ShouldUpdateSpamScore)
userMessage.SetUnspammable();
//For now, simply capture all commands no matter what.
userMessage.SetHidden();
//userMessage.SetCommand();
Log("Module " + commandModule.ModuleName + " processed command from " + UserLogString,
MyExtensions.Logging.LogLevel.Debug);
}
else
{
//If an error was given, add it to our response
if (!string.IsNullOrWhiteSpace(commandError))
{
response.errors.Add("Command error: " + commandError);
userMessage.SetHidden();
userMessage.SetUnspammable();
}
}
ChatTags warning = manager.AddMessage(userMessage);
//Send off on relay
/*if(userMessage.Display && userMessage.tag == manager.IrcTag)
{
if(relay.SendMessage(userMessage.message))
Logger.LogGeneral("Sent message on IRC relay!", MyExtensions.Logging.LogLevel.SuperDebug);
else
Logger.LogGeneral("Couldn't send on IRC relay!", MyExtensions.Logging.LogLevel.SuperDebug);
}*/
if (warning != ChatTags.None)
outputs.Add(NewWarningFromTag(QuickParams(warning)));
response.result = response.errors.Count == 0;
//Now send out userlist if active status changed
UpdateActiveUserList(null, null);
//Since we added a new message, we need to broadcast.
if (response.result && userMessage.Display)
manager.BroadcastMessageList(); //CRASH ALMOST CERTAINLY HAPPENS HERE!!!!!!!###$$$$$!!!!!!!!!!*$*$*$*
//Step 2: run regular message through all modules' regular message processor (probably no output?)
if(manager.ChatSettings.AcceptedTags.Contains(userMessage.tag))
{
foreach (Module module in manager.GetModuleListCopy())
{
if (Monitor.TryEnter(module.Lock, manager.ChatSettings.MaxModuleWait))
{
try
{
module.ProcessMessage(userMessage, currentUsers[ThisUser.UID], currentUsers);
}
finally
{
Monitor.Exit(module.Lock);
}
}
else
{
Log("Skipped " + module.ModuleName + " message processing",
MyExtensions.Logging.LogLevel.Warning);
}
}
}
//Step 3: run all modules' post processor (no message required)
//Is this even necessary? It was necessary before because the bot ran on a timer. Without a timer,
//each module can just specify that it wants to do things at random points with its own timer.
//Step 4: iterate over returned messages and send them out appropriately
OutputMessages(outputs, ThisUser.UID, tag);
//end of regular message processing
}
}
catch (Exception messageError)
{
response.errors.Add("Internal server error: " + messageError/*.Message*/);
//response.errors.Add("Message was missing fields");
}
}
// else if (type == "createroom")
// {
// try
// {
// List<int> users = json.users.ToObject<List<int>>();
// string error;
//
// if(!manager.CreatePMRoom(new HashSet<int>(users), ThisUser.UID, out error))
// {
// response.errors.Add(error);
// }
// else
// {
// MySend((new SystemMessageJSONObject("You created a chat room for " + string.Join(", ", users.Select(x => manager.GetUser(x).Username)))).ToString());
// manager.BroadcastUserList();
// }
// }
// catch
// {
// response.errors.Add("Could not parse PM creation room message");
// }
// }
else if (type == "request")
{
try
{
string wanted = json.request;
if(wanted == "userList")
{
MySend(manager.ChatUserList(UID));
response.result = true;
}
else if (wanted == "messageList")
{
MySend(manager.ChatMessageList(UID));
response.result = true;
}
else
{
response.errors.Add("Invalid request field");
}
}
catch
{
response.errors.Add("Request was missing fields");
}
}
//Send the "OK" message back.
MySend(response.ToString(), true);
}
private void OutputMessages(List<JSONObject> outputs, int receiverUID, string defaultTag = "")
{
//We're not the ones you're looking for...
if (ThisUser.UID != receiverUID)
return;
if (!manager.ChatSettings.AcceptedTags.Contains(defaultTag))
defaultTag = manager.ChatSettings.GlobalTag;
foreach(JSONObject jsonMessage in outputs)
{
//System messages are easy: just send to user.
if (jsonMessage is SystemMessageJSONObject)
{
MySend(jsonMessage.ToString());
}
else if (jsonMessage is WarningJSONObject)
{
MySend(jsonMessage.ToString());
}
// else if (jsonMessage is SystemRequest)
// {
// //System requests made by modules are passed to the manager. I don't handle that crap.
// manager.OnRequest((SystemRequest)jsonMessage);
// }
else if (jsonMessage is ModuleJSONObject)
{
ModuleJSONObject tempJSON = jsonMessage as ModuleJSONObject;
tempJSON.uid = uid;
if(string.IsNullOrWhiteSpace(tempJSON.tag))
tempJSON.tag = manager.ChatSettings.GlobalTag;
if(tempJSON.broadcast)
{
manager.BroadcastExclude(tempJSON.ToString(), tempJSON.tag);
//manager.Broadcast(tempJSON.ToString());
Log("Broadcast a module message", MyExtensions.Logging.LogLevel.Debug);
}
else
{
//No recipients? You probably meant to send it to the current user.
if(tempJSON.recipients.Count == 0)
MySend(tempJSON.ToString());
else
manager.SendMessage(tempJSON);
}
}
}
}
private void DefaultOutputMessages(List<JSONObject> outputs, int receiverUID)
{
OutputMessages(outputs, receiverUID);
}
private string ParseUser(string user)
{
if (user.StartsWith("??"))
{
return StringExtensions.AutoCorrectionMatch(user.Replace("??", ""),
manager.UsersForModules().Select(x => x.Value.Username).ToList());
}
else if (user.StartsWith("?"))
{
return StringExtensions.AutoCorrectionMatch(user.Replace("?", ""),
manager.UsersForModules().Where(x => x.Value.LoggedIn).Select(x => x.Value.Username).ToList());
}
else if (user.StartsWith("#"))
{
//Console.WriteLine("User: '" + user + "'");
int id = 0;
string username = "no";
if (int.TryParse(user.Replace("#", ""), out id) &&
manager.UsersForModules().ContainsKey(id))
{
username = manager.UsersForModules()[id].Username;
}
return username;
}
return user;
}
/// <summary>
/// Parse and build command if possible. Assign module which will handle command.
/// </summary>
/// <returns>Successful command parse</returns>
/// <param name="message">Message.</param>
/// <param name="commandModule">Command module.</param>
/// <param name="userCommand">User command.</param>
private bool TryCommandParse(UserMessageJSONObject message, out Module commandModule, out UserCommand userCommand, out string error)
{
userCommand = null;
commandModule = null;
error = "";
UserCommand tempUserCommand = null;
List<Module> modules = manager.GetModuleListCopy(UID);
string realMessage = System.Net.WebUtility.HtmlDecode(message.message);
//Check through all modules for possible command match
foreach(Module module in modules)
{
//We already found the module, so get out.
if(commandModule != null)
break;
//Check through this module's command for possible match
foreach(ModuleCommand command in module.Commands)
{
Match match = Regex.Match(realMessage, command.FullRegex, RegexOptions.Singleline);
//Match partialMatch = Regex.Match(message.message, command.CommandRegex, RegexOptions.Singleline);
//This command matched, so preparse the command and get out of here.
if (match.Success)
{
//Build arguments from regex.
List<string> arguments = new List<string>();
for (int i = 2; i < match.Groups.Count; i++)
arguments.Add(match.Groups[i].Value.Trim());
//We have a user command. Cool, but will it parse? Ehhhh.
tempUserCommand = new UserCommand(match.Groups[1].Value, arguments, message, command);
//Now preprocess the command to make sure certain standard fields check out (like username)
for (int i = 0; i < command.Arguments.Count; i++)
{
//Users need to exist. If not, throw error.
if (command.Arguments[i].Type == ArgumentType.User)
{
tempUserCommand.Arguments[i] = ParseUser(tempUserCommand.Arguments[i]);
for (int j = 0; j < tempUserCommand.ArgumentParts[i].Count; j++)
tempUserCommand.ArgumentParts[i][j] = ParseUser(tempUserCommand.ArgumentParts[i][j]);
if (!((tempUserCommand.MatchedCommand.Arguments[i].Repeat == RepeatType.ZeroOrOne ||
tempUserCommand.MatchedCommand.Arguments[i].Repeat == RepeatType.ZeroOrMore) &&
string.IsNullOrWhiteSpace(tempUserCommand.Arguments[i])) &&
(tempUserCommand.ArgumentParts[i].Count == 0 && manager.UserLookup(tempUserCommand.Arguments[i]) < 0 ||
tempUserCommand.ArgumentParts[i].Any(x => manager.UserLookup(x) < 0)))
{
error = "User does not exist";
return false;
}
}
else if (command.Arguments[i].Type == ArgumentType.Module)
{
if (!modules.Any(x => x.Nickname == arguments[i].ToLower()))
{
error = "Module does not exist";
return false;
}
}
}
commandModule = module;
break;
}
}
}
if (commandModule == null && ModuleCommand.IsACommand(message.message))
{
//OOPS! A command was parsed but it wasn't parsed correctly. Doop
error = "\"" + message.message + "\" was not recognized. Maybe something was misspelled, or you were missing arguments";
return false;
}
userCommand = new UserCommand(tempUserCommand);
return (commandModule != null);
}
}
public class GlobalModule : Module
{
public GlobalModule()
{
Commands.AddRange(new List<ModuleCommand> {
new ModuleCommand("about", new List<CommandArgument>(), "See information about the chat server"),
new ModuleCommand("policy", new List<CommandArgument>(), "View the SmileBASIC Source chat policy"),
new ModuleCommand("uactest", new List<CommandArgument> {
new CommandArgument("shortuser", ArgumentType.User)
}, "Test user autocorrection (use ? on username to enable shorthand)"),
new ModuleCommand("help", new List<CommandArgument>(), "See all modules which you can get help with"),
new ModuleCommand("help", new List<CommandArgument>() {
new CommandArgument("module", ArgumentType.Module)
}, "Get help about a particular module"),
new ModuleCommand("helpregex", new List<CommandArgument>() {
new CommandArgument("module", ArgumentType.Module)
}, "Get help about a particular module + regex")
});
}
public override List<JSONObject> ProcessCommand(UserCommand command, UserInfo user, Dictionary<int, UserInfo> users)
{
List<JSONObject> outputs = new List<JSONObject>();
ModuleJSONObject output = new ModuleJSONObject();
switch (command.Command)
{
case "about":
output = new ModuleJSONObject();
BandwidthContainer bandwidth = ChatRunner.Bandwidth; //Chat.GetBandwidth();
DateTime built = ChatRunner.MyBuildDate();
DateTime crashed = ChatRunner.LastCrash();
string crashedString = "never";
if (crashed.Ticks > 0)
crashedString = StringExtensions.LargestTime(DateTime.Now - crashed) + " ago";
output.message =
"---Build info---\n" +
"Version: " + ChatRunner.AssemblyVersion() + "\n" +
"Runtime: " + StringExtensions.LargestTime(DateTime.Now - ChatRunner.Startup) + "\n" +
"Built " + StringExtensions.LargestTime(DateTime.Now - built) + " ago (" + built.ToString("R") + ")\n" +
"---Data usage---\n" +
"Outgoing: " + bandwidth.GetTotalBandwidthOutgoing() + " (1h: " + bandwidth.GetHourBandwidthOutgoing() + ")\n" +
"Incoming: " + bandwidth.GetTotalBandwidthIncoming() + " (1h: " + bandwidth.GetHourBandwidthIncoming() + ")\n" +
"---Websocket---\n" +
"Library Version: " + WebSocketServer.Version + "\n" +
"Last full crash: " + crashedString;
outputs.Add(output);
break;
case "policy":
output = new ModuleJSONObject(ChatServer.Policy);
outputs.Add(output);
break;
case "help":
output = new ModuleJSONObject();
if (command.Arguments.Count == 0)
{
output.message = "Which module would you like help with?\n";
foreach (Module module in ChatRunner.Server.GetModuleListCopy(user.UID))
output.message += "\n" + module.Nickname;
output.message += "\n\nRerun help command with a module name to see commands for that module";
outputs.Add(output);
}
else
{
output.message = GetModuleHelp(command.Arguments[0], user);
outputs.Add(output);
}
break;
case "helpregex":
output.message = GetModuleHelp(command.Arguments[0], user, true);
outputs.Add(output);
break;
case "uactest":
output = new ModuleJSONObject();
output.message = "User " + command.OriginalArguments[0] + " corrects to " + command.Arguments[0];
outputs.Add(output);
break;
}
return outputs;
}
public string GetModuleHelp(string moduleString, UserInfo user, bool showRegex = false)
{
string message = "Help for the " + moduleString + " module:\n";
Module module = null;
try
{
module = ChatRunner.Server.GetModuleListCopy(user.UID).First(x => x.Nickname == moduleString);
if (!string.IsNullOrWhiteSpace(module.GeneralHelp))
message += "\n" + module.GeneralHelp + "\n";
foreach(ModuleCommand moduleCommand in module.Commands)
message += "\n" + moduleCommand.DisplayString + (showRegex ? " : " + moduleCommand.FullRegex : "");
if (showRegex)
{
if (module.ArgumentHelp.Count > 0)
message += "\n\nSome argument regex (uses standard regex syntax):";
foreach (KeyValuePair<string, string> argHelp in module.ArgumentHelp)
message += "\n" + argHelp.Key + " - " + argHelp.Value;
}
}
catch
{
message += "\nThis module is hidden or does not exist.";
}
return message;
}
}
}