forked from GarethWright/WinApp.NET
/
ContactsList.cs
832 lines (756 loc) · 29.5 KB
/
ContactsList.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinAppNET.AppCode;
using System.Data.Common;
using System.Data.SQLite;
using System.Threading;
using System.IO;
using WhatsAppApi.Helper;
using WinAppNET.Controls;
using WinAppNET.Dialogs;
using System.Configuration;
namespace WinAppNET
{
public partial class ContactsList : Form
{
public BindingList<Contact> contacts = new BindingList<Contact>();
public Dictionary<string, ChatWindow> ChatWindows = new Dictionary<string, ChatWindow>();
protected string username;
protected string password;
private static List<string> picturesToSync = new List<string>();
private static Thread WAlistener;
private string GetPassword()
{
string pass = System.Configuration.ConfigurationManager.AppSettings.Get("Password");
try
{
byte[] foo = Convert.FromBase64String(pass);
return pass;
}
catch (Exception)
{
return string.Empty;
}
}
private string getUsername()
{
return System.Configuration.ConfigurationManager.AppSettings.Get("Username");
}
public ContactsList()
{
InitializeComponent();
this.FormClosing += this.ContactsList_OnClosing;
}
private void ContactsList_OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
foreach (ChatWindow w in this.ChatWindows.Values)
{
//close chat windows
w.DoDispose();
}
}
delegate void remoteDelegate();
protected void _loadConversations()
{
if (this.InvokeRequired)
{
remoteDelegate r = new remoteDelegate(_loadConversations);
this.Invoke(r);
}
else
{
this.flowLayoutPanel1.Controls.Clear();
DbProviderFactory fact = DbProviderFactories.GetFactory("System.Data.SQLite");
using (DbConnection cnn = fact.CreateConnection())
{
cnn.ConnectionString = MessageStore.ConnectionString;
cnn.Open();
DbCommand cmd = cnn.CreateCommand();
cmd = cnn.CreateCommand();
cmd.CommandText = "SELECT jid FROM Messages GROUP BY jid";
DbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string jid = reader["jid"].ToString();
Contact contact = ContactStore.GetContactByJid(jid);
if (contact != null)
{
this.contacts.Add(contact);
//add to richtextbox
ListContact c = new ListContact(contact.jid);
c.DoubleClick += this.contact_dblClick;
this.flowLayoutPanel1.Controls.Add(c);
}
}
}
//done
this.label1.Hide();
}
}
protected void SyncWaContactsAsync()
{
ContactStore.SyncWaContacts();
//resync pictures
picturesToSync = this.refreshContactPictures();
this.requestProfilePicture();
this._loadConversations();
}
private void contact_dblClick(object sender, EventArgs e)
{
ListContact c = sender as ListContact;
if (c != null)
{
this.OpenConversationThread(c.contact.jid, true, true);
}
}
public void OpenConversation(object jid)
{
Helper.ChatWindowParameters cwp = jid as Helper.ChatWindowParameters;
this._openConversation(cwp.jid, cwp.stealFocus, cwp.onTop);
}
protected void _openConversation(object jid, bool stealFocus, bool onTop)
{
if (!this.ChatWindows.ContainsKey(jid.ToString()))
{
ChatWindow c = new ChatWindow(jid.ToString(), stealFocus, onTop);
c.TopMost = false;
this.ChatWindows.Add(jid.ToString(), c);//create
}
else if (this.ChatWindows[jid.ToString()].IsDisposed)
{
ChatWindow c = new ChatWindow(jid.ToString(), stealFocus, onTop);
c.TopMost = false;
this.ChatWindows[jid.ToString()] = c;//renew
}
else
{
this.ChatWindows[jid.ToString()].DoActivate();
return;
}
try
{
Application.Run(this.ChatWindows[jid.ToString()]);
}
catch (Exception e)
{
throw e;
}
}
public void OpenConversationThread(string jid, bool stealFocus, bool onTop)
{
Helper.ChatWindowParameters cwp = new Helper.ChatWindowParameters(jid, stealFocus, onTop);
try
{
Thread t = new Thread(new ParameterizedThreadStart(OpenConversation));
t.IsBackground = true;
t.Start(cwp);
}
catch (Exception e)
{
throw e;
}
}
protected ChatWindow getChat(string jid, bool forceOpen, bool onTop)
{
if (forceOpen)
{
this.OpenConversationThread(jid, forceOpen, onTop);
}
if (this.ChatWindows.ContainsKey(jid) && !this.ChatWindows[jid].IsDisposed)
{
return this.ChatWindows[jid];
}
while (forceOpen)
{
Thread.Sleep(100);
if (this.ChatWindows.ContainsKey(jid) && this.ChatWindows[jid].IsLoaded)
{
return this.ChatWindows[jid];
}
}
return null;
}
//protected void ProcessMessages(ProtocolTreeNode[] nodes)
//{
// foreach (ProtocolTreeNode node in nodes)
// {
// if (node.tag.Equals("message"))
// {
// ProtocolTreeNode body = node.GetChild("body");
// ProtocolTreeNode media = node.GetChild("media");
// ProtocolTreeNode paused = node.GetChild("paused");
// ProtocolTreeNode composing = node.GetChild("composing");
// ProtocolTreeNode notification = node.GetChild("notification");
// string jid = node.GetAttribute("from");
// if (body != null || media != null)
// {
// //extract and save nickname
// if (node.GetChild("notify") != null && node.GetChild("notify").GetAttribute("name") != null)
// {
// string nick = node.GetChild("notify").GetAttribute("name");
// Contact c = ContactStore.GetContactByJid(jid);
// if (c != null)
// {
// c.nickname = nick;
// ContactStore.UpdateNickname(c);
// }
// }
// try
// {
// this.getChat(jid, true, false).AddMessage(node);
// }
// catch (Exception)
// { }
// //refresh list
// this._loadConversations();
// }
// if (paused != null)
// {
// try
// {
// if (this.getChat(jid, false, false) != null)
// {
// this.getChat(jid, false, false).SetOnline();
// }
// }
// catch (Exception)
// {
// //throw e;
// }
// }
// if (composing != null)
// {
// try
// {
// if (this.getChat(jid, false, false) != null)
// {
// this.getChat(jid, false, false).SetTyping();
// }
// }
// catch (Exception)
// {
// //throw e;
// }
// }
// if (notification != null)
// {
// if (notification.GetAttribute("type") == "picture" && notification.GetChild("set") != null)
// {
// ChatWindow.GetImageAsync(notification.GetChild("set").GetAttribute("jid"), false);
// }
// }
// }
// else if (node.tag.Equals("presence"))
// {
// string jid = node.GetAttribute("from");
// if (node.GetAttribute("type") != null && node.GetAttribute("type").Equals("available"))
// {
// try
// {
// if (this.getChat(jid, false, false) != null)
// {
// this.getChat(jid, false, false).SetOnline();
// }
// }
// catch (Exception)
// {
// //throw e;
// }
// }
// if (node.GetAttribute("type") != null && node.GetAttribute("type").Equals("unavailable"))
// {
// try
// {
// if (this.getChat(jid, false, false) != null)
// {
// this.getChat(jid, false, false).SetUnavailable();
// }
// }
// catch (Exception)
// { }
// }
// }
// else if (node.tag.Equals("iq"))
// {
// string jid = node.GetAttribute("from");
// if (node.children.First().tag.Equals("query"))
// {
// DateTime lastseen = DateTime.Now;
// int seconds = Int32.Parse(node.GetChild("query").GetAttribute("seconds"));
// lastseen = lastseen.Subtract(new TimeSpan(0, 0, seconds));
// try
// {
// if (this.getChat(jid, false, false) != null)
// {
// getChat(jid, false, false).SetLastSeen(lastseen);
// }
// }
// catch (Exception)
// { }
// }
// else if (node.children.First().tag.Equals("group"))
// {
// string subject = node.children.First().GetAttribute("subject");
// Contact cont = ContactStore.GetContactByJid(jid);
// if (cont != null)
// {
// cont.nickname = subject;
// ContactStore.UpdateNickname(cont);
// }
// else
// {
// }
// }
// else if (node.children.First().tag.Equals("picture"))
// {
// string pjid = node.GetAttribute("from");
// string id = node.GetAttribute("id");
// byte[] rawpicture = node.GetChild("picture").GetData();
// Contact c = ContactStore.GetContactByJid(pjid);
// if (c != null)
// {
// Image img = null;
// using (var ms = new MemoryStream(rawpicture))
// {
// try
// {
// img = Image.FromStream(ms);
// string targetdir = Directory.GetCurrentDirectory() + "\\data\\profilecache";
// if (!Directory.Exists(targetdir))
// {
// Directory.CreateDirectory(targetdir);
// }
// img.Save(targetdir + "\\" + c.jid + ".jpg");
// try
// {
// if (this.getChat(pjid, false, false) != null)
// {
// this.getChat(pjid, false, false).SetPicture(img);
// }
// }
// catch (Exception e)
// {
// throw e;
// }
// }
// catch (Exception e)
// {
// throw e;
// }
// }
// }
// if(picturesToSync.Remove(pjid))
// this.requestProfilePicture();
// }
// else if (node.children.First().tag.Equals("error") && node.children.First().GetAttribute("code") == "404")
// {
// string pjid = node.GetAttribute("from");
// picturesToSync.Remove(pjid);
// this.requestProfilePicture();
// }
// }
// }
//}
private void KeepAlive()
{
while (true)
{
Thread.Sleep(150000);
WappSocket.Instance.SendActive();
}
}
protected void Listen()
{
while (true)
{
try
{
WappSocket.Instance.PollMessages();
}
catch (Exception)
{
//reset
WappSocket.Instance.Disconnect();
WappSocket.Instance.Connect();
return;
}
Thread.Sleep(500);
}
}
private void doRefreshContactPictures()
{
picturesToSync = this.refreshContactPictures();
this.requestProfilePicture();
}
private List<string> refreshContactPictures()
{
Contact[] contacts = ContactStore.GetAllContacts();
List<string> toSync = new List<string>();
foreach (Contact c in contacts)
{
string path = ChatWindow.getCacheImagePath(c.jid);
if(!File.Exists(path))
{
toSync.Add(c.jid);
}
}
return toSync;
}
private bool checkCredentials()
{
if (!(WappSocket.Instance != null && WappSocket.Instance.ConnectionStatus == WhatsAppApi.WhatsApp.CONNECTION_STATUS.LOGGEDIN))
{
this.username = this.getUsername();
this.password = this.GetPassword();
if (!string.IsNullOrEmpty(this.username) && !string.IsNullOrEmpty(this.password))
{
WappSocket.Create(username, password, "WinApp.NET", false);
WappSocket.Instance.Connect();
WappSocket.Instance.Login();
if (WappSocket.Instance.ConnectionStatus == WhatsAppApi.WhatsApp.CONNECTION_STATUS.LOGGEDIN)
{
return true;
}
else
{
WappSocket.Instance.Disconnect();
}
}
return false;
}
else
{
return true;
}
}
private void ContactsList_Load(object sender, EventArgs e)
{
string nickname = "WhatsAPINet";
this.username = this.getUsername();
this.password = this.GetPassword();
ContactStore.CheckTable();
MessageStore.CheckTable();
WappSocket.Create(this.username, this.password, nickname, true);
this.bindAll();
Thread t = new Thread(new ThreadStart(WappSocket.Instance.Connect));
t.IsBackground = true;
t.Start();
int vertScrollWidth = SystemInformation.VerticalScrollBarWidth;
this.flowLayoutPanel1.Padding = new Padding(0, 0, vertScrollWidth, 0);
this._loadConversations();
}
private void bindAll()
{
//bindings
WappSocket.Instance.OnConnectFailed += Instance_OnConnectFailed;
WappSocket.Instance.OnConnectSuccess += Instance_OnConnectSuccess;
WappSocket.Instance.OnDisconnect += Instance_OnDisconnect;
WappSocket.Instance.OnError += Instance_OnError;
WappSocket.Instance.OnGetGroupParticipants += Instance_OnGetGroupParticipants;
WappSocket.Instance.OnGetGroups += Instance_OnGetGroups;
WappSocket.Instance.OnGetLastSeen += Instance_OnGetLastSeen;
WappSocket.Instance.OnGetMessage += Instance_OnGetMessage;
WappSocket.Instance.OnGetContactName += Instance_OnGetContactName;
WappSocket.Instance.OnGetMessageAudio += Instance_OnGetMessageAudio;
WappSocket.Instance.OnGetMessageImage += Instance_OnGetMessageImage;
WappSocket.Instance.OnGetMessageLocation += Instance_OnGetMessageLocation;
WappSocket.Instance.OnGetMessageReceivedClient += Instance_OnGetMessageReceivedClient;
WappSocket.Instance.OnGetMessageReceivedServer += Instance_OnGetMessageReceivedServer;
WappSocket.Instance.OnGetMessageVcard += Instance_OnGetMessageVcard;
WappSocket.Instance.OnGetMessageVideo += Instance_OnGetMessageVideo;
WappSocket.Instance.OnGetPaused += Instance_OnGetPaused;
WappSocket.Instance.OnGetPhoto += Instance_OnGetPhoto;
WappSocket.Instance.OnGetPhotoPreview += Instance_OnGetPhotoPreview;
WappSocket.Instance.OnGetPresence += Instance_OnGetPresence;
WappSocket.Instance.OnGetTyping += Instance_OnGetTyping;
WappSocket.Instance.OnLoginFailed += Instance_OnLoginFailed;
WappSocket.Instance.OnLoginSuccess += Instance_OnLoginSuccess;
WappSocket.Instance.OnNotificationPicture += Instance_OnNotificationPicture;
WappSocket.Instance.OnGetSyncResult += Instance_OnGetSyncResult;
}
void Instance_OnGetSyncResult(int index, string sid, Dictionary<string, string> existingUsers, string[] failedNumbers)
{
ContactStore.OnSyncResult(existingUsers, failedNumbers);
}
private void Instance_OnGetGroups(WhatsAppApi.Response.WaGroupInfo[] groups)
{
throw new NotImplementedException();
}
void Instance_OnGetContactName(string from, string contactName)
{
this.updateNick(from, contactName);
}
void Instance_OnNotificationPicture(string type, string jid, string id)
{
throw new NotImplementedException();
}
void Instance_OnLoginSuccess(string number, byte[] data)
{
this.saveConfig();
WAlistener = new Thread(new ThreadStart(Listen));
WAlistener.IsBackground = true;
WAlistener.Start();
Thread alive = new Thread(new ThreadStart(KeepAlive));
alive.IsBackground = true;
alive.Start();
Thread picsync = new Thread(new ThreadStart(this.doRefreshContactPictures));
picsync.IsBackground = true;
picsync.Start();
}
void Instance_OnLoginFailed(string data)
{
if (data == "not-authorized")
{
//show reg form
WappCredentials creds = new WappCredentials();
DialogResult r = creds.ShowDialog();
if (r != System.Windows.Forms.DialogResult.OK)
{
//cancelled, close application
Application.Exit();
return;
}
this.username = this.getUsername();
this.password = this.GetPassword();
if (!string.IsNullOrEmpty(this.username) && !string.IsNullOrEmpty(this.password))
{
WappSocket.Create(this.username, this.password, "WinApp.NET", true);
this.bindAll();
WappSocket.Instance.Connect();
}
}
if (data == "blocked")
{
throw new Exception("blocked!");
}
}
private void saveConfig()
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection app = config.AppSettings;
app.Settings.Remove("Username");
app.Settings.Add("Username", this.username);
app.Settings.Remove("Password");
app.Settings.Add("Password", this.password);
config.Save(ConfigurationSaveMode.Modified);
}
void Instance_OnGetTyping(string from)
{
try
{
this.getChat(from, false, false).SetTyping();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
void Instance_OnGetPresence(string from, string type)
{
if (!from.StartsWith(this.username))
{
try
{
switch (type)
{
case "available":
this.getChat(from, false, false).SetOnline();
break;
case "unavailable":
this.getChat(from, false, false).SetUnavailable();
break;
default:
throw new Exception(type);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
void Instance_OnGetPhotoPreview(string from, string id, byte[] data)
{
Contact c = ContactStore.GetContactByJid(from);
if (c != null)
{
Image img = null;
using (var ms = new MemoryStream(data))
{
try
{
img = Image.FromStream(ms);
string targetdir = Directory.GetCurrentDirectory() + "\\data\\profilecache";
if (!Directory.Exists(targetdir))
{
Directory.CreateDirectory(targetdir);
}
img.Save(targetdir + "\\" + c.jid + ".jpg");
try
{
if (this.getChat(from, false, false) != null)
{
this.getChat(from, false, false).SetPicture(img);
}
}
catch (Exception e)
{
throw e;
}
}
catch (Exception e)
{
throw e;
}
}
}
if (picturesToSync.Remove(from))
{
this.requestProfilePicture();
}
}
void Instance_OnGetPhoto(string from, string id, byte[] data)
{
throw new NotImplementedException();
}
void Instance_OnGetPaused(string from)
{
try
{
this.getChat(from, false, false).SetOnline();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
void Instance_OnGetMessageVideo(string from, string id, string fileName, int fileSize, string url, byte[] preview)
{
throw new NotImplementedException();
}
void Instance_OnGetMessageVcard(string from, string id, string name, byte[] data)
{
throw new NotImplementedException();
}
void Instance_OnGetMessageReceivedServer(string from, string id)
{
//throw new NotImplementedException();
}
void Instance_OnGetMessageReceivedClient(string from, string id)
{
//throw new NotImplementedException();
}
void Instance_OnGetMessageLocation(string from, string id, double lon, double lat, string url, string name, byte[] preview)
{
throw new NotImplementedException();
}
void Instance_OnGetMessageImage(string from, string id, string fileName, int fileSize, string url, byte[] preview)
{
throw new NotImplementedException();
}
void Instance_OnGetMessageAudio(string from, string id, string fileName, int fileSize, string url, byte[] preview)
{
throw new NotImplementedException();
}
void Instance_OnGetMessage(ProtocolTreeNode messageNode, string from, string id, string name, string message, bool receipt_sent)
{
try
{
this.getChat(from, true, true).AddMessage(from, message, (from.StartsWith(this.username)));
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
this._loadConversations();
}
void Instance_OnGetLastSeen(string from, DateTime lastSeen)
{
try
{
this.getChat(from, false, false).SetLastSeen(lastSeen);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
void Instance_OnGetGroupParticipants(string gjid, string[] jids)
{
throw new NotImplementedException();
}
void Instance_OnError(string id, string from, int code, string text)
{
if(id.StartsWith("get_photo_"))
{
if (picturesToSync.Remove(from))
{
this.requestProfilePicture();
}
}
}
void Instance_OnDisconnect(Exception ex)
{
if (ex != null)
{
throw new NotImplementedException();
}
}
void Instance_OnConnectSuccess()
{
WappSocket.Instance.Login();
}
void Instance_OnConnectFailed(Exception ex)
{
throw new NotImplementedException();
}
private void updateNick(string jid, string nick)
{
Contact c = ContactStore.GetContactByJid(jid);
if (c != null)
{
c.nickname = nick;
ContactStore.UpdateNickname(c);
}
}
private void requestProfilePicture()
{
if (picturesToSync.Count > 0)
{
string jid = picturesToSync.First();
ChatWindow.GetImageAsync(jid, false);
}
}
private void tileNew_Click(object sender, EventArgs e)
{
ContactsSelector selector = new ContactsSelector();
DialogResult res = selector.ShowDialog();
if (res == DialogResult.OK)
{
this.OpenConversationThread(selector.SelectedJID, true, true);
}
selector.Dispose();
}
private void tileGoogle_Click(object sender, EventArgs e)
{
//sync contacts
Dialogs.frmGoogleSync gsync = new Dialogs.frmGoogleSync();
gsync.ShowDialog();
//reset
this.contacts.Clear();
this.label1.Text = "Updating contacts...";
this.label1.Show();
//picturesToSync = this.refreshContactPictures();
//this.requestProfilePicture();
Thread t = new Thread(new ThreadStart(SyncWaContactsAsync));
t.IsBackground = true;
t.Start();
}
}
}