Skip to content

Commit

Permalink
Shell for connecting to IRC Chat bot
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyLo1 committed Feb 28, 2019
1 parent f5d4255 commit 0f03d9f
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 0 deletions.
53 changes: 53 additions & 0 deletions ALANNShell2/ALANNShell2.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" 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>{A8BEB5F8-9E33-4DBC-B203-5F149FD77815}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>ALANNShell2</RootNamespace>
<AssemblyName>ALANNShell2</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</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.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
6 changes: 6 additions & 0 deletions ALANNShell2/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
115 changes: 115 additions & 0 deletions ALANNShell2/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace ALANNShell
{
public delegate void UpdateText(string msg);

public class Shell
{
static readonly string serverAddr = "127.0.0.1";
static readonly int serverPort = 5000;
static readonly string clientAddr = "127.0.0.1";
static readonly int clientPort = 5003;

static IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(serverAddr), serverPort);
static IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, clientPort);
static UdpClient outSocket = new UdpClient();
static UdpClient inSocket = new UdpClient(clientEndPoint);

static readonly string logPath = @"d:\logs\ALANNShell.log";

public Shell()
{
ThreadStart ts = new ThreadStart(Listening);
Thread th = new Thread(ts);
th.Start();

outSocket.Connect(serverEndPoint);
}

private async void Listening()
{
UpdateText updateMsg = UpdateMsg;

while (true)
{
var result = await inSocket.ReceiveAsync();
var data = result.Buffer;
string msg = Encoding.ASCII.GetString(data, 0, data.Length);
updateMsg(msg);
}
}

private void UpdateMsg(string msg)
{
var msgType = msg.Substring(0, 1);

switch (msgType)
{
case "?":
Console.WriteLine(msg);
LogMsg(msg, logPath);
break;

default:
;
break;
}
}

public void SendMsg(string str)
{
var lines = str.Split('\n');

foreach (var line in lines)
{
var trimmedLine = line.Trim();
if (trimmedLine == "") return;

var data = Encoding.ASCII.GetBytes(trimmedLine);
outSocket.SendAsync(data, data.Length);
LogMsg(trimmedLine, logPath);
}
}

private void LogMsg(string msg, string path)
{
if (!File.Exists(path))
{
File.Create(path);
TextWriter tw = new StreamWriter(path);
tw.WriteLine(msg);
tw.Close();
}
else
{
using (var tw = new StreamWriter(path, true))
{
tw.WriteLine(msg);
}
}
}
}

class Program
{
static void Main(string[] args)
{
Shell shell = new Shell();

Console.WriteLine("ALANN Shell running");

while (true)
{
var msg = Console.ReadLine();
shell.SendMsg(msg);
}
}
}
}
36 changes: 36 additions & 0 deletions ALANNShell2/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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("ALANNShell2")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ALANNShell2")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[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("a8beb5f8-9e33-4dbc-b203-5f149fd77815")]

// 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:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

0 comments on commit 0f03d9f

Please sign in to comment.