Browse Source

Added initial Relay module.

Added .IsChannel static to Channel class.
tags/3.1.0
Teknikode 3 years ago
parent
commit
3be96ac393

+ 8
- 1
Combot.sln View File

@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.22823.1
VisualStudioVersion = 14.0.23107.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Interface", "Interface\Interface.csproj", "{AE4AFD7A-CAA4-46A0-9E00-2B3D53F8D0AB}"
EndProject
@@ -77,6 +77,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Whois", "Modules\Whois\Whoi
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Custom Commands", "Modules\Custom Commands\Custom Commands.csproj", "{B84A8D1B-C10A-4879-912A-83725A1638D1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Relay", "Modules\Relay\Relay.csproj", "{4F01E8C8-C13E-4584-8925-7A6DA55E02DB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -207,6 +209,10 @@ Global
{B84A8D1B-C10A-4879-912A-83725A1638D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B84A8D1B-C10A-4879-912A-83725A1638D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B84A8D1B-C10A-4879-912A-83725A1638D1}.Release|Any CPU.Build.0 = Release|Any CPU
{4F01E8C8-C13E-4584-8925-7A6DA55E02DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F01E8C8-C13E-4584-8925-7A6DA55E02DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F01E8C8-C13E-4584-8925-7A6DA55E02DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F01E8C8-C13E-4584-8925-7A6DA55E02DB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -239,5 +245,6 @@ Global
{84166880-FC04-4F50-90CD-8486B797948A} = {D157677E-4D33-4156-B55E-E21C4B5A3024}
{A6EDD673-A10D-48BC-81D9-506D08AEC083} = {D157677E-4D33-4156-B55E-E21C4B5A3024}
{B84A8D1B-C10A-4879-912A-83725A1638D1} = {D157677E-4D33-4156-B55E-E21C4B5A3024}
{4F01E8C8-C13E-4584-8925-7A6DA55E02DB} = {D157677E-4D33-4156-B55E-E21C4B5A3024}
EndGlobalSection
EndGlobal

+ 1
- 1
Combot/Modules/Module.cs View File

@@ -182,7 +182,7 @@ namespace Combot.Modules

public object GetOptionValue(string name)
{
object foundValue = null;
object foundValue = string.Empty;
Option foundOption = Options.Find(opt => opt.Name == name);
if (foundOption != null)
{

+ 9
- 0
IRCServices/Channel.cs View File

@@ -130,5 +130,14 @@ namespace Combot.IRCServices
{
Bans.Remove(mask);
}

public static bool IsChannel(string channel)
{
if (channel.StartsWith("&") || channel.StartsWith("#"))
{
return true;
}
return false;
}
}
}

+ 3
- 3
IRCServices/Messaging/Messages.cs View File

@@ -140,7 +140,7 @@ namespace Combot.IRCServices.Messaging
}
else
{
if (recipient.StartsWith("&") || recipient.StartsWith("#"))
if (Channel.IsChannel(recipient))
{
ChannelMessage msg = new ChannelMessage();
msg.Channel = recipient;
@@ -204,7 +204,7 @@ namespace Combot.IRCServices.Messaging
}
});
}
if (recipient.StartsWith("&") || recipient.StartsWith("#"))
if (Channel.IsChannel(recipient))
{
ChannelNotice msg = new ChannelNotice();
msg.Channel = recipient;
@@ -236,7 +236,7 @@ namespace Combot.IRCServices.Messaging
break;
// The message was a mode change message for a channel or nick
case "MODE":
if (recipient.StartsWith("&") || recipient.StartsWith("#"))
if (Channel.IsChannel(recipient))
{
ChannelModeChangeInfo modeMsg = new ChannelModeChangeInfo();
modeMsg.Modes = new List<ChannelModeInfo>();

+ 179
- 0
Modules/Relay/Module.Default.json View File

@@ -0,0 +1,179 @@
{
"Name": "Relay",
"ClassName": "Relay",
"Enabled": true,
"ChannelBlacklist": [ ],
"NickBlacklist": [ ],
"Commands": [
{
"Name": "Relay",
"Description": "Relay messages from a channel or nick to another channel or nick.",
"Enabled": true,
"ChannelBlacklist": [ ],
"NickBlacklist": [ ],
"Triggers": [
"relay"
],
"Arguments": [
{
"Name": "Method",
"Description": "What you want to do with the relay.",
"AllowedValues": [
"Add",
"Edit",
"Del",
"Delete",
"View"
],
"MessageTypes": [
0,
1,
2
],
"Required": true
},
{
"Name": "ID",
"Description": "The ID of the relay you want to edit or delete.",
"AllowedValues": [ ],
"DependentArguments": [
{
"Name": "Method",
"Values": [
"Edit",
"Del",
"Delete"
]
}
],
"MessageTypes": [
0,
1,
2
],
"Required": true
},
{
"Name": "ID",
"Description": "The ID of the introduction you want to view.",
"AllowedValues": [ ],
"DependentArguments": [
{
"Name": "Method",
"Values": [
"View"
]
}
],
"MessageTypes": [
0,
1,
2
],
"Required": false
},
{
"Name": "Source",
"Description": "The channel or nick that is the source of the relay.",
"AllowedValues": [ ],
"DependentArguments": [
{
"Name": "Method",
"Values": [
"Add",
"Edit"
]
}
],
"MessageTypes": [
0,
1,
2
],
"Required": true
},
{
"Name": "Target",
"Description": "The channel or nick you want the relay to message.",
"AllowedValues": [ ],
"DependentArguments": [
{
"Name": "Method",
"Values": [
"Add",
"Edit"
]
}
],
"MessageTypes": [
0,
1,
2
],
"Required": true
},
{
"Name": "Type",
"Description": "The type of action to trigger the relay.",
"AllowedValues": [
"Message",
"Notice",
"Join",
"Part",
"Quit",
"Kick",
"Mode"
],
"DependentArguments": [
{
"Name": "Method",
"Values": [
"Add",
"Edit"
]
}
],
"MessageTypes": [
0,
1,
2
],
"Required": true
},
{
"Name": "Modes",
"Description": "The modes to hook to.",
"AllowedValues": [ ],
"DependentArguments": [
{
"Name": "Type",
"Values": [
"Mode"
]
}
],
"MessageTypes": [
0,
1,
2
],
"Required": true
}
],
"AllowedMessageTypes": [
0,
1,
2
],
"AllowedAccess": [
3,
4,
5,
6
],
"ShowHelp": true,
"SpamCheck": true
}
],
"Options": []
}

+ 33
- 0
Modules/Relay/Properties/AssemblyInfo.cs View File

@@ -0,0 +1,33 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Relay Module")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Teknik")]
[assembly: AssemblyProduct("Combot")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("5c350b8e-5820-4034-9d67-49d2ddbfcc6f")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:

+ 256
- 0
Modules/Relay/Relay.cs View File

@@ -0,0 +1,256 @@
using Combot.IRCServices;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Combot.Modules.Plugins
{
public class Relay : Module
{
public override void Initialize()
{
Bot.CommandReceivedEvent += HandleCommandEvent;
}

public override void ParseCommand(CommandMessage command)
{
Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
switch (foundCommand.Name)
{
case "Relay":
string method = command.Arguments["Method"];
switch (method.ToLower())
{
case "add":
AddRelay(command);
break;
case "edit":
EditRelay(command);
break;
case "delete":
case "del":
DeleteRelay(command);
break;
case "view":
ViewRelay(command);
break;
}
break;
}
}

private void AddRelay(CommandMessage command)
{
string source = command.Arguments.ContainsKey("Source") ? command.Arguments["Source"] : command.Location;
string target = command.Arguments.ContainsKey("Target") ? command.Arguments["Target"] : command.Nick.Nickname;
string type = command.Arguments.ContainsKey("Type") ? command.Arguments["Type"] : "Message";
string modes = command.Arguments.ContainsKey("Modes") ? command.Arguments["Modes"] : string.Empty;

// verify access in source and target
if (!CheckAccess(source, command.Nick.Nickname, command.Access))
{
string invalid = string.Format("You do not have permission to use '{0}' as a source.", source);
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
return;
}
if (!CheckAccess(target, command.Nick.Nickname, command.Access))
{
string invalid = string.Format("You do not have permission to use '{0}' as a target.", source);
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
return;
}

RelayType relayType = RelayType.Message;
Enum.TryParse(type, out relayType);

AddNick(command.Nick);
string query = "INSERT INTO `relays` SET " +
"`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
"`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {1} && `nickname` = {2}), " +
"`source` = {3}, " +
"`target` = {4}, " +
"`type` = {5}, " +
"`modes` = {6}, " +
"`date_added` = {7}";
Bot.Database.Execute(query, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, command.Nick.Nickname, source, target, (int)relayType, modes, command.TimeStamp });
List<Dictionary<string, object>> results = GetRelayList(command.Nick.Nickname);
string relayMessage = string.Format("Added relay from \u0002{0}\u0002 to \u0002{1}\u0002. You now have \u0002{2}\u0002 relays created.", source, target, results.Count);
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, relayMessage);
}

private void EditRelay(CommandMessage command)
{
string source = command.Arguments.ContainsKey("Source") ? command.Arguments["Source"] : command.Location;
string target = command.Arguments.ContainsKey("Target") ? command.Arguments["Target"] : command.Nick.Nickname;
string type = command.Arguments.ContainsKey("Type") ? command.Arguments["Type"] : "Message";
string modes = command.Arguments.ContainsKey("Modes") ? command.Arguments["Modes"] : string.Empty;

// verify access in source and target
if (!CheckAccess(source, command.Nick.Nickname, command.Access))
{
string invalid = string.Format("You do not have permission to use '{0}' as a source.", source);
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
return;
}
if (!CheckAccess(target, command.Nick.Nickname, command.Access))
{
string invalid = string.Format("You do not have permission to use '{0}' as a target.", source);
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
return;
}

RelayType relayType = RelayType.Message;
Enum.TryParse(type, out relayType);

int num = HasValidID(command);

if (num > 0)
{
List<Dictionary<string, object>> results = GetRelayList(command.Nick.Nickname);
int id = Convert.ToInt32(results[num - 1]["id"]);
string query = "UPDATE `relays` SET " +
"`source` = {0}, " +
"`target` = {1}, " +
"`type` = {2}, " +
"`modes` = {3} " +
"WHERE `id` = {4}";
Bot.Database.Execute(query, new object[] { source, target, (int)relayType, modes, id });
string relayMessage = string.Format("Updated relay \u0002{0}\u0002 to be from \u0002{1}\u0002 to \u0002{2}\u0002.", num, source, target);
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, relayMessage);
}
else
{
string invalid = "Invalid relay ID.";
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
}
}

private void DeleteRelay(CommandMessage command)
{
int num = HasValidID(command);

if (num > 0)
{
List<Dictionary<string, object>> results = GetRelayList(command.Nick.Nickname);
int id = Convert.ToInt32(results[num - 1]["id"]);
string query = "DELETE FROM `relays` " +
"WHERE `id` = {0}";
Bot.Database.Execute(query, new object[] { id });
string relayMessage = string.Format("Relay #\u0002{0}\u0002 has been deleted.", num);
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, relayMessage);
}
else
{
string invalid = "Invalid relay ID.";
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
}
}

private void ViewRelay(CommandMessage command)
{
List<Dictionary<string, object>> results = GetRelayList(command.Nick.Nickname);

if (command.Arguments.ContainsKey("ID"))
{
int num = HasValidID(command);
if (num > 0)
{
int relayInt = 0;
Int32.TryParse(results[num - 1]["type"].ToString(), out relayInt);

string relayMessage = string.Format("Relay #\u0002{0}\u0002 - Source: \u0002{1}\u0002 | Target: \u0002{2}\u0002 | Type: \u0002{3}\u0002",
num, results[num - 1]["source"], results[num - 1]["target"], (RelayType)relayInt);
if ((RelayType)relayInt == RelayType.Mode)
{
relayMessage = string.Format("{0} | Modes: \u0002{1}\u0002", relayMessage, results[num - 1]["modes"]);
}
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, relayMessage);
}
else
{
string invalid = "Invalid relay ID.";
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
}
}
else
{
if (results.Any())
{
for (int i = 0; i < results.Count; i++)
{
int relayInt = 0;
Int32.TryParse(results[i]["type"].ToString(), out relayInt);

string relayMessage = string.Format("Relay #\u0002{0}\u0002 - Source: \u0002{1}\u0002 | Target: \u0002{2}\u0002 | Type: \u0002{3}\u0002",
i + 1, results[i]["source"], results[i]["target"], (RelayType)relayInt);
if ((RelayType)relayInt == RelayType.Mode)
{
relayMessage = string.Format("{0} | Modes: \u0002{1}\u0002", relayMessage, results[i]["modes"]);
}
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, relayMessage, true);
}
}
else
{
string invalid = "You do not have any relays set.";
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
}
}
}

private List<Dictionary<string, object>> GetRelayList(string nickname)
{
// Check to see if they have reached the max number of introductions
string search = "SELECT `relays`.`id`, `relays`.`source`, `relays`.`target`, `relays`.`type`, `relays`.`modes` FROM `relays` " +
"INNER JOIN `nicks` " +
"ON `relays`.`nick_id` = `nicks`.`id` " +
"INNER JOIN `servers` " +
"ON `nicks`.`server_id` = `servers`.`id` " +
"WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1}";
return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, nickname });
}

private bool CheckAccess(string source, string nick, List<AccessType> types)
{
// Owners get to have all the fun
if (Bot.ServerConfig.Owners.Contains(nick))
return true;

// The source is a channel
if (Bot.IRC.Channels.Exists(chan => chan.Name == source))
{
foreach (AccessType access in types)
{
bool valid = Bot.CheckChannelAccess(source, nick, AccessType.Operator);
if (!valid)
return false;
}
}
// The source is a nickname
else
{
if (source != nick)
return false;
}
return true;
}

/* Returns the parsed ID field if valid, otherwise returns 0 */
private int HasValidID(CommandMessage command)
{
int num = 0;
int ret = 0;
List<Dictionary<string, object>> results = GetRelayList(command.Nick.Nickname);

if (int.TryParse(command.Arguments["ID"], out num))
{
if (results.Count >= num && num > 0)
{
ret = num;
}
}

return ret;
}
}
}

+ 83
- 0
Modules/Relay/Relay.csproj View File

@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{4F01E8C8-C13E-4584-8925-7A6DA55E02DB}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Relay</RootNamespace>
<AssemblyName>Relay</AssemblyName>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\..\Bin\Debug\Modules\Relay\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\Bin\Release\Modules\Relay\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Relay.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RelayType.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Combot\Combot.csproj">
<Project>{23e4c371-16e4-4fac-8b11-44288399bb55}</Project>
<Name>Combot</Name>
</ProjectReference>
<ProjectReference Include="..\..\IRCServices\IRCServices.csproj">
<Project>{65fcbf1c-8c9e-4688-becc-185d9030899f}</Project>
<Name>IRCServices</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="Module.Default.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>md "$(SolutionDir)$(ConfigurationName)\Modules\$(TargetName)"
copy /Y "$(TargetPath)" "$(SolutionDir)$(ConfigurationName)\Modules\$(TargetName)"
copy /Y "$(TargetDir)Module.Default.json" "$(SolutionDir)$(ConfigurationName)\Modules\$(TargetName)"</PostBuildEvent>
</PropertyGroup>
<Import Project="..\..\packages\GitVersionTask.2.0.1\Build\GitVersionTask.targets" Condition="Exists('..\..\packages\GitVersionTask.2.0.1\Build\GitVersionTask.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\GitVersionTask.2.0.1\Build\GitVersionTask.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\GitVersionTask.2.0.1\Build\GitVersionTask.targets'))" />
</Target>
<!-- 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.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

+ 19
- 0
Modules/Relay/RelayType.cs View File

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

namespace Combot.Modules.Plugins
{
public enum RelayType
{
Message,
Notice,
Join,
Part,
Quit,
Kick,
Mode
}
}

+ 4
- 0
Modules/Relay/packages.config View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="GitVersionTask" version="2.0.1" targetFramework="net451" developmentDependency="true" />
</packages>

Loading…
Cancel
Save