-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSeriousBusinessCat.cs
819 lines (696 loc) · 27.8 KB
/
SeriousBusinessCat.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
using Microsoft.Lync.Model;
using Microsoft.Lync.Model.Conversation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using MessageBox = System.Windows.MessageBox;
namespace BusinessCats
{
public class SeriousBusinessCat
{
protected MainWindow _main;
public WhisperCat whisperCat;
Microsoft.Lync.Model.LyncClient client = null;
Microsoft.Lync.Model.Extensibility.Automation automation = null;
string _selfUserName = "";
string _selfUserNameLast = "";
private List<LyncConversation> _conversations = new List<LyncConversation>();
public SeriousBusinessCat(MainWindow main)
{
this._main = main;
this.whisperCat = new WhisperCat(this);
try
{
#if DEBUG
var _testWhisper = new WhisperWindow(null, null, null, "Your key: 12345\r\nTheir key: 6789A");
_testWhisper.AddWhisper(new Whisper(true, "testing a really really long message, at least it seems pretty long, but i guess it is really not that long to begin with", "cipher1"));
_testWhisper.AddWhisper(new Whisper(false, "okay, looks good", "cipher2"));
_testWhisper.AddWhisper(new Whisper(true, "glad you think so, you jerk", "cipher3"));
_testWhisper.Show();
#endif
//Start the conversation
automation = LyncClient.GetAutomation();
client = LyncClient.GetClient();
ConversationManager conversationManager = client.ConversationManager;
conversationManager.ConversationAdded += ConversationManager_ConversationAdded;
conversationManager.ConversationRemoved += ConversationManager_ConversationRemoved;
//conversationManager.ConversationAdded += new EventHandler<ConversationManagerEventArgs>(conversationManager_ConversationAdded);
_main.lbConversations.ItemsSource = _conversations;
RefreshConversations(true);
try
{
_selfUserName = client.Self.Contact.GetContactInformation(ContactInformationType.PrimaryEmailAddress).ToString();
if (_selfUserName.Contains("@"))
{
_selfUserName = _selfUserName.Split('@')[0];
}
_selfUserNameLast = _selfUserName;
if (_selfUserNameLast.Contains('.'))
{
_selfUserNameLast = _selfUserNameLast.Split('.')[1];
}
}
catch (Exception)
{
// ignore
}
}
catch (LyncClientException lyncClientException)
{
MessageBox.Show("Failed to connect to Lync: " + lyncClientException.ToString());
Console.Out.WriteLine(lyncClientException);
}
catch (SystemException systemException)
{
if (Util.IsLyncException(systemException))
{
// Log the exception thrown by the Lync Model API.
MessageBox.Show("Failed to connect to Lync with system error: " + systemException.ToString());
Console.WriteLine("Error: " + systemException);
}
else
{
// Rethrow the SystemException which did not come from the Lync Model API.
throw;
}
}
}
private void ConversationManager_ConversationRemoved(object sender, ConversationManagerEventArgs e)
{
RefreshConversations();
}
private void ConversationManager_ConversationAdded(object sender, ConversationManagerEventArgs e)
{
RefreshConversations();
}
public LyncConversation FindConversationFromTitle(string title)
{
try {
var matches = new List<LyncConversation>();
foreach (var c in _conversations)
{
string desc = c.DescribeAsTitle();
if (title == desc)
{
matches.Add(c);
}
}
if (title.StartsWith("Conversation ("))
{
string titleCountString = Regex.Match(title, @"\d+").Value;
int count = 0;
if (!int.TryParse(titleCountString, out count))
{
return null;
}
// try to find a conversation with correct number of participants
foreach (var c in _conversations)
{
if (c.conversation.Participants.Count == count)
{
matches.Add(c);
}
}
}
else
{
// will be the name of the contact
foreach (var c in _conversations)
{
if (c.conversation.Participants.Count > 2)
{
continue;
}
foreach (var p in c.conversation.Participants)
{
if (p.IsSelf)
{
continue;
}
object val;
if (p.Properties.TryGetValue(ParticipantProperty.Name, out val) && val is string)
{
string name = (string)val;
if (name == title)
{
matches.Add(c);
}
}
}
}
}
if (matches.Count == 0)
{
return null;
}
if (matches.Count == 1)
{
return matches[0];
}
matches = matches.OrderByDescending(c => {
object val;
if (c.conversation.Properties.TryGetValue(ConversationProperty.LastActivityTimeStamp, out val) && val is DateTime)
{
var lastActivity = (DateTime)val;
return lastActivity;
}
else {
return DateTime.MinValue;
}
}).ToList();
return matches[0];
} catch (Exception)
{
// ignore it
return null;
}
}
public LyncConversation FindConversation(Conversation conv)
{
foreach (var c in _conversations)
{
if (c.conversation == conv)
{
return c;
}
}
return null;
}
public LyncConversation TrackConversation(Conversation conv)
{
var participants = new Dictionary<Participant, LyncParticipant>();
foreach (var participant in conv.Participants)
{
if (participant.IsSelf)
{
continue;
}
InstantMessageModality modality = (InstantMessageModality)participant.Modalities[ModalityTypes.InstantMessage];
modality.InstantMessageReceived += imModality_InstantMessageReceived;
participants.Add(participant, new LyncParticipant { modality = modality, participant = participant });
}
conv.ParticipantAdded += Conv_ParticipantAdded;
conv.ParticipantRemoved += Conv_ParticipantRemoved;
var lyncConv = new LyncConversation() { conversation = conv, participants = participants };
lyncConv.UpdateDesc();
return lyncConv;
}
public void UpdateConversation(LyncConversation conv)
{
var activeParticipants = new List<Participant>();
foreach (var participant in conv.conversation.Participants)
{
if (participant.IsSelf)
{
continue;
}
activeParticipants.Add(participant);
if (conv.participants.ContainsKey(participant))
{
continue;
}
InstantMessageModality modality = (InstantMessageModality)participant.Modalities[ModalityTypes.InstantMessage];
modality.InstantMessageReceived += imModality_InstantMessageReceived;
conv.participants.Add(participant, new LyncParticipant { modality = modality, participant = participant });
}
var deadParticipants = new List<Participant>();
foreach (var participant in conv.participants.Keys)
{
if (!activeParticipants.Contains(participant))
{
deadParticipants.Add(participant);
}
}
foreach (var participant in deadParticipants)
{
conv.participants.Remove(participant);
}
conv.UpdateDesc();
}
private void Conv_ParticipantRemoved(object sender, ParticipantCollectionChangedEventArgs e)
{
RefreshConversations();
}
private void Conv_ParticipantAdded(object sender, ParticipantCollectionChangedEventArgs e)
{
RefreshConversations();
}
protected void DoRefreshConversations(bool selectActive = false)
{
_conversations.RemoveAll((c) => {
if (c.conversation.State == ConversationState.Terminated)
{
return true;
}
else
{
return false;
}
});
foreach (var lyncConv in client.ConversationManager.Conversations)
{
var conv = FindConversation(lyncConv);
if (null == conv)
{
_conversations.Add(TrackConversation(lyncConv));
continue;
}
else
{
UpdateConversation(conv);
}
}
_conversations.Sort(Comparer<LyncConversation>.Create((l, r) => {
int lGroup = l.participants.Count > 2 ? 1 : 0;
int rGroup = r.participants.Count > 2 ? 1 : 0;
if (lGroup > rGroup)
{
return -1;
}
else if (lGroup < rGroup)
{
return 1;
}
else {
return string.Compare(l.ToString(), r.ToString());
}
}));
_main.lbConversations.Items.Refresh();
if (selectActive)
{
string activeConversationTitle = Util.GetActiveLyncConversationTitle();
var activeConversation = FindConversationFromTitle(activeConversationTitle);
if (activeConversation != null)
{
_main.lbConversations.SelectedItem = activeConversation;
}
}
//var selfContact = client.Self.Contact;
//foreach (var infoType in Enum.GetValues(typeof(ContactInformationType)).Cast<ContactInformationType>())
//{
// try {
// var infoTypeName = Enum.GetName(typeof(ContactInformationType), infoType);
// var infoValue = selfContact.GetContactInformation(infoType).ToString();
// if (!string.IsNullOrEmpty(infoValue))
// {
// System.Diagnostics.Debug.Print($"{infoTypeName}: {infoValue}");
// }
// } catch (Exception)
// {
// // ignore
// }
//}
}
public void AddConversation(string subject = "")
{
_main.lbConversations.Dispatcher.Invoke(() => {
var conv = client.ConversationManager.AddConversation();
if (conv != null && !string.IsNullOrEmpty(subject))
{
//conv.AddParticipant(client.Self.Contact); sending messages to yourself causes trouble but good way to test things
if (conv.CanSetProperty(ConversationProperty.Subject))
{
var task = LyncConversation.DoUpdateSubject(conv, subject);
}
}
});
}
public void RefreshConversations(bool selectActive = false)
{
_main.lbConversations.Dispatcher.Invoke(() => { DoRefreshConversations(selectActive); });
}
void imModality_InstantMessageReceived(object sender, MessageSentEventArgs e)
{
try
{
var modality = (InstantMessageModality)sender;
IDictionary<InstantMessageContentType, string> messageFormatProperty = e.Contents;
string source = (string)modality.Participant.Contact.GetContactInformation(ContactInformationType.DisplayName);
string text = "";
string raw = "";
if (messageFormatProperty.ContainsKey(InstantMessageContentType.PlainText))
{
if (messageFormatProperty.TryGetValue(InstantMessageContentType.PlainText, out raw))
{
text = raw;
}
}
else if (messageFormatProperty.ContainsKey(InstantMessageContentType.RichText))
{
if (messageFormatProperty.TryGetValue(InstantMessageContentType.RichText, out raw))
{
text = Util.TextFromRtf(raw);
}
}
else if (messageFormatProperty.ContainsKey(InstantMessageContentType.Html))
{
if (messageFormatProperty.TryGetValue(InstantMessageContentType.Html, out raw))
{
var reg = new System.Text.RegularExpressions.Regex("<[^>]+>");
var stripped = reg.Replace(raw, "");
text = System.Web.HttpUtility.HtmlDecode(stripped);
}
}
//else if (messageFormatProperty.ContainsKey(InstantMessageContentType.Gif))
//{
// if (messageFormatProperty.TryGetValue(InstantMessageContentType.Gif, out raw))
// {
// // gif, "base64:{data}"
// }
//}
//else if (messageFormatProperty.ContainsKey(InstantMessageContentType.Ink))
//{
// // InkSerializedFormat (application/x-ms-ink)
// if (messageFormatProperty.TryGetValue(InstantMessageContentType.Ink, out raw))
// {
// // isf, just use Microsoft.Ink to Load/Save
// }
//}
if (!string.IsNullOrEmpty(text))
{
if (whisperCat.HandleWhisper(modality.Conversation, modality.Participant, text))
{
return;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public void StartWhispering()
{
var conv = GetActiveConversation();
if (conv == null)
{
return;
}
if (conv.participants.Count > 1)
{
MessageBox.Show("It is hard to make friends in a crowded hallway.");
return;
}
var participant = conv.participants.Values.First();
whisperCat.InitiateWhisperHandshake(conv.conversation, participant.participant);
}
private LyncConversation GetActiveConversation()
{
if (_main.lbConversations.SelectedItem == null)
{
MessageBox.Show("Please select a business conversation with your business associates to whom you intend to send your business cat ascii art", "whoa there partner");
return null;
}
var conv = (LyncConversation)_main.lbConversations.SelectedItem;
return conv;
}
public void DoSendImage(string data)
{
if (string.IsNullOrEmpty(data))
{
return;
}
var conv = GetActiveConversation();
if (conv == null)
{
return;
}
DoSendMessage(conv.conversation, data, InstantMessageContentType.Gif);
}
void DoSendText(string data, InstantMessageContentType type)
{
if (string.IsNullOrEmpty(data))
{
return;
}
var conv = GetActiveConversation();
if (conv == null)
{
return;
}
DoSendMessage(conv.conversation, data, type);
}
protected async Task DoSendMessageAsync(Conversation conversation, string data, InstantMessageContentType type)
{
try
{
InstantMessageModality imModality = (InstantMessageModality)conversation.Modalities[ModalityTypes.InstantMessage];
var formattedMessage = new Dictionary<InstantMessageContentType, string>();
formattedMessage.Add(type, data);
if (imModality.CanInvoke(ModalityAction.SendInstantMessage))
{
await Task.Factory.FromAsync(imModality.BeginSendMessage, imModality.EndSendMessage, formattedMessage, imModality);
}
}
catch (LyncClientException ex)
{
MessageBox.Show(ex.Message);
}
}
public void DoSendMessage(Conversation conversation, string data, InstantMessageContentType type)
{
var task = DoSendMessageAsync(conversation, data, type);
//task.Wait(100);
}
public void Blp()
{
string data = @"{\rtf\ansi lol}";
DoSendText(data, InstantMessageContentType.RichText);
}
protected string ImageToData(System.Drawing.Image image)
{
using (var memStream = new System.IO.MemoryStream())
{
var quantizer = new ImageQuantization.OctreeQuantizer(255, 8);
using (var quantized = quantizer.Quantize(image))
{
quantized.Save(memStream, System.Drawing.Imaging.ImageFormat.Gif);
}
//image.Save(memStream, System.Drawing.Imaging.ImageFormat.Gif);
return "base64:" + System.Convert.ToBase64String(memStream.ToArray());
}
}
protected string ScaleToWidth(System.Drawing.Image image, int width)
{
var newWidth = width;
var newHeight = (int)((image.Height * (width / (double)image.Width)) + 0.5);
string data = "";
if (newWidth == image.Width && newHeight == image.Height)
{
data = ImageToData(image);
}
else {
using (var newImage = new System.Drawing.Bitmap(image, newWidth, newHeight))
{
data = ImageToData(newImage);
}
}
System.Diagnostics.Debug.Print("Resize ({0},{1}) -> ({2},{3}) -- len={4}", image.Width, image.Height, newWidth, newHeight, data.Length);
return data;
}
protected const int threshold = 65000;
public string DataFromImage(System.Drawing.Image image, bool useMaxWidth = true)
{
int maxWidth = useMaxWidth ? _main.CalcMaxWidth() : int.MaxValue;
if (maxWidth <= 0)
{
maxWidth = 1;
}
if (maxWidth > image.Width)
{
maxWidth = image.Width;
}
// The quantizer runs super slow with big images so start at a small reasonable size
int newWidth = 640; // starting point
if (newWidth > maxWidth)
{
newWidth = maxWidth;
}
string data = "";
// keep scaling down until we go below the threshold
data = ScaleToWidth(image, newWidth);
if (data.Length < threshold && newWidth < maxWidth)
{
// already below threshold at our starting point? try starting at 1600 px then
newWidth = 1600;
if (newWidth > maxWidth)
{
newWidth = maxWidth;
}
data = ScaleToWidth(image, newWidth);
}
if (data.Length < threshold && newWidth < maxWidth)
{
// still below threshold at our starting point? start with the real size of the image i suppose, up to the hardlimit
const int hardLimit = 3840;
newWidth = image.Width;
if (newWidth > hardLimit)
{
newWidth = hardLimit;
}
if (newWidth > maxWidth)
{
newWidth = maxWidth;
}
data = ScaleToWidth(image, newWidth);
}
while (data.Length >= threshold)
{
int nextWidth = newWidth;
// scale down
double diffRatio = Math.Sqrt(threshold / (double)data.Length);
nextWidth = (int)(nextWidth * diffRatio);
// align to 8 px
nextWidth = (nextWidth / 8) * 8;
if (nextWidth == newWidth)
{
nextWidth -= 8;
}
if (nextWidth <= 0)
{
throw new Exception("Something very troubling is going on resizing this image you gave me.");
}
newWidth = nextWidth;
data = ScaleToWidth(image, newWidth);
}
// now we are below the threshold! this time let's just move up until we go over.
while (newWidth < maxWidth)
{
newWidth += 16;
string newData = ScaleToWidth(image, newWidth);
if (newData.Length < threshold)
{
data = newData;
} else
{
break;
}
}
return data;
}
public string DataFromFile(string filename)
{
using (var image = System.Drawing.Image.FromFile(filename))
{
if (System.IO.Path.GetExtension(filename).ToLower() == "gif" && image.Width < _main.CalcMaxWidth())
{
string data = "base64:" + System.Convert.ToBase64String(System.IO.File.ReadAllBytes(filename));
if (data.Length < threshold)
{
return data;
}
}
return DataFromImage(image);
}
}
public string DataFromClipboard()
{
using (var image = System.Windows.Forms.Clipboard.GetImage())
{
if (image == null)
{
return "";
}
return DataFromImage(image);
}
}
public string DataFromString(string data)
{
using (var stream = new System.IO.MemoryStream(System.Convert.FromBase64String(data)))
{
using (var image = System.Drawing.Image.FromStream(stream))
{
return DataFromImage(image);
}
}
}
private void SaveSnip(System.Drawing.Image bmp)
{
var snipPath = Util.GetSnipPath();
var fileName = Util.GetUtcTimeStamp() + ".png";
var filePath = System.IO.Path.Combine(snipPath, fileName);
bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
}
public void Snip()
{
_main.Visibility = Visibility.Hidden;
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
{
try
{
using (var bmp = SnippingTool.Snip())
{
_main.Visibility = Visibility.Visible;
if (bmp != null)
{
SaveSnip(bmp);
System.Windows.Forms.Clipboard.SetImage(bmp);
}
}
}
finally
{
_main.Visibility = Visibility.Visible;
}
}));
}
public void SnipAndSend()
{
_main.Visibility = Visibility.Hidden;
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
{
try
{
using (var bmp = SnippingTool.Snip())
{
_main.Visibility = Visibility.Visible;
if (bmp != null)
{
SaveSnip(bmp);
DoSendImage(DataFromImage(bmp, useMaxWidth: false));
try
{
System.Windows.Forms.Clipboard.SetImage(bmp);
}
catch (Exception)
{
// ignore
}
}
}
}
finally
{
_main.Visibility = Visibility.Visible;
}
}));
}
public void Paste()
{
if (System.Windows.Forms.Clipboard.ContainsImage())
{
DoSendImage(DataFromClipboard());
}
else if (System.Windows.Forms.Clipboard.ContainsText(System.Windows.Forms.TextDataFormat.Rtf))
{
string rtf = System.Windows.Forms.Clipboard.GetText(System.Windows.Forms.TextDataFormat.Rtf);
string data = rtf.Replace(@"{\rtf\", @"{\rtf1\");
DoSendText(data, InstantMessageContentType.RichText);
}
//else if (System.Windows.Forms.Clipboard.ContainsText(System.Windows.Forms.TextDataFormat.Html))
//{
// DoSendText(System.Windows.Forms.Clipboard.GetText(System.Windows.Forms.TextDataFormat.Html), InstantMessageContentType.Html);
//}
else if (System.Windows.Forms.Clipboard.ContainsText())
{
DoSendText(System.Windows.Forms.Clipboard.GetText(), InstantMessageContentType.PlainText);
}
}
}
}