/
PublicMessageHandler.cs
144 lines (115 loc) · 4.92 KB
/
PublicMessageHandler.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
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Alaris.Calculator;
using Alaris.Calculator.lexer;
using System.IO;
using Alaris.Calculator.node;
using Alaris.Calculator.parser;
using Alaris.Framework;
using Alaris.Irc;
using Alaris.Mathematics.Types;
namespace Alaris
{
public partial class AlarisBot
{
/// <summary>
/// The method called when a public message occurs in one of the channels the bot is on.
/// </summary>
/// <param name = "user">
/// The data about the user who sent the message.
/// </param>
/// <param name = "chan">
/// The channel where the message occured.
/// </param>
/// <param name = "msg">
/// The message that was sent.
/// </param>
protected override void OnPublicMessage(UserInfo user, string chan, string msg)
{
var smsg = msg;
Task.Factory.StartNew(() =>
{
var urlsin = Utilities.GetUrls(smsg);
if (urlsin.Count <= 0) return;
try
{
Parallel.ForEach(urlsin, url => Utilities.HandleWebTitle(chan, url));
return;
}
catch (Exception ex)
{
Log.Error("Invalid webpage address: {0}", ex.Message);
//_connection.Sender.PublicMessage(chan, IrcConstants.Red + "Invalid address.");
return;
}
});
if(msg.StartsWith("@calc ", StringComparison.InvariantCultureIgnoreCase) || msg.StartsWith("@c ", StringComparison.InvariantCultureIgnoreCase))
{
if (msg.StartsWith("@calc ", StringComparison.InvariantCultureIgnoreCase)) msg = msg.Replace("@calc ", string.Empty);
else if (msg.StartsWith("@c ", StringComparison.InvariantCultureIgnoreCase)) msg = msg.Replace("@c ", string.Empty);
using (var reader = new StringReader(msg))
{
var lexer = new Lexer(reader);
var parser = new Parser(lexer);
Start ast;
try {
ast = parser.Parse(); }
catch(Exception x)
{
Log.Error("Math", x.ToString());
return;
}
var printer = new AstPrinter();
ast.Apply(printer);
printer.Dispose();
var calc = new AstCalculator();
ast.Apply(calc);
SendMsg(chan, calc.CalculatedResult.ToString());
return;
}
}
// Lua code runner.
//LuaEngine.LuaHelper.HandleLuaCommands(_manager.Lua.LuaVM, chan, msg);
if(msg.Equals("@UnloadPlugins", StringComparison.InvariantCultureIgnoreCase))
{
InstanceHolder<AlarisBot>.Instance.UnloadAll();
SendMsg(chan, "Done.");
}
if(msg.Equals("@LoadPlugins", StringComparison.InvariantCultureIgnoreCase))
{
InstanceHolder<AlarisBot>.Instance.LoadAll();
SendMsg(chan, "Done.");
}
if(msg.Equals("@ReloadPlugins", StringComparison.InvariantCultureIgnoreCase))
{
InstanceHolder<AlarisBot>.Instance.ReloadAll();
SendMsg(chan, "Done.");
}
if(msg.StartsWith("@sort "))
{
var rest = msg.Remove(0, 6);
var rgx = new Regex(@"(?<num>(?<prefix>\S)?(\d+(\.\d+)?))");
if (!rgx.IsMatch(rest))
return;
try
{
var matches = rgx.Matches(rest);
var nums = new AutoSortedArray<double>();
foreach (Match match in matches)
nums.SimpleAdd(double.Parse(match.Groups["num"].ToString()));
nums.Sort();
SendMsg(chan, nums.ToString());
}
catch(Exception x)
{
Log.Error(x.ToString());
Log.Debug(double.Parse("3.55", CultureInfo.InvariantCulture).ToString());
SendMsg(chan, "Hiba!");
}
}
base.OnPublicMessage(user, chan, smsg);
}
}
}