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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.TranslateCommand;
  22. import com.md87.charliebravo.commands.WhoisCommand;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. /**
  28. *
  29. * @author chris
  30. */
  31. public class InputHandler implements IChannelMessage, IPrivateMessage {
  32. protected IRCParser parser;
  33. protected final Config config;
  34. protected final List<Command> commands = new ArrayList<Command>();
  35. protected final Map<String, Response> responses = new HashMap<String, Response>();
  36. public InputHandler(final Config config) {
  37. this.config = config;
  38. commands.add(new GoogleCommand());
  39. commands.add(new QuitCommand());
  40. commands.add(new HelpCommand());
  41. commands.add(new FollowupsCommand());
  42. commands.add(new AuthenticateCommand());
  43. commands.add(new CalcCommand());
  44. commands.add(new WhoisCommand());
  45. commands.add(new TranslateCommand());
  46. commands.add(new IssueCommand());
  47. commands.add(new GitCommand());
  48. commands.add(new SetCommand());
  49. }
  50. public Config getConfig() {
  51. return config;
  52. }
  53. public List<Command> getCommands() {
  54. return new ArrayList<Command>(commands);
  55. }
  56. public Response getLastResponse(final String target) {
  57. return responses.get(target);
  58. }
  59. public IRCParser getParser() {
  60. return parser;
  61. }
  62. public void setParser(final IRCParser parser) {
  63. this.parser = parser;
  64. parser.getCallbackManager().addCallback("OnChannelMessage", this);
  65. parser.getCallbackManager().addCallback("OnPrivateMessage", this);
  66. }
  67. public void onChannelMessage(final IRCParser tParser, final ChannelInfo cChannel,
  68. final ChannelClientInfo cChannelClient, final String sMessage, final String sHost) {
  69. if (sMessage.startsWith(tParser.getMyNickname() + ", ")) {
  70. handleInput(ClientInfo.parseHost(sHost), cChannel.getName(),
  71. sMessage.substring((tParser.getMyNickname() + ", ").length()));
  72. }
  73. }
  74. public void onPrivateMessage(final IRCParser tParser, final String sMessage, final String sHost) {
  75. handleInput(ClientInfo.parseHost(sHost), ClientInfo.parseHost(sHost), sMessage);
  76. }
  77. protected void handleInput(final String source, final String target, final String text) {
  78. final Response response = new Response(parser, source, target);
  79. Command command = null;
  80. int index = 0;
  81. try {
  82. if (responses.containsKey(target)) {
  83. for (Followup followup : responses.get(target).getFollowups()) {
  84. if (followup.matches(text)) {
  85. command = followup;
  86. }
  87. }
  88. }
  89. if (command == null) {
  90. for (Command pcommand : commands) {
  91. if (text.toLowerCase().startsWith(pcommand.getClass()
  92. .getSimpleName().replace("Command", "").toLowerCase())) {
  93. command = pcommand;
  94. index = pcommand.getClass().getSimpleName().length() - 6;
  95. }
  96. }
  97. }
  98. if (command != null) {
  99. command.execute(this, response, text.substring(Math.min(text.length(), index)));
  100. }
  101. } catch (Throwable ex) {
  102. response.sendMessage("an error has occured: " + ex.getMessage());
  103. response.addFollowup(new StacktraceFollowup(ex));
  104. }
  105. if (command != null) {
  106. if (response.isInheritFollows() && responses.containsKey(target)) {
  107. response.addFollowups(responses.get(target).getFollowups());
  108. }
  109. responses.put(target, response);
  110. }
  111. }
  112. protected static class StacktraceFollowup implements Followup {
  113. private final Throwable ex;
  114. private final int index;
  115. public StacktraceFollowup(Throwable ex) {
  116. this(ex, 0);
  117. }
  118. public StacktraceFollowup(Throwable ex, int index) {
  119. this.ex = ex;
  120. this.index = index;
  121. }
  122. public boolean matches(String line) {
  123. return index == 0 ? line.equals("stacktrace") : line.equals("more");
  124. }
  125. public void execute(final InputHandler handler, Response response, String line) throws Exception {
  126. StringBuilder trace = new StringBuilder();
  127. int i;
  128. for (i = index; i < ex.getStackTrace().length; i++) {
  129. if (trace.length() > 0) {
  130. int next = ex.getStackTrace()[i].toString().length() + 2;
  131. if (trace.length() + next + 33 + (index == 0 ? 5 : 4)
  132. + String.valueOf(i - index).length() > response.getMaxLength()) {
  133. break;
  134. }
  135. trace.append("; ");
  136. }
  137. trace.append(ex.getStackTrace()[i].toString());
  138. }
  139. response.sendMessage("the " + (index == 0 ? "first" :
  140. i < ex.getStackTrace().length ? "next" : "last")
  141. + " " + (i - index) + " elements of the trace are: " + trace);
  142. if (i < ex.getStackTrace().length) {
  143. response.addFollowup(new StacktraceFollowup(ex, i));
  144. }
  145. }
  146. }
  147. }