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.

Response.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.md87.charliebravo;
  23. import com.dmdirc.parser.irc.IRCParser;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. /**
  27. *
  28. * @author chris
  29. */
  30. public class Response {
  31. private final IRCParser parser;
  32. private final String source, target;
  33. private final List<Followup> followups = new ArrayList<Followup>();
  34. private boolean inheritFollows = false;
  35. public Response(IRCParser parser, String source, String target) {
  36. this.parser = parser;
  37. this.source = source;
  38. this.target = target;
  39. }
  40. public String getSource() {
  41. return source;
  42. }
  43. public String getTarget() {
  44. return target;
  45. }
  46. public void sendMessage(final String message) {
  47. sendMessage(message, false);
  48. }
  49. public void sendMessage(final String message, final boolean suffix) {
  50. final String line = (suffix ? "" : source + ", ")
  51. + message + (suffix ? ", " + source : "") + ".";
  52. sendRawMessage(line);
  53. }
  54. public void sendRawMessage(final String line) {
  55. parser.sendMessage(target, line);
  56. for (Followup followup : new ArrayList<Followup>(followups)) {
  57. if (followup instanceof RepeatFollowup) {
  58. followups.remove(followup);
  59. }
  60. }
  61. followups.add(new RepeatFollowup(line));
  62. }
  63. public void addFollowup(final Followup followup) {
  64. followups.add(followup);
  65. }
  66. public List<Followup> getFollowups() {
  67. return followups;
  68. }
  69. public int getMaxLength() {
  70. return parser.getMaxLength("PRIVMSG", target) - source.length() - 3;
  71. }
  72. public boolean isInheritFollows() {
  73. return inheritFollows;
  74. }
  75. public void setInheritFollows(boolean inheritFollows) {
  76. this.inheritFollows = inheritFollows;
  77. }
  78. public void addFollowups(List<Followup> followups) {
  79. this.followups.addAll(followups);
  80. }
  81. protected static class RepeatFollowup implements Followup {
  82. private final String myLine;
  83. public RepeatFollowup(String myLine) {
  84. this.myLine = myLine;
  85. }
  86. public boolean matches(String line) {
  87. return line.startsWith("repeat") || line.equals("say again");
  88. }
  89. public void execute(InputHandler handler, Response response, String line) throws Exception {
  90. response.setInheritFollows(true);
  91. response.sendRawMessage(myLine);
  92. }
  93. }
  94. }