0

Okay so I am unfamiliar with IRC Protocol but I have read through a fair bit of it to get a better understanding but I am still unsure as to how I would set it so my bot would only follow my commands or read who the bots "admins" are from a text file. I want to implement this in order to stop everyone from making him quit and spam.

If you need anything more specific feel free to ask and I will update the post.

As requested:

    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);
        }
    }

    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) //is the command received long enough to be a bot command?
            {
                string command = ex[3]; //grab the command sent

                switch (command)
                {
                    case ":!join":
                        sendData("JOIN", ex[4]);
                        //if the command is !join send the "JOIN" command to the server with the parameters set by the user
                        break;
                    case ":!say":
                        sendData("PRIVMSG", ex[2] + " " + ex[4]);
                        //if the command is !say, send a message to the chan (ex[2]) followed by the actual message (ex[4]).
                        break;
                    case ":!quit":
                        sendData("QUIT", ex[4]);
                        //if the command is quit, send the QUIT command to the server with a quit message
                        shouldRun = false;
                        //turn shouldRun to false - the server will stop sending us data so trying to read it will not work and result in an error. This stops the loop from running and we will close off the connections properly
                        break;
                    case ":!part":
                        sendData("PART", ex[4]);
                        break;
                    case ":!query":
                        sendData("QUERY", ex[4] + ex[4]);
                        break;
                    case ":!useradd":
                        sendData("USERADD", ex[4]);
                        break;

                }
            }
        }
    }
svick
  • 236,525
  • 50
  • 385
  • 514
Hello World
  • 1,379
  • 4
  • 20
  • 41
  • Only follow your commands: you can see how to identify the sender of an IRC message, can't you? Read admins from a file: try [StreamReader](http://msdn.microsoft.com/en-us/library/f2ke0fzy.aspx) – Rup Jun 06 '12 at 12:09
  • I'd suggest you post the part of the code where you process received messages in order for everyone to have something to start with – Alex Jun 06 '12 at 12:11
  • I have just added this in for you. – Hello World Jun 06 '12 at 12:13
  • you should expand it to keep track of who (as in, which nickname) wrote what, then you can easily (as suggested above) compare it to the contents of a text file. I suspect you'll have to tweak the code which reads from `sr` – Alex Jun 06 '12 at 12:16
  • How exactly would I get it to track when a certain message is sent to it? – Hello World Jun 06 '12 at 12:23
  • Because at the minute it simply checks to see if "!join" etc... is sent, it doesn't check that the message is directly aimed at it. – Hello World Jun 06 '12 at 12:24
  • You need to look at other parts of `ex`. If you've got a `PRIVMSG` or a `NOTICE` then `ex[0]` should be the sender and `ex[2]` the target, either the bot name or the channel. – Rup Jun 06 '12 at 12:50
  • I don't quite understand what you mean by the ex[0] should be the sender. – Hello World Jun 06 '12 at 13:23
  • 3
    You are receiving an IRC command in `data` then splitting it into parts as an array `ex`. The protocol for a message is `:sender PRIVMSG target-user-or-channel :message`. Therefore the first part after the split will be `:sender`. – Rup Jun 06 '12 at 13:25
  • If you're having trouble with this stuff it might be easier to [use an IRC library to handle the protocol instead](http://stackoverflow.com/questions/1962075/irc-client-library-in-c-sharp) – Rup Jun 06 '12 at 13:26
  • Ah I see Rup, thanks I got a little lost, once I have finished working on my solution I will probably answer my own question just to help others out :) – Hello World Jun 06 '12 at 13:40

0 Answers0