-
Notifications
You must be signed in to change notification settings - Fork 19
/
RelayRoute.cs
114 lines (96 loc) · 3.48 KB
/
RelayRoute.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
using System;
using System.Collections.Generic;
using System.IO;
using MCGalaxy;
using MCGalaxy.Events.ServerEvents;
using MCGalaxy.Modules.Relay;
using MCGalaxy.Modules.Relay.Discord;
using MCGalaxy.Modules.Relay.IRC;
namespace PluginRelayRoute
{
public sealed class RelayRoutePlugin : Plugin
{
public override string MCGalaxy_Version { get { return "1.9.3.4"; } }
public override string name { get { return "RelayRoute"; } }
List<Route> routes = new List<Route>();
class Route {
public RelayBot srcBot, dstBot;
public string srcChan, dstChan;
}
public override void Load(bool startup) {
OnChannelMessageEvent.Register(OnMessage, Priority.Low);
OnConfigUpdatedEvent.Register(OnConfigUpdated, Priority.Low);
OnConfigUpdated();
}
public override void Unload(bool shutdown) {
OnChannelMessageEvent.Unregister(OnMessage);
OnConfigUpdatedEvent.Unregister(OnConfigUpdated);
}
void OnMessage(RelayBot bot, string channel, RelayUser user, string message, ref bool cancel) {
// ignore messages from relay bots themselves
if (cancel || user.ID == bot.UserID) return;
foreach (Route route in routes)
{
if (route.srcBot != bot) continue;
if (route.srcChan != channel) continue;
string msg = "(" + bot.RelayName + ") " + user.Nick + ": " + message;
route.dstBot.SendMessage(route.dstChan, msg);
}
}
void OnConfigUpdated() { LoadRoutes(); }
const string ROUTES_FILE = "text/relayroutes.txt";
static string[] default_routes = new string[] {
"# This file contains a list of routes for relay bots",
"# Each route should be on a separate line",
"# Note: Only messages sent by other users are routed",
"#",
"# Each route must use the following format: ",
"# [source service] [source channel] : [destination service] [destination channel]",
"#",
"# Some examples:",
"# - Route from Discord channel 123456789 to IRC channel #test",
"# Discord 123456789 : IRC #test",
"# - Route from Discord channel 123456789 to Discord channel 987654321",
"# Discord 123456789 : Discord 987654321",
"# - Route from IRC channel #test to Discord channel 123456789",
"# IRC #test : Discord 123456789",
};
void LoadRoutes() {
if (!File.Exists(ROUTES_FILE))
File.WriteAllLines(ROUTES_FILE, default_routes);
string[] lines = File.ReadAllLines(ROUTES_FILE);
List<Route> r = new List<Route>();
foreach (string line in lines)
{
if (line.IsCommentLine()) continue;
try {
r.Add(ParseRouteLine(line));
} catch (Exception ex) {
Logger.LogError("Error parsing route '" + line + "'", ex);
}
}
routes = r;
}
Route ParseRouteLine(string line) {
string[] bits = line.Split(':');
if (bits.Length != 2)
throw new ArgumentException("Route requires exactly 1 separating :");
Route r = new Route();
ParseRouteNode(bits[0], out r.srcBot, out r.srcChan);
ParseRouteNode(bits[1], out r.dstBot, out r.dstChan);
return r;
}
void ParseRouteNode(string part, out RelayBot bot, out string chan) {
string[] bits = part.Trim().Split(' ');
if (bits.Length != 2)
throw new ArgumentException("A space is required between route service and channel");
bot = GetRouteBot(bits[0]);
chan = bits[1];
}
RelayBot GetRouteBot(string service) {
if (service.CaselessEq("IRC")) return IRCPlugin.Bot;
if (service.CaselessEq("Discord")) return DiscordPlugin.Bot;
throw new ArgumentException("Unknown service '" + service + "'");
}
}
}