-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLogTail.cs
86 lines (69 loc) · 1.86 KB
/
LogTail.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
using System;
using System.Linq;
using MikuBot.Commands;
using MikuBot.ExtraPlugins.Helpers;
using MikuBot.Modules;
namespace MikuBot.ExtraPlugins
{
public class LogTail : MsgCommandModuleBase
{
private MessageBuffer messageBuffer;
private int IntParamOrDefault(ParamCollection col, int index, int def)
{
if (!col.HasParam(index))
return def;
if (int.TryParse(col[index], out var val))
return val;
else
return def;
}
public override string CommandDescription
{
get
{
return "Displays the last log lines.";
}
}
public override string Name
{
get { return "LogTail"; }
}
public override string UsageHelp
{
get
{
return "logtail [<number of lines>] [<channel name>]";
}
}
public override void HandleCommand(MsgCommand msg, IBotContext bot)
{
if (!msg.BotCommand.Is(Name))
return;
var lineCount = Math.Min(IntParamOrDefault(msg.BotCommand.Params, 0, 10), 20);
var channel = msg.BotCommand.Params.HasParam(1) ? new IrcName(msg.BotCommand.Params[1]) : msg.ChannelOrSenderNick;
if (!channel.IsChannel)
{
bot.Writer.Msg(msg.ChannelOrSenderNick, "Channel must be specified.");
return;
}
var lines = messageBuffer.GetLines(channel, lineCount).ToArray();
var receiver = new Receiver(bot.Writer, msg.Sender.Nick);
if (!lines.Any())
receiver.Msg("No lines.");
foreach (var line in lines)
{
receiver.Msg(string.Format("[{0}] <{1}> {2}", line.Timestamp.ToShortTimeString(), line.Message.Sender.Nick, line.Message.Text));
}
//File.ReadLines(logFile).Reverse().Take()
/*using (var reader = new StreamReader(logFile)) {
reader.BaseStream.Seek()
}*/
}
public override void OnLoaded(IBotContext bot, IModuleFile moduleFile)
{
if (moduleFile == null)
return;
messageBuffer = ((ExtraPluginsModuleFile)moduleFile).MessageBuffer;
}
}
}