示例#1
0
        public static string GetReceiver(IrcIdentity sender, Channel channel)
        {
            if (null != channel)
            {
                return channel.Name;
            }

            return sender.Username;
        }
        internal static void Dispatch(string commandName, string[] parameters, IrcIdentity sender, ServerUser serverUser, Channel channel)
        {
            if (CommandList.ContainsKey(commandName))
            {
                var command = CommandList[commandName];

                var commandParameters = new CommandParameters
                    {
                        CommandName = commandName,
                        Parameters = parameters,
                        Sender = sender,
                        ServerUser = serverUser,
                        Channel = channel,
                        Command = command
                    };

                command.Function(commandParameters);
            }
            else
            {
                var commandCompressed = commandName;

                if (null != parameters && parameters.Length > 0)
                {
                    commandCompressed += string.Format(" ({0})", string.Join(",", parameters));
                }
                Debug.WriteLine(
                    string.Format(
                        "Unknown command {0} (from {1})",
                        commandCompressed,
                        (sender != null) ? sender.Nickname.ToString() : "Anonymous"),
                    null != channel ? channel.ToString() : serverUser.ToString());
                var message = string.Format(
                    "Unknown command {0}",
                    commandCompressed);
                serverUser.SendMessage(Command.GetReceiver(sender, channel), message);
            }
        }
示例#3
0
        /// <summary>
        /// The main.
        /// </summary>
        /// <param name="args">
        /// The args.
        /// </param>
        internal static void Main(string[] args)
        {
            ServerUser serverUser = null;
            Channel channel = null;

            Commands.Register();

            var isRunning = true;
            var answer = "My name is Batman";
            while (isRunning)
            {
                try
                {
                    Console.Write("> ");
                    string line = Console.ReadLine();
                    if (line == null)
                    {
                        break;
                    }

                    if (line.Length == 0)
                    {
                        continue;
                    }

                    string[] parts = line.Split(' ');
                    string command = parts[0].ToLower();
                    string[] parameters = parts.Skip(1).ToArray();

                    switch (command)
                    {
                        case "j":
                            serverUser = new ServerUser(
                                ConfigurationManager.AppSettings.Get("HostName"),
                                ConfigurationManager.AppSettings.Get("UserName"),
                                ConfigurationManager.AppSettings.Get("TheWord"),
                                ConfigurationManager.AppSettings.Get("NickName"),
                                ConfigurationManager.AppSettings.Get("RealName"),
                                int.Parse(ConfigurationManager.AppSettings.Get("MillisecondsBetweenCommands")))
                                             {
                                                 CommandPrefix = "+"
                                             };
                            string channelName = ConfigurationManager.AppSettings.Get("ChannelName");
                            if (parameters.Length > 0)
                            {
                                channelName = parameters[0];
                            }

                            channel = new Channel(serverUser, channelName)
                                          {
                                              KickBots = true
                                          };

                            channel.SendMessage(string.Format(
                                "Hello everyone! Write \"+hello\" if you would like me to respond to your chat messages. :) For other commands - try \"+help\"."));
                            break;
                        case "q":
                        case "l":
                            if (null != channel)
                            {
                                channel.SendMessage("I'm leaving for now, see you again!");
                                channel.Leave();
                                serverUser.LogOut();
                            }

                            if (command == "q")
                            {
                                isRunning = false;
                            }

                            break;
                        case "t":
                            if (channel == null)
                            {
                                break;
                            }

                            if (parameters.Length == 1)
                            {
                                channel.DoInterpreteMessages = parameters[0] == "on";
                            }
                            else
                            {
                                Console.WriteLine("Usage: t on|off");
                            }

                            break;
                        case "r":
                            var question = parameters.Length == 0 ? answer : string.Join(" ", parameters);
                            answer = Commands.AskBot(Commands.ChatBot.Chato, question, "zenioxbottest", "Dr. Test");
                            Console.WriteLine(answer);
                            break;
                        case "e":
                            var a = Services.DetectLanguage("Hej");
                            break;
                            //case "e":
                            //TranslateCommand(new CommandParameters
                            //                     {
                            //                         Command = c,
                            //                         CommandName = c.Name,
                            //                         Parameters = "sv|en Hej, jag heter Lars".Split(' ')
                            //                     });
                            //break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Fatal error {0}\r{1}", e.Message, e.StackTrace);
                }
            }
        }
示例#4
0
        /// <summary>
        /// The interprete.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="serverUser">
        /// The server user.
        /// </param>
        /// <param name="channel">
        /// The channel.
        /// </param>
        internal static void Interprete(string message, IrcIdentity sender, ServerUser serverUser, Channel channel)
        {
            if (null == sender)
            {
                return;
            }

            var user = User.GetOrCreate(sender);

            if (user.Translate)
            {
                var language = Services.DetectLanguage(message, false, user.Language == null ? 0.1 : 0.3);

                if ((language != null) && (language != "en"))
                {
                    user.Language = language;
                }

                if (user.Language != null)
                {
                    message = Services.Translate(user.Language, "en", message);
                    channel.SendMessage(string.Format("{0} ({1}): {2}", user.NickName, user.Language, message));
                }
            }

            if (!user.TalkTo)
            {
                return;
            }

            var answer = AskBot(
                ChatBot.Romulus,
                message,
                sender.Username,
                serverUser.NickName);

            if (channel != null)
            {
                answer = string.Format("-> {0}: {1}", sender.Nickname, answer);
            }

            serverUser.SendMessage(Command.GetReceiver(sender, channel), answer);
        }
示例#5
0
 /// <summary>
 /// The add.
 /// </summary>
 /// <param name="channel">
 /// The channel.
 /// </param>
 internal void Add(Channel channel)
 {
     this.channelList.Add(channel.Name, channel);
     Debug.WriteLine(string.Format("Added channel {0}.", channel), this.ToString());
 }