All pastes #204548 Raw Edit

staulkor

public text v1 · immutable
#204548 ·published 2006-10-16 01:44 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Meebey.SmartIrc4net;

namespace IRCBotGUI
{
    public partial class sIRCBotGUI : Form
    {
        delegate void SingleStringMethodInvoker(string s);
        private IrcClient irc;
        private Thread _connect;
        private static Options _options = new Options();

        public sIRCBotGUI()
        {
            InitializeComponent();
            UpdateButtons(false);
        }

        void Connect()
        {
            try
            {
                //Some options
                irc = new IrcClient();
                irc.AutoRejoin = true;
                irc.SendDelay = 500;

                //Event Handlers
                irc.OnConnecting += new EventHandler(irc_OnConnecting);
                irc.OnConnected += new EventHandler(irc_OnConnected);
                irc.OnConnectionError += new EventHandler(irc_OnConnectionError);
                irc.OnDisconnected += new EventHandler(irc_OnDisconnected);
                irc.OnChannelMessage += new IrcEventHandler(irc_OnChannelMessage);
                irc.OnRawMessage += new IrcEventHandler(irc_OnRawMessage);
                irc.OnError += new ErrorEventHandler(irc_OnError);
                irc.OnErrorMessage += new IrcEventHandler(irc_OnErrorMessage);

                //Connect to server
                irc.Connect(Options._server, Options._port);
                irc.Login(Options._botnick, Options._botname);
                irc.RfcJoin(Options._channel);
                irc.Listen();
            }
            catch (Exception ex)
            {
                OutputLine(" * Fatal Error: " + ex.Message);
            }
        }

        void irc_OnRawMessage(object sender, IrcEventArgs e)
        {
            if (e.Data.Channel == null && e.Data.Nick != null)
            {
            }
        }

        void OutputLine(string line)
        {
            try
            {
                if (richTextBox1.InvokeRequired)
                {
                    richTextBox1.Invoke(new SingleStringMethodInvoker(richTextBox1.AppendText), line);
                    richTextBox1.Invoke(new MethodInvoker(richTextBox1.ScrollToCaret));
                    Application.DoEvents();
                }
                else
                {
                    richTextBox1.AppendText(line);
                    richTextBox1.ScrollToCaret();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(" * Error: " + ex.Message);
            }
        }

        void UpdateButtons(bool soonToBeConnected)
        {
            if (soonToBeConnected)
            {
                toolStripButtonConnect.Enabled = false;
                toolStripButtonDisconnect.Enabled = true;
                connectToolStripMenuItem.Enabled = false;
                disconnectToolStripMenuItem.Enabled = true;
                toolStripConnect.Enabled = false;
                toolStripDisconnect.Enabled = true;
            }
            else
            {
                toolStripButtonConnect.Enabled = true;
                toolStripButtonDisconnect.Enabled = false;
                connectToolStripMenuItem.Enabled = true;
                disconnectToolStripMenuItem.Enabled = false;
                toolStripConnect.Enabled = true;
                toolStripDisconnect.Enabled = false;
            }
        }

        void OutputMessage(string message)
        {
            OutputLine(message);
            OutputLine(Environment.NewLine);
        }

        void irc_OnChannelMessage(object sender, IrcEventArgs e)
        {
            OutputLine("<");
            OutputLine(e.Data.Nick);
            OutputLine("> ");
            OutputLine(e.Data.Message);
            OutputLine(Environment.NewLine);
        }

        void irc_OnDisconnected(object sender, EventArgs e)
        {
            OutputMessage(" * Disconnected at " + DateTime.Now.ToShortTimeString());
        }

        void irc_OnConnectionError(object sender, EventArgs e)
        {
            OutputMessage(" * Connection Error at " + DateTime.Now.ToShortTimeString());
        }

        void irc_OnErrorMessage(object sender, IrcEventArgs e)
        {
            OutputLine(" * Error: ");
            OutputLine(e.Data.Message);
            OutputLine(" at " + DateTime.Now.ToShortTimeString());
            OutputLine(Environment.NewLine);
        }

        void irc_OnError(object sender, ErrorEventArgs e)
        {
            OutputLine(" * Error: ");
            OutputLine(e.Data.Message);
            OutputLine(" at " + DateTime.Now.ToShortTimeString());
            OutputLine(Environment.NewLine);
        }

        void irc_OnConnected(object sender, EventArgs e)
        {
            OutputMessage(" * Connected at " + DateTime.Now.ToShortTimeString());
        }

        void irc_OnConnecting(object sender, EventArgs e)
        {
            OutputMessage(" * Connecting...");
        }

        void OnPing (object sender, ReadLineEventArgs e)
        {
            if (e.Line.StartsWith("PING"))
            {
                OutputLine(" * PONG");
            }
        }

        void OnDisconnected(object sender, EventArgs e)
        {
            irc.AutoRetry = true;
            irc.AutoRetryDelay = 5;
            irc.AutoReconnect = true;
            irc.AutoRejoin = true;

            try
            {
                irc.Connect(Options._server, Options._port);
            }
            catch (Exception ex)
            {
                OutputLine(" * Failed to reconnect: " + ex.Message + "\r\n");
            }
        }

        void toolStripButtonConnect_Click(object sender, EventArgs e)
        {
            _connect = new Thread(new ThreadStart(Connect));
            _connect.IsBackground = true;
            _connect.Start();
            UpdateButtons(true);
        }

        void toolStripButtonDisconnect_Click(object sender, EventArgs e)
        {
            UpdateButtons(false);
            if (irc.IsConnected)
                irc.Disconnect();
        }

        void connectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButtonConnect_Click(sender, e);
        }

        void disconnectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButtonDisconnect_Click(sender, e);
        }

        void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _options.Show();
        }

        void toolStripConnect_Click(object sender, EventArgs e)
        {
            toolStripButtonConnect_Click(sender, e);
        }

        void toolStripDisconnect_Click(object sender, EventArgs e)
        {
            toolStripButtonDisconnect_Click(sender, e);
        }

        void toolStripExit_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        void sIRCBotGUI_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
                Hide();
        }

        void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }
    }
}