[Hattrickirc-commitlog] HattrickIRC/src/org/hattrick/hattrickirc/bot HattrickBot.java, 1.6, 1.7 Me
Status: Beta
Brought to you by:
suls
From: Mathias S. <su...@us...> - 2006-10-01 17:41:14
|
Update of /cvsroot/hattrickirc/HattrickIRC/src/org/hattrick/hattrickirc/bot In directory sc8-pr-cvs5.sourceforge.net:/tmp/cvs-serv20808/src/org/hattrick/hattrickirc/bot Modified Files: HattrickBot.java Messenger.java Log Message: Changes from Boy van der Werf (nethyperon) Index: HattrickBot.java =================================================================== RCS file: /cvsroot/hattrickirc/HattrickIRC/src/org/hattrick/hattrickirc/bot/HattrickBot.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- HattrickBot.java 8 Jul 2005 13:57:28 -0000 1.6 +++ HattrickBot.java 1 Oct 2006 17:41:07 -0000 1.7 @@ -24,6 +24,7 @@ */ package org.hattrick.hattrickirc.bot; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -33,6 +34,7 @@ import org.hattrick.hattrickirc.HattrickIrc; import org.hattrick.hattrickirc.config.BotConfig; import org.hattrick.hattrickirc.config.IrcConfig; +import org.hattrick.hattrickirc.data.Event; import org.hattrick.hattrickirc.data.Match; import org.jibble.pircbot.PircBot; @@ -68,6 +70,8 @@ } joinChannel(ircConfig.getChannel()); + + operators.addAll(botConfig.getOperators()); } public Messenger getMessenger() { @@ -75,93 +79,177 @@ } protected void onMessage(String channel, String sender, String login, String hostname, String message) { - - // everybody is allowed to perform these commands - // about - if(message.startsWith(botConfig.getPrefix() + "about")) { - sendMessage(ircConfig.getChannel(), "HattrickIRC Copyright (C) 2005 - Mathias Sulser <su...@su...>"); - sendMessage(ircConfig.getChannel(), "This application uses information from the online game service Hattrick.org." + - " This use has been approved by the developers and copyright owners of Hattrick.org, Extralives AB"); - } - - // only the owner and operators are allowed to perform these commands - if(sender.equalsIgnoreCase(botConfig.getOwner()) || operators.contains(sender)) { - // adds a match - if(message.startsWith(botConfig.getPrefix() + "add")) { - int matchId; + if (message.startsWith(botConfig.getPrefix())) { + String commandString = message.substring(botConfig.getPrefix().length()); + String[] commands = commandString.split(" "); + if (commands.length > 0) { try { - matchId = getMatchId(message); - Match match = HattrickIrc.getInstance().getMatchManager().addMatch(matchId); - logger.info("Match added: " + match.getHomeTeam().getTeamName() + " - " + match.getAwayTeam().getTeamName()); + Method method = this.getClass().getDeclaredMethod(commands[0], new Class[] {String.class, String[].class}); + String[] args = new String[commands.length - 1]; + for (int i = 1; i < commands.length; i++) { + args[i-1] = commands[i]; + } + method.invoke(this, new Object[] {sender, args}); } catch (Exception e) { - logger.error("couldn't add match: ", e); + return; } } - - // deletes a match - if(message.startsWith(botConfig.getPrefix() + "del")) { - int matchId; - try { - matchId = getMatchId(message); - Match match = HattrickIrc.getInstance().getMatchManager().removeMatch(matchId); - logger.info("Match removed: " + match.getHomeTeam().getTeamName() + " - " + match.getAwayTeam().getTeamName()); - } catch (Exception e) { - logger.error("couldn't remove match: ", e); + } + } + + protected void about(String sender, String[] args) { + sendMessage(ircConfig.getChannel(), "HattrickIRC Copyright (C) 2005 - Mathias Sulser <su...@su...>"); + sendMessage(ircConfig.getChannel(), "This application uses information from the online game service Hattrick.org." + + " This use has been approved by the developers and copyright owners of Hattrick.org, Extralives AB"); + } + + protected void add(String sender, String[] args) { + if (args.length > 0 && isOperator(sender)) { + try { + Match match = HattrickIrc.getInstance().getMatchManager().addMatch(args[0]); + if (match != null) { + logger.info("Match added: " + match.getHomeTeam().getTeamName() + " - " + match.getAwayTeam().getTeamName()); + info(sender, new String[] {args[0]}); + } else { + sendMessage(ircConfig.getChannel(), "Invalid match ID: " + args[0]); + logger.warn("Invalid match ID: " + args[0]); } + } catch (Exception e) { + logger.error("Couldn't add match: ", e); } - - // info - if(message.startsWith(botConfig.getPrefix() + "info")) { + } + } + + protected void del(String sender, String[] args) { + if (args.length > 0 && isOperator(sender)) { + try { + Match match = HattrickIrc.getInstance().getMatchManager().removeMatch(args[0]); + logger.info("Match removed: " + match.getHomeTeam().getTeamName() + " - " + match.getAwayTeam().getTeamName()); + } catch (Exception e) { + logger.error("couldn't remove match: ", e); + } + } + } + + protected void info(String sender, String[] args) { + if (isOperator(sender)) { + if (args.length > 0) { + Match currentMatch = HattrickIrc.getInstance().getMatchManager().getMatch(args[0]); + StringBuffer msgText = new StringBuffer(); + msgText.append(currentMatch.getMinute()); + msgText.append("': "); + msgText.append(currentMatch.getHomeTeam().getTeamName()); + msgText.append(" - "); + msgText.append(currentMatch.getAwayTeam().getTeamName()); + msgText.append(" "); + msgText.append(currentMatch.getHomeGoals()); + msgText.append(" - "); + msgText.append(currentMatch.getAwayGoals()); + + sendMessage(ircConfig.getChannel(), msgText.toString()); + } else { Collection matchColl = HattrickIrc.getInstance().getMatchManager().getMatches().values(); for(Iterator iter = matchColl.iterator(); iter.hasNext(); ) { Match currentMatch = (Match) iter.next(); - sendMessage(ircConfig.getChannel(), currentMatch.getHomeTeam().getTeamName() + - " - " + currentMatch.getAwayTeam().getTeamName() + - " " + currentMatch.getHomeGoals() + - " - " + currentMatch.getAwayGoals()); - } + StringBuffer msgText = new StringBuffer(); + msgText.append(currentMatch.getMinute()); + msgText.append("': "); + msgText.append(currentMatch.getHomeTeam().getTeamName()); + msgText.append(" - "); + msgText.append(currentMatch.getAwayTeam().getTeamName()); + msgText.append(" "); + msgText.append(currentMatch.getHomeGoals()); + msgText.append(" - "); + msgText.append(currentMatch.getAwayGoals()); + + sendMessage(ircConfig.getChannel(), msgText.toString()); + } } } - - // only the owner is allowed to perform these commands - if(sender.equalsIgnoreCase(botConfig.getOwner())) { - // exit - if(message.startsWith(botConfig.getPrefix() + "exit")) { - HattrickIrc.getInstance().getConnection().logout(); - } - - // operatoradd - if(message.startsWith(botConfig.getPrefix() + "operatoradd") && sender.equalsIgnoreCase(botConfig.getOwner())) { - operators.add(getNickName("operatoradd", message)); - logger.info("operator added: " + getNickName("operatoradd", message)); + } + + protected void allinfo(String sender, String[] args) { + if (isOperator(sender)) { + Collection matchColl = HattrickIrc.getInstance().getMatchManager().getMatches().values(); + for(Iterator iter = matchColl.iterator(); iter.hasNext(); ) { + Match currentMatch = (Match) iter.next(); + StringBuffer msgText = new StringBuffer(); + msgText.append("[").append(currentMatch.getMatchId()).append("] "); + msgText.append(currentMatch.getMinute()); + msgText.append("': "); + msgText.append(currentMatch.getHomeTeam().getTeamName()); + msgText.append(" - "); + msgText.append(currentMatch.getAwayTeam().getTeamName()); + msgText.append(" "); + msgText.append(currentMatch.getHomeGoals()); + msgText.append(" - "); + msgText.append(currentMatch.getAwayGoals()); + msgText.append(" (Events: "); + msgText.append(currentMatch.getEvents().size()); + msgText.append(")"); + + sendMessage(ircConfig.getChannel(), msgText.toString()); } - - // operatordel - if(message.startsWith(botConfig.getPrefix() + "operatordel") && sender.equalsIgnoreCase(botConfig.getOwner())) { - operators.remove(getNickName("operatordel", message)); - logger.info("operator removed: " + getNickName("operatordel", message)); + } + } + + protected void report(String sender, String[] args) { + if (args.length > 0 && isOperator(sender)) { + Match currentMatch = HattrickIrc.getInstance().getMatchManager().getMatch(args[0]); + for(Iterator iter = currentMatch.getEvents().iterator(); iter.hasNext(); ) { + Event event = (Event) iter.next(); + sendMessage(ircConfig.getChannel(), event.getEventText(currentMatch)); } - - // botaccess - if(message.startsWith(botConfig.getPrefix() + "botaccess") && sender.equalsIgnoreCase(botConfig.getOwner())) { - StringBuffer buffer = new StringBuffer(); - for(Iterator iter = operators.iterator(); iter.hasNext(); ) { - buffer.append(iter.next() + " "); + } + } + + protected void exit(String sender, String[] args) { + if (isOwner(sender)) { + HattrickIrc.getInstance().getConnection().logout(); + } + } + + protected void operatoradd(String sender, String[] args) { + if (args.length > 0 && isOwner(sender)) { + operators.add(args[0]); + logger.info("operator added: " + args[0]); + } + } + + protected void operatordel(String sender, String[] args) { + if (args.length > 0 && isOwner(sender)) { + operators.remove(args[0]); + logger.info("operator removed: " + args[0]); + } + } + + protected void botaccess(String sender, String[] args) { + if (isOwner(sender)) { + sendMessage(getIrcConfig().getChannel(), "Owner: " + botConfig.getOwner()); + StringBuffer buffer = new StringBuffer(); + for(Iterator iter = operators.iterator(); iter.hasNext(); ) { + buffer.append(iter.next()); + if (iter.hasNext()) { + buffer.append(", "); } - sendMessage(ircConfig.getChannel(), "Owner: " + botConfig.getOwner() + " Operators: " + buffer.toString()); } + sendMessage(getIrcConfig().getChannel(), "Operators: " + buffer.toString()); } } - - private int getMatchId(String msg) throws Exception { - msg = msg.substring(botConfig.getPrefix().length() + 4); - if(msg.length() != 8) { - throw new Exception("incorrect matchid: " + msg); - } - return Integer.parseInt(msg); + + protected final boolean isOwner(String sender) { + return sender.equalsIgnoreCase(botConfig.getOwner()); } - private String getNickName(String cmd, String msg) { - return msg.substring(botConfig.getPrefix().length() + cmd.length() + 1); + protected final boolean isOperator(String sender) { + return (isOwner(sender) || operators.contains(sender)); + } + + public IrcConfig getIrcConfig() { + return this.ircConfig; + } + + public BotConfig getBotConfig() { + return this.botConfig; } } Index: Messenger.java =================================================================== RCS file: /cvsroot/hattrickirc/HattrickIRC/src/org/hattrick/hattrickirc/bot/Messenger.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- Messenger.java 8 Jul 2005 12:44:09 -0000 1.5 +++ Messenger.java 1 Oct 2006 17:41:07 -0000 1.6 @@ -24,12 +24,6 @@ */ package org.hattrick.hattrickirc.bot; -import java.util.Iterator; -import java.util.Map; - -import org.hattrick.hattrickirc.HattrickIrc; -import org.hattrick.hattrickirc.config.BotConfig; -import org.hattrick.hattrickirc.config.IrcConfig; import org.hattrick.hattrickirc.data.Event; import org.hattrick.hattrickirc.data.Match; @@ -41,16 +35,14 @@ private HattrickBot bot; private boolean output = false; - private IrcConfig ircConfig = HattrickIrc.getInstance().getConfig().getIrcConfig(); - private BotConfig botConfig = HattrickIrc.getInstance().getConfig().getBotConfig(); public Messenger(HattrickBot bot) { this.bot = bot; } - public void send(Event event) { + public void send(Match match, Event event) { if(output && EventFilter.isAllowed(event)) { - bot.sendMessage(ircConfig.getChannel(), event.getEventText()); + bot.sendMessage(bot.getIrcConfig().getChannel(), event.getEventText(match)); } } @@ -58,23 +50,4 @@ this.output = output; } - /** - * @param registeredMatches - */ - public void newTopic(Map registeredMatches) { - // set a new topic if the option is enabled and at least one - // match is added to the list - if(botConfig.isChangeTopic() && !registeredMatches.isEmpty()) { - StringBuffer topic = new StringBuffer(); - for(Iterator iter = registeredMatches.values().iterator(); iter.hasNext(); ) { - Match currentMatch = (Match) iter.next(); - topic.append(currentMatch.getHomeTeam().getTeamName() + " " - + currentMatch.getHomeGoals() + " : " - + currentMatch.getAwayGoals() + " " - + currentMatch.getAwayTeam().getTeamName() + " " - ); - } - bot.setTopic(ircConfig.getChannel(), topic.toString()); - } - } } |