Ejemplo n.º 1
0
 public String toString() {
   if (needsShorten) {
     needsShorten = false;
     title = Utils.shortenAllUrls(StringTools.unescapeHTML(title));
     url = Utils.shortenUrl(url);
   }
   return channel + ' ' + Utils.timeAgo(date) + " | " + title + " | " + url;
 }
Ejemplo n.º 2
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);
    }
Ejemplo n.º 3
0
 public void newEntries(Feed feed, Iterable<FeedEntry> entries) throws InterruptedException {
   for (String s : feed.channels) {
     Channel channel = Shocky.getChannel(s);
     if (channel == null) return;
     PircBotX bot = channel.getBot();
     for (FeedEntry entry : entries) {
       Shocky.sendChannel(bot, channel, Utils.mungeAllNicks(channel, 0, "RSS: " + entry));
       Thread.sleep(2000);
     }
   }
 }
Ejemplo n.º 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();
        }
      }
    }
Ejemplo n.º 5
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();
      }
    }