Browse Source

Add check to see if config file exists, and if not, copy the default file.

Add logging of nickname information.
tags/3.0.5
Teknikode 4 years ago
parent
commit
b2cf52a218
2 changed files with 107 additions and 16 deletions
  1. 29
    8
      Combot/Configurations/Config.cs
  2. 78
    8
      Combot/Modules/Module.cs

+ 29
- 8
Combot/Configurations/Config.cs View File

@@ -50,7 +50,7 @@ namespace Combot.Configurations
if (!Servers.Exists(server => server.Name == config.Name))
{
config.ModifyEvent += SaveServers;
config.LoadEvent += LoadServers;
config.LoadEvent += UpdateServers;
Servers.Add(config);
}
ConfigRWLock.ExitWriteLock();
@@ -66,7 +66,7 @@ namespace Combot.Configurations
ConfigRWLock.ExitReadLock();

// Save config to file
string ConfigPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.config");
string ConfigPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.json");
using (StreamWriter streamWriter = new StreamWriter(ConfigPath, false))
{
streamWriter.Write(configContents);
@@ -78,7 +78,17 @@ namespace Combot.Configurations
public void LoadServers()
{
ConfigFileRWLock.EnterReadLock();
string ConfigPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.config");
string ConfigPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.json");

if (!File.Exists(ConfigPath))
{
string defaultPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.Default.json");
if (File.Exists(defaultPath))
{
File.Copy(defaultPath, ConfigPath);
}
}

if (File.Exists(ConfigPath))
{
string configContents;
@@ -94,6 +104,7 @@ namespace Combot.Configurations
for (int i = 0; i < Servers.Count; i++)
{
Servers[i].ModifyEvent += SaveServers;
Servers[i].LoadEvent += UpdateServers;
}
ConfigRWLock.ExitWriteLock();
}
@@ -103,7 +114,17 @@ namespace Combot.Configurations
public void UpdateServers()
{
ConfigFileRWLock.EnterReadLock();
string ConfigPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.config");
string ConfigPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.json");

if (!File.Exists(ConfigPath))
{
string defaultPath = Path.Combine(Directory.GetCurrentDirectory(), @"Combot.Servers.Default.json");
if (File.Exists(defaultPath))
{
File.Copy(defaultPath, ConfigPath);
}
}

if (File.Exists(ConfigPath))
{
string configContents;
@@ -116,15 +137,15 @@ namespace Combot.Configurations
ConfigRWLock.EnterWriteLock();
List<ServerConfig> newConfigs = JsonConvert.DeserializeObject<List<ServerConfig>>(configContents, JsonSettings);

for (int i = 0; i < newConfigs.Count; i++)
foreach (ServerConfig newConfig in newConfigs)
{
if (Servers.Count > i)
if (Servers.Exists(server => server.Name == newConfig.Name))
{
Servers[i].Copy(newConfigs[i]);
Servers.Find(server => server.Name == newConfig.Name).Copy(newConfig);
}
else
{
Servers.Add(newConfigs[i]);
Servers.Add(newConfig);
}
}
ConfigRWLock.ExitWriteLock();

+ 78
- 8
Combot/Modules/Module.cs View File

@@ -75,7 +75,7 @@ namespace Combot.Modules
{
nickAccessTypes.Add(Bot.PrivilegeModeMapping[privilege]);
}
if ((Bot.ServerConfig.Owners.Contains(command.Nick.Nickname) && command.Nick.Modes.Contains(UserMode.r)) || command.Nick.Nickname == Bot.IRC.Nickname)
if ((Bot.ServerConfig.Owners.Contains(command.Nick.Nickname) && command.Nick.Modes.Exists(mode => mode == UserMode.r || mode == UserMode.o)) || command.Nick.Nickname == Bot.IRC.Nickname)
{
nickAccessTypes.Add(AccessType.Owner);
}
@@ -201,7 +201,7 @@ namespace Combot.Modules
ConfigRWLock.ExitReadLock();

// Save config to file
string path = Path.Combine(ConfigPath, "Module.config");
string path = Path.Combine(ConfigPath, "Module.json");
using (StreamWriter streamWriter = new StreamWriter(path, false))
{
streamWriter.Write(configContents);
@@ -213,7 +213,17 @@ namespace Combot.Modules
public void LoadConfig()
{
ConfigFileRWLock.EnterReadLock();
string path = Path.Combine(ConfigPath, "Module.config");
string path = Path.Combine(ConfigPath, "Module.json");

if (!File.Exists(path))
{
string defaultPath = Path.Combine(ConfigPath, "Module.Default.json");
if (File.Exists(defaultPath))
{
File.Copy(defaultPath, path);
}
}

if (File.Exists(path))
{
string configContents;
@@ -261,19 +271,72 @@ namespace Combot.Modules
}
}

public void AddNick(string nickname)
public void AddNick(Nick nick)
{
string search = "SELECT * FROM `nicks` WHERE " +
"`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}) AND " +
"`nickname` = {1}";
List<Dictionary<string, object>> results = Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, nickname });
List<Dictionary<string, object>> results = Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, nick.Nickname });

if (!results.Any())
{
string insert = "INSERT INTO `nicks` SET " +
"`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
"`nickname` = {1}";
Bot.Database.Execute(insert, new object[] { Bot.ServerConfig.Name, nickname });
Bot.Database.Execute(insert, new object[] { Bot.ServerConfig.Name, nick.Nickname });
}
AddNickInfo(nick);
}

public void AddNickInfo(Nick nickInfo)
{
int argIndex = 2;
string search = "SELECT * FROM `nickinfo` WHERE " +
"`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `nicks`.`server_id` = `servers`.`id` WHERE `nicks`.`nickname` = {0} AND `servers`.`name` = {1})";
List<object> argList = new List<object>() { nickInfo.Nickname, Bot.ServerConfig.Name };
if (!string.IsNullOrEmpty(nickInfo.Username))
{
search += " AND `username` = {" + argIndex + "}";
argList.Add(nickInfo.Username);
argIndex++;
}
if (!string.IsNullOrEmpty(nickInfo.Realname))
{
search += " AND `realname` = {" + argIndex + "}";
argList.Add(nickInfo.Realname);
argIndex++;
}
if (!string.IsNullOrEmpty(nickInfo.Host))
{
search += " AND `host` = {" + argIndex + "}";
argList.Add(nickInfo.Host);
}
List<Dictionary<string, object>> results = Bot.Database.Query(search, argList.ToArray());

if (!results.Any())
{
argIndex = 2;
string insert = "INSERT INTO `nickinfo` SET " +
"`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `nicks`.`server_id` = `servers`.`id` WHERE `nicks`.`nickname` = {0} AND `servers`.`name` = {1})";
argList = new List<object>() { nickInfo.Nickname, Bot.ServerConfig.Name };
if (!string.IsNullOrEmpty(nickInfo.Username))
{
insert += ", `username` = {" + argIndex + "}";
argList.Add(nickInfo.Username);
argIndex++;
}
if (!string.IsNullOrEmpty(nickInfo.Realname))
{
insert += ", `realname` = {" + argIndex + "}";
argList.Add(nickInfo.Realname);
argIndex++;
}
if (!string.IsNullOrEmpty(nickInfo.Host))
{
insert += ", `host` = {" + argIndex + "}";
argList.Add(nickInfo.Host);
}
Bot.Database.Execute(insert, argList.ToArray());
}
}

@@ -290,12 +353,19 @@ namespace Combot.Modules
return nickname;
}

public void SendResponse(MessageType messageType, string location, string nickname, string message)
public void SendResponse(MessageType messageType, string location, string nickname, string message, bool silent = false)
{
switch (messageType)
{
case MessageType.Channel:
Bot.IRC.Command.SendPrivateMessage(location, message);
if (silent)
{
Bot.IRC.Command.SendNotice(nickname, message);
}
else
{
Bot.IRC.Command.SendPrivateMessage(location, message);
}
break;
case MessageType.Query:
Bot.IRC.Command.SendPrivateMessage(nickname, message);

Loading…
Cancel
Save