Java IRC bot
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

IssueCommand.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.dmdirc.util.Downloader;
  24. import com.md87.charliebravo.Command;
  25. import com.md87.charliebravo.Followup;
  26. import com.md87.charliebravo.InputHandler;
  27. import com.md87.charliebravo.Response;
  28. import java.util.HashMap;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.regex.Matcher;
  32. import java.util.regex.Pattern;
  33. /**
  34. *
  35. * @author chris
  36. */
  37. public class IssueCommand implements Command {
  38. public void execute(InputHandler handler, Response response, String line) throws Exception {
  39. if (line.isEmpty() || !line.matches("^[0-9]+$")) {
  40. response.sendMessage("You need to specify an issue number", true);
  41. } else {
  42. final List<String> result = Downloader.getPage("http://bugs.dmdirc.com/view.php?id="
  43. + line);
  44. final StringBuilder builder = new StringBuilder();
  45. for (String resline : result) {
  46. builder.append(resline);
  47. }
  48. if (builder.indexOf("APPLICATION ERROR #1100") > -1) {
  49. response.sendMessage("That issue was not found", true);
  50. } else if (builder.indexOf("<p>Access Denied.</p>") > -1) {
  51. response.sendMessage("that issue is private. Please see "
  52. + "http://bugs.dmdirc.com/view/" + line);
  53. } else {
  54. final Map<String, String> data = new HashMap<String, String>();
  55. final Pattern pattern = Pattern.compile(
  56. "<td class=\"category\".*?>\\s*(.*?)\\s*"
  57. + "</td>\\s*(?:<!--.*?-->\\s*)?<td.*?>\\s*(.*?)\\s*</td>",
  58. Pattern.CASE_INSENSITIVE + Pattern.DOTALL);
  59. final Matcher matcher = pattern.matcher(builder);
  60. while (matcher.find()) {
  61. data.put(matcher.group(1).toLowerCase(), matcher.group(2));
  62. }
  63. response.sendMessage("issue " + data.get("id") + " is \""
  64. + data.get("summary").substring(9) + "\". Current "
  65. + "status is " + data.get("status") + " ("
  66. + data.get("resolution") + "). See http://bugs.dmdirc.com/view/"
  67. + data.get("id"));
  68. response.addFollowup(new IssueFollowup(data));
  69. }
  70. }
  71. }
  72. protected static class IssueFollowup implements Followup {
  73. private final Map<String, String> data;
  74. public IssueFollowup(Map<String, String> data) {
  75. this.data = data;
  76. }
  77. public boolean matches(String line) {
  78. return data.containsKey(line.toLowerCase());
  79. }
  80. public void execute(InputHandler handler, Response response, String line) throws Exception {
  81. response.setInheritFollows(true);
  82. response.sendMessage("the " + line.toLowerCase() + " of issue "
  83. + data.get("id") + " is: " + data.get(line.toLowerCase()));
  84. }
  85. }
  86. }