Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AccountRecipient.cs
1using System.Threading.Tasks;
3
5{
6 internal class AccountRecipient : IRecipient
7 {
8 private readonly Accounts accounts;
9 private readonly IAccount account;
10
11 internal AccountRecipient(Accounts Accounts, IAccount Account)
12 {
13 this.accounts = Accounts;
14 this.account = Account;
15 }
16
17 internal Accounts Accounts => this.accounts;
18 internal IAccount Account => this.account;
19
20 public Task<bool> IQ(string Type, string Id, XmppAddress To, XmppAddress From, string Language, Stanza Stanza, ISender Sender)
21 {
22 return this.accounts.IQ(Type, Id, To, From, Language, Stanza, Sender);
23 }
24
25 public Task<bool> IQ(string Type, string Id, XmppAddress To, XmppAddress From, string Language, string ContentXml, ISender Sender)
26 {
27 return this.accounts.IQ(Type, Id, To, From, Language, ContentXml, Sender);
28 }
29
30 public async Task<bool> Message(string Type, string Id, XmppAddress To, XmppAddress From, string Language, Stanza Stanza, ISender Sender)
31 {
32 if (string.IsNullOrEmpty(Type) || Type == "normal" || Type == "chat" || Type == "headline")
33 {
34 // Rules defined in §8.5.2 of RFC 6121.
35
36 IClientConnection[] Connections = this.accounts.Server.GetClientConnections(To.BareJid);
37 bool Forwarded = false;
38
39 if (!(Connections is null) && Connections.Length > 0)
40 {
41 foreach (IClientConnection Connection in Connections)
42 {
43 if (Connection.State == XmppConnectionState.Active)
44 {
45 PresenceEventArgs e = Connection.LastPresence;
46
47 if (!(e is null) && e.Priority >= 0)
48 {
49 if (await Connection.Message(Type, Id, Connection.Address, From, Language, Stanza, Sender))
50 Forwarded = true;
51 }
52 }
53 }
54 }
55
56 if (!Forwarded && Type != "headline")
57 {
58 if (await this.accounts.Server.PersistenceLayer.StoreOfflineMessage(Type, Id, To, From, Language, Stanza))
59 {
60 if (!(Sender is null) && Sender is ICommunicationLayer ComLayer)
61 await ComLayer.Information("Message stored for later delivery.");
62 }
63 else if (!(Sender is null))
64 await (Sender?.MessageErrorServiceUnavailable(From, To, string.Empty, string.Empty) ?? Task.CompletedTask);
65 }
66 }
67
68 return true;
69 }
70
71 public async Task<bool> Message(string Type, string Id, XmppAddress To, XmppAddress From, string Language, string ContentXml, ISender Sender)
72 {
73 if (string.IsNullOrEmpty(Type) || Type == "normal" || Type == "chat" || Type == "headline")
74 {
75 // Rules defined in §8.5.2 of RFC 6121.
76
77 IClientConnection[] Connections = this.accounts.Server.GetClientConnections(To.BareJid);
78 bool Forwarded = false;
79
80 if (!(Connections is null) && Connections.Length > 0)
81 {
82 foreach (IClientConnection Connection in Connections)
83 {
84 if (Connection.State == XmppConnectionState.Active)
85 {
86 PresenceEventArgs e = Connection.LastPresence;
87
88 if (!(e is null) && e.Priority >= 0)
89 {
90 if (await Connection.Message(Type, Id, Connection.Address, From, Language, ContentXml, Sender))
91 Forwarded = true;
92 }
93 }
94 }
95 }
96
97 if (!Forwarded && Type != "headline")
98 {
99 if (await this.accounts.Server.PersistenceLayer.StoreOfflineMessage(Type, Id, To, From, Language, ContentXml))
100 {
101 if (!(Sender is null) && Sender is ICommunicationLayer ComLayer)
102 await ComLayer.Information("Message stored for later delivery.");
103 }
104 else if (!(Sender is null))
105 await (Sender?.MessageErrorServiceUnavailable(From, To, string.Empty, string.Empty) ?? Task.CompletedTask);
106 }
107 }
108
109 return true;
110 }
111
112 public Task<bool> Presence(string Type, string Id, XmppAddress To, XmppAddress From, string Language, Stanza Stanza, ISender Sender)
113 {
114 return this.accounts.Presence(Type, Id, To, From, Language, Stanza, Sender);
115 }
116
117 public Task<bool> Presence(string Type, string Id, XmppAddress To, XmppAddress From, string Language, string ContentXml, ISender Sender)
118 {
119 return this.accounts.Presence(Type, Id, To, From, Language, ContentXml, Sender);
120 }
121 }
122}
Component managing accounts.
Definition: Accounts.cs:15
Presence information event arguments.
Contains information about a stanza.
Definition: Stanza.cs:10
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
CaseInsensitiveString BareJid
Bare JID
Definition: XmppAddress.cs:45
IClientConnection[] GetClientConnections()
Get active client connections
Definition: XmppServer.cs:789
Interface for observable classes implementing communication protocols.
Interface for XMPP user accounts.
Definition: IAccount.cs:9
PresenceEventArgs LastPresence
Last presence received.
XmppConnectionState State
Connection state.
Interface for recipients of stanzas.
Definition: IRecipient.cs:9
Interface for senders of stanzas.
Definition: ISender.cs:10
Task< bool > Message(string Type, string Id, XmppAddress To, XmppAddress From, string Language, string ContentXml)
Message stanza.
XmppConnectionState
State of XMPP connection.