Ejemplo n.º 1
0
 public IrcUserMessage(IrcMessage Message)
 {
     this.Target = Message.Payload.Remove(Message.Payload.IndexOf(' '));
     this.Message = Message.Payload.Substring(Message.Payload.IndexOf(':') + 1);
     string[] parts = Message.Prefix.Split('!', '@');
     this.Source = new IrcUser(parts[0], parts[1]);
     this.Source.Hostname = parts[2];
     if (this.Target.StartsWith("#"))
         ChannelMessage = true;
     else
         this.Target = this.Source.Nick;
 }
Ejemplo n.º 2
0
 public IrcNotice(IrcMessage message)
 {
     this.Source = message.Prefix;
     this.Message = message.Payload.Substring(message.Payload.IndexOf(':') + 1);
 }
Ejemplo n.º 3
0
        private void HandleMessage(IrcMessage message)
        {
            // scope in switch...case is stupid in C#
            string raw, channelName, reason;
            IrcChannel channel;
            switch (message.Command)
            {
                case "001":
                    if (ConnectionComplete != null)
                        ConnectionComplete(this, null);
                    break;
                case "PRIVMSG":
                    IrcCommand command = null;
                    IrcUserMessage userMessage = new IrcUserMessage(message);
                    if (UserMessageRecieved != null)
                        UserMessageRecieved(this, new UserMessageEventArgs(userMessage));
                    string messagePrepend = "", destination = userMessage.Target;
                    string sourceNick = userMessage.Source.Nick;
                    if (userMessage.Message.Contains("@"))
                    {
                        sourceNick = userMessage.Message.Substring(userMessage.Message.IndexOf("@") + 1).Trim();
                        userMessage.Message = userMessage.Message.Remove(userMessage.Message.IndexOf("@"));
                    }
                    if (message.Payload.StartsWith("#")) // channel message
                        messagePrepend = sourceNick + ": \u000f";
                    if (userMessage.Message.StartsWith(ControlCharacter))
                    {
                        command = new IrcCommand(userMessage.Source, userMessage.Message.Trim(), messagePrepend);
                        command.Destination = destination;
                        if (RegisteredCommands.ContainsKey(command.Command.ToLower()))
                        {
                            Thread tempThread = new Thread(() =>
                            {
                                try
                                {
                                    if (PreCommand != null)
                                    {
                                        var eventArgs = new CommandEventArgs(command);
                                        PreCommand(this, eventArgs);
                                        command = eventArgs.Command;
                                    }
                                    string response = RegisteredCommands[command.Command.ToLower()](command);
                                    if (response != null)
                                        SendMessage(destination, messagePrepend + response);
                                    if (PostCommand != null)
                                        PostCommand(this, new CommandEventArgs(command));
                                }
                                catch (Exception e) {
                                    SendMessage(destination, messagePrepend + "Error: " + e.ToString().Haste());

                                }
                            });
                            tempThread.Start();
                        }
                    }
                    break;
                case "PING":
                    SendRawMessage("PONG " + message.Payload);
                    ServerPingAddress = message.Payload;
                    LastPingRecieved = DateTime.Now;
                    break;
                case "PONG":
                    LastPongRecieved = DateTime.Now;
                    break;
                case "332": // Channel topic
                    raw = message.Payload.Substring(message.Payload.IndexOf(' ') + 1);
                    channelName = raw.Remove(raw.IndexOf(' ') );
                    raw = raw.Substring(raw.IndexOf(' ') + 1).Substring(1);
                    channel = GetChannel(channelName);
                    channel.Topic = raw;
                    break;
                case "353":
                    raw = message.Payload.Substring(message.Payload.IndexOf(' ') + 3);
                    channelName = raw.Remove(raw.IndexOf(' '));
                    raw = raw.Substring(raw.IndexOf(':') + 1);
                    string[] names = raw.Split(' ');
                    channel = GetChannel(channelName);
                    foreach (var user in names)
                    {
                        if (user.StartsWith("+"))
                            channel.Users.Add(user.Substring(1));
                        else if (user.StartsWith("@"))
                            channel.Users.Add(user.Substring(1));
                        else
                            channel.Users.Add(user);
                    }
                    break;
                case "366": // End of names
                    raw = message.Payload.Substring(message.Payload.IndexOf(' ') + 1);
                    channelName = raw.Remove(raw.IndexOf(' '));
                    if (ChannelListRecieved != null)
                        ChannelListRecieved(this, new ChannelListEventArgs(GetChannel(channelName)));
                    break;
                case "MODE":
                    if (message.Payload.StartsWith("#"))
                    {
                        ChannelModeChangeEventArgs eventArgs = new ChannelModeChangeEventArgs();
                        eventArgs.Source = new IrcUser(message.Prefix);
                        eventArgs.Channel = GetChannel(message.Payload.Remove(message.Payload.IndexOf(' ')));
                        eventArgs.ModeChange = message.Payload.Substring(message.Payload.IndexOf(' ') + 1);
                        if (ChannelModeChanged != null)
                            ChannelModeChanged(this, eventArgs);
                    }
                    break;
                case "NOTICE":
                    if (NoticeRecieved != null)
                        NoticeRecieved(this, new NoticeEventArgs(new IrcNotice(message)));
                    break;
                case "PART":
                    // #IAmABotAMA :\"hello world\"
                    channelName = message.Parameters[0];
                    channel = GetChannel(channelName);
                    reason = message.Payload.Substring(message.Payload.IndexOf(':') + 1);
                    if (new IrcUser(message.Prefix).Nick != User.Nick)
                    {
                        channel.Users.Remove(new IrcUser(message.Prefix).Nick);
                        if (UserPartedChannel != null)
                            UserPartedChannel(this, new UserPartEventArgs(new IrcUser(message.Prefix), channel, reason));
                    }
                    break;
                case "JOIN":
                    channelName = message.Parameters[0];
                    channel = GetChannel(channelName);
                    channel.Users.Add(new IrcUser(message.Prefix).Nick);
                    if (UserJoinedChannel != null)
                        UserJoinedChannel(this, new UserJoinEventArgs(new IrcUser(message.Prefix), channel));
                    break;
                case "QUIT":
                    reason = message.Payload.Substring(message.Payload.IndexOf(':') + 1);
                    if (new IrcUser(message.Prefix).Nick != User.Nick)
                    {
                        foreach (var c in Channels)
                        {
                            if (c.Users.Contains(new IrcUser(message.Prefix).Nick))
                                c.Users.Remove(new IrcUser(message.Prefix).Nick);
                        }
                        if (UserQuit != null)
                            UserQuit(this, new UserQuitEventArgs(new IrcUser(message.Prefix), reason));
                    }
                    else
                    {
                        if (ServerQuit != null)
                            ServerQuit(this, null);
                        // Reconnect
                        if (IrcConnection.Connected)
                            IrcConnection.Disconnect(false);
                        IrcConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        ReadBuffer = new byte[1024];
                        ReadBufferIndex = 0;
                        IrcConnection.BeginConnect(ServerHostname, ServerPort, ConnectComplete, null);
                    }
                    break;
                case "311": // WHOIS response
                    string[] whois = message.Payload.Split(' ');
                    var result = new IrcUser(whois[1], whois[2].Substring(1));
                    result.Hostname = whois[3];
                    if (WhoIsCallback != null)
                    {
                        WhoIsCallback(result);
                        WhoIsCallback = null;
                    }
                    break;
                case "TOPIC":
                    if (TopicCallback != null)
                    {
                        TopicCallback(message.Payload.Substring(message.Payload.IndexOf(":") + 1));
                        TopicCallback = null;
                    }
                    break;
                case "NICK":
                    if (NickChanged != null)
                        NickChanged(this, new NickChangedEventArgs(new IrcUser(message.Prefix), message.Payload));
                    break;
            }
        }
Ejemplo n.º 4
0
 public IrcNotice(IrcMessage message)
 {
     this.Source  = message.Prefix;
     this.Message = message.Payload.Substring(message.Payload.IndexOf(':') + 1);
 }