Skip to content

Commit

Permalink
using dynamic
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.origo.ethz.ch/ircddotnet/trunk@14 3fea7bcf-81c2-4a08-a3b7-d3cc9173df77
  • Loading branch information
FreeApophis committed May 21, 2010
1 parent a2a7541 commit 123286a
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 195 deletions.
50 changes: 50 additions & 0 deletions IrcD.Net/Commands/CommandBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* The ircd.net project is an IRC deamon implementation for the .NET Plattform
* It should run on both .NET and Mono
*
* Copyright (c) 2009-2010 Thomas Bruderer <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using System.Collections.Generic;
using System.Text;

namespace IrcD.Commands
{
public abstract class CommandBase
{
protected CommandBase(IrcDaemon ircDaemon, string name)
{
IrcDaemon = ircDaemon;
this.name = name;
}

protected IrcDaemon IrcDaemon;
protected StringBuilder Command;

private readonly string name;

public string Name
{
get
{
return name;
}
}

public abstract void Handle(UserInfo info, List<string> args);
public abstract void Send(InfoBase receiver, object[] args);
}
}
98 changes: 98 additions & 0 deletions IrcD.Net/Commands/CommandList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* The ircd.net project is an IRC deamon implementation for the .NET Plattform
* It should run on both .NET and Mono
*
* Copyright (c) 2009-2010 Thomas Bruderer <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using IrcD.Utils;

namespace IrcD.Commands
{

public class CommandList : DynamicObject
{
private readonly Dictionary<string, CommandBase> commandList = new Dictionary<string, CommandBase>();

public void Add(CommandBase command)
{
commandList.Add(command.Name, command);
}

public void Handle(string command, UserInfo info, List<string> args)
{
CommandBase commandObject;

if (commandList.TryGetValue(command, out commandObject))
{
commandObject.Handle(info, args);
}
else
{
#if DEBUG
Logger.Log("Command " + command + "is not yet implemented");
#endif

if (info.Registered)
{
// we only inform the client about invalid commands if he is already successfully registered
// we dont want to make "wrong protocol ping-pong"
//TODO:
//SendUnknownCommand(info, command);
}

}

#if DEBUG
var parsedLine = new StringBuilder();
parsedLine.Append("[" + info.Usermask + "]-[" + command + "]");

foreach (string arg in args)
{
parsedLine.Append("-<" + arg + ">");
}

Logger.Log(parsedLine.ToString());

#endif
}

public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
try
{
CommandBase command;

if (commandList.TryGetValue(binder.Name, out command))
{
command.Send((InfoBase)args[0], args.Skip(1).ToArray());
}
result = null;
return true;
}
catch (Exception)
{
result = null;
return false;
}
}
}
}
51 changes: 51 additions & 0 deletions IrcD.Net/Commands/Ping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* The ircd.net project is an IRC deamon implementation for the .NET Plattform
* It should run on both .NET and Mono
*
* Copyright (c) 2009-2010 Thomas Bruderer <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using System.Collections.Generic;
namespace IrcD.Commands
{
public class Ping : CommandBase
{
public Ping(IrcDaemon ircDaemon)
: base(ircDaemon, "PING")
{ }

public override void Handle(UserInfo info, List<string> args)
{
if (!info.Registered)
{
// SendNotRegistered(info);
return;
}

IrcDaemon.Commands.Pong();
}

public override void Send(InfoBase receiver, object[] args)
{
Command.Length = 0;
Command.Append(IrcDaemon.ServerPrefix);
Command.Append(" PING ");

receiver.WriteLine(Command);
}

}
}
1 change: 1 addition & 0 deletions IrcD.Net/Database/DatabaseCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using System.Data.SQLite;

namespace IrcD.Database
Expand Down
10 changes: 6 additions & 4 deletions IrcD.Net/IrcD.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Reference Include="DbLinq.Sqlite">
<HintPath>lib\DbLinq.Sqlite.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
Expand All @@ -76,9 +77,12 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ChannelInfo.cs" />
<Compile Include="Commands\CommandBase.cs" />
<Compile Include="Commands\CommandList.cs" />
<Compile Include="Commands\Ping.cs" />
<Compile Include="Database\DatabaseCommon.cs" />
<Compile Include="Database\DatabaseModel.cs" />
<Compile Include="Enums\IrcMode.cs" />
<Compile Include="IrcMode.cs" />
<Compile Include="InfoBase.cs" />
<Compile Include="IrcDaemon.cs" />
<Compile Include="IrcServer.cs" />
Expand Down Expand Up @@ -114,9 +118,7 @@
<Compile Include="Utils\Logger.cs" />
<Compile Include="Utils\WildCard.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Commands\" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
Expand Down
Loading

0 comments on commit 123286a

Please sign in to comment.