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.

DefineCommand.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.commands;
  23. import com.md87.charliebravo.Command;
  24. import com.md87.charliebravo.Followup;
  25. import com.md87.charliebravo.InputHandler;
  26. import com.md87.charliebravo.Response;
  27. import java.io.BufferedReader;
  28. import java.io.IOException;
  29. import java.io.InputStreamReader;
  30. import java.net.MalformedURLException;
  31. import java.net.URL;
  32. import java.net.URLConnection;
  33. import java.net.URLEncoder;
  34. import java.nio.charset.Charset;
  35. import org.json.JSONArray;
  36. import org.json.JSONException;
  37. import org.json.JSONObject;
  38. /**
  39. *
  40. * @author chris
  41. */
  42. public class DefineCommand implements Command {
  43. public void execute(final InputHandler handler, Response response, String line) throws MalformedURLException, IOException, JSONException {
  44. URL url = new URL("http://apps.md87.co.uk/services/wiktionary/?query=" +
  45. URLEncoder.encode(line, Charset.defaultCharset().name()));
  46. URLConnection connection = url.openConnection();
  47. connection.addRequestProperty("Referer", "http://chris.smith.name/");
  48. String input;
  49. StringBuilder builder = new StringBuilder();
  50. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  51. while((input = reader.readLine()) != null) {
  52. builder.append(input);
  53. }
  54. JSONObject json = new JSONObject(builder.toString());
  55. if (json.getInt("responseStatus") != 200) {
  56. throw new IOException(json.getString("responseDetails"));
  57. }
  58. if (json.getJSONArray("responseData").length() == 0) {
  59. response.sendMessage("There were no results for '" + line + "'", true);
  60. } else {
  61. final StringBuilder res = new StringBuilder();
  62. res.append("there ");
  63. if (json.getJSONArray("responseData").length() == 1) {
  64. res.append("was 1 match");
  65. } else {
  66. res.append("were ");
  67. res.append(json.getJSONArray("responseData").length());
  68. res.append(" matches");
  69. }
  70. res.append(" for '");
  71. res.append(line);
  72. res.append("'");
  73. if (json.getJSONArray("responseData").length() == 1) {
  74. res.append(". It is ");
  75. } else {
  76. res.append(". Result 1 is ");
  77. }
  78. final String name = json.getJSONArray("responseData").getJSONObject(0).getString("title");
  79. res.append('\'');
  80. res.append(name);
  81. res.append("', which has ");
  82. final int defs = json.getJSONArray("responseData").getJSONObject(0)
  83. .getJSONArray("definitions").length();
  84. res.append(defs);
  85. res.append(" definition");
  86. if (defs != 1) {
  87. res.append("s, the first of which is");
  88. }
  89. res.append(": ");
  90. res.append(json.getJSONArray("responseData").getJSONObject(0)
  91. .getJSONArray("definitions").get(0));
  92. response.sendMessage(res.toString());
  93. response.addFollowup(new NextWordFollowup(json.getJSONArray("responseData"), 1));
  94. response.addFollowup(new NextDefinitionFollowup(json.getJSONArray("responseData")
  95. .getJSONObject(0).getJSONArray("definitions"), 1,
  96. new NextWordFollowup(json.getJSONArray("responseData"), 1)));
  97. }
  98. }
  99. protected static class NextWordFollowup implements Followup {
  100. private final JSONArray words;
  101. private final int next;
  102. public NextWordFollowup(JSONArray words, int next) {
  103. this.words = words;
  104. this.next = next;
  105. }
  106. public boolean matches(String line) {
  107. return next < words.length() && line.startsWith("next word");
  108. }
  109. public void execute(InputHandler handler, Response response, String line) throws Exception {
  110. final StringBuilder res = new StringBuilder();
  111. res.append("result " + (next + 1) + " is ");
  112. final String name = words.getJSONObject(next).getString("title");
  113. res.append('\'');
  114. res.append(name);
  115. res.append("', which has ");
  116. final int defs = words.getJSONObject(next)
  117. .getJSONArray("definitions").length();
  118. res.append(defs);
  119. res.append(" definition");
  120. if (defs != 1) {
  121. res.append("s, the first of which is");
  122. }
  123. res.append(": ");
  124. res.append(words.getJSONObject(next)
  125. .getJSONArray("definitions").get(0));
  126. response.sendMessage(res.toString());
  127. response.addFollowup(new NextWordFollowup(words, next + 1));
  128. response.addFollowup(new NextDefinitionFollowup(words.getJSONObject(next)
  129. .getJSONArray("definitions"), 1, new NextWordFollowup(words, next + 1)));
  130. }
  131. }
  132. protected static class NextDefinitionFollowup implements Followup {
  133. private final JSONArray defs;
  134. private final int next;
  135. private final NextWordFollowup nextword;
  136. public NextDefinitionFollowup(JSONArray defs, int next, NextWordFollowup nextword) {
  137. this.defs = defs;
  138. this.next = next;
  139. this.nextword = nextword;
  140. }
  141. public boolean matches(String line) {
  142. return next < defs.length() && line.startsWith("next definition");
  143. }
  144. public void execute(InputHandler handler, Response response, String line) throws Exception {
  145. final StringBuilder res = new StringBuilder();
  146. res.append("definition " + (next + 1) + " is: ");
  147. res.append(defs.get(next));
  148. response.sendMessage(res.toString());
  149. response.addFollowup(nextword);
  150. response.addFollowup(new NextDefinitionFollowup(defs, next + 1, nextword));
  151. }
  152. }
  153. }