예제 #1
0
    void Say(ITriggerMsg e)
    {
        var    rest    = e.GetArg(out string channel);
        string message = null;

        // say [channel] <message>
        // Send message to specified channel.
        if (channel.StartsWith("#", StringComparison.Ordinal))
        {
            message = rest.ToJoined();
        }
        // say <message>
        // Send message to current channel.
        if (!message.HasValue())
        {
            channel = e.Channel;
            message = e.MessageWithoutTrigger();
        }

        if (ParseArgs.Success(channel, message) && Say(channel, message))
        {
            string origin = string.Empty;
            if (e.Channel != null)
            {
                origin = '/' + e.Channel;
            }

            log.Message("Say: {0}{1} -> {2}", e.Nick, origin, channel);
        }
    }
예제 #2
0
    static void TimerChange(int index, TimeSpan delta, ITriggerMsg e, Timers timers)
    {
        // Return if invalid or 0 delta.
        if (delta == TimeSpan.Zero)
        {
            return;
        }

        if (timers.Change(index, delta, out TimerDescription desc))
        {
            if (string.IsNullOrEmpty(desc.Message))
            {
                e.Reply("Changed timer {0} to {1} ({2}).",
                        index, desc.Duration.Str(), desc.Remaining.Str());
            }
            else
            {
                e.Reply("Changed \"{0}\" to {1} ({2}).",
                        desc.Message, desc.Duration.Str(), desc.Remaining.Str());
            }
        }
        else
        {
            e.Reply("No such timer.");
        }
    }
예제 #3
0
        // Returns true if trigger calls should be throttled. Returns false otherwise.
        public bool Triggers(ITriggerMsg msg)
        {
            var          entry = GetOrAdd(msg.ReturnTo);
            ThrottleInfo info;

            // Lock on trigger calling. Trigger calling is also possibly multithreaded,
            // because of the varying threading models now offered in MessageDispatcher.
            lock (entry.Triggers)
            {
                // Checks of both Triggers and Output if their throttle is active.
                if (ThrottleActive(msg, entry))
                {
                    return(true);
                }

                info = entry.Triggers.Check();
            }

            // If this trigger call causes the throttle to activate, share information about
            // why and how long trigger calling will be ignored.
            if (info != null)
            {
                var report =
                    string.Format("Ignoring trigger calls from {0} for {1}. ({2} calls in {3})",
                                  msg.ReturnTo, Short(info.ThrottleDuration),
                                  info.MessageLimit, Short(info.Interval));

                log.Message(report);
                msg.Reply(report);
                return(true);
            }

            return(false);
        }
예제 #4
0
    // markov <seeds>
    void Markov(ITriggerMsg e)
    {
        var msg = e.Message.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);

        msg = msg.Slice(1, 0);
        EmitSentence(e.ReturnTo, msg, e.Nick);
    }
예제 #5
0
 // timer stop [index]
 static void TimerStop(int index, ITriggerMsg e, Timers timers)
 {
     // Stop all timers.
     if (index < 0)
     {
         timers.StopAll();
         e.Reply("Stopped all your timers.");
     }
     // Stop one timer.
     else
     {
         if (timers.Stop(index, out TimerDescription desc))
         {
             if (string.IsNullOrEmpty(desc.Message))
             {
                 e.Reply("Stopped timer {0} :: {1}/{2}",
                         index, desc.Elapsed.Str(), desc.Duration.Str());
             }
             else
             {
                 e.Reply("Stopped \"{0}\" :: {1}/{2}",
                         desc.Message, desc.Elapsed.Str(), desc.Duration.Str());
             }
         }
         else
         {
             e.Reply("No such timer.");
         }
     }
 }
예제 #6
0
    void ExecuteSearch(ITriggerMsg e, Site site)
    {
        var searchTerms = e.MessageWithoutTrigger();

        if (!string.IsNullOrEmpty(searchTerms))
        {
            var results = site.Search(searchTerms);

            if (results.Success)
            {
                if (results.Count > 0)
                {
                    PrintResults(results, site, e.ReturnTo);
                }
                else
                {
                    e.Reply("Sorry, the query '{0}' didn't result in any hits.", searchTerms);
                }
            }
            else
            {
                e.Reply("Error executing search: " + results.Exception.Message);
            }
        }
    }
예제 #7
0
        static bool ThrottleActive(ITriggerMsg msg, SourceEntry entry)
        {
            if (entry.Triggers.ThrottleActive)
            {
                msg.SendNotice("Sorry, currently ignoring trigger calls from {0}. Time remaining: {1}",
                               msg.ReturnTo, entry.Triggers.TimeLeft);
                return(true);
            }

            // Lock on output checks.
            lock (entry.Output)
            {
                // Refrain from doing any trigger work when output is being throttled anyways. This is not 100% safe,
                // since some trigger calls might not output to originating channel/user (ReturnTo), but is worth it to
                // protect against abuse.
                if (entry.Output.ThrottleActive)
                {
                    // Only attempt to send if source (ReturnTo) of this trigger call isn't the nick, since that would
                    // mean its subject to the active throttle which we just checked.
                    if (msg.ReturnTo != msg.Nick)
                    {
                        msg.SendNotice("Sorry, currently staying silent in {0}. Time remaining: {1}",
                                       msg.ReturnTo, entry.Output.TimeLeft);
                    }

                    return(true);
                }
            }

            return(false);
        }
예제 #8
0
    public void Tell(ITriggerMsg e)
    {
        // tell <nick> <message>
        var message =
            e.GetArg(out string destinationNick)
            .ToJoined(JoinedOptions.TrimExterior);

        if (ParseArgs.Success(destinationNick, message))
        {
            var inbox = inboxes.GetOrNew(destinationNick);

            if (inbox.Add(e.Nick, message))
            {
                e.Reply("Sending message to {0} when they're active. [{1}/{2}]",
                        destinationNick, inbox.MessagesCount, inbox.Capacity);

                inboxes.Save(inbox);
            }
            else
            {
                e.Reply("Sorry, {0}'s inbox is full!", destinationNick);
            }
        }
        // Short circuit tell to tell-read if not enough arguments.
        else
        {
            Read(e);
        }
    }
예제 #9
0
 public void Enable(ITriggerMsg e)
 {
     if (manager.EnableNick(e.Channel, e.Nick))
     {
         e.SendNotice("Re-enabling URL-Titling for you.");
     }
 }
예제 #10
0
 public SingleTimer(int index, Action <int> remove, ITriggerMsg msg)
 {
     Index    = index;
     Remove   = remove;
     tmr      = new Timer(Finish);
     this.msg = msg;
 }
예제 #11
0
    public void Query(ITriggerMsg msg, bool debug)
    {
        WebToIrc wIrc;

        if (msg.Channel != null)
        {
            wIrc = config.ConstructWebToIrc(msg.Channel);
        }
        else
        {
            wIrc = config.ConstructWebToIrc("_all");
        }

        foreach (var arg in msg.Arguments)
        {
            var result = wIrc.WebInfo(arg);

            if (debug)
            {
                foreach (string s in result.Messages)
                {
                    msg.Reply(s);
                }
            }

            if (result.Success)
            {
                msg.Reply(result.Title);
            }
            else
            {
                msg.Reply(result.Exception.Message);
            }
        }
    }
예제 #12
0
    public void HandleTrigger(ITriggerMsg e)
    {
        var rest = e.GetArg(out string command, toLower: true);

        // timeleft
        if (!command.HasValue())
        {
            ShowAll(e.ReturnTo);
            return;
        }

        // timeleft set Title 2104-01-01
        if (command == "set" || command == "add")
        {
            Set(e.Nick, rest);
        }

        // timeleft del Title
        else if (command == "del")
        {
            Del(e.Nick, rest.ToJoined(JoinedOptions.TrimRemove));
        }

        // timeleft Title
        else
        {
            Show(e.ReturnTo, e.ArgString());
        }
    }
예제 #13
0
        public static string MessageWithoutTrigger(this ITriggerMsg msg, bool trim = false)
        {
            if (msg == null)
            {
                throw new ArgumentNullException(nameof(msg));
            }

            var message = string.Empty;

            if (msg.MessageParts.Count > 1)
            {
                message =
                    msg.Message
                    .Substring(msg.MessageParts[0].Length + 1);
            }

            if (trim)
            {
                return(message.Trim());
            }
            else
            {
                return(message);
            }
        }
예제 #14
0
    // markov-nick <nick> [seeds]
    void MarkovNick(ITriggerMsg e)
    {
        var msg = e.Message.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);

        if (msg.Length > 1)
        {
            var source = msg[1].ToUpperInvariant();

            List <Sentence> sentences;
            // When we have seeds.
            if (msg.Length > 2)
            {
                msg       = msg.Slice(2, 0);
                sentences = chainey.Build(msg, source, false);
            }
            // When we don't have seeds.
            else
            {
                sentences = chainey.BuildRandom(1, source);
            }

            if (sentences.Count > 0)
            {
                SendSentence(e.ReturnTo, sentences[0], e.Nick);
            }
        }
    }
예제 #15
0
    // -----------------
    // Chainey triggers.
    // -----------------

    // remove <sentence>
    void Remove(ITriggerMsg e)
    {
        if (meido.AuthLevel(e.Nick) >= 2)
        {
            chainey.Remove(e.ArgString());
            e.Reply("Removed sentence.");
        }
    }
예제 #16
0
 public void Dump(ITriggerMsg e)
 {
     // Only allow owner to dump (writing temp files).
     if (meido.AuthLevel(e.Nick) == 3)
     {
         QueryTriggers.Dump(e);
     }
 }
예제 #17
0
    public void Timer(ITriggerMsg e)
    {
        var timers = ircTimers.GetTimers(e.Nick);
        var rest   = e.GetArg(out string command, toLower: true);

        // timer
        if (!command.HasValue())
        {
            EmitDescriptions(timers.Descriptions(), e);
            return;
        }

        // timer <delta> [index]
        // Delta is +N or -N
        char deltaSign = command[0];

        if (deltaSign == '+' || deltaSign == '-')
        {
            TimerChange(
                ParseIdx(rest.GetArg(), 0),
                ParseTs(command),
                e, timers
                );
        }

        // timer stop [index]
        else if (command == "stop")
        {
            TimerStop(
                ParseIdx(rest.GetArg(), -1),
                e, timers
                );
        }

        // timer change <index> <delta>
        else if (command == "change")
        {
            var args = rest.GetArgs(2);
            if (ParseArgs.Success(args))
            {
                TimerChange(
                    ParseIdx(args[0], -1),
                    ParseTs(args[1]),
                    e, timers
                    );
            }
        }

        // timer <duration> [message]
        else
        {
            TimerStart(
                ParseTs(command),
                rest.ToJoined(JoinedOptions.TrimExterior),
                e, timers
                );
        }
    }
예제 #18
0
    void EightBall(ITriggerMsg e)
    {
        string choice = RandomChoice.Shake8Ball();

        irc.SendMessage(e.ReturnTo, "The Magic 8-Ball says...");
        // Wait for 1.5 seconds.
        Thread.Sleep(1500);
        irc.SendMessage(e.ReturnTo, choice + ".");
    }
예제 #19
0
        public static HelpRequest FromHelpTrigger(ITriggerMsg msg, string triggerPre)
        {
            if (msg.Arguments.Count > 0)
            {
                return(new HelpRequest(msg.Arguments, triggerPre));
            }

            return(new HelpRequest());
        }
예제 #20
0
    void Choose(ITriggerMsg e)
    {
        string choice = RandomChoice.RndChoice(e.Arguments);

        if (choice != null)
        {
            e.Reply(choice);
        }
    }
예제 #21
0
 static void EmitDescriptions(TimerDescription[] descs, ITriggerMsg msg)
 {
     msg.SendNotice("Your currently running timers:");
     foreach (var desc in descs)
     {
         EmitDescription(desc, msg);
     }
     msg.SendNotice(" -----");
 }
예제 #22
0
 public static void Dump(ITriggerMsg msg)
 {
     foreach (var arg in msg.Arguments)
     {
         if (Uri.TryCreate(arg, UriKind.Absolute, out Uri url))
         {
             msg.Reply(Dump(url));
         }
     }
 }
예제 #23
0
    bool Allowed(ITriggerMsg e)
    {
        if (conf.ActiveChannels.Contains(e.Channel) || meido.AuthLevel(e.Nick) >= 2)
        {
            return(true);
        }

        e.Reply("Access denied, please contact my owner for information.");
        return(false);
    }
예제 #24
0
    static void OutputError <T>(ITriggerMsg e, GenericExpression <T> expr)
    {
        string error = expr.ErrorMessage;

        if (expr.ErrorPosition >= 0)
        {
            error += " | Postion: " + expr.ErrorPosition;
        }

        e.Reply(error);
    }
예제 #25
0
        public void AuthTrigger(ITriggerMsg msg)
        {
            var passwd = msg.ArgString();

            if (passwd.HasValue() && meidoComm.Auth(msg.Nick, passwd))
            {
                msg.Reply("You've successfully authenticated.");
            }

            msg.Reply("Your current Authentication Level is " + meidoComm.AuthLevel(msg.Nick));
        }
예제 #26
0
    public void Clear(ITriggerMsg e)
    {
        var inbox = inboxes.Get(e.Nick);
        int count = inbox.MessagesCount;

        inbox.ClearMessages();

        irc.SendNotice(e.Nick, "Cleared all your messages. (Count: {0})", count);
        if (count > 0)
        {
            inboxes.Save(e.Nick);
        }
    }
예제 #27
0
    // --- Set Weather Location, 'W' trigger ---

    void SetWeatherLocation(ITriggerMsg e)
    {
        var location = e.MessageWithoutTrigger(trim: true);

        if (!string.IsNullOrEmpty(location))
        {
            defaultLocations.Set(e.Nick, location);
            defaultLocations.Serialize(storagePath);
            e.Reply("Your default location has been set to '{0}'.", location);
        }
        else
        {
            e.Reply("Your current default location is '{0}'.", defaultLocations.Get(e.Nick));
        }
    }
예제 #28
0
    static bool CheckPreconditions(ITriggerMsg e, VerifiedExpression expr, string symbol)
    {
        if (!expr.Success)
        {
            OutputError(e, expr);
            return(false);
        }
        if (!Verifier.VerifySymbol(symbol))
        {
            e.Reply("Symbol contains illegal characters.");
            return(false);
        }

        return(true);
    }
예제 #29
0
        public void Fire(Trigger tr, ITriggerMsg msg)
        {
            string source = msg.Nick;

            if (msg.Channel != null)
            {
                source += "/" + msg.Channel;
            }

            log.Message("{0} {1}", source, msg.Message);

            if (FirePredicate(msg, tr.Option))
            {
                tr.Call(msg);
            }
        }
예제 #30
0
        public static string[] ArgArray(this ITriggerMsg msg)
        {
            if (msg == null)
            {
                throw new ArgumentNullException(nameof(msg));
            }

            if (msg.Arguments.Count > 0)
            {
                var arguments = new string[msg.Arguments.Count];
                msg.Arguments.CopyTo(arguments, 0);
                return(arguments);
            }

            return(Array.Empty <string>());
        }