Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MucRoom.cs
1using System;
8
10{
14 [TypeName(TypeNameSerialization.FullName)]
15 [ArchivingTime(nameof(ArchiveDays))]
16 [CollectionName("MucRooms")]
17 [Index("Domain", "Room")]
19 {
20 private readonly Dictionary<CaseInsensitiveString, MucOccupant> occupantsByNick = new Dictionary<CaseInsensitiveString, MucOccupant>();
21 private readonly Dictionary<CaseInsensitiveString, MucOccupant> occupantsByFullJid = new Dictionary<CaseInsensitiveString, MucOccupant>();
22 private readonly Dictionary<CaseInsensitiveString, MucOccupant> occupantsByBareJid = new Dictionary<CaseInsensitiveString, MucOccupant>();
23 private readonly object synchObj = new object();
24 private string objectId = null;
25 private CaseInsensitiveString room = null;
26 private CaseInsensitiveString domain = null;
27 private CaseInsensitiveString creator = null;
28 private string name = string.Empty;
29 private string description = string.Empty;
30 private string language = string.Empty;
31 private string subject = string.Empty;
32 private string subjectJid = string.Empty;
33 private DateTime subjectTimestamp = DateTime.MinValue;
34 private bool logging = false;
35 private bool allowChangeSubject = false;
36 private bool allowInvites = false;
37 private CanSendPrivateMessages allowPrivateMessages = CanSendPrivateMessages.Anyone;
38 private int maxOccupants = 20;
39 private BroadcastPresence presenceBroadcast = BroadcastPresence.All;
40 private RetrieveMembershipList getMemberList = RetrieveMembershipList.All;
41 private bool persistent = false;
42 private bool archive = false;
43 private bool moderated = false;
44 private bool membersOnly = false;
45 private bool passwordProtected = false;
46 private bool _public = false;
47 private bool locked = false;
48 private string password = string.Empty;
49 private DiscoverReadJids discoverRealJids = DiscoverReadJids.Moderators;
50 private int maxHistoryFetch = 50;
51 private int archiveMessagesDays = 0;
52 private CaseInsensitiveString[] members = null;
53 private CaseInsensitiveString[] admins = null;
54 private CaseInsensitiveString[] owners = null;
55
59 public MucRoom()
60 {
61 }
62
63 [ObjectId]
64 public string ObjectID
65 {
66 get => this.objectId;
67 set => this.objectId = value;
68 }
69
70 public CaseInsensitiveString Room
71 {
72 get => this.room;
73 set => this.room = value;
74 }
75
76 public CaseInsensitiveString Domain
77 {
78 get => this.domain;
79 set => this.domain = value;
80 }
81
82 public CaseInsensitiveString Creator
83 {
84 get => this.creator;
85 set => this.creator = value;
86 }
87
88 [DefaultValueStringEmpty]
89 public string Name
90 {
91 get => this.name;
92 set => this.name = value;
93 }
94
95 [DefaultValueStringEmpty]
96 public string Description
97 {
98 get => this.description;
99 set => this.description = value;
100 }
101
102 [DefaultValueStringEmpty]
103 public string Language
104 {
105 get => this.language;
106 set => this.language = value;
107 }
108
109 [DefaultValue(false)]
110 public bool Logging
111 {
112 get => this.logging;
113 set => this.logging = value;
114 }
115
116 [DefaultValue(false)]
117 public bool AllowChangeSubject
118 {
119 get => this.allowChangeSubject;
120 set => this.allowChangeSubject = value;
121 }
122
123 [DefaultValue(false)]
124 public bool AllowInvites
125 {
126 get => this.allowInvites;
127 set => this.allowInvites = value;
128 }
129
130 [DefaultValue(CanSendPrivateMessages.Anyone)]
131 public CanSendPrivateMessages AllowPrivateMessages
132 {
133 get => this.allowPrivateMessages;
134 set => this.allowPrivateMessages = value;
135 }
136
137 [DefaultValue(20)]
138 public int MaxOccupants
139 {
140 get => this.maxOccupants;
141 set => this.maxOccupants = value;
142 }
143
144 [DefaultValue(BroadcastPresence.All)]
145 public BroadcastPresence PresenceBroadcast
146 {
147 get => this.presenceBroadcast;
148 set => this.presenceBroadcast = value;
149 }
150
151 [DefaultValue(RetrieveMembershipList.All)]
152 public RetrieveMembershipList GetMemberList
153 {
154 get => this.getMemberList;
155 set => this.getMemberList = value;
156 }
157
158 [DefaultValue(false)]
159 public bool Persistent
160 {
161 get => this.persistent;
162 set
163 {
164 this.persistent = value;
165 this.archive |= value;
166 }
167 }
168
169 [DefaultValue(false)]
170 public bool Archive
171 {
172 get => this.archive;
173 set => this.archive = value;
174 }
175
176 [DefaultValue(false)]
177 public bool Moderated
178 {
179 get => this.moderated;
180 set => this.moderated = value;
181 }
182
183 [DefaultValue(false)]
184 public bool MembersOnly
185 {
186 get => this.membersOnly;
187 set => this.membersOnly = value;
188 }
189
190 [DefaultValue(false)]
191 public bool Public
192 {
193 get => this._public;
194 set => this._public = value;
195 }
196
197 [DefaultValue(false)]
198 public bool PasswordProtected
199 {
200 get => this.passwordProtected;
201 set => this.passwordProtected = value;
202 }
203
204 [Encrypted(32)]
205 public string Password
206 {
207 get => this.password;
208 set => this.password = value;
209 }
210
214 public string[] EncryptedProperties => new string[] { nameof(this.Password) };
215
216 [DefaultValue(DiscoverReadJids.Moderators)]
217 public DiscoverReadJids DiscoverRealJids
218 {
219 get => this.discoverRealJids;
220 set => this.discoverRealJids = value;
221 }
222
223 [DefaultValue(50)]
224 public int MaxHistoryFetch
225 {
226 get => this.maxHistoryFetch;
227 set => this.maxHistoryFetch = value;
228 }
229
230 [DefaultValue(0)]
231 public int ArchiveMessagesDays
232 {
233 get => this.archiveMessagesDays;
234 set => this.archiveMessagesDays = value;
235 }
236
237 [DefaultValueNull]
238 public CaseInsensitiveString[] Admins
239 {
240 get => this.admins;
241 set => this.admins = value;
242 }
243
244 [DefaultValueNull]
245 public CaseInsensitiveString[] Owners
246 {
247 get => this.owners;
248 set => this.owners = value;
249 }
250
251 [IgnoreMember]
252 public bool Locked
253 {
254 get => this.locked;
255 set => this.locked = value;
256 }
257
258 [DefaultValueStringEmpty]
259 public string Subject
260 {
261 get => this.subject;
262 set => this.subject = value;
263 }
264
265 [DefaultValueStringEmpty]
266 public CaseInsensitiveString SubjectJid
267 {
268 get => this.subjectJid;
269 set => this.subjectJid = value;
270 }
271
272 [DefaultValueDateTimeMinValue]
273 public DateTime SubjectTimestamp
274 {
275 get => this.subjectTimestamp;
276 set => this.subjectTimestamp = value;
277 }
278
282 public int ArchiveDays => this.archive ? int.MaxValue : 0;
283
284 internal bool TryGetOccupantFromNick(CaseInsensitiveString NickName, out MucOccupant Occupant)
285 {
286 lock (this.synchObj)
287 {
288 return this.occupantsByNick.TryGetValue(NickName, out Occupant);
289 }
290 }
291
292 internal bool TryGetOccupantFromFullJid(CaseInsensitiveString FullJid, out MucOccupant Occupant)
293 {
294 lock (this.synchObj)
295 {
296 return this.occupantsByFullJid.TryGetValue(FullJid, out Occupant);
297 }
298 }
299
300 internal bool TryGetOccupantFromBareJid(CaseInsensitiveString BareJid, out MucOccupant Occupant)
301 {
302 lock (this.synchObj)
303 {
304 return this.occupantsByBareJid.TryGetValue(BareJid, out Occupant);
305 }
306 }
307
308 internal bool Add(MucOccupant Occupant)
309 {
310 lock (this.synchObj)
311 {
312 if (this.occupantsByNick.Count >= this.maxOccupants && !(Occupant.Affiliation is Affiliations.Admin) && !(Occupant.Affiliation is Member))
313 return false;
314
315 this.occupantsByNick[Occupant.NickName] = Occupant;
316 this.occupantsByBareJid[Occupant.BareJid] = Occupant;
317
318 if (!(Occupant.FullJids is null))
319 {
320 foreach (XmppAddress FullJid in Occupant.FullJids)
321 this.occupantsByFullJid[FullJid.Address] = Occupant;
322 }
323 }
324
325 return true;
326 }
327
328 internal bool Remove(MucOccupant Occupant)
329 {
330 bool Result;
331
332 lock (this.synchObj)
333 {
334 Result = this.occupantsByNick.Remove(Occupant.NickName);
335 this.occupantsByBareJid.Remove(Occupant.BareJid);
336
337 if (!(Occupant.FullJids is null))
338 {
339 foreach (XmppAddress FullJid in Occupant.FullJids)
340 this.occupantsByFullJid.Remove(FullJid.Address);
341 }
342 }
343
344 return Result;
345 }
346
347 internal MucOccupant[] GetOccupants()
348 {
349 lock (this.synchObj)
350 {
351 MucOccupant[] Result = new MucOccupant[this.occupantsByNick.Count];
352 this.occupantsByNick.Values.CopyTo(Result, 0);
353 return Result;
354 }
355 }
356
357 internal void RegisterFullJid(MucOccupant Occupant, XmppAddress FullJid)
358 {
359 lock (this.synchObj)
360 {
361 this.occupantsByFullJid[FullJid.Address] = Occupant;
362 this.occupantsByBareJid[FullJid.BareJid] = Occupant;
363 }
364
365 Occupant.AddFullJid(FullJid);
366 }
367
368 internal bool UnregisterFullJid(MucOccupant Occupant, XmppAddress FullJid)
369 {
370 int c = Occupant.RemoveFullJid(FullJid);
371
372 lock (this.synchObj)
373 {
374 this.occupantsByFullJid.Remove(FullJid.Address);
375
376 if (c == 0)
377 {
378 this.occupantsByBareJid.Remove(FullJid.BareJid);
379 return true;
380 }
381 }
382
383 return false;
384 }
385
386 internal Affiliation GetDefaultAffiliation(CaseInsensitiveString BareJid)
387 {
388 if (!(this.owners is null) && Array.IndexOf(this.owners, BareJid) >= 0)
389 return new Owner();
390
391 if (!(this.admins is null) && Array.IndexOf(this.admins, BareJid) >= 0)
392 return new Affiliations.Admin();
393
394 if (!(this.members is null) && Array.IndexOf(this.members, BareJid) >= 0)
395 return new Member();
396
397 return new Affiliations.None();
398 }
399
400 internal bool CanBroadcastPresence(MucOccupant Occupant)
401 {
402 return Occupant?.Role?.CanBroadcastPresence(this.presenceBroadcast) ?? false;
403 }
404
405 internal bool CanGetMembership(MucOccupant Occupant)
406 {
407 return Occupant?.Role?.CanGetMembership(this.getMemberList) ?? false;
408 }
409
410 internal bool CanDiscoverRealJids(MucOccupant Occupant)
411 {
412 return Occupant?.Role?.CanDiscoverRealJids(this.discoverRealJids) ?? false;
413 }
414
415 internal bool CanSendPrivateMessage(MucOccupant Occupant)
416 {
417 if (!(Occupant.Role?.SendPrivateMessages ?? false))
418 return false;
419
420 return Occupant?.Role?.CanSendPrivateMessage(this.allowPrivateMessages) ?? false;
421 }
422
423 internal bool IsModerator(MucOccupant Occupant)
424 {
425 return Occupant.Role is Moderator;
426 }
427
428 internal bool IsAdmin(MucOccupant Occupant)
429 {
430 if (!(this.admins is null) && Array.IndexOf(this.admins, Occupant.BareJid) >= 0)
431 return true;
432
433 return IsOwner(Occupant);
434 }
435
436 internal bool IsOwner(MucOccupant Occupant)
437 {
438 return this.IsOwner(Occupant.BareJid);
439 }
440
441 internal bool IsOwner(CaseInsensitiveString BareJid)
442 {
443 if (!(this.owners is null) && Array.IndexOf(this.owners, BareJid) >= 0)
444 return true;
445
446 return false;
447 }
448
449 internal bool AddMember(CaseInsensitiveString BareJid)
450 {
451 return Add(ref this.members, BareJid);
452 }
453
454 internal bool IsMember(CaseInsensitiveString BareJid)
455 {
456 return !(this.members is null) && Array.IndexOf(this.members, BareJid) >= 0;
457 }
458
459 internal bool AddAdmin(CaseInsensitiveString BareJid)
460 {
461 return Add(ref this.admins, BareJid);
462 }
463
464 internal bool AddOwner(CaseInsensitiveString BareJid)
465 {
466 return Add(ref this.owners, BareJid);
467 }
468
469 internal bool RemoveMember(CaseInsensitiveString BareJid)
470 {
471 return Remove(ref this.members, BareJid);
472 }
473
474 internal bool RemoveAdmin(CaseInsensitiveString BareJid)
475 {
476 return Remove(ref this.admins, BareJid);
477 }
478
479 internal bool RemoveOwner(CaseInsensitiveString BareJid)
480 {
481 return Remove(ref this.owners, BareJid);
482 }
483
484 private bool Add(ref CaseInsensitiveString[] List, CaseInsensitiveString BareJid)
485 {
486 if (List is null)
487 List = new CaseInsensitiveString[] { BareJid };
488 else if (Array.IndexOf(List, BareJid) >= 0)
489 return false;
490 else
491 {
492 CaseInsensitiveString[] New = (CaseInsensitiveString[])List.Clone();
493 int c = New.Length;
494 Array.Resize(ref New, c + 1);
495 New[c] = BareJid;
496 List = New;
497 }
498
499 return true;
500 }
501
502 private bool Remove(ref CaseInsensitiveString[] List, CaseInsensitiveString BareJid)
503 {
504 if (List is null)
505 return false;
506
507 CaseInsensitiveString[] New = (CaseInsensitiveString[])List.Clone();
508 int i = Array.IndexOf(New, BareJid);
509 if (i < 0)
510 return false;
511
512 int c = New.Length;
513
514 if (i < c - 1)
515 Array.Copy(New, i + 1, New, i, c - 1 - i);
516
517 Array.Resize(ref New, c - 1);
518 List = New;
519
520 return true;
521 }
522 }
523}
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
CaseInsensitiveString Address
XMPP Address
Definition: XmppAddress.cs:37
CaseInsensitiveString BareJid
Bare JID
Definition: XmppAddress.cs:45
Represents a case-insensitive string.
static readonly CaseInsensitiveString Empty
Empty case-insensitive string
int Length
Gets the number of characters in the current CaseInsensitiveString object.
int IndexOf(CaseInsensitiveString value, StringComparison comparisonType)
Reports the zero-based index of the first occurrence of the specified string in the current System....
Abstract base class for MUC affiliations.
Definition: Affiliation.cs:12
int ArchiveDays
Number of days to archive field.
Definition: MucRoom.cs:282
string[] EncryptedProperties
Array of properties that are encrypted.
Definition: MucRoom.cs:214
abstract bool SendPrivateMessages
If user can send private messages
Definition: Role.cs:54
abstract bool CanSendPrivateMessage(CanSendPrivateMessages Level)
If occupants of this role can send private messages.
abstract bool CanBroadcastPresence(BroadcastPresence Level)
If occupants of this role should be broadcast.
abstract bool CanDiscoverRealJids(DiscoverReadJids Level)
If occupants of this role can discover occupant's real JIDs.
abstract bool CanGetMembership(RetrieveMembershipList Level)
If occupants of this role can get membership list.
Interface for objects containing encrypted properties. Mark the properties that are encrypted with th...
TypeNameSerialization
How the type name should be serialized.