/
messages.cs
472 lines (414 loc) · 12.9 KB
/
messages.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
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Threading;
using System.Linq;
namespace ChatEssentials
{
/// <summary>
/// A generic object sent from the server. All things sent from the server must have at
/// least an ID and a type.
/// </summary>
[Serializable]
public abstract class JSONObject
{
private static long LastID = DateTime.Now.Ticks;
private static readonly Object IDLock = new Object();
/// <summary>
/// Use this if you need to reorder messages; the ID is automatically generated
/// based on the last JSON message created.
/// </summary>
public void SetIDNow()
{
lock (IDLock)
{
id = DateTime.Now.Ticks;
if (id <= LastID)
id = LastID + 1;
LastID = id;
}
}
public readonly string type;
public long id = 0;
public JSONObject(string type)
{
this.type = type;
SetIDNow();
}
public JSONObject(JSONObject copy)
{
if (copy != null)
{
type = copy.type;
id = copy.id;
}
}
public override string ToString ()
{
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.StringEscapeHandling = StringEscapeHandling.EscapeHtml;
return JsonConvert.SerializeObject(this, settings);
}
}
public enum MessageBaseSendType
{
OnlyRecipients,
IncludeSender,
BroadcastExceptSender,
Broadcast
}
/// <summary>
/// Any JSON sent from the server that contains a message should derive from this class.
/// </summary>
public class MessageBaseJSONObject : JSONObject
{
public const string DefaultTag = "none";
public const string DefaultEncoding = "text";
public const string DefaultSubtype = "";
public string tag;
public string encoding;
public string subtype;
public bool safe;
public UserJSONObject sender;
protected string rawmessage;
protected DateTime rawtime;
protected DateTime expiration;
/// <summary>
/// Determines how the message will be sent. Defaults to recipients only
/// </summary>
public MessageBaseSendType sendtype;
/// <summary>
/// Set the recipients of this message. If the sendtype is "Broadcast", this
/// field is completely ignored. If sendtype is "IncludeSender", the sender
/// will be included in the list of recipients.
/// </summary>
public List<int> recipients;
public string message
{
get
{
if (safe)
return System.Security.SecurityElement.Escape(rawmessage);
else
return rawmessage;
}
set
{
rawmessage = value;
}
}
public string time
{
get { return rawtime.ToString("o"); }// + " UTC"; }
set { }
}
public DateTime GetCreationTime()
{
return rawtime;
}
public string GetRawMessage()
{
return rawmessage;
}
public List<int> RealRecipientList(List<int> allUsers)
{
if (sendtype == MessageBaseSendType.Broadcast)
{
return new List<int>(allUsers);
}
else if (sendtype == MessageBaseSendType.BroadcastExceptSender)
{
if(HasSender())
return allUsers.Except(new[] { sender.uid }).ToList();
else
return new List<int>(allUsers);
}
else if (sendtype == MessageBaseSendType.OnlyRecipients)
{
return new List<int>(recipients);
}
else if (sendtype == MessageBaseSendType.IncludeSender)
{
//Only include the sender if they're valid and they're not already in the recipient list.
if (HasSender() && !recipients.Contains(sender.uid))
return recipients.Concat(new[] { sender.uid }).ToList();
else
return new List<int>(recipients);
}
else
{
//Send to nobody if you can't make up your goddamn mind
return new List<int>();
}
}
public void SetNoRecipients()
{
recipients = new List<int>();
sendtype = MessageBaseSendType.OnlyRecipients;
}
public bool IsSendable()
{
return RealRecipientList(new List<int>(){ 1 }).Count > 0;
}
public bool HasSender()
{
return sender != null && sender.uid > 0;
}
public bool HasExpired()
{
return DateTime.Now > expiration;
}
public MessageBaseJSONObject() : this("none") {}
public MessageBaseJSONObject(string type, string message = "", UserInfo user = null) : base(type)
{
this.encoding = DefaultEncoding;
this.tag = DefaultTag;
this.subtype = DefaultTag;
this.message = message;
this.sender = new UserJSONObject(user);
this.sendtype = MessageBaseSendType.IncludeSender;
this.safe = true;
this.recipients = new List<int>();
//this.moduleRecipients = new List<string>();
this.rawtime = DateTime.Now;
this.expiration = DateTime.Now.AddHours(23.9);
}
public MessageBaseJSONObject(MessageBaseJSONObject copy) : base(copy)
{
if(copy != null)
{
tag = copy.tag;
encoding = copy.encoding;
subtype = copy.subtype;
rawmessage = copy.rawmessage;
safe = copy.safe;
sender = new UserJSONObject(copy.sender);
sendtype = copy.sendtype;
recipients = new List<int>(copy.recipients);
rawtime = copy.rawtime;
}
}
}
//Responses from the server should have this format.
public class ResponseJSONObject : JSONObject
{
public ResponseJSONObject() : base("response") {}
public string from = "unknown";
public bool result = false;
public List<string> errors = new List<string>();
public Dictionary<string, object> extras = new Dictionary<string, object>();
}
//Warnings to be sent to (usually one) users
public class WarningMessageJSONObject : MessageBaseJSONObject
{
private void Setup()
{
expiration = DateTime.Now.AddSeconds(60);
//sendtype = MessageBaseSendType.IncludeSender;
}
public WarningMessageJSONObject(string message = "", UserInfo user = null) : base("warning", message, user)
{
Setup();
}
public WarningMessageJSONObject(WarningMessageJSONObject copy) : base(copy)
{
Setup();
}
}
//System messages have a similar format to warnings.
public class SystemMessageJSONObject : MessageBaseJSONObject
{
private void Setup()
{
expiration = DateTime.Now.AddSeconds(5);
//sendtype = MessageBaseSendType.IncludeSender;
}
public SystemMessageJSONObject(string message = "", UserInfo user = null) : base("system", message, user)
{
Setup();
}
public SystemMessageJSONObject(SystemMessageJSONObject copy) : base(copy)
{
Setup();
}
}
//The list of messages sent out in JSON should follow this format
public class MessageListJSONObject : JSONObject
{
public MessageListJSONObject() : base("messageList") {}
public List<MessageBaseJSONObject> messages = new List<MessageBaseJSONObject>();
}
//Sending out a list of users should follow this format
public class UserListJSONObject : JSONObject
{
public UserListJSONObject() : base("userList") {}
public List<UserJSONObject> users = new List<UserJSONObject>();
public List<RoomJSONObject> rooms = new List<RoomJSONObject>();
}
//A single user within the UserList JSON object
public class UserJSONObject
{
public readonly string username = "";
public readonly string stars = "";
public readonly int level = 0;
public readonly int uid = 0;
public readonly long joined = 0;
public string avatar = "";
public bool active = false;
public bool banned = false;
public List<Badge> badges = new List<Badge>();
//This stupid thing is JUST for dumb libraries that require a default
//constructor (even though... nevermind)
public UserJSONObject() : this((UserInfo)null) {}
//public UserJSONObject
public UserJSONObject(UserInfo user)
{
if (user != null)
{
uid = user.UID;
username = user.Username;
avatar = user.Avatar;
stars = user.StarString;
level = user.Level;
active = user.Active;
joined = user.UnixJoinDate;
banned = user.Banned;
badges = user.Badges;
}
}
public UserJSONObject(UserJSONObject copy)
{
if(copy != null)
{
uid = copy.uid;
username = copy.username;
avatar = copy.avatar;
stars = copy.stars;
level = copy.level;
active = copy.active;
joined = copy.joined;
banned = copy.banned;
badges = copy.badges;
}
}
// public void StripUnimportantData()
// {
// badges = new List<Badge>();
// }
}
//A single PM room within the UserList JSON Object
public class RoomJSONObject
{
public readonly string name = "";
public readonly List<UserJSONObject> users = new List<UserJSONObject> ();
//public readonly List<RoomUserJSONObject> users = new List<RoomUserJSONObject>();
public RoomJSONObject(PMRoom room, Dictionary<int, UserInfo> users)
{
name = room.Name;
this.users = room.Users.ToList().Select(x => new UserJSONObject(users[x])).ToList();
}
}
// public class RoomUserJSONObject
// {
// public readonly string username = "";
// public string avatar = "";
// public readonly int uid = 0;
// public readonly bool active = false;
// public readonly bool banned = false;
//
// public RoomUserJSONObject(User user)
// {
// username = user.Username;
// avatar = user.Avatar;
// uid = user.UID;
// active = user.Active;
// banned = user.Banned;
// }
// }
public class ModuleJSONObject : MessageBaseJSONObject
{
//public UserJSONObject user = null;
public ModuleJSONObject(string message = "", UserInfo user = null) : base("module", message, user)
{
this.module = "";
//this.sendtype = MessageBaseSendType.IncludeSender;
}
// public ModuleJSONObject(string message) : base("module")
// {
// this.rawMessage = message;
// }
//private string rawMessage = "";
//public int uid = 0;
//public string tag = "";
public string module = "";
//public List<int> recipients = new List<int>();
// public string message
// {
// get
// {
// if (safe)
// return System.Security.SecurityElement.Escape(System.Net.WebUtility.HtmlDecode(rawMessage));
// else
// return rawMessage;
// }
// set
// {
// rawMessage = value;
// }
// }
}
public class SystemRequest
{
public readonly SystemRequests Request;
public readonly TimeSpan Timeout;
public SystemRequest(SystemRequests request) : this(request, TimeSpan.FromTicks(0)) {}
public SystemRequest(SystemRequests request, TimeSpan timeout)
{
this.Request = request;
this.Timeout = timeout;
}
public static implicit operator SystemRequest(SystemRequests request)
{
return new SystemRequest(request);
}
}
public enum SystemRequests
{
Reset,
LockDeath,
SaveModules
}
//A message. It SHOULD be readonly, honestly.
[Serializable]
public class MessageJSONObject : MessageBaseJSONObject
{
private bool spamupdate;
//This may become private.
public double spamvalue = 0;
public MessageJSONObject() : base("message") {}
public MessageJSONObject(string message, UserInfo user, string tag = "") : base("message", message, user)
{
this.tag = tag;
this.spamupdate = true;
this.spamvalue = 0;
this.sendtype = MessageBaseSendType.Broadcast;
}
public MessageJSONObject(MessageJSONObject copy) : base(copy)
{
if (copy != null)
{
tag = copy.tag;
spamupdate = copy.spamupdate;
spamvalue = copy.spamvalue;
}
}
public void SetSpammable(bool spammable)
{
spamupdate = spammable;
}
public bool IsSpammable()
{
return spamupdate;
}
}
}