-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathRosterItem.cs
More file actions
512 lines (429 loc) · 12.1 KB
/
RosterItem.cs
File metadata and controls
512 lines (429 loc) · 12.1 KB
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Waher.Content.Xml;
using Waher.Networking.XMPP.Events;
using Waher.Networking.XMPP.StanzaErrors;
namespace Waher.Networking.XMPP
{
/// <summary>
/// State of a presence subscription.
/// </summary>
public enum SubscriptionState
{
/// <summary>
/// The user does not have a subscription to the contact's presence, and the contact does not have a subscription to the
/// user's presence; this is the default value, so if the subscription attribute is not included then the state is to be understood as
/// "none".
/// </summary>
None,
/// <summary>
/// The user has a subscription to the contact's presence, but the contact does not have a subscription to the user's presence.
/// </summary>
To,
/// <summary>
/// The contact has a subscription to the user's presence, but the user does not have a subscription to the contact's presence.
/// </summary>
From,
/// <summary>
/// The user and the contact have subscriptions to each other's presence (also called a "mutual subscription").
/// </summary>
Both,
/// <summary>
/// Roster item is to be removed, or has been removed. Only available in roster push messages.
/// </summary>
Remove,
/// <summary>
/// Unknown subscription state.
/// </summary>
Unknown
}
/// <summary>
/// Pending subscription states.
/// </summary>
public enum PendingSubscription
{
/// <summary>
/// Pending subscribe
/// </summary>
Subscribe,
/// <summary>
/// Pending unsubscribe
/// </summary>
Unsubscribe,
/// <summary>
/// No pending subscriptions
/// </summary>
None
}
/// <summary>
/// Maintains information about an item in the roster.
/// </summary>
public class RosterItem
{
private readonly Dictionary<string, PresenceEventArgs> resources;
private readonly string bareJid;
private string[] groups;
private SubscriptionState state;
private PendingSubscription pendingSubscription;
private PresenceEventArgs lastPresence = null;
private string name;
/// <summary>
/// Maintains information about an item in the roster.
/// </summary>
/// <param name="BareJID">Bare JID of the roster item.</param>
/// <param name="Name">Name of the roster item.</param>
/// <param name="Groups">Groups assigned to the roster item.</param>
public RosterItem(string BareJID, string Name, params string[] Groups)
: this(BareJID, Name, Groups, null)
{
}
/// <summary>
/// Maintains information about an item in the roster.
/// </summary>
/// <param name="BareJID">Bare JID of the roster item.</param>
/// <param name="Name">Name of the roster item.</param>
/// <param name="Groups">Groups assigned to the roster item.</param>
/// <param name="Prev">Inherit resources from the previous roster item.</param>
internal RosterItem(string BareJID, string Name, string[] Groups, RosterItem Prev)
{
this.groups = Groups;
this.state = SubscriptionState.Unknown;
this.bareJid = BareJID;
this.name = Name;
this.pendingSubscription = PendingSubscription.None;
this.resources = Prev?.resources ?? new Dictionary<string, PresenceEventArgs>();
}
internal RosterItem(XmlElement Item, Dictionary<string, RosterItem> Roster)
{
this.bareJid = XML.Attribute(Item, "jid");
this.name = XML.Attribute(Item, "name");
switch (XML.Attribute(Item, "subscription"))
{
case "both":
this.state = SubscriptionState.Both;
break;
case "to":
this.state = SubscriptionState.To;
break;
case "from":
this.state = SubscriptionState.From;
break;
case "none":
case "":
this.state = SubscriptionState.None;
break;
case "remove":
this.state = SubscriptionState.Remove;
break;
default:
this.state = SubscriptionState.Unknown;
break;
}
switch (XML.Attribute(Item, "ask").ToLower())
{
case "subscribe":
this.pendingSubscription = PendingSubscription.Subscribe;
break;
case "unsubscribe":
this.pendingSubscription = PendingSubscription.Unsubscribe;
break;
default:
this.pendingSubscription = PendingSubscription.None;
break;
}
List<string> Groups = new List<string>();
foreach (XmlNode N in Item.ChildNodes)
{
if (N.LocalName == "group")
Groups.Add(N.InnerText);
}
this.groups = Groups.ToArray();
if (this.state == SubscriptionState.Both || this.state == SubscriptionState.To)
{
if (Roster.TryGetValue(this.bareJid, out RosterItem Prev))
this.resources = Prev.resources;
this.lastPresence = Prev?.lastPresence;
}
if (this.resources is null)
this.resources = new Dictionary<string, PresenceEventArgs>();
}
/// <summary>
/// Any groups the roster item belongs to.
/// </summary>
public string[] Groups
{
get => this.groups;
internal set => this.groups = value;
}
/// <summary>
/// Checks if the roster item is in a specific group.
/// </summary>
/// <param name="Group">Name of group.</param>
/// <returns>If the item is in the group.</returns>
public bool IsInGroup(string Group)
{
return this.IsInGroup(Group, false);
}
/// <summary>
/// Checks if the roster item is in a specific group.
/// </summary>
/// <param name="Group">Name of group.</param>
/// <param name="IgnoreCase">If comparison should be case insensitive (default=false).</param>
/// <returns>If the item is in the group.</returns>
public bool IsInGroup(string Group, bool IgnoreCase)
{
if (this.groups is null)
return false;
foreach (string s in this.groups)
{
if (string.Compare(s, Group, IgnoreCase) == 0)
return true;
}
return false;
}
/// <summary>
/// Checks if the roster item is in any of an array of groups.
/// </summary>
/// <param name="Groups">Group anmes.</param>
/// <returns>If the item is in any of the specified groups.</returns>
public bool IsInAnyGroup(string[] Groups)
{
return this.IsInAnyGroup(Groups, false);
}
/// <summary>
/// Checks if the roster item is in any of an array of groups.
/// </summary>
/// <param name="Groups">Group anmes.</param>
/// <param name="IgnoreCase">If comparison should be case insensitive (default=false).</param>
/// <returns>If the item is in any of the specified groups.</returns>
public bool IsInAnyGroup(string[] Groups, bool IgnoreCase)
{
if (Groups is null || Groups.Length == 0)
return true;
if (this.groups is null)
return false;
Dictionary<string, bool> Ordered;
if (IgnoreCase)
Ordered = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
else
Ordered = new Dictionary<string, bool>();
foreach (string Group in Groups)
Ordered[Group] = true;
foreach (string Group in this.groups)
{
if (Ordered.ContainsKey(Group))
return true;
}
return false;
}
/// <summary>roup
/// Current subscription state.
/// </summary>
public SubscriptionState State
{
get => this.state;
internal set => this.state = value;
}
/// <summary>
/// Bare JID of the roster item.
/// </summary>
public string BareJid => this.bareJid;
/// <summary>
/// Name of the roster item.
/// </summary>
public string Name
{
get => this.name;
internal set => this.name = value;
}
/// <summary>
/// If there's a pending unanswered presence subscription or unsubscription request made to the contact.
/// </summary>
public PendingSubscription PendingSubscription
{
get => this.pendingSubscription;
internal set => this.pendingSubscription = value;
}
/// <summary>
/// Active resources utilized by contact.
/// </summary>
public PresenceEventArgs[] Resources
{
get
{
lock (this.resources)
{
PresenceEventArgs[] Result = new PresenceEventArgs[this.resources.Count];
this.resources.Values.CopyTo(Result, 0);
return Result;
}
}
}
/// <summary>
/// Serializes the roster item for transmission to the server.
/// </summary>
/// <param name="Output">Output</param>
public void Serialize(StringBuilder Output)
{
Output.Append("<item jid='");
Output.Append(XML.Encode(this.bareJid));
Output.Append("' name='");
Output.Append(XML.Encode(this.name));
if (this.state == SubscriptionState.Remove)
Output.Append("' subscription='remove");
Output.Append("'>");
foreach (string Group in this.groups)
{
Output.Append("<group>");
Output.Append(XML.Encode(Group));
Output.Append("</group>");
}
Output.Append("</item>");
}
/// <summary>
/// Full JID of last resource sending online presence.
/// </summary>
public string LastPresenceFullJid
{
get
{
if (!(this.lastPresence is null))
return this.lastPresence.From;
else
return string.Empty;
}
}
/// <summary>
/// Last presence received from a resource having this bare JID.
/// </summary>
public PresenceEventArgs LastPresence => this.lastPresence;
internal void PresenceReceived(XmppClient Client, PresenceEventArgs e)
{
PresenceEventArgs[] ToTest = null;
lock (this.resources)
{
if (e.Type == PresenceType.Unavailable)
{
this.resources.Remove(e.From);
if (!(this.lastPresence is null) && this.lastPresence.From == e.From)
this.lastPresence = null;
}
else if (e.Type == PresenceType.Available)
{
int c = this.resources.Count;
if (c > 0 && Client.MonitorContactResourcesAlive && !this.resources.ContainsKey(e.From))
{
ToTest = new PresenceEventArgs[c];
this.resources.Values.CopyTo(ToTest, 0);
}
this.resources[e.From] = e;
this.lastPresence = e;
if (this.pendingSubscription == PendingSubscription.Subscribe)
this.pendingSubscription = PendingSubscription.None; // Might be out of synch.
}
}
if (!(ToTest is null))
{
foreach (PresenceEventArgs e2 in ToTest)
{
if (!e2.Testing)
{
e2.Testing = true;
Client.SendPing(e2.From, this.PingResult, new object[] { Client, e2 });
}
}
}
}
private Task PingResult(object Sender, IqResultEventArgs e)
{
object[] P = (object[])e.State;
XmppClient Client = (XmppClient)P[0];
PresenceEventArgs e2 = (PresenceEventArgs)P[1];
e2.Testing = false;
if (e.Ok)
return Task.CompletedTask;
if (!(e.ErrorElement is null) && e.ErrorElement.LocalName == FeatureNotImplementedException.LocalName)
return Task.CompletedTask;
Client.Unavail(e2);
return Task.CompletedTask;
}
/// <summary>
/// If the roster item has received presence from an online resource having the given bare JID.
/// </summary>
public bool HasLastPresence
{
get
{
return !(this.lastPresence is null);
}
}
/// <summary>
/// Returns the name of the contact, or the Bare JID, if there's no name provided.
/// </summary>
public string NameOrBareJid
{
get
{
if (string.IsNullOrEmpty(this.name))
return this.bareJid;
else
return this.name;
}
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (!(obj is RosterItem Item))
return false;
int i, c;
if (this.state != Item.state ||
this.pendingSubscription != Item.pendingSubscription ||
this.bareJid != Item.bareJid ||
this.name != Item.bareJid ||
(c = this.groups.Length) != Item.groups.Length)
{
return false;
}
for (i = 0; i < c; i++)
{
if (this.groups[i] != Item.groups[i])
return false;
}
return true;
}
/// <inheritdoc/>
public override int GetHashCode()
{
int Result = this.state.GetHashCode();
Result *= 33;
Result |= this.pendingSubscription.GetHashCode();
Result *= 33;
Result |= this.bareJid.GetHashCode();
Result *= 33;
Result |= this.name.GetHashCode();
foreach (string Group in this.groups)
{
Result *= 33;
Result |= Group.GetHashCode();
}
return Result;
}
internal PresenceEventArgs[] UnavailAllResources()
{
this.lastPresence = null;
lock (this.resources)
{
int c = this.resources.Count;
if (c == 0)
return null;
PresenceEventArgs[] Result = new PresenceEventArgs[c];
this.resources.Values.CopyTo(Result, 0);
this.resources.Clear();
return Result;
}
}
}
}