/** Admins command */ private void cmdAdmins(MessageEvent me) { String msg = me.getMessage(); int start = msg.indexOf(' '); if (start == -1) { me.getChannel().say(prettyPrint(data.admins)); return; } int end = msg.indexOf(' ', start + 1); if (end == -1) { me.getChannel().say("Wrong syntax!"); return; } String subcmd = msg.substring(start + 1, end); if (subcmd.equals("add")) { String nick = msg.substring(end + 1); if (isAdmin(nick)) return; nick = getTrueName(me.getChannel(), nick); data.admins.add(nick); me.getChannel().say("User " + nick + " was added!"); } else if (subcmd.equals("rem")) { String nick = msg.substring(end + 1); if (!isAdmin(nick)) return; nick = getTrueName(me.getChannel(), nick); data.admins.remove(nick); me.getChannel().say("User " + nick + " was removed!"); } else { me.getChannel().say("Wrong syntax!"); } }
private void cmdKill(MessageEvent me) { String msg = me.getMessage(); int start = msg.indexOf(' '); if (start == -1) { me.getChannel().say("Need a nick!"); } me.getChannel().say(msg.substring(start + 1) + " " + "killed!"); }
private void cmdTime(MessageEvent me) { String msg = me.getMessage(); int start = msg.indexOf(' '); if (start == -1) { me.getChannel().say("Need a place!"); } me.getChannel().say(Time.getTime(msg.substring(start + 1))); }
/** Announce command */ private void cmdAnnounce(MessageEvent me) { String msg = me.getMessage(); if (msg.equals(command_char + "announcer start")) { if (announcerRunning) { me.getChannel().say("Announcer is already running!"); return; } announcer = new Timer(); announcer.scheduleAtFixedRate( new TimerTask() { @Override public void run() { channel.say(announcerStr); } }, 0, announcerPeriod); announcerRunning = true; me.getChannel().say("Announcer started!"); } else if (msg.equals(command_char + "announcer stop")) { if (!announcerRunning) { me.getChannel().say("Announcer is not running!"); return; } announcer.cancel(); announcer.purge(); announcerRunning = false; me.getChannel().say("Announcer stopped!"); } else { if (announcerRunning) { me.getChannel().say("Announcer is already running!"); return; } int start = msg.indexOf(' ') + 1; int end = msg.indexOf(' ', start); try { announcerPeriod = Integer.parseInt(msg.substring(start, end)) * 1000; announcerStr = msg.substring(end + 1); me.getChannel() .say( "Announcer configured, use \"" + command_char + "announcer start\" to start annoucing!"); } catch (Exception e) { me.getChannel().say("Wrong syntax!"); } } }
/** Kick command */ private void cmdKick(MessageEvent me) { String msg = me.getMessage(), nick = null, reason = null; int start = msg.indexOf(' ') + 1; if (start == -1) { me.getChannel().say("A username is required!"); return; } int space = msg.indexOf(' ', start); if (space != -1) { nick = msg.substring(start, space); reason = msg.substring(space + 1); } else { nick = msg.substring(start); reason = ""; } me.getChannel().kick(nick, reason); }
/** Language checker */ private void checkLanguage(MessageEvent me, User user) { String msg = me.getMessage().toLowerCase(); int count = 0; for (String word : msg.split(" ")) { for (String badword : data.badWords) { if (word.toLowerCase().equals(badword)) { count++; } } } if (count > 0) { user.warnings++; if (user.warnings == 1) { me.getChannel().say(me.getNick() + ": please watch your language!"); } else if (user.warnings >= 2) { me.getChannel().kick(me.getNick(), "Language"); } } }
/** Away checker */ private void checkAway(MessageEvent me) { String msg = me.getMessage().toLowerCase(); for (int i = 0; i < awayList.size(); i++) { String nick = awayList.get(i); // Check if msg sender is nick if (nick.equals(me.getNick())) { for (int j = 0; j < data.notices.size(); j++) { Notice n = data.notices.get(j); if (n.to.toLowerCase().equals(me.getNick().toLowerCase())) { me.getChannel().say(me.getNick() + ", " + n.from + " says: " + n.msg); data.notices.remove(n); } } awayList.remove(nick); } // Check if nick is contained in msg if (msg.contains(nick.toLowerCase())) { me.getChannel().say(nick + " is currently away."); } } }
/** Greetings command */ private void cmdGreetings(MessageEvent me) { String msg = me.getMessage(); int start = msg.indexOf(' '); if (start == -1) { me.getChannel().say(prettyPrint(data.greetings)); return; } int end = msg.indexOf(' ', start + 1); if (end == -1) { me.getChannel().say("Wrong syntax! 1"); return; } String subcmd = msg.substring(start + 1, end); if (subcmd.equals("add")) { data.greetings.add(msg.substring(end + 1)); } else if (subcmd.equals("rem")) { data.greetings.remove(msg.substring(end + 1)); } else { me.getChannel().say("Wrong syntax!"); } }
/** Notice command */ private void cmdNotice(MessageEvent me) { String msg = me.getMessage(); int start = msg.indexOf(' '); if (start == -1) { me.getChannel().say("There are " + data.notices.size() + " notice(s) waiting!"); return; } int end = msg.indexOf(' ', start + 1); if (end == -1) { me.getChannel().say("Wrong syntax!"); return; } Notice n = new Notice(); n.to = msg.substring(start + 1, end); if (getTrueName(me.getChannel(), n.to) != null && !isAway(n.to)) { me.getChannel().say(getTrueName(me.getChannel(), n.to) + " is present and not away!"); return; } n.from = me.getNick(); n.msg = msg.substring(end + 1); data.notices.add(n); me.getChannel().say("Notice added!"); }
@Override /** Callback whenever the Bot receives data. */ public void receiveEvent(IRCEvent e) { Type type = e.getType(); // Connects to server successfully if (type == Type.CONNECT_COMPLETE) { e.getSession().join(data.channel); // Connects to channel successfully } else if (type == Type.JOIN_COMPLETE) { channel = ((JoinCompleteEvent) e).getChannel(); e.getSession().sayPrivate("nickserv", "identify " + data.botPassword); // User joins channel } else if (type == Type.JOIN) { JoinEvent je = (JoinEvent) e; int x = new Random().nextInt(data.greetings.size()); je.getChannel().say(String.format(data.greetings.get(x), je.getNick())); for (int i = 0; i < data.notices.size(); i++) { Notice n = data.notices.get(i); if (n.to.toLowerCase().equals(je.getNick().toLowerCase())) { je.getChannel().say(je.getNick() + ", " + n.from + " says: " + n.msg); data.notices.remove(n); } } // User leaves channel } else if (type == Type.QUIT) { QuitEvent qe = (QuitEvent) e; users.remove(qe.getNick()); // Receives message in channel } else if (type == Type.CHANNEL_MESSAGE) { MessageEvent me = (MessageEvent) e; String msg = me.getMessage(); User user = loadUser(me.getNick()); boolean found = true; if (isCommand(msg)) { if (isAdmin(me.getNick())) { String cmd = getCommand(msg); if (cmd.equals("help")) cmdHelp(me); else if (cmd.equals("kick")) cmdKick(me); else if (cmd.equals("admins")) cmdAdmins(me); else if (cmd.equals("roll")) cmdRoll(me); else if (cmd.equals("announcer")) cmdAnnounce(me); else if (cmd.equals("badwords")) cmdBadWord(me); else if (cmd.equals("greetings")) cmdGreetings(me); else if (cmd.equals("quit")) cmdQuit(); else if (cmd.equals("save")) cmdSave(); else if (cmd.equals("notice")) cmdNotice(me); else if (cmd.equals("kill")) cmdKill(me); else if (cmd.equals("time")) cmdTime(me); else found = false; } String cmd = getCommand(msg); if (cmd.equals("away")) cmdAway(me); else if (!found) me.getChannel().say("Unknown command."); } if (me.getMessage().contains("lol")) { me.getChannel().say("lol"); } else { checkLanguage(me, user); checkAway(me); } } }