Skip to content
This repository has been archived by the owner on Sep 29, 2020. It is now read-only.

No more space/tab mixing #6

Merged
merged 1 commit into from
Feb 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 42 additions & 42 deletions IRC-Relay/IRC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace IRCRelay
{
public class IRC
{
private IrcClient ircClient;
private System.Timers.Timer timer = null;
private IrcClient ircClient;
private System.Timers.Timer timer = null;

private string server;
private int port;
Expand All @@ -22,83 +22,83 @@ public class IRC
private string targetGuild;
private string targetChannel;

private bool logMessages;
private bool logMessages;

public IRC(string server, int port, string nick, string channel, string loginName,
string authstring, string authuser, string targetGuild, string targetChannel, bool logMessages)
{
ircClient = new IrcClient();

ircClient.Encoding = System.Text.Encoding.UTF8;
ircClient.SendDelay = 200;
ircClient.Encoding = System.Text.Encoding.UTF8;
ircClient.SendDelay = 200;

ircClient.ActiveChannelSyncing = true;
ircClient.ActiveChannelSyncing = true;

ircClient.AutoRetry = true;
ircClient.AutoRejoin = true;
ircClient.AutoRelogin = true;
ircClient.AutoRejoinOnKick = true;

ircClient.AutoRetry = true;
ircClient.AutoRejoin = true;
ircClient.AutoRelogin = true;
ircClient.AutoRejoinOnKick = true;
ircClient.OnError += this.OnError;
ircClient.OnChannelMessage += this.OnChannelMessage;
ircClient.OnDisconnected += this.OnDisconnected;

ircClient.OnError += this.OnError;
ircClient.OnChannelMessage += this.OnChannelMessage;
ircClient.OnDisconnected += this.OnDisconnected;
timer = new System.Timers.Timer();

timer = new System.Timers.Timer();
timer.Elapsed += Timer_Callback;

timer.Elapsed += Timer_Callback;

timer.Enabled = true;
timer.AutoReset = true;
timer.Enabled = true;
timer.AutoReset = true;
timer.Interval = TimeSpan.FromSeconds(30.0).TotalMilliseconds;

/* Connection Info */
this.server = server;
this.port = port;
this.nick = nick;
this.channel = channel;
this.loginName = loginName;
this.authstring = authstring;
this.server = server;
this.port = port;
this.nick = nick;
this.channel = channel;
this.loginName = loginName;
this.authstring = authstring;
this.authuser = authuser;
this.targetGuild = targetGuild;
this.targetChannel = targetChannel;
this.logMessages = logMessages;
}
}

public void SendMessage(string message)
{
ircClient.SendMessage(SendType.Message, channel, message);
}
}

public void SpawnBot()
{
new Thread(() =>
{
try
{
ircClient.Connect(server, port);
new Thread(() =>
{
try
{
ircClient.Connect(server, port);

ircClient.Login(nick, loginName);

if (authstring.Length != 0)
{
{
ircClient.SendMessage(SendType.Message, authuser, authstring);

Thread.Sleep(1000); // login delay
}
Thread.Sleep(1000); // login delay
}

ircClient.RfcJoin(channel);
ircClient.RfcJoin(channel);

ircClient.Listen();
ircClient.Listen();

timer.Start();
timer.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return;
}
}

}).Start();
}).Start();
}

private void Timer_Callback(Object source, ElapsedEventArgs e)
Expand Down
18 changes: 9 additions & 9 deletions IRC-Relay/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ public static void Main(string[] args)

private async Task MainAsync()
{
try
{
config = Settings.Config.Load();
}
catch
{
Console.WriteLine("Unable to load config. Ensure Settings.xml is formatted correctly.");
try
{
config = Settings.Config.Load();
}
catch
{
Console.WriteLine("Unable to load config. Ensure Settings.xml is formatted correctly.");
config = Settings.Config.CreateDefaultConfig();
Settings.Config.Save(config);
return;
}
return;
}

client = new DiscordSocketClient();
commands = new CommandService();
Expand Down
58 changes: 29 additions & 29 deletions IRC-Relay/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace IRCRelay.Settings
{
public class Config
{
// ---------------------------------------------------------------------------------------
// Insert configurable values below:
public string IRCServer;
public string IRCPort;
public class Config
{
// ---------------------------------------------------------------------------------------
// Insert configurable values below:
public string IRCServer;
public string IRCPort;
public string IRCChannel;
public string IRCNick;

Expand All @@ -21,19 +21,19 @@ public class Config
public string DiscordBotToken;
public string DiscordGuildName;
public string DiscordChannelName;
// ---------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------

// Name of configuration file.
[XmlIgnore]
public const string FileName = "Settings.xml";
// Name of configuration file.
[XmlIgnore]
public const string FileName = "Settings.xml";

// Empty constructor for XmlSerializer.
public Config()
{
}
// Empty constructor for XmlSerializer.
public Config()
{
}

// Used to load the default configuration if Load() fails.
public static Config CreateDefaultConfig()
// Used to load the default configuration if Load() fails.
public static Config CreateDefaultConfig()
{
Config config = new Config()
{
Expand All @@ -56,20 +56,20 @@ public static Config CreateDefaultConfig()

// Loads the configuration from file.
public static Config Load()
{
var serializer = new XmlSerializer(typeof(Config));
{
var serializer = new XmlSerializer(typeof(Config));

using (var fStream = new FileStream(Config.FileName, FileMode.Open))
return (Config)serializer.Deserialize(fStream);
}
using (var fStream = new FileStream(Config.FileName, FileMode.Open))
return (Config)serializer.Deserialize(fStream);
}

// Saves the configuration to file.
public static void Save(Config config)
{
var serializer = new XmlSerializer(typeof(Config));
// Saves the configuration to file.
public static void Save(Config config)
{
var serializer = new XmlSerializer(typeof(Config));

using (var fStream = new FileStream(Config.FileName, FileMode.Create))
serializer.Serialize(fStream, config);
}
}
using (var fStream = new FileStream(Config.FileName, FileMode.Create))
serializer.Serialize(fStream, config);
}
}
}
96 changes: 48 additions & 48 deletions IRC-Relay/XmlSerializable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,56 @@

namespace IRCRelay.Config
{
public class XmlSerializable<T>
where T : XmlSerializable<T>
{
public static T Load(string fileName)
{
FileStream fs = null;
T ret = null;
try
{
fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
XmlSerializer xs = new XmlSerializer(typeof(T));
ret = (T)xs.Deserialize(fs);
}
catch (Exception)
{
throw;
}
finally
{
if (fs != null)
fs.Close();
}
public class XmlSerializable<T>
where T : XmlSerializable<T>
{
public static T Load(string fileName)
{
FileStream fs = null;
T ret = null;
try
{
fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
XmlSerializer xs = new XmlSerializer(typeof(T));
ret = (T)xs.Deserialize(fs);
}
catch (Exception)
{
throw;
}
finally
{
if (fs != null)
fs.Close();
}

return ret;
}
return ret;
}

public static bool Save(string fileName, T data)
{
FileStream fs = null;
try
{
fs = File.Create(fileName);
XmlSerializer xs = new XmlSerializer(typeof(T));
xs.Serialize(fs, data);
public static bool Save(string fileName, T data)
{
FileStream fs = null;
try
{
fs = File.Create(fileName);
XmlSerializer xs = new XmlSerializer(typeof(T));
xs.Serialize(fs, data);

return true;
}
catch
{
throw;
}
finally
{
if (fs != null)
fs.Close();
}
}
return true;
}
catch
{
throw;
}
finally
{
if (fs != null)
fs.Close();
}
}

public bool Save(string fileName)
{
public bool Save(string fileName)
{
if (this is T s)
{
return Save(fileName, s);
Expand All @@ -65,6 +65,6 @@ public bool Save(string fileName)
{
return false;
}
}
}
}
}
}