-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJabberManager.cs
267 lines (235 loc) · 8.8 KB
/
JabberManager.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using jabber.client;
using System.ComponentModel;
using System.Security;
using System.Security.Cryptography;
using System.Timers;
namespace LolChatWin
{
public class JabberManager
{
public Dictionary<string, User> users = new Dictionary<string, User>();
public BindingList<User> theUsers = new BindingList<User>();
JabberClient c = new JabberClient();
RosterManager k = new RosterManager();
public delegate void UserChangedHandler(User e);
public event UserChangedHandler UserChanged;
public delegate void MsgHandler(User From, string message, DateTime Date);
public event MsgHandler OnMessage;
public delegate void ErrorHandler(string error);
public event ErrorHandler OnError;
public delegate void ConnectedHandler();
public event ConnectedHandler OnConnect;
public event ConnectedHandler OnDisconnect;
Timer durationTimer = new Timer(30000);
public JabberManager()
{
c.OnPresence += new PresenceHandler(c_OnPresence);
c.OnInvalidCertificate += new System.Net.Security.RemoteCertificateValidationCallback(c_OnInvalidCertificate);
k.OnRosterItem += new RosterItemHandler(k_OnRosterItem);
k.OnRosterEnd += new bedrock.ObjectHandler(k_OnRosterEnd);
c.OnMessage += new MessageHandler(c_OnMessage);
c.OnDisconnect += new bedrock.ObjectHandler(c_OnDisconnect);
c.OnAuthError += new jabber.protocol.ProtocolHandler(c_OnAuthError);
c.OnError += new bedrock.ExceptionHandler(c_OnError);
durationTimer.Elapsed += new ElapsedEventHandler(durationTimer_Elapsed);
durationTimer.Start();
}
void c_OnError(object sender, Exception ex)
{
if (OnError != null)
OnError(ex.Message);
}
void durationTimer_Elapsed(object sender, ElapsedEventArgs e)
{
foreach (var user in theUsers)
{
if (UserChanged != null)
{
if (c.InvokeControl != null)
{
c.InvokeControl.Invoke(UserChanged,new object[]{ user });
}
}
}
}
public void Disconnect()
{
c.Close(true);
}
static byte[] entropy = System.Text.Encoding.Unicode.GetBytes("Salt Is Not A Password");
public static string EncryptString(System.Security.SecureString input)
{
byte[] encryptedData = System.Security.Cryptography.ProtectedData.Protect(
System.Text.Encoding.Unicode.GetBytes(ToInsecureString(input)),
entropy,
System.Security.Cryptography.DataProtectionScope.CurrentUser);
return Convert.ToBase64String(encryptedData);
}
public static SecureString DecryptString(string encryptedData)
{
try
{
byte[] decryptedData = System.Security.Cryptography.ProtectedData.Unprotect(
Convert.FromBase64String(encryptedData),
entropy,
System.Security.Cryptography.DataProtectionScope.CurrentUser);
return ToSecureString(System.Text.Encoding.Unicode.GetString(decryptedData));
}
catch
{
return new SecureString();
}
}
public static SecureString ToSecureString(string input)
{
SecureString secure = new SecureString();
foreach (char c in input)
{
secure.AppendChar(c);
}
secure.MakeReadOnly();
return secure;
}
public static string ToInsecureString(SecureString input)
{
string returnValue = string.Empty;
IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(input);
try
{
returnValue = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(ptr);
}
finally
{
System.Runtime.InteropServices.Marshal.ZeroFreeBSTR(ptr);
}
return returnValue;
}
public void Initialize(string username, string password, int server, ISynchronizeInvoke si )
{
c.InvokeControl = si;
c.User = username;
c.Password = "AIR_" + password;
c.Port = 5223;
c.SSL = true;
c.AutoRoster = true;
c.AutoLogin = true;
c.AutoPresence = true;
if (server == 0 )
{
c.NetworkHost = "chat.na.lol.riotgames.com";
}
else if (server == 1)
{
c.NetworkHost = "chat.eu.lol.riotgames.com";
}
else
{
c.NetworkHost = "chat.eun1.lol.riotgames.com";
}
c.Server = "pvp.net";
k.Stream = c;
users.Clear();
theUsers.Clear();
c.Connect();
}
bool c_OnInvalidCertificate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}
void c_OnAuthError(object sender, System.Xml.XmlElement rp)
{
if (OnError != null) OnError("Wrong username or password");
if (OnDisconnect != null)
{
OnDisconnect();
}
}
void c_OnDisconnect(object sender)
{
if (OnDisconnect != null)
{
OnDisconnect();
}
}
void k_OnRosterEnd(object sender)
{
if (OnConnect != null)
{
OnConnect();
}
}
void c_OnMessage(object sender, jabber.protocol.client.Message msg)
{
if ((OnMessage != null) && (!msg.Body.StartsWith("<body>")))
{
if (users.ContainsKey(msg.From.User))
{
OnMessage(users[msg.From.User], msg.Body, DateTime.Now);
}
}
}
void k_OnRosterItem(object sender, jabber.protocol.iq.Item ri)
{
if (users.ContainsKey(ri.JID.User))
{
if (ri.Subscription == jabber.protocol.iq.Subscription.remove)
{
users[ri.JID.User].status = null;
if (UserChanged != null)
UserChanged(users[ri.JID.User]);
users.Remove(ri.JID.User);
}
else
{
User us = users[ri.JID.User];
us.Nickname = ri.Nickname;
us.Group = ri.GetGroups().First().GroupName;
us.item = ri;
}
}
else
{
User us = new User(ri.JID.User, ri.Nickname, ri.GetGroups().First().GroupName, "", ri);
users.Add(ri.JID.User, us);
}
}
public void SendMessage(string text, User u)
{
c.Message(u.item.JID, text);
}
void c_OnPresence(object sender, jabber.protocol.client.Presence pres)
{
if (users.ContainsKey(pres.From.User))
{
users[pres.From.User].status = pres.Status;
}
else
{
User us = new User(pres.From.User, "", "", pres.Status, null);
users.Add(pres.From.User, us);
theUsers.Add(us);
}
if (users[pres.From.User].State == "offline")
{
users[pres.From.User].isOnline = false;
if (theUsers.Contains(users[pres.From.User]))
theUsers.Remove(users[pres.From.User]);
}
else
{
users[pres.From.User].isOnline = (users[pres.From.User].Nickname != "") ;
if (!theUsers.Contains(users[pres.From.User]))
theUsers.Add(users[pres.From.User]);
}
if (theUsers.Contains(users[pres.From.User]))
{
if (UserChanged != null)
UserChanged(users[pres.From.User]);
}
}
}
}