-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBind.cs
686 lines (594 loc) · 18.7 KB
/
Bind.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Linq;
using System.Threading;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Reflection;
using Misuzilla.Net.Irc;
using Misuzilla.Applications.TwitterIrcGateway;
using Misuzilla.Applications.TwitterIrcGateway.AddIns;
using Misuzilla.Applications.TwitterIrcGateway.AddIns.Console;
using Misuzilla.Applications.TwitterIrcGateway.AddIns.TypableMap;
namespace Spica.Applications.TwitterIrcGateway.AddIns.Bind
{
#region EventArgs
/// <summary>
/// PRIVメッセージ受信時イベントのデータを提供します。
/// </summary>
public class BindPrivMessageReceivedEventArgs : CancelableEventArgs
{
/// <summary>
/// 受信したIRCメッセージを取得します
/// </summary>
public PrivMsgMessage Message { get; set; }
public BindPrivMessageReceivedEventArgs(PrivMsgMessage msg)
{
Message = msg;
}
}
#endregion
#region Interface
public interface IBindNode
{
String GetChannelName();
String GetNodeName();
Type GetContextType();
void OnMessageReceived(BindPrivMessageReceivedEventArgs e);
}
#endregion
#region Node
[XmlType("Node")]
public abstract class BindNodeBase : IConfiguration, IBindNode
{
[Description("ノードを有効化または無効化します")]
public Boolean Enabled { get; set; }
[Browsable(false)]
[XmlIgnore]
public BindAddIn AddIn { get; private set; }
public BindNodeBase()
{
Enabled = true;
}
public virtual void Initialize(BindAddIn addIn)
{
AddIn = addIn;
}
public virtual void Uninitialize()
{
}
public abstract String GetChannelName();
public abstract String GetNodeName();
public abstract Type GetContextType();
public abstract void OnMessageReceived(BindPrivMessageReceivedEventArgs e);
public virtual string ToShortString()
{
// Feed - http://example.com
// Lists - UserId/ListId
return String.Format("{0} - {1}", GetNodeName(), ToString());
}
public virtual string ToLongString()
{
// [*] Feed - http://example.com
// [ ] Lists - UserId/ListId
return String.Format("[{0}] {1}", Enabled ? "*" : " ", ToShortString());
}
public TApi CreateApi<TApi>()
where TApi : ApiBase, new()
{
return new TApi()
{
EnableCompression = AddIn.EnableCompression
};
}
/// <summary>
/// バインドされたチャンネルにメッセージを送信します。
/// </summary>
protected void SendMessage(String sender, String content, Boolean notice)
{
var receiver = GetChannelName();
if (String.IsNullOrEmpty(receiver))
receiver = AddIn.CurrentSession.CurrentNick;
foreach (String line in BindUtility.SplitLineBreak(content))
{
IRCMessage message = null;
if (notice)
message = new NoticeMessage(receiver, line);
else
message = new PrivMsgMessage(receiver, line);
message.SenderNick = sender;
message.SenderHost = String.Format("{0}@{1}", GetNodeName().ToLowerInvariant(), Server.ServerName);
AddIn.CurrentSession.Send(message);
}
}
/// <summary>
/// バインドされたチャンネルに例外メッセージを送信します。
/// </summary>
protected void SendException(Exception exception)
{
if (!AddIn.Config.IgnoreWatchError)
{
SendMessage(BindAddIn.DefaultSenderNick, BindUtility.RemoveLineBreak(exception.Message), true);
}
}
}
public abstract class BindTimerNodeBase : BindNodeBase
{
[Description("ノードをチェックする間隔を秒単位で指定します")]
public Int32 Interval { get; set; }
private static readonly Random _random = new Random();
private Timer _timer = null;
private Object _timerSync = new object();
private Boolean _isFirstTime = true;
public BindTimerNodeBase()
{
Interval = 90;
}
public override void Initialize(BindAddIn addIn)
{
base.Initialize(addIn);
Update();
}
public override void Uninitialize()
{
Stop();
base.Uninitialize();
}
public override string ToShortString()
{
// Feed - http://example.com (3600)
// Lists - UserId/ListId (60)
return String.Format("{0} - {1} ({2})", GetNodeName(), ToString(), Interval);
}
protected virtual Boolean IsValid()
{
return Enabled;
}
#region Timer
/// <summary>
/// タイマーの状態を更新します。
/// </summary>
public void Update()
{
if (IsValid())
Start();
else
Stop();
}
/// <summary>
/// タイマーを開始します。
/// </summary>
public void Start()
{
if (_timer == null)
_timer = new Timer(OnTimerCallbackInternal, null, Timeout.Infinite, Timeout.Infinite);
// HACK: MONO のバグ対策に dueTime を適度にずらす
lock (_random)
{
Int32 intervalMillSec = Interval * 1000;
Int32 randomDueTime = _random.Next(0, 3000);
_timer.Change(randomDueTime, intervalMillSec);
}
}
/// <summary>
/// タイマーを停止します。
/// </summary>
public void Stop()
{
if (_timer != null)
{
_timer.Dispose();
_timer = null;
}
}
/// <summary>
/// タイマーのコールバック処理を強制的に実行させます。
/// </summary>
public void Force()
{
ThreadPool.QueueUserWorkItem((state) => { OnTimerCallbackInternal(state); });
}
/// <summary>
/// タイマーのコールバック処理
/// </summary>
private void OnTimerCallbackInternal(Object state)
{
if (Monitor.TryEnter(_timerSync))
{
try
{
OnTimerCallback(_isFirstTime);
_isFirstTime = false;
}
finally
{
Monitor.Exit(_timerSync);
}
}
}
/// <summary>
/// タイマーのコールバック処理
/// </summary>
protected abstract void OnTimerCallback(Boolean isFirstTime);
#endregion
}
#endregion
#region Context
[Description("チャンネルにバインドさせるノードの設定を行うコンテキストに切り替えます")]
public class BindContext : Context
{
private BindAddIn AddIn { get { return CurrentSession.AddInManager.GetAddIn<BindAddIn>(); } }
public override IConfiguration[] Configurations { get { return new IConfiguration[] { AddIn.Config }; } }
protected override void OnConfigurationChanged(IConfiguration config, System.Reflection.MemberInfo memberInfo, object value)
{
if (config is BindConfiguration)
{
AddIn.Config = config as BindConfiguration;
AddIn.SaveConfig();
}
}
[Description("指定したノードを更新します")]
public void Update(String arg)
{
FindAt(arg, node =>
{
if (node is BindTimerNodeBase)
{
var timerNode = node as BindTimerNodeBase;
timerNode.Force();
}
Console.NotifyMessage(String.Format("ノード {0} を更新しました。", node.ToShortString()));
});
}
[Description("存在するノードをすべて表示します")]
public void List()
{
if (AddIn.Config.Nodes.Count == 0)
{
Console.NotifyMessage("ノードは現在設定されていません。");
return;
}
for (Int32 i = 0; i < AddIn.Config.Nodes.Count; ++i)
{
var node = AddIn.Config.Nodes[i];
Console.NotifyMessage(String.Format("{0}: {1}", i, node.ToLongString()));
}
}
[Description("指定したノードを有効化します")]
public void Enable(String arg)
{
SwitchEnable(arg, true);
}
[Description("指定したノードを無効化します")]
public void Disable(String arg)
{
SwitchEnable(arg, false);
}
[Description("指定したノードを入れ替えます")]
public void Swap(String[] args)
{
if (args.Length != 2)
{
Console.NotifyMessage("ノードの指定が正しくありません。");
return;
}
FindAt(args[0], (node1, index1) =>
{
FindAt(args[1], (node2, index2) =>
{
var tmp = AddIn.Config.Nodes[index1];
AddIn.Config.Nodes[index1] = AddIn.Config.Nodes[index2];
AddIn.Config.Nodes[index2] = tmp;
AddIn.SaveConfig();
Console.NotifyMessage(String.Format("ノード {0} と ノード {1} を入れ替えしました。", node1.ToShortString(), node2.ToShortString()));
});
});
}
[Description("指定したノードを削除します")]
public void Remove(String arg)
{
FindAt(arg, node =>
{
node.Uninitialize();
AddIn.Config.Nodes.Remove(node);
AddIn.SaveConfig();
Console.NotifyMessage(String.Format("ノード {0} を削除しました。", node.ToShortString()));
});
}
[Description("指定したノードを編集します")]
public void Edit(String arg)
{
FindAt(arg, node =>
{
// コンテキストを作成
BindEditContextBase context = Console.GetContext(node.GetContextType(), CurrentServer, CurrentSession) as BindEditContextBase;
context.Node = node;
context.IsNew = false;
Console.PushContext(context);
});
}
[Description("ノードを新規追加します")]
public void New(String nodeName)
{
Type nodeType;
if (!AddIn.BindableNodes.TryGetValue(nodeName, out nodeType))
{
String nodeNames = String.Join("|", AddIn.BindableNodes.Select(d => d.Key).ToArray());
Console.NotifyMessage("不明なノードの種類が指定されました。");
Console.NotifyMessage(String.Format("これらのノードが指定可能です。 [{0}]", nodeNames));
return;
}
// ノードを作成
BindNodeBase node = Activator.CreateInstance(nodeType) as BindNodeBase;
node.Initialize(AddIn);
// コンテキストを作成
BindEditContextBase context = Console.GetContext(node.GetContextType(), CurrentServer, CurrentSession) as BindEditContextBase;
context.Node = node;
context.IsNew = true;
Console.PushContext(context);
}
private void SwitchEnable(String arg, Boolean enable)
{
FindAt(arg, node =>
{
node.Enabled = enable;
if (node is BindTimerNodeBase)
{
var timerNode = node as BindTimerNodeBase;
timerNode.Update();
}
AddIn.SaveConfig();
Console.NotifyMessage(String.Format("ノード {0} を{1}化しました。", node.ToShortString(), (enable ? "有効" : "無効")));
});
}
private void FindAt(String arg, Action<BindNodeBase> action)
{
FindAt(arg, (node, index) => action(node));
}
private void FindAt(String arg, Action<BindNodeBase, Int32> action)
{
Int32 index;
if (Int32.TryParse(arg, out index))
{
if (index < AddIn.Config.Nodes.Count && index > -1)
{
action(AddIn.Config.Nodes[index], index);
}
else
{
Console.NotifyMessage("存在しないノードが指定されました。");
}
}
else
{
Console.NotifyMessage("ノードの指定が正しくありません。");
}
}
}
public abstract class BindEditContextBase : Context
{
protected BindAddIn AddIn { get { return CurrentSession.AddInManager.GetAddIn<BindAddIn>(); } }
public BindNodeBase Node { get; set; }
public Boolean IsNew { get; set; }
public override IConfiguration[] Configurations { get { return new IConfiguration[] { Node }; } }
public override string ContextName { get { return (IsNew ? "New" : "Edit") + Node.GetNodeName(); } }
[Description("ノードの設定を保存してコンテキストを終了します")]
public void Save()
{
OnPreSaveConfig();
// 状態を保存
if (IsNew) AddIn.Config.Nodes.Add(Node);
AddIn.SaveConfig();
OnPostSaveConfig();
Console.NotifyMessage(String.Format("ノードの設定を{0}しました。", (IsNew ? "新規作成" : "保存")));
Exit();
}
/// <summary>
/// 設定保存前の処理
/// </summary>
protected virtual void OnPreSaveConfig()
{
}
/// <summary>
/// 設定保存後の処理
/// </summary>
protected virtual void OnPostSaveConfig()
{
}
/// <summary>
/// グループを作成します。
/// </summary>
/// <param name="groupName">グループ名</param>
protected void CreateGroup(String groupName)
{
CreateGroup(groupName, AddIn.Config.InitialModes);
}
/// <summary>
/// グループを作成します。
/// </summary>
/// <param name="groupName">グループ名</param>
/// <param name="modes">モード</param>
protected void CreateGroup(String groupName, String modes)
{
if (!String.IsNullOrEmpty(groupName) && !CurrentSession.Groups.ContainsKey(groupName))
{
// グループを作成する
Misuzilla.Applications.TwitterIrcGateway.Group group = new Misuzilla.Applications.TwitterIrcGateway.Group(groupName);
foreach (ChannelMode mode in ChannelMode.Parse(modes ?? String.Empty))
group.ChannelModes.Add(mode);
CurrentSession.Groups.Add(groupName, group);
CurrentSession.JoinChannel(CurrentSession, group);
Console.NotifyMessage(String.Format("グループ名 {0} を作成しました。", groupName));
CurrentSession.SaveGroups();
}
}
}
#endregion
#region Configuration
public class BindConfiguration : IConfiguration
{
[Description("チャンネル作成時のモードを指定します")]
public String InitialModes { get; set; }
[Description("エラーを無視するかどうかを指定します")]
public Boolean IgnoreWatchError { get; set; }
[Browsable(false)]
public List<BindNodeBase> Nodes { get; set; }
public BindConfiguration()
{
InitialModes = "+pni";
IgnoreWatchError = true;
Nodes = new List<BindNodeBase>();
}
}
#endregion
public class BindAddIn : AddInBase
{
public const String DefaultSenderNick = "$Bind";
public const String DefaultChannelName = "#Bind";
public BindConfiguration Config { get; set; }
public Dictionary<String, Type> BindableNodes { get; set; }
private TypableMapCommandProcessor _typableMapCommands = null;
// TODO: 無理矢理過ぎるのでどうにかしたい
internal new Server CurrentServer { get { return base.CurrentServer; } }
internal new Session CurrentSession { get { return base.CurrentSession; } }
internal Boolean EnableTypableMap { get { return CurrentSession.Config.EnableTypableMap; } }
internal Boolean EnableCompression { get { return CurrentSession.Config.EnableCompression; } }
public BindAddIn()
{
BindableNodes = new Dictionary<String, Type>(StringComparer.InvariantCultureIgnoreCase);
}
public override void Initialize()
{
base.Initialize();
// アセンブリからノードを読み込む
LoadBindableNodes();
// 設定を読み込みと関連付け
Config = CurrentSession.AddInManager.GetConfig<BindConfiguration>();
foreach (var node in Config.Nodes)
{
node.Initialize(this);
}
CurrentSession.PreMessageReceived += new EventHandler<MessageReceivedEventArgs>(CurrentSession_PreMessageReceived);
CurrentSession.AddInsLoadCompleted += (sender, e) =>
{
// コンテキストを登録
CurrentSession.AddInManager.GetAddIn<ConsoleAddIn>().RegisterContext<BindContext>();
// TypableMapが使えるかどうか
var typableMapSupport = CurrentSession.AddInManager.GetAddIn<TypableMapSupport>();
if (typableMapSupport != null)
_typableMapCommands = typableMapSupport.TypableMapCommands;
};
}
public override void Uninitialize()
{
SaveConfig();
foreach (var node in Config.Nodes)
{
node.Uninitialize();
}
base.Uninitialize();
}
/// <summary>
/// IBindableNode を継承した型を読み込む
/// </summary>
private void LoadBindableNodes()
{
Assembly asm = Assembly.GetExecutingAssembly();
Type nodeType = typeof(IBindNode);
foreach (Type type in asm.GetTypes())
{
if (nodeType.IsAssignableFrom(type) && !type.IsAbstract && type.IsClass)
{
// いちいちインスタンス化してるのがちょっとあれ
IBindNode node = Activator.CreateInstance(type) as IBindNode;
BindableNodes[node.GetNodeName()] = type;
}
}
}
/// <summary>
/// バインドされたチャンネル宛のメッセージだったら奪い取る
/// </summary>
private void CurrentSession_PreMessageReceived(object sender, MessageReceivedEventArgs e)
{
var message = e.Message as PrivMsgMessage;
if (message == null)
return;
var receiver = message.Receiver;
if (!String.IsNullOrEmpty(receiver))
{
var nodes = Config.Nodes
.Where(n => String.Compare(n.GetChannelName(), receiver, true) == 0)
.ToList();
if (nodes.Count > 0)
{
var cancel = true;
foreach (var node in nodes)
{
var eventArgs = new BindPrivMessageReceivedEventArgs(message);
node.OnMessageReceived(eventArgs);
// Twitter に流したいノードでがひとつでもあったら流す
cancel &= eventArgs.Cancel;
}
// #Console とかを考慮
e.Cancel |= cancel;
}
}
}
/// <summary>
/// 設定を保存します。
/// </summary>
internal void SaveConfig()
{
CurrentSession.AddInManager.SaveConfig(Config);
}
/// <summary>
/// 時間を頭につける
/// </summary>
public String ApplyDateTime(String str, DateTime dateTime, Boolean isFirstTime)
{
if (isFirstTime)
return String.Format("{0} {1}", dateTime.ToString("HH:mm"), str);
else
return str;
}
/// <summary>
/// Twitter の Status に TypableMap をつける
/// </summary>
public String ApplyTypableMap(String str, Status status)
{
if (_typableMapCommands != null)
return ApplyTypableMap(str, status, new TypableMapStatusRepositoryWrapper(_typableMapCommands.TypableMap));
return str;
}
/// <summary>
/// 任意の型に TypableMap をつける
/// </summary>
public String ApplyTypableMap<T>(String str, T value, ITypableMapGenericRepository<T> typableMap)
{
if (EnableTypableMap)
{
if (typableMap != null)
{
String typableMapId = typableMap.Add(value);
// TypableMapKeyColorNumber = -1 の場合には色がつかなくなる
if (CurrentSession.Config.TypableMapKeyColorNumber < 0)
return str + String.Format(" ({0})", typableMapId);
else
return str + String.Format(" \x03{0}({1})\x03", CurrentSession.Config.TypableMapKeyColorNumber, typableMapId);
}
}
return str;
}
/// <summary>
/// ClientMessageWait に設定された時間だけ眠らせる
/// </summary>
public void SleepClientMessageWait()
{
if (CurrentSession.Config.ClientMessageWait > 0)
Thread.Sleep(CurrentSession.Config.ClientMessageWait);
}
}
}