daily pastebin goal
74%
SHARE
TWEET

Java IRC Bot (PircBot + Jsoup)

a guest Mar 7th, 2012 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.IOException;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. import org.jibble.pircbot.PircBot;
  5. import org.jibble.pircbot.IrcException;
  6. import org.jsoup.Jsoup;
  7.  
  8. public class InfoBot extends PircBot {
  9.         protected final Pattern urlPattern;
  10.  
  11.         public InfoBot() throws IOException, IrcException {
  12.                 this.urlPattern = Pattern.compile("(https?://[^\\s]+)");
  13.                 this.setName("TestBot");
  14.                 this.setVerbose(true);
  15.                 this.connect("irc.efnet.nl");
  16.                 this.joinChannel("#test");
  17.         }
  18.  
  19.         public void onMessage(final String channel, final String sender, final String login, final String hostname, final String message) {
  20.                 final Matcher matcher = this.urlPattern.matcher(message);
  21.                 while (matcher.find()) {
  22.                         final String url = matcher.group();
  23.                         try {
  24.                                 final String title = Jsoup.connect(url).get().select("title").first().text();
  25.                                 this.sendMessage(channel, title);
  26.                         } catch (final IOException e) {
  27.                                 System.out.println("Could not fetch URL: " + url);
  28.                         }
  29.                 }
  30.         }
  31.  
  32.         public static void main(final String[] args) throws IOException, IrcException {
  33.                 final InfoBot infoBot = new InfoBot();
  34.         }
  35. }
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top