Java IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InputHandler.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.md87.charliebravo;
  6. import com.dmdirc.parser.irc.ChannelClientInfo;
  7. import com.dmdirc.parser.irc.ChannelInfo;
  8. import com.dmdirc.parser.irc.ClientInfo;
  9. import com.dmdirc.parser.irc.IRCParser;
  10. import com.dmdirc.parser.irc.callbacks.interfaces.IChannelMessage;
  11. import com.dmdirc.parser.irc.callbacks.interfaces.IPrivateMessage;
  12. import com.md87.charliebravo.commands.AuthenticateCommand;
  13. import com.md87.charliebravo.commands.CalcCommand;
  14. import com.md87.charliebravo.commands.FollowupsCommand;
  15. import com.md87.charliebravo.commands.GitCommand;
  16. import com.md87.charliebravo.commands.GoogleCommand;
  17. import com.md87.charliebravo.commands.HelpCommand;
  18. import com.md87.charliebravo.commands.IssueCommand;
  19. import com.md87.charliebravo.commands.QuitCommand;
  20. import com.md87.charliebravo.commands.SetCommand;
  21. import com.md87.charliebravo.commands.SkillCommand;
  22. import com.md87.charliebravo.commands.TranslateCommand;
  23. import com.md87.charliebravo.commands.WhoisCommand;
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.regex.Matcher;
  29. /**
  30. *
  31. * @author chris
  32. */
  33. public class InputHandler implements IChannelMessage, IPrivateMessage {
  34. protected IRCParser parser;
  35. protected final Config config;
  36. protected final List<Command> commands = new ArrayList<Command>();
  37. protected final Map<String, Response> responses = new HashMap<String, Response>();
  38. public InputHandler(final Config config) {
  39. this.config = config;
  40. commands.add(new GoogleCommand());
  41. commands.add(new QuitCommand());
  42. commands.add(new HelpCommand());
  43. commands.add(new FollowupsCommand());
  44. commands.add(new AuthenticateCommand());
  45. commands.add(new CalcCommand());
  46. commands.add(new WhoisCommand());
  47. commands.add(new TranslateCommand());
  48. commands.add(new IssueCommand());
  49. commands.add(new GitCommand());
  50. commands.add(new SetCommand());
  51. commands.add(new SkillCommand());
  52. }
  53. public Config getConfig() {
  54. return config;
  55. }
  56. public List<Command> getCommands() {
  57. return new ArrayList<Command>(commands);
  58. }
  59. public Response getLastResponse(final String target) {
  60. return responses.get(target);
  61. }
  62. public IRCParser getParser() {
  63. return parser;
  64. }
  65. public void setParser(final IRCParser parser) {
  66. this.parser = parser;
  67. parser.getCallbackManager().addCallback("OnChannelMessage", this);
  68. parser.getCallbackManager().addCallback("OnPrivateMessage", this);
  69. }
  70. public void onChannelMessage(final IRCParser tParser, final ChannelInfo cChannel,
  71. final ChannelClientInfo cChannelClient, final String sMessage, final String sHost) {
  72. if (sMessage.matches("^" + Matcher.quoteReplacement(tParser.getMyNickname()) + "[,:!] .*")) {
  73. handleInput(ClientInfo.parseHost(sHost), cChannel.getName(),
  74. sMessage.substring(tParser.getMyNickname().length() + 2));
  75. }
  76. }
  77. public void onPrivateMessage(final IRCParser tParser, final String sMessage, final String sHost) {
  78. handleInput(ClientInfo.parseHost(sHost), ClientInfo.parseHost(sHost), sMessage);
  79. }
  80. protected void handleInput(final String source, final String target, final String text) {
  81. new Thread(new Runnable() {
  82. public void run() {
  83. handleInputImpl(source, target, text);
  84. }
  85. }).start();
  86. }
  87. protected void handleInputImpl(final String source, final String target, final String text) {
  88. final Response response = new Response(parser, source, target);
  89. Command command = null;
  90. int index = 0;
  91. try {
  92. if (responses.containsKey(target)) {
  93. for (Followup followup : responses.get(target).getFollowups()) {
  94. if (followup.matches(text)) {
  95. command = followup;
  96. }
  97. }
  98. }
  99. if (command == null) {
  100. for (Command pcommand : commands) {
  101. if (text.toLowerCase().startsWith(pcommand.getClass()
  102. .getSimpleName().replace("Command", "").toLowerCase())) {
  103. command = pcommand;
  104. index = pcommand.getClass().getSimpleName().length() - 6;
  105. }
  106. }
  107. }
  108. if (command != null) {
  109. boolean cont = true;
  110. if (command.getClass().isAnnotationPresent(CommandOptions.class)) {
  111. final CommandOptions opts = command.getClass()
  112. .getAnnotation(CommandOptions.class);
  113. final String id = (String) parser.getClientInfoOrFake(source)
  114. .getMap().get("OpenID");
  115. if (opts.requireAuthorisation() && id == null) {
  116. response.sendMessage("You must be authorised to use that command", true);
  117. cont = false;
  118. } else if (opts.requireLevel() > -1 &&
  119. (!config.hasOption(id, "admin.level")
  120. || (Integer.valueOf(config.getOption(id, "admin.level"))
  121. < opts.requireLevel()))) {
  122. response.sendMessage("You have insufficient access to " +
  123. "use that command", true);
  124. response.addFollowup(new LevelErrorFollowup(response.getSource(),
  125. opts.requireLevel(),
  126. config.hasOption(id, "admin.level")
  127. ? Integer.valueOf(config.getOption(id, "admin.level")) : -1));
  128. cont = false;
  129. } else {
  130. int count = 0;
  131. final StringBuilder missing = new StringBuilder();
  132. for (String setting : opts.requiredSettings()) {
  133. if (!config.hasOption(id, setting)) {
  134. if (missing.length() > 0) {
  135. missing.append(", ");
  136. }
  137. count++;
  138. missing.append(setting);
  139. }
  140. }
  141. if (count > 0) {
  142. cont = false;
  143. response.sendRawMessage("You must have the following setting"
  144. + (count == 1 ? "" : "s")
  145. + " in order to use that command, " + response.getSource()
  146. + ": " + missing.toString()
  147. .replaceAll("^(.*), (.*?)$", "$1 and $2") + ".");
  148. }
  149. }
  150. }
  151. if (cont) {
  152. command.execute(this, response, text.substring(Math.min(text.length(), index)));
  153. }
  154. }
  155. } catch (Throwable ex) {
  156. response.sendMessage("an error has occured: " + ex.getMessage());
  157. response.addFollowup(new StacktraceFollowup(ex));
  158. }
  159. if (command != null) {
  160. if (response.isInheritFollows() && responses.containsKey(target)) {
  161. response.addFollowups(responses.get(target).getFollowups());
  162. }
  163. responses.put(target, response);
  164. }
  165. }
  166. protected static class LevelErrorFollowup implements Followup {
  167. private final String source;
  168. private final int required, desired;
  169. public LevelErrorFollowup(String source, int required, int desired) {
  170. this.source = source;
  171. this.required = required;
  172. this.desired = desired;
  173. }
  174. public boolean matches(String line) {
  175. return line.equalsIgnoreCase("details");
  176. }
  177. public void execute(InputHandler handler, Response response, String line) throws Exception {
  178. final boolean you = response.getSource().equals(source);
  179. response.sendMessage("that command requires level " + required
  180. + " access, but " + (you ? "you" : source) + " "
  181. + (desired == -1 ? "do" + (you ? "" : "es") + " not have any assigned level"
  182. : "only ha" + (you ? "ve" : "s") + " level " + desired));
  183. }
  184. }
  185. protected static class StacktraceFollowup implements Followup {
  186. private final Throwable ex;
  187. private final int index;
  188. public StacktraceFollowup(Throwable ex) {
  189. this(ex, 0);
  190. }
  191. public StacktraceFollowup(Throwable ex, int index) {
  192. this.ex = ex;
  193. this.index = index;
  194. }
  195. public boolean matches(String line) {
  196. return index == 0 ? line.equals("stacktrace") : line.equals("more");
  197. }
  198. public void execute(final InputHandler handler, Response response, String line) throws Exception {
  199. StringBuilder trace = new StringBuilder();
  200. int i;
  201. for (i = index; i < ex.getStackTrace().length; i++) {
  202. if (trace.length() > 0) {
  203. int next = ex.getStackTrace()[i].toString().length() + 2;
  204. if (trace.length() + next + 33 + (index == 0 ? 5 : 4)
  205. + String.valueOf(i - index).length() > response.getMaxLength()) {
  206. break;
  207. }
  208. trace.append("; ");
  209. }
  210. trace.append(ex.getStackTrace()[i].toString());
  211. }
  212. response.sendMessage("the " + (index == 0 ? "first" :
  213. i < ex.getStackTrace().length ? "next" : "last")
  214. + " " + (i - index) + " elements of the trace are: " + trace);
  215. if (i < ex.getStackTrace().length) {
  216. response.addFollowup(new StacktraceFollowup(ex, i));
  217. }
  218. }
  219. }
  220. }