using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
/*#####################################################################*/
class IrcBot
{
public static string SERVER = "irc.e-kolay.net";
private static int PORT = 6667;
private static string USER = "USER cSharp 8 * : bOt";
private static string NICK = "cXc";
private static string ALTNICK = "cYc";
private static string PING = "PING :";
private static string CHANNEL = "#tr-kanki";
public static StreamWriter writer;
static void Main (string[] args)
{
NetworkStream stream;
TcpClient irc;
string inputLine;
StreamReader reader;
string nickname;
try
{
irc = new TcpClient (SERVER, PORT);
stream = irc.GetStream ();
reader = new StreamReader (stream);
writer = new StreamWriter (stream);
PingSender ping = new PingSender ();
ping.Start ();
writer.WriteLine (USER);
writer.Flush ();
writer.WriteLine ("NICK " + NICK);
writer.Flush ();
writer.WriteLine ("JOIN " + CHANNEL);
writer.Flush ();
while (true)
{
while ( (inputLine = reader.ReadLine () ) != null )
{
string str = inputLine;
char[] seps = {' '};
string[] parts = str.Split(seps);
string Number = parts[1];
if (inputLine.EndsWith("Bu nick Zaten Kullanılıyor"))
{
NICK = ALTNICK;
writer.WriteLine("NICK " + NICK);
}
if (Number == "422")
{
writer.WriteLine("JOIN :" + CHANNEL);
writer.Flush();
}
if (Number == "MODE")
{
if (inputLine.EndsWith(parts[2] + " +o " + NICK))
{
nickname = inputLine.Substring(1, inputLine.IndexOf ("!") - 1);
writer.WriteLine("PRIVMSG " + parts[2] + " :" + nickname + " Op icin tesekkurler ?");
}
}
if (Number == "KICK")
{
if (parts[3] == NICK)
{
nickname = inputLine.Substring(1, inputLine.IndexOf ("!") - 1);
writer.WriteLine ("JOIN " + parts[2]);
}
}
if (inputLine.EndsWith ("JOIN :" + CHANNEL) )
{
nickname = inputLine.Substring(1, inputLine.IndexOf ("!") - 1);
writer.WriteLine ("NOTICE " + nickname + " :seLam " + nickname + " Kanalımıza Hoşgeldin ");
Thread.Sleep (100);
}
if (parts[0] == "PING")
{
writer.WriteLine (PING + IrcBot.SERVER);
}
}
writer.Close ();
reader.Close ();
irc.Close ();
}
}
catch (Exception e)
{
Console.WriteLine (e.ToString () );
Thread.Sleep (500);
string[] argv = { };
Main (argv);
}
}
}
class PingSender
{
static string PING = "PING :";
private Thread pingSender;
public PingSender ()
{
pingSender = new Thread (new ThreadStart (this.Run) );
}
public void Start ()
{
pingSender.Start ();
}
public void Run ()
{
while (true)
{
IrcBot.writer.WriteLine (PING + IrcBot.SERVER);
IrcBot.writer.Flush ();
Thread.Sleep (5000);
}
}
}