Browse Source

Added configuration saving and loading.

Added save config command for bot session access.
Added packages/ to git ignore.
Added Json.NET to nuget package list.
tags/3.0.0
Teknikode 4 years ago
parent
commit
a5e849b804

+ 1
- 1
.gitignore View File

@@ -128,7 +128,7 @@ publish/

# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
#packages/
packages/

# Windows Azure Build Output
csx

+ 41
- 9
Combot/Bot.cs View File

@@ -5,32 +5,44 @@ using System.Text;
using System.Net;
using System.Threading.Tasks;
using Combot.IRCServices;
using Combot.Configurations;
using Combot.IRCServices.Messaging;

namespace Combot
{
public class Bot
{
public event Action<BotError> ErrorEvent;
public Config Config;
public ServerConfig ServerConfig;
public IRC IRC;
public bool Connected = false;

public Bot()
public Bot(ServerConfig serverConfig)
{
Config = new Config();
IRC = new IRC();

ServerConfig = serverConfig;
IRC.ConnectEvent += HandleConnectEvent;
IRC.DisconnectEvent += HandleDisconnectEvent;
IRC.Message.ServerReplyEvent += HandleReplyEvent;
}

public bool Connect()
{
bool serverConnected = false;
int i = 0;
do
{
if (Config.Server.Hosts.Count > i)
if (ServerConfig.Hosts.Count > i)
{
Connected = IRC.Connect(Config.Server.Hosts[i].Address, Config.Server.Hosts[i].Port, 5000);
IPAddress[] ipList = Dns.GetHostAddresses(ServerConfig.Hosts[i].Host);
foreach (IPAddress ip in ipList)
{
serverConnected = IRC.Connect(ip, ServerConfig.Hosts[i].Port, 5000);
if (serverConnected)
{
break;
}
}
i++;
}
else
@@ -38,11 +50,11 @@ namespace Combot
break;
}
}
while (!Connected);
while (!serverConnected);

if (Connected)
if (serverConnected)
{
IRC.Login(Config.Server.Name, new Nick() { Nickname = Config.Nick, Host = Dns.GetHostName(), Realname = Config.Realname });
IRC.Login(ServerConfig.Name, new Nick() { Nickname = ServerConfig.Nickname, Host = Dns.GetHostName(), Realname = ServerConfig.Realname, Username = ServerConfig.Username });
}

return Connected;
@@ -56,9 +68,29 @@ namespace Combot
return Connected;
}

private void HandleConnectEvent()
{
Connected = true;
}

private void HandleDisconnectEvent()
{
Connected = false;
}

private void HandleReplyEvent(object sender, IReply e)
{
if (e.GetType() == typeof(ServerReplyMessage))
{
ServerReplyMessage reply = (ServerReplyMessage)e;
if (reply.ReplyCode == IRCReplyCode.RPL_ENDOFMOTD && Connected)
{
foreach (ChannelConfig channel in ServerConfig.Channels)
{
IRC.IRCSendJoin(channel.Name, channel.Key);
}
}
}
}
}
}

+ 12
- 1
Combot/Combot.csproj View File

@@ -30,6 +30,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
@@ -40,7 +44,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Bot.cs" />
<Compile Include="Config.cs" />
<Compile Include="Configurations\ChannelConfig.cs" />
<Compile Include="Configurations\Config.cs" />
<Compile Include="Configurations\HostConfig.cs" />
<Compile Include="Configurations\JsonHelper.cs" />
<Compile Include="Configurations\ServerConfig.cs" />
<Compile Include="Types.cs" />
<Compile Include="Methods.cs" />
<Compile Include="Modules\Module.cs" />
@@ -56,6 +64,9 @@
<ItemGroup>
<WCFMetadata Include="Service References\" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

+ 0
- 63
Combot/Config.cs View File

@@ -1,63 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;

namespace Combot
{
public class Config
{
private Server _server;
public Server Server
{
get
{
return _server;
}

set
{
if (value != _server)
{
_server = value;
}
}
}

private string _realname;
public string Realname
{
get
{
return _realname;
}

set
{
if (value != _realname)
{
_realname = value;
}
}
}

private string _nick;
public string Nick
{
get
{
return _nick;
}

set
{
if (value != _nick)
{
_nick = value;
}
}
}
}
}

+ 51
- 0
Combot/Configurations/ChannelConfig.cs View File

@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Combot.Configurations
{
public class ChannelConfig
{
public ChannelConfig()
{
Name = string.Empty;
Key = string.Empty;
}

private string _Name;
public string Name
{
get
{
return _Name;
}

set
{
if (_Name != value)
{
_Name = value;
}
}
}

private string _Key;
public string Key
{
get
{
return _Key;
}

set
{
if (_Key != value)
{
_Key = value;
}
}
}
}
}

+ 102
- 0
Combot/Configurations/Config.cs View File

@@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using Newtonsoft.Json;

namespace Combot.Configurations
{
public class Config
{
private ReaderWriterLockSlim ConfigRWLock;
private ReaderWriterLockSlim ConfigFileRWLock;
private JsonSerializerSettings JsonSettings;

public Config()
{
ConfigRWLock = new ReaderWriterLockSlim();
ConfigFileRWLock = new ReaderWriterLockSlim();
Servers = new List<ServerConfig>();
JsonSettings = new JsonSerializerSettings();
JsonSettings.Converters.Add(new IPAddressConverter());
JsonSettings.Converters.Add(new IPEndPointConverter());
JsonSettings.Formatting = Formatting.Indented;
}

private List<ServerConfig> _servers;
public List<ServerConfig> Servers
{
get
{
return _servers;
}

private set
{
if (value != _servers)
{
_servers = value;
}
}
}

public void AddServer(ServerConfig config)
{
ConfigRWLock.EnterWriteLock();
if (!Servers.Exists(server => server.Name == config.Name))
{
config.ModifyEvent += SaveServers;
Servers.Add(config);
}
ConfigRWLock.ExitWriteLock();
}

public void SaveServers()
{
ConfigFileRWLock.EnterWriteLock();

// Serialize Config
ConfigRWLock.EnterReadLock();
string configContents = JsonConvert.SerializeObject(Servers, JsonSettings);
ConfigRWLock.ExitReadLock();

// Save config to file
string ConfigPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.config");
using (StreamWriter streamWriter = new StreamWriter(ConfigPath, false))
{
streamWriter.Write(configContents);
}

ConfigFileRWLock.ExitWriteLock();
}

public void LoadServers()
{
ConfigFileRWLock.EnterReadLock();
string ConfigPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.config");
if (File.Exists(ConfigPath))
{
string configContents;
using (StreamReader streamReader = new StreamReader(ConfigPath, Encoding.UTF8))
{
configContents = streamReader.ReadToEnd();
}

// Load the deserialized file into the config
ConfigRWLock.EnterWriteLock();
Servers = JsonConvert.DeserializeObject<List<ServerConfig>>(configContents, JsonSettings);

for (int i = 0; i < Servers.Count; i++)
{
Servers[i].ModifyEvent += SaveServers;
}
ConfigRWLock.ExitWriteLock();
}
ConfigFileRWLock.ExitReadLock();
}
}
}

+ 51
- 0
Combot/Configurations/HostConfig.cs View File

@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Combot.Configurations
{
public class HostConfig
{
public HostConfig()
{
Host = string.Empty;
Port = 0;
}

private string _Host;
public string Host
{
get
{
return _Host;
}

set
{
if (_Host != value)
{
_Host = value;
}
}
}

private int _Port;
public int Port
{
get
{
return _Port;
}

set
{
if (_Port != value)
{
_Port = value;
}
}
}
}
}

+ 58
- 0
Combot/Configurations/JsonHelper.cs View File

@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Combot.Configurations
{
public class IPAddressConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(IPAddress));
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
IPAddress ip = (IPAddress)value;
writer.WriteValue(ip.ToString());
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
return IPAddress.Parse(token.Value<string>());
}
}

public class IPEndPointConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(IPEndPoint));
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
IPEndPoint ep = (IPEndPoint)value;
writer.WriteStartObject();
writer.WritePropertyName("Address");
serializer.Serialize(writer, ep.Address);
writer.WritePropertyName("Port");
writer.WriteValue(ep.Port);
writer.WriteEndObject();
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
IPAddress address = jo["Address"].ToObject<IPAddress>(serializer);
int port = jo["Port"].Value<int>();
return new IPEndPoint(address, port);
}
}
}

+ 151
- 0
Combot/Configurations/ServerConfig.cs View File

@@ -0,0 +1,151 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;

namespace Combot.Configurations
{
public class ServerConfig
{
public event Action ModifyEvent;

public ServerConfig()
{
AutoConnect = false;
Channels = new List<ChannelConfig>();
Hosts = new List<HostConfig>();
Nickname = string.Empty;
Realname = string.Empty;
Username = string.Empty;
}

public void Save()
{
if (ModifyEvent != null)
{
ModifyEvent();
}
}

private string _Name;
public string Name
{
get
{
return _Name;
}

set
{
if (_Name != value)
{
_Name = value;
}
}
}

private string _Nickname;
public string Nickname
{
get
{
return _Nickname;
}

set
{
if (_Nickname != value)
{
_Nickname = value;
}
}
}

private string _Username;
public string Username
{
get
{
return _Username;
}

set
{
if (_Username != value)
{
_Username = value;
}
}
}

private string _Realname;
public string Realname
{
get
{
return _Realname;
}

set
{
if (_Realname != value)
{
_Realname = value;
}
}
}

private List<HostConfig> _Hosts;
public List<HostConfig> Hosts
{
get
{
return _Hosts;
}

set
{
if (_Hosts != value)
{
_Hosts = value;
}
}
}

private List<ChannelConfig> _Channels;
public List<ChannelConfig> Channels
{
get
{
return _Channels;
}

set
{
if (_Channels != value)
{
_Channels = value;
}
}
}

private bool _AutoConnect;
public bool AutoConnect
{
get
{
return _AutoConnect;
}

set
{
if (_AutoConnect != value)
{
_AutoConnect = value;
}
}
}
}
}

+ 0
- 8
Combot/Types.cs View File

@@ -31,12 +31,4 @@ namespace Combot
public ErrorType Type { get; set; }
public string Message { get; set; }
}

public class Server
{
public string Name { get; set; }
public List<IPEndPoint> Hosts { get; set; }
public List<string> Channels { get; set; }
public bool AutoConnect { get; set; }
}
}

+ 4
- 0
Combot/packages.config View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net451" />
</packages>

+ 12
- 1
IRCServices/IRC.cs View File

@@ -14,6 +14,7 @@ namespace Combot.IRCServices
{
public List<Channel> Channels = new List<Channel>();
public Messages Message;
public event Action ConnectEvent;
public event Action DisconnectEvent;
public event Action<TCPError> TCPErrorEvent;
public string Nickname { get; set; }
@@ -57,6 +58,11 @@ namespace Combot.IRCServices
TCPReader = new Thread(ReadTCPMessages);
TCPReader.IsBackground = true;
TCPReader.Start();

if (ConnectEvent != null)
{
ConnectEvent();
}
}
}

@@ -72,6 +78,10 @@ namespace Combot.IRCServices
_TCP.Disconnect();
}

ChannelRWLock.EnterWriteLock();
Channels = new List<Channel>();
ChannelRWLock.ExitWriteLock();

if (DisconnectEvent != null)
{
DisconnectEvent();
@@ -84,7 +94,7 @@ namespace Combot.IRCServices
{
Nickname = nick.Nickname;
IRCSendNick(nick.Nickname);
IRCSendUser(nick.Nickname, nick.Host, serverName, nick.Realname);
IRCSendUser(nick.Username, nick.Host, serverName, nick.Realname);
}

private void ReadTCPMessages()
@@ -174,6 +184,7 @@ namespace Combot.IRCServices
nickFound = false;
nick = new Nick();
}
nick.Nickname = nickname;
nick.Host = host;
nick.Realname = realname;
nick.Username = username;

+ 1
- 0
IRCServices/Messaging/Messages.cs View File

@@ -206,6 +206,7 @@ namespace Combot.IRCServices.Messaging
{
UserModeChangeInfo modeMsg = new UserModeChangeInfo();
modeMsg.Modes = new List<UserModeInfo>();
modeMsg.Nick = new Nick() { Nickname = senderNick, Realname = senderRealname, Host = senderHost };

string[] modeArgs = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
char[] modeInfo = modeArgs[0].TrimStart(':').ToCharArray();

+ 0
- 1
Interface/MainWindow.xaml View File

@@ -7,6 +7,5 @@
<Grid>
<Button x:Name="ToggleConnectionButton" Content="{Binding ToggleConnectionText}" HorizontalAlignment="Left" Margin="10,0,0,10" Width="156" Command="{Binding ToggleConnection}" Height="33" VerticalAlignment="Bottom"/>
<TextBox x:Name="BufferWindow" Margin="10,10,10,48" TextWrapping="Wrap" Text="{Binding CurrentBuffer, Mode=TwoWay}"/>
<Button Content="Join Channel" HorizontalAlignment="Left" Margin="171,0,0,10" Width="121" Command="{Binding JoinChannel}" Height="33" VerticalAlignment="Bottom"/>
</Grid>
</Window>

+ 38
- 34
Interface/ViewModels/MainViewModel.cs View File

@@ -6,12 +6,14 @@ using System.Threading.Tasks;
using System.Net;
using Combot;
using Combot.IRCServices.Messaging;
using Combot.Configurations;

namespace Interface.ViewModels
{
public class MainViewModel : ViewModelBase
{
public Bot Combot = new Bot();
public List<Bot> CombotSessions = new List<Bot>();
public Config Config = new Config();

public string ApplicationTitle { get; set; }

@@ -25,36 +27,43 @@ namespace Interface.ViewModels
public string ToggleConnectionText { get { return _ToggleConnectionText; } set { _ToggleConnectionText = value; OnPropertyChanged("ToggleConnectionText"); } }

public DelegateCommand ToggleConnection { get; private set; }
public DelegateCommand JoinChannel { get; private set; }

public MainViewModel()
{
ApplicationTitle = "Combot";
Combot.Config.Nick = "Combot_V3";
Combot.Config.Realname = "Combot_V3_realname";
Combot.Config.Server = new Server();
Combot.Config.Server.AutoConnect = true;
Combot.Config.Server.Channels = new List<string>() { "#testing" };
Combot.Config.Server.Name = "Rizon";
IPAddress[] ipList = Dns.GetHostAddresses("irc.rizon.net");
Combot.Config.Server.Hosts = new List<IPEndPoint>();
foreach (IPAddress ip in ipList)
Config.LoadServers();
/*
ServerConfig serverConfig = new ServerConfig();
serverConfig.AutoConnect = true;
serverConfig.Channels = new List<ChannelConfig>() { new ChannelConfig() { Name = "#testing", Key = string.Empty } };
serverConfig.Name = "Rizon";
serverConfig.Nickname = "Combot_V3";
serverConfig.Realname = "Combot_Realname";
serverConfig.Username = "Combot_Username";
serverConfig.Hosts = new List<HostConfig>() { new HostConfig() { Host = "irc.rizon.net", Port = 6667 } };
Config.Servers.Add(serverConfig);
Config.SaveServers();
*/

foreach (ServerConfig server in Config.Servers)
{
Combot.Config.Server.Hosts.Add(new System.Net.IPEndPoint(ip, 6667));
}
Bot Combot = new Bot(server);

Combot.IRC.Message.ErrorMessageEvent += ErrorMessageHandler;
Combot.IRC.Message.ServerReplyEvent += ServerReplyHandler;
Combot.IRC.Message.ChannelMessageReceivedEvent += ChannelMessageReceivedHandler;
Combot.IRC.Message.ChannelNoticeReceivedEvent += ChannelNoticeReceivedHandler;
Combot.IRC.Message.PrivateMessageReceivedEvent += PrivateMessageReceivedHandler;
Combot.IRC.Message.PrivateNoticeReceivedEvent += PrivateNoticeReceivedHandler;

Combot.IRC.Message.ErrorMessageEvent += ErrorMessageHandler;
Combot.IRC.Message.ServerReplyEvent += ServerReplyHandler;
Combot.IRC.Message.ChannelMessageReceivedEvent += ChannelMessageReceivedHandler;
Combot.IRC.Message.ChannelNoticeReceivedEvent += ChannelNoticeReceivedHandler;
Combot.IRC.Message.PrivateMessageReceivedEvent += PrivateMessageReceivedHandler;
Combot.IRC.Message.PrivateNoticeReceivedEvent += PrivateNoticeReceivedHandler;
Combot.IRC.ConnectEvent += ConnectHandler;
Combot.IRC.DisconnectEvent += DisconnectHandler;
Combot.IRC.TCPErrorEvent += TCPErrorHandler;

Combot.IRC.DisconnectEvent += DisconnectHandler;
Combot.IRC.TCPErrorEvent += TCPErrorHandler;
CombotSessions.Add(Combot);
}

ToggleConnection = new DelegateCommand(ExecuteToggleConnection, CanToggleConnection);
JoinChannel = new DelegateCommand(ExecuteJoinChannel, CanJoinChannel);
}

private void TCPErrorHandler(Combot.IRCServices.TCP.TCPError error)
@@ -92,6 +101,11 @@ namespace Interface.ViewModels
CurrentBuffer += message.Message + Environment.NewLine;
}

private void ConnectHandler()
{
Connected = true;
}

private void DisconnectHandler()
{
Connected = false;
@@ -116,22 +130,12 @@ namespace Interface.ViewModels

private void Connect()
{
Connected = Combot.Connect();
CombotSessions.ForEach(Combot => Combot.Connect());
}

private void Disconnect()
{
Connected = Combot.Disconnect();
CombotSessions.ForEach(Combot => Combot.Disconnect());
}

private void ExecuteJoinChannel()
{
if (_Connected)
{
Combot.IRC.IRCSendJoin("#testing");
}
}

private bool CanJoinChannel() { return true; }
}
}

Loading…
Cancel
Save