public void run() {
    Console c = System.console();
    if (c == null) return;

    String line;
    while (true) {
      line = c.readLine();
      if (line != null) {
        Command cmd = Command.getCommand(null, Command.EType.Console, line);
        if (cmd != null) {
          CommandCallback callback = new CommandCallback();
          cmd.doCommand(null, Command.EType.Console, callback, null, null, line);
          Shocky.send(null, Command.EType.Console, null, null, callback.toString());
        }
      }
    }
  }
Example #2
0
    public void doCommand(
        PircBotX bot,
        EType type,
        CommandCallback callback,
        Channel channel,
        User sender,
        String message) {
      String[] args = message.split(" ");
      callback.type = EType.Notice;
      if (args.length < 3) {
        callback.append(help(bot, type, channel, sender));
        return;
      }

      String[] nicks = args[1].toLowerCase().split(Pattern.quote(";"));
      String quote = StringTools.implode(args, 2, " ");
      if (!quotes.containsKey(channel.getName()))
        quotes.put(channel.getName(), new ArrayList<Quote>());
      quotes.get(channel.getName()).add(new Quote(nicks, quote));
      callback.append("Done.");
    }
Example #3
0
    public void doCommand(
        PircBotX bot,
        EType type,
        CommandCallback callback,
        Channel channel,
        User sender,
        String message) {
      String[] args = message.split(" ");
      if (args.length == 1 && type != EType.Channel) {
        callback.type = EType.Notice;
        callback.append(help(bot, type, channel, sender));
        return;
      }

      String aChannel = type == EType.Channel ? channel.getName() : null, aNick = null;
      int aId = 0;

      if (args.length == 2) {
        if (args[1].charAt(0) == '#') aChannel = args[1];
        else if (StringTools.isNumber(args[1])) aId = Integer.parseInt(args[1]);
        else aNick = args[1];
      } else if (args.length == 3) {
        if (args[1].charAt(0) == '#') {
          aChannel = args[1];
          if (StringTools.isNumber(args[2])) aId = Integer.parseInt(args[2]);
          else aNick = args[2];
        } else if (StringTools.isNumber(args[2])) {
          aId = Integer.parseInt(args[2]);
          aNick = args[1];
        }
      } else if (args.length == 4) {
        aChannel = args[1];
        aNick = args[2];
        aId = Integer.parseInt(args[3]);
      }
      if (aChannel == null) {
        callback.type = EType.Notice;
        callback.append(help(bot, type, channel, sender));
        return;
      }

      if (aNick != null) aNick = aNick.toLowerCase();
      ArrayList<Quote> list = new ArrayList<Quote>();
      for (Quote quote : quotes.get(aChannel))
        if (aNick == null || quote.nicks.contains(aNick)) list.add(quote);
      if (list.isEmpty()) {
        callback.append("No quotes found");
        return;
      }

      if (aId == 0) aId = new Random().nextInt(list.size() + 1);
      if (aId < 0) aId = list.size() - aId - 1;
      aId = Math.min(Math.max(aId, 1), list.size() + 1);

      String quote = Utils.mungeAllNicks(channel, list.get(aId - 1).quote);
      callback.append("[" + aChannel + ": " + (aId) + "/" + (list.size()) + "] " + quote);
    }
Example #4
0
    public void doCommand(Parameters params, CommandCallback callback) {
      String action = null;
      URL url = null;
      long interval = (5 * 60 * 1000);
      Channel c = null;
      callback.type = EType.Notice;

      try {
        if (params.type == EType.Channel) {
          if (params.tokenCount == 0) {
            c = params.channel;
          } else if (params.tokenCount == 1) {
            c = Shocky.getChannel(params.nextParam());
          } else if (params.tokenCount == 2) {
            action = params.nextParam();
            c = params.channel;
            url = new URL(params.nextParam());
          } else if (params.tokenCount == 3) {
            action = params.nextParam();
            String s = params.nextParam();
            if (s.startsWith("#")) c = Shocky.getChannel(s);
            else {
              interval = Utils.parseInterval(s);
              c = params.channel;
            }
            url = new URL(params.nextParam());
          } else if (params.tokenCount == 4) {
            action = params.nextParam();
            interval = Utils.parseInterval(params.nextParam());
            c = Shocky.getChannel(params.nextParam());
            url = new URL(params.nextParam());
          }
          if (c == null) {
            callback.append("No such channel");
            return;
          }
        } else {
          if (params.tokenCount == 1) {
            c = Shocky.getChannel(params.nextParam());
          } else if (params.tokenCount == 3) {
            action = params.nextParam();
            c = Shocky.getChannel(params.nextParam());
            url = new URL(params.nextParam());
          }
          if (c == null) {
            callback.append("No such channel");
            return;
          }
        }
      } catch (MalformedURLException e) {
        callback.append("Malformed URL");
        return;
      }
      if (action != null) params.checkOp();

      if (action == null) {
        StringBuffer sb = new StringBuffer();
        for (Feed feed : feeds) {
          if (!feed.channels.contains(c.getName())) continue;
          if (sb.length() != 0) sb.append("\n");
          sb.append(feed.getURL()).append(' ').append(Utils.timeAgo(feed.getInterval() / 1000));
        }
        callback.append(sb);
      } else if (action.equals("add")) {
        for (Feed feed : feeds) {
          if (feed.getURL().equals(url)) {
            if (feed.channels.contains(c.getName())) {
              callback.append("Feed already on channel's list");
            } else {
              feed.channels.add(c.getName());
              callback.append("Added");
            }
            return;
          }
        }
        if (interval < 300) interval = 300;
        interval *= 1000;
        feeds.add(new Feed(url, interval, null, c.getName()));
        callback.append("Added");
        startUpdater();
      } else if (action.equals("remove")) {
        Iterator<Feed> iter = feeds.iterator();
        boolean modified = false;
        try {
          while (iter.hasNext()) {
            Feed feed = iter.next();
            if (feed.getURL().equals(url)) {
              if (feed.channels.contains(c.getName())) {
                feed.channels.remove(feed.channels.indexOf(c.getName()));
                if (feed.channels.isEmpty()) {
                  iter.remove();
                  modified = true;
                }
                callback.append("Removed");
              } else callback.append("Feed isn't on channel's list");
              return;
            }
          }
          callback.append("Feed isn't on channel's list");
        } finally {
          if (modified) startUpdater();
        }
      }
    }
Example #5
0
 public void doCommand(Parameters params, CommandCallback callback) {
   callback.append(getQ());
 }
Example #6
0
    public void doCommand(
        PircBotX bot,
        EType type,
        CommandCallback callback,
        Channel channel,
        User sender,
        String message) {
      if (!canUseController(bot, type, sender)) return;
      String[] args = message.split(" ");
      callback.type = EType.Notice;
      if (args.length == 1 && type != EType.Channel) {
        callback.append(help(bot, type, channel, sender));
        return;
      }

      String aChannel = type == EType.Channel ? channel.getName() : null, aNick = null;
      int aId = Integer.MIN_VALUE;

      if (args.length == 2) {
        if (args[1].charAt(0) == '#') aChannel = args[1];
        else if (StringTools.isNumber(args[1])) aId = Integer.parseInt(args[1]);
        else aNick = args[1];
      } else if (args.length == 3) {
        if (args[1].charAt(0) == '#') {
          aChannel = args[1];
          if (StringTools.isNumber(args[2])) aId = Integer.parseInt(args[2]);
          else aNick = args[2];
        } else if (StringTools.isNumber(args[2])) {
          aId = Integer.parseInt(args[2]);
          aNick = args[1];
        }
      } else if (args.length == 4) {
        aChannel = args[1];
        aNick = args[2];
        aId = Integer.parseInt(args[3]);
      }
      if (aChannel == null) {
        callback.append(help(bot, type, channel, sender));
        return;
      }

      if (aId == Integer.MIN_VALUE) {
        callback.append("Please specify a number.");
        return;
      }

      if (aNick != null) aNick = aNick.toLowerCase();
      ArrayList<Quote> list = new ArrayList<Quote>();
      for (Quote quote : quotes.get(aChannel))
        if (aNick == null || quote.nicks.contains(aNick)) list.add(quote);
      if (list.isEmpty()) {
        callback.append("No quotes found");
        return;
      }

      if (aId < 0) aId = list.size() - aId - 1;
      aId = Math.min(Math.max(aId, 1), list.size() + 1);

      Quote quote = list.remove(aId - 1);
      for (Iterator<Quote> quoteIter = quotes.get(aChannel).iterator(); quoteIter.hasNext(); ) {
        Quote quote2 = quoteIter.next();
        if (quote2.equals(quote)) quoteIter.remove();
      }
      callback.append("Removed quote: " + quote.quote);
    }
Example #7
0
    public void doCommand(
        PircBotX bot,
        EType type,
        CommandCallback callback,
        Channel channel,
        User sender,
        String message) {
      String[] args = message.split(" ");
      if (args.length == 1) {
        callback.type = EType.Notice;
        callback.append(help(bot, type, channel, sender));
        return;
      }

      StringBuilder sb = new StringBuilder();
      for (int i = 1; i < args.length; i++) {
        if (i != 1) sb.append(" ");
        sb.append(args[i]);
      }

      HTTPQuery q;
      StringBuilder result = new StringBuilder();
      try {
        q =
            new HTTPQuery(
                "http://www.urbandictionary.com/iphone/search/define?term="
                    + URLEncoder.encode(sb.toString(), "UTF8"),
                "GET");
      } catch (Exception e) {
        e.printStackTrace();
        return;
      }
      q.connect(true, false);
      String line = q.readWhole();

      try {
        JSONObject json = new JSONObject(line);
        String resulttype = json.getString("result_type");
        if (resulttype.contentEquals("no_results")) {
          callback.append("No results.");
          return;
        }
        JSONObject entry = json.getJSONArray("list").getJSONObject(0);
        String word = entry.getString("word");
        String definition = entry.getString("definition");
        String example = entry.getString("example");
        String permalink = entry.getString("permalink");
        result.append(Utils.shortenUrl(permalink));
        result.append(" \u0002");
        result.append(word);
        result.append("\u0002: ");
        result.append(definition);
        if (example != null && example.length() > 0) {
          result.append(" Example: ");
          result.append(example);
        }
        String output = StringTools.ircFormatted(result, true);
        callback.append(output);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }