74 lines
2.6 KiB
C#
74 lines
2.6 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Csbot.Core
|
|
{
|
|
/// <summary>
|
|
/// This class contains the core of csbot, including (and mostly limited to) the IRC communication handling.
|
|
/// </summary>
|
|
class Core
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
Config config = JsonConvert.DeserializeObject<Config>(new StreamReader("config.json").ReadToEnd());
|
|
config.csbot = new User(config.nick, "Csbot, the original.", "");
|
|
TcpClient irc = new TcpClient(config.server, config.port);
|
|
NetworkStream ircStream = irc.GetStream();
|
|
StreamReader ircIn = new StreamReader(ircStream);
|
|
StreamWriter ircOut = new StreamWriter(ircStream);
|
|
ircOut.AutoFlush = true;
|
|
|
|
String line;
|
|
Message received;
|
|
|
|
while ((line = ircIn.ReadLine()) != null)
|
|
{
|
|
if (config.debug)
|
|
{
|
|
Console.WriteLine(line);
|
|
}
|
|
|
|
received = new Message(line, config.commandStart);
|
|
|
|
if ("PING".Equals(received.action))
|
|
{
|
|
Message pong = new Message();
|
|
pong.line = "PONG :" + received.text;
|
|
pong.Send(ircOut, config.debug);
|
|
}
|
|
|
|
if ("NOTICE".Equals(received.action) && (received.text.Contains("Found your hostname") || received.text.Contains("Your hostname does not match up")))
|
|
{
|
|
config.csbot.getUserLine().Send(ircOut, config.debug);
|
|
config.csbot.getNickLine().Send(ircOut, config.debug);
|
|
}
|
|
|
|
if ("422".Equals(received.action) || "376".Equals(received.action))
|
|
{
|
|
Message nickServAuth = new Message("PRIVMSG NickServ :identify " + config.password);
|
|
nickServAuth.Send(ircOut, config.debug);
|
|
|
|
foreach (String chan in config.channels)
|
|
{
|
|
Message join = new Message();
|
|
join.line = "JOIN " + chan;
|
|
join.Send(ircOut, config.debug);
|
|
}
|
|
}
|
|
|
|
if (received.isCommand)
|
|
{
|
|
Thread parser = new Thread(Parser.Run);
|
|
parser.Start(new ParserParameters(received, config, ircOut));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|