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;
2using System.Collections.Generic;
8
10{
14 [TypeName(TypeNameSerialization.FullName)]
15 [ArchivingTime(nameof(ArchiveDays))]
16 [CollectionName("MucRooms")]
17 [Index("Domain", "Room")]
18 public class MucRoom
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 [DefaultValueStringEmpty]
205 public string Password
206 {
207 get => this.password;
208 set => this.password = value;
209 }
210
211 [DefaultValue(DiscoverReadJids.Moderators)]
212 public DiscoverReadJids DiscoverRealJids
213 {
214 get => this.discoverRealJids;
215 set => this.discoverRealJids = value;
216 }
217
218 [DefaultValue(50)]
219 public int MaxHistoryFetch
220 {
221 get => this.maxHistoryFetch;
222 set => this.maxHistoryFetch = value;
223 }
224
225 [DefaultValue(0)]
226 public int ArchiveMessagesDays
227 {
228 get => this.archiveMessagesDays;
229 set => this.archiveMessagesDays = value;
230 }
231
232 [DefaultValueNull]
233 public CaseInsensitiveString[] Admins
234 {
235 get => this.admins;
236 set => this.admins = value;
237 }
238
239 [DefaultValueNull]
240 public CaseInsensitiveString[] Owners
241 {
242 get => this.owners;
243 set => this.owners = value;
244 }
245
246 [IgnoreMember]
247 public bool Locked
248 {
249 get => this.locked;
250 set => this.locked = value;
251 }
252
253 [DefaultValueStringEmpty]
254 public string Subject
255 {
256 get => this.subject;
257 set => this.subject = value;
258 }
259
260 [DefaultValueStringEmpty]
261 public CaseInsensitiveString SubjectJid
262 {
263 get => this.subjectJid;
264 set => this.subjectJid = value;
265 }
266
267 [DefaultValueDateTimeMinValue]
268 public DateTime SubjectTimestamp
269 {
270 get => this.subjectTimestamp;
271 set => this.subjectTimestamp = value;
272 }
273
277 public int ArchiveDays => this.archive ? int.MaxValue : 0;
278
279 internal bool TryGetOccupantFromNick(CaseInsensitiveString NickName, out MucOccupant Occupant)
280 {
281 lock (this.synchObj)
282 {
283 return this.occupantsByNick.TryGetValue(NickName, out Occupant);
284 }
285 }
286
287 internal bool TryGetOccupantFromFullJid(CaseInsensitiveString FullJid, out MucOccupant Occupant)
288 {
289 lock (this.synchObj)
290 {
291 return this.occupantsByFullJid.TryGetValue(FullJid, out Occupant);
292 }
293 }
294
295 internal bool TryGetOccupantFromBareJid(CaseInsensitiveString BareJid, out MucOccupant Occupant)
296 {
297 lock (this.synchObj)
298 {
299 return this.occupantsByBareJid.TryGetValue(BareJid, out Occupant);
300 }
301 }
302
303 internal bool Add(MucOccupant Occupant)
304 {
305 lock (this.synchObj)
306 {
307 if (this.occupantsByNick.Count >= this.maxOccupants && !(Occupant.Affiliation is Affiliations.Admin) && !(Occupant.Affiliation is Member))
308 return false;
309
310 this.occupantsByNick[Occupant.NickName] = Occupant;
311 this.occupantsByBareJid[Occupant.BareJid] = Occupant;
312
313 if (!(Occupant.FullJids is null))
314 {
315 foreach (XmppAddress FullJid in Occupant.FullJids)
316 this.occupantsByFullJid[FullJid.Address] = Occupant;
317 }
318 }
319
320 return true;
321 }
322
323 internal bool Remove(MucOccupant Occupant)
324 {
325 bool Result;
326
327 lock (this.synchObj)
328 {
329 Result = this.occupantsByNick.Remove(Occupant.NickName);
330 this.occupantsByBareJid.Remove(Occupant.BareJid);
331
332 if (!(Occupant.FullJids is null))
333 {
334 foreach (XmppAddress FullJid in Occupant.FullJids)
335 this.occupantsByFullJid.Remove(FullJid.Address);
336 }
337 }
338
339 return Result;
340 }
341
342 internal MucOccupant[] GetOccupants()
343 {
344 lock (this.synchObj)
345 {
346 MucOccupant[] Result = new MucOccupant[this.occupantsByNick.Count];
347 this.occupantsByNick.Values.CopyTo(Result, 0);
348 return Result;
349 }
350 }
351
352 internal void RegisterFullJid(MucOccupant Occupant, XmppAddress FullJid)
353 {
354 lock (this.synchObj)
355 {
356 this.occupantsByFullJid[FullJid.Address] = Occupant;
357 this.occupantsByBareJid[FullJid.BareJid] = Occupant;
358 }
359
360 Occupant.AddFullJid(FullJid);
361 }
362
363 internal bool UnregisterFullJid(MucOccupant Occupant, XmppAddress FullJid)
364 {
365 int c = Occupant.RemoveFullJid(FullJid);
366
367 lock (this.synchObj)
368 {
369 this.occupantsByFullJid.Remove(FullJid.Address);
370
371 if (c == 0)
372 {
373 this.occupantsByBareJid.Remove(FullJid.BareJid);
374 return true;
375 }
376 }
377
378 return false;
379 }
380
381 internal Affiliation GetDefaultAffiliation(CaseInsensitiveString BareJid)
382 {
383 if (!(this.owners is null) && Array.IndexOf(this.owners, BareJid) >= 0)
384 return new Owner();
385
386 if (!(this.admins is null) && Array.IndexOf(this.admins, BareJid) >= 0)
387 return new Affiliations.Admin();
388
389 if (!(this.members is null) && Array.IndexOf(this.members, BareJid) >= 0)
390 return new Member();
391
392 return new Affiliations.None();
393 }
394
395 internal bool CanBroadcastPresence(MucOccupant Occupant)
396 {
397 return Occupant?.Role?.CanBroadcastPresence(this.presenceBroadcast) ?? false;
398 }
399
400 internal bool CanGetMembership(MucOccupant Occupant)
401 {
402 return Occupant?.Role?.CanGetMembership(this.getMemberList) ?? false;
403 }
404
405 internal bool CanDiscoverRealJids(MucOccupant Occupant)
406 {
407 return Occupant?.Role?.CanDiscoverRealJids(this.discoverRealJids) ?? false;
408 }
409
410 internal bool CanSendPrivateMessage(MucOccupant Occupant)
411 {
412 if (!(Occupant.Role?.SendPrivateMessages ?? false))
413 return false;
414
415 return Occupant?.Role?.CanSendPrivateMessage(this.allowPrivateMessages) ?? false;
416 }
417
418 internal bool IsModerator(MucOccupant Occupant)
419 {
420 return Occupant.Role is Moderator;
421 }
422
423 internal bool IsAdmin(MucOccupant Occupant)
424 {
425 if (!(this.admins is null) && Array.IndexOf(this.admins, Occupant.BareJid) >= 0)
426 return true;
427
428 return IsOwner(Occupant);
429 }
430
431 internal bool IsOwner(MucOccupant Occupant)
432 {
433 return this.IsOwner(Occupant.BareJid);
434 }
435
436 internal bool IsOwner(CaseInsensitiveString BareJid)
437 {
438 if (!(this.owners is null) && Array.IndexOf(this.owners, BareJid) >= 0)
439 return true;
440
441 return false;
442 }
443
444 internal bool AddMember(CaseInsensitiveString BareJid)
445 {
446 return Add(ref this.members, BareJid);
447 }
448
449 internal bool IsMember(CaseInsensitiveString BareJid)
450 {
451 return !(this.members is null) && Array.IndexOf(this.members, BareJid) >= 0;
452 }
453
454 internal bool AddAdmin(CaseInsensitiveString BareJid)
455 {
456 return Add(ref this.admins, BareJid);
457 }
458
459 internal bool AddOwner(CaseInsensitiveString BareJid)
460 {
461 return Add(ref this.owners, BareJid);
462 }
463
464 internal bool RemoveMember(CaseInsensitiveString BareJid)
465 {
466 return Remove(ref this.members, BareJid);
467 }
468
469 internal bool RemoveAdmin(CaseInsensitiveString BareJid)
470 {
471 return Remove(ref this.admins, BareJid);
472 }
473
474 internal bool RemoveOwner(CaseInsensitiveString BareJid)
475 {
476 return Remove(ref this.owners, BareJid);
477 }
478
479 private bool Add(ref CaseInsensitiveString[] List, CaseInsensitiveString BareJid)
480 {
481 if (List is null)
482 List = new CaseInsensitiveString[] { BareJid };
483 else if (Array.IndexOf(List, BareJid) >= 0)
484 return false;
485 else
486 {
487 CaseInsensitiveString[] New = (CaseInsensitiveString[])List.Clone();
488 int c = New.Length;
489 Array.Resize(ref New, c + 1);
490 New[c] = BareJid;
491 List = New;
492 }
493
494 return true;
495 }
496
497 private bool Remove(ref CaseInsensitiveString[] List, CaseInsensitiveString BareJid)
498 {
499 if (List is null)
500 return false;
501
502 CaseInsensitiveString[] New = (CaseInsensitiveString[])List.Clone();
503 int i = Array.IndexOf(New, BareJid);
504 if (i < 0)
505 return false;
506
507 int c = New.Length;
508
509 if (i < c - 1)
510 Array.Copy(New, i + 1, New, i, c - 1 - i);
511
512 Array.Resize(ref New, c - 1);
513 List = New;
514
515 return true;
516 }
517 }
518}
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:277
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.
TypeNameSerialization
How the type name should be serialized.