コード例 #1
0
 public void Connect()
 {
     FileDescriptor = IrcClient.ConnectIrc();
 }
コード例 #2
0
 public string ReceiveRawMessage()
 {
     return(IrcClient.ReadString(FileDescriptor));
 }
コード例 #3
0
        public static void Main(string[] args)
        {
            Config.Add("parse_links", false);
            Config.Add("autojoin", true);
            Config.Add("respond_chance", 0.05);
            Config.Add("conversation_length", 2);
            Config.Add("conversation_decay_chance", 0.8);
            Config.Add("conversation_decay_time", 20);
            Config.Add("conversation_start_chance_highlight", 0.5);
            Config.Add("conversation_start_chance_command", 0.7);
            Config.Add("conversation_start_chance_question", 0.7);

            Config.ProtectAdded = false;

            string join_default = "#topkek-test";

            QuestionAggregator aggre = new QuestionAggregator();

            aggre.Load("/home/hexafluoride/log-4"); // replace this with your IRC log

            IrcClient client = new IrcClient();

            client.Connect();

            string   conversing_with     = "";
            int      conversation_length = 0;
            DateTime last_conversed      = DateTime.Now;

            while (true)
            {
                string     raw = client.ReceiveRawMessage();
                IrcMessage msg = new IrcMessage(raw);

                if (msg.CommandId == 376)
                {
                    client.Join(join_default);
                }

                if (msg.CommandName == "PRIVMSG")
                {
                    string target  = msg.Parameters.Split(' ')[0];
                    string message = string.Join(":", msg.Parameters.Split(':').Skip(1));
                    string source  = msg.Prefix.Nick;
                    Console.WriteLine("{0} -> {1} from {2}", message, target, source);

                    if (!SeenNicks.ContainsKey(source))
                    {
                        Console.WriteLine("Added {0} to recent nick list.", source);
                    }

                    SeenNicks[source] = DateTime.Now;

                    bool authed = msg.Prefix.Host == "fluoride" && msg.Prefix.Nick == "h";

                    if (message.Contains("http") && ((bool)Config.Get("parse_links")))
                    {
                        try
                        {
                            System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();

                            Regex regex = new Regex(@"(?<Protocol>\w+):\/\/(?<Domain>[\w@][\w-.:@]+)\/?[\w\.~?=%&=\-@/$,]*");

                            if (regex.IsMatch(message))
                            {
                                string url = regex.Matches(message)[0].Value;

                                var task = LinkResolver.GetSummary(url);
                                task.Wait();
                                string summary = task.Result.Value;

                                if (summary != "-")
                                {
                                    summary = HttpUtility.HtmlDecode(summary);

                                    sw.Stop();

                                    client.SendPrivateMessage(target, string.Format("{0} ({1}s)", summary, sw.Elapsed.TotalSeconds.ToString("0.00")));
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                    if (MatchesHealRequest(message))
                    {
                        string nick = msg.Prefix.Nick;

                        double rnd = Random.NextDouble();
                        if (message.ToLower().EndsWith("s**t") || message.ToLower().EndsWith("w***e") || message.ToLower().EndsWith("bitch"))
                        {
                            if (rnd < 0.9 && nick != "h")
                            {
                                client.SendAction(target, string.Format("h-heals {0} (3+{1} HP~!)", nick, Random.Next(90, 110)));
                            }
                            else
                            {
                                client.SendAction(target, string.Format("h-heals {0} (3+{1} HP~! Critical heal~!)", nick, Random.Next(250, 300)));
                            }
                        }
                        else
                        {
                            if (rnd < 0.9 && nick != "h")
                            {
                                client.SendAction(target, string.Format("heals {0} (3+{1} HP!)", nick, Random.Next(90, 110)));
                            }
                            else
                            {
                                client.SendAction(target, string.Format("heals {0} (3+{1} HP! Critical heal!)", nick, Random.Next(250, 300)));
                            }
                        }
                    }
                    if (message.StartsWith(".yt") || message.StartsWith(".youtube"))
                    {
                        string rest   = string.Join(" ", message.Split(' ').Skip(1));
                        var    result = Youtube.Search(rest);

                        if (result != "-")
                        {
                            client.SendPrivateMessage(target, result);
                        }
                    }
                    if (message.StartsWith(".config"))
                    {
                        var    words = message.Split(' ').Skip(1).ToList();
                        string name  = words.Skip(1).FirstOrDefault();
                        string arg   = string.Join(" ", words.Skip(2)).Trim();

                        if (words[0] == "get")
                        {
                            var value = Config.Get(name);
                            if (value != null)
                            {
                                client.SendPrivateMessage(target, name + " is " + value.ToString());
                            }
                            else
                            {
                                client.SendPrivateMessage(target, name + " doesn't exist. Try using .config set " + name + " <value>.");
                            }
                        }
                        if (words[0] == "set")
                        {
                            var option = Config.GetOption(name);

                            if (option == null)
                            {
                                Config.Add(name, arg);
                            }
                            else if ((option.Protected && authed) || !option.Protected)
                            {
                                Config.Set(name, arg);
                            }
                            else
                            {
                                client.SendPrivateMessage(target, name + " is a protected value!");
                            }
                        }
                        if (words[0] == "dump")
                        {
                            if (authed)
                            {
                                foreach (var option in Config.Options)
                                {
                                    client.SendPrivateMessage(target, option.Name + " is \"" + option.Value.ToString() + "\"" + (option.Protected ? " (protected)" : ""));
                                }
                            }
                        }
                    }
                    if (message.StartsWith(".answer"))
                    {
                        string question = message.Substring(".answer".Length).Trim();
                        var    pair     = aggre.GetPair(question);

                        if (pair != null && pair.Response != null)
                        {
                            client.SendPrivateMessage(target, Nickify(pair.Response.Content.Replace("{0}", source)));
                            conversing_with = source;
                            if (Random.NextDouble() < (double)Config.Get("conversation_start_chance_command"))
                            {
                                conversation_length = (int)Config.Get("conversation_length");
                            }
                            last_conversed = DateTime.Now;
                        }
                    }
                    else if (message.Contains("topkek_2003"))
                    {
                        var pair = aggre.GetPair(message);

                        if (pair != null && pair.Response != null)
                        {
                            client.SendPrivateMessage(target, Nickify(pair.Response.Content.Replace("{0}", source)));
                            conversing_with = source;
                            if (Random.NextDouble() < (double)Config.Get("conversation_start_chance_highlight"))
                            {
                                conversation_length = (int)Config.Get("conversation_length");
                            }
                            last_conversed = DateTime.Now;
                        }
                    }
                    else if (new LogLine()
                    {
                        Content = message
                    }.IsQuestion())
                    {
                        if (Random.NextDouble() < (double)Config.Get("respond_chance"))
                        {
                            var pair = aggre.GetPair(message);

                            if (pair != null && pair.Response != null)
                            {
                                client.SendPrivateMessage(target, Nickify(pair.Response.Content.Replace("{0}", source)));
                                conversing_with = source;
                                if (Random.NextDouble() < (double)Config.Get("conversation_start_chance_question"))
                                {
                                    conversation_length = (int)Config.Get("conversation_length");
                                }
                                last_conversed = DateTime.Now;
                            }
                        }
                    }
                    else if (conversation_length > 0 && (DateTime.Now - last_conversed).TotalSeconds < (int)Config.Get("conversation_decay_time"))
                    {
                        if (source == conversing_with)
                        {
                            var pair = aggre.GetPair(message);

                            if (pair != null && pair.Response != null)
                            {
                                client.SendPrivateMessage(target, Nickify(pair.Response.Content.Replace("{0}", source)));

                                if (Random.NextDouble() < (double)Config.Get("conversation_decay_chance"))
                                {
                                    conversation_length--;
                                }
                            }
                        }
                    }
                    if (message.StartsWith(".join"))
                    {
                        client.Join(message.Substring(".join".Length).Trim());
                    }
                    if (message == ".stopfollow")
                    {
                        if (source == conversing_with)
                        {
                            conversing_with = "";
                        }
                    }
                }
                if (msg.CommandName == "PING")
                {
                    client.SendIrcMessage(new IrcMessage("PONG", msg.Parameters));
                }
            }

//            while (true)
//            {
//                var pair = aggre.GetPair(Console.ReadLine());
//                Console.WriteLine(pair.Response.Content);
//            }
        }