예제 #1
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);
        }
    }
예제 #2
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);
            }
        }
    }
예제 #3
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.");
         }
     }
 }
예제 #4
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);
            }
        }
    }
예제 #5
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.");
        }
    }
예제 #6
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));
        }
예제 #7
0
    void Finish(object state)
    {
        if (string.IsNullOrEmpty(Message))
        {
            msg.Reply("Timer {0} has finished! :: {1}", Index, Duration);
        }
        else
        {
            msg.Reply("{0} :: {1}", Message, Duration);
        }

        Remove(Index);
    }
예제 #8
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));
        }
    }
예제 #9
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);
        }
예제 #10
0
    // -----------------
    // Chainey triggers.
    // -----------------

    // remove <sentence>
    void Remove(ITriggerMsg e)
    {
        if (meido.AuthLevel(e.Nick) >= 2)
        {
            chainey.Remove(e.ArgString());
            e.Reply("Removed sentence.");
        }
    }
예제 #11
0
    void Choose(ITriggerMsg e)
    {
        string choice = RandomChoice.RndChoice(e.Arguments);

        if (choice != null)
        {
            e.Reply(choice);
        }
    }
예제 #12
0
    // timer <duration> [message]
    static void TimerStart(TimeSpan duration, string message, ITriggerMsg e, Timers timers)
    {
        // Return if invalid or negative duration.
        if (duration <= TimeSpan.Zero)
        {
            return;
        }

        int tmrNo = timers.Enqueue(duration, e, message);

        if (tmrNo >= 0)
        {
            e.Reply("Your timer has started. [{0}] {1}", tmrNo, duration.Str());
        }
        else
        {
            e.Reply("Max timer count reached. Please wait for some timers to finish or stop them manually.");
        }
    }
예제 #13
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));
         }
     }
 }
예제 #14
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);
    }
예제 #15
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);
    }
예제 #16
0
 void WeatherSearch(ITriggerMsg e, WeatherLocation location)
 {
     if (TryGetConditions(location, out WeatherConditions cond))
     {
         if (cond.Success)
         {
             irc.SendMessage(e.ReturnTo, WeatherFormat.IrcFormat(cond));
         }
         else if (cond.HttpErrorIs(HttpStatusCode.NotFound))
         {
             e.Reply("Sorry, couldn't find anything for query '{0}'.", location);
         }
         else
         {
             e.Reply(cond.Exception.Message);
         }
     }
     else
     {
         e.Reply(weatherError);
     }
 }
예제 #17
0
    // --- Weather Search, 'w' trigger ---

    void WeatherSearch(ITriggerMsg e)
    {
        var location = GetLocation(e);

        if (!string.IsNullOrWhiteSpace(location))
        {
            var weatherLoc = WeatherLocation.Parse(location);
            if (weatherLoc.IsValidQuery && VerifyCountry(weatherLoc.Country))
            {
                WeatherSearch(e, weatherLoc);
            }
            else
            {
                e.Reply("Invalid query format. Please use \"city, country\" or \"zip, country\", " +
                        "where country is a 2-letter country code (ISO 3166).");
            }
        }
        else
        {
            e.Reply("Either specify a location or set your default location with the 'W' trigger. " +
                    "(That is an upper case W)");
        }
    }
예제 #18
0
    void DefVar(ITriggerMsg e)
    {
        var expression = e.GetArg(out string symbol).ToJoined();

        if (ParseArgs.Success(symbol, expression))
        {
            var expr = VerifiedExpression.Parse(expression, CalcEnv);
            if (CheckPreconditions(e, expr, symbol))
            {
                double result = ShuntingYard.Calculate(expr);
                if (CalcEnv.Variable(symbol, out double previous))
                {
                    e.Reply("{0} = {1} (Previous value: {2})", symbol, result, previous);
                }
                else
                {
                    e.Reply("{0} = {1}", symbol, result);
                }

                CalcEnv.AssignVariable(symbol, result);
            }
        }
    }
예제 #19
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);
    }
예제 #20
0
    void HandleTrigger(ITriggerMsg e)
    {
        var exprStr = e.MessageWithoutTrigger();

        if (exprStr.HasValue())
        {
            var expr = VerifiedExpression.Parse(exprStr, CalcEnv);

            if (expr.Success)
            {
                double result = ShuntingYard.Calculate(expr);
                e.Reply(result.ToString());
            }
            else
            {
                OutputError(e, expr);
            }
        }
    }
예제 #21
0
        public void Trigger(ITriggerMsg msg)
        {
            var request = HelpRequest.FromHelpTrigger(msg, triggers.Prefix);

            // Help trigger with query: lookup query and print help.
            if (request.IsValid)
            {
                var result = DoRequest(request);
                if (result.Success)
                {
                    result.PrintHelp(msg);
                }
                else
                {
                    msg.Reply("Sorry, I couldn't find any help for '{0}'.",
                              request.NormalizedQuery);
                }
            }
            // Just the help trigger, no query: print all the subjects we've got.
            else
            {
                var trigIds =
                    from id in triggers.PrimeIdentifiers
                    orderby id
                    select id;

                var topics =
                    from help in helpOnTopics.Values
                    orderby help.Topic
                    select help.Topic;

                var irc = msg.Irc;
                irc.SendMessage(msg.ReturnTo, "Triggers: " + string.Join(ListSep, trigIds));
                irc.SendMessage(msg.ReturnTo, "Other: " + string.Join(ListSep, topics));
            }
        }
예제 #22
0
    public void Nyaa(ITriggerMsg e)
    {
        string command  = null;
        int?   assocPat = null;
        string input    = string.Empty;

        using (var argEnum = new ArgEnumerator(e)
        {
            ToLower = true
        })
        {
            if (argEnum.Next() == "ex")
            {
                // nyaa ex <assocPat> <add|del|show>
                // Exclude Patterns associated with a pattern.
                if (int.TryParse(argEnum.Next(), out int assocOut))
                {
                    assocPat = assocOut;
                    command  = argEnum.Next();
                }
                // nyaa ex <add|del|show>
                // Global Exclude Patterns.
                else
                {
                    assocPat = -1;
                    command  = argEnum.Current;
                }
            }
            // nyaa [add|del|show]
            else if (argEnum.Current.HasValue())
            {
                command = argEnum.Current;
            }

            if (command.HasValue())
            {
                input = argEnum.GetRemaining().ToJoined();
            }
        }

        switch (command)
        {
        // Restrict access to add and del, but not show.
        case "add":
            if (Allowed(e))
            {
                Add(e.Channel, e.Nick, input, assocPat);
            }
            return;

        case "del":
            if (Allowed(e))
            {
                Del(e.Channel, e.Nick, input, assocPat);
            }
            return;

        case "show":
            ShowAll(e.Channel, e.Nick, assocPat);
            return;

        case null:
            if (conf.Feed != null)
            {
                e.Reply("Currently fetching {0} every {1} minutes. See nyaa add|del|show for usage.",
                        conf.Feed, conf.Interval.Minutes);
            }
            else
            {
                e.Reply(feedError);
            }
            return;
        }
    }
예제 #23
0
        public void AdminTrigger(ITriggerMsg msg)
        {
            if (meidoComm.AuthLevel(msg.Nick) >= 2)
            {
                var argv = msg.GetArg(out string trigger, toLower: true);

                // Admin triggers.
                switch (trigger)
                {
                case "j":
                case "join":
                    foreach (var chan in argv)
                    {
                        irc.RfcJoin(chan);
                    }

                    return;

                case "p":
                case "part":
                    foreach (var chan in argv)
                    {
                        irc.RfcPart(chan);
                    }

                    return;

                case "nick":
                    var newNick = argv.GetArg();
                    if (newNick.HasValue())
                    {
                        msg.Reply("Attempting to change nick from {0} to {1}.", irc.Nickname, newNick);
                        irc.RfcNick(newNick);
                    }
                    else
                    {
                        msg.Reply("Current nick is {0}.", irc.Nickname);
                    }

                    return;

                case "ch":
                case "channels":
                    var channels = string.Join(" ", irc.GetChannels());
                    msg.Reply(channels);
                    return;
                }
                // Owner only triggers.
                if (meidoComm.AuthLevel(msg.Nick) == 3)
                {
                    switch (trigger)
                    {
                    case "dc":
                    case "disconnect":
                        msg.Reply("Disconnecting from {0}.", irc.Address);
                        bot.Dispose();
                        return;

                    case "restart":
                        Program.RestartMeido();
                        return;

                    case "gc-collect":
                        long before = GC.GetTotalMemory(false);
                        GC.Collect();
                        msg.Reply("Garbage Collection Meido: {0:N0} -> {1:N0}", before, GC.GetTotalMemory(true));
                        return;
                    }
                }
            }
            else
            {
                msg.Reply("Authentication Level insufficient. Either you're not an admin or you need to authenticate.");
            }
        }