Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net.Sockets;
- using System.IO;
- struct IRCConfig
- {
- public string server;
- public int port;
- public string nick;
- public string name;
- }
- class IRCBot
- {
- TcpClient IRCConnection = null;
- IRCConfig config;
- NetworkStream ns = null;
- StreamReader sr = null;
- StreamWriter sw = null;
- public IRCBot(IRCConfig config)
- {
- this.config = config;
- try
- {
- IRCConnection = new TcpClient(config.server, config.port);
- }
- catch
- {
- Console.WriteLine("Connection Error");
- }
- try
- {
- ns = IRCConnection.GetStream();
- sr = new StreamReader(ns);
- sw = new StreamWriter(ns);
- }
- catch
- {
- Console.WriteLine("Communication error");
- }
- finally
- {
- if (sr != null)
- sr.Close();
- if (sw != null)
- sw.Close();
- if (ns != null)
- ns.Close();
- if (IRCConnection != null)
- IRCConnection.Close();
- }
- }
- public void sendData(string cmd, string param)
- {
- if (param == null)
- {
- sw.WriteLine(cmd);
- sw.Flush();
- Console.WriteLine(cmd);
- }
- else
- {
- sw.WriteLine(cmd + " " + param);
- sw.Flush();
- Console.WriteLine(cmd + " " + param);
- }
- }
- sendData("USER", config.nick + " japanbot " + " japan" + " :" + config.name);
- sendData("NICK", config.nick);
- public void IRCWork()
- {
- string[] ex;
- string data;
- bool shouldRun = true;
- while (shouldRun)
- {
- data = sr.ReadLine();
- Console.WriteLine(data);
- char[] charSeparator = new char[] { ' ' };
- ex = data.Split(charSeparator, 5);
- if (ex[0] == "PING")
- {
- sendData("PONG", ex[1]);
- }
- }
- }
- if (ex.Length > 4)
- { string command = ex[3];
- switch (command)
- {
- case ":!join":
- sendData("JOIN", ex[4]);
- break;
- case ":!say":
- sendData("PRIVMSG", ex[2] + " " + ex[4]);
- break;
- case ":!quit":
- sendData("QUIT", ex[4]);
- shouldRun = false;
- break;
- }
- }
- if (ex.Length > 3)
- {
- string command = ex[3];
- switch (command)
- {
- case ":!part":
- sendData("PART", ex[2]);
- break;
- }
- }
- IRCConfig conf = new IRCConfig();
- conf.name = "japanbot";
- conf.nick = "japanbot";
- conf.port = 6667;
- conf.server = "whatever";
- new IRCBot(conf);
- Console.WriteLine("jews did japanbot never forget");
- Console.ReadLine();
Add Comment
Please, Sign In to add comment