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.

IssueCommand.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.md87.charliebravo.commands;
  6. import com.dmdirc.util.Downloader;
  7. import com.md87.charliebravo.Command;
  8. import com.md87.charliebravo.Followup;
  9. import com.md87.charliebravo.InputHandler;
  10. import com.md87.charliebravo.Response;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.regex.Matcher;
  15. import java.util.regex.Pattern;
  16. /**
  17. *
  18. * @author chris
  19. */
  20. public class IssueCommand implements Command {
  21. public void execute(InputHandler handler, Response response, String line) throws Exception {
  22. if (line.isEmpty() || !line.matches("^[0-9]+$")) {
  23. response.sendMessage("You need to specify an issue number", true);
  24. } else {
  25. final List<String> result = Downloader.getPage("http://bugs.dmdirc.com/view.php?id="
  26. + line);
  27. final StringBuilder builder = new StringBuilder();
  28. for (String resline : result) {
  29. builder.append(resline);
  30. }
  31. if (builder.indexOf("APPLICATION ERROR #1100") > -1) {
  32. response.sendMessage("That issue was not found", true);
  33. } else if (builder.indexOf("<p>Access Denied.</p>") > -1) {
  34. response.sendMessage("that issue is private. Please see "
  35. + "http://bugs.dmdirc.com/view/" + line);
  36. } else {
  37. final Map<String, String> data = new HashMap<String, String>();
  38. final Pattern pattern = Pattern.compile(
  39. "<td class=\"category\".*?>\\s*(.*?)\\s*"
  40. + "</td>\\s*(?:<!--.*?-->\\s*)?<td.*?>\\s*(.*?)\\s*</td>",
  41. Pattern.CASE_INSENSITIVE + Pattern.DOTALL);
  42. final Matcher matcher = pattern.matcher(builder);
  43. while (matcher.find()) {
  44. data.put(matcher.group(1).toLowerCase(), matcher.group(2));
  45. }
  46. response.sendMessage("issue " + data.get("id") + " is \""
  47. + data.get("summary").substring(9) + "\". Current "
  48. + "status is " + data.get("status") + " ("
  49. + data.get("resolution") + "). See http://bugs.dmdirc.com/view/"
  50. + data.get("id"));
  51. response.addFollowup(new IssueFollowup(data));
  52. }
  53. }
  54. }
  55. protected static class IssueFollowup implements Followup {
  56. private final Map<String, String> data;
  57. public IssueFollowup(Map<String, String> data) {
  58. this.data = data;
  59. }
  60. public boolean matches(String line) {
  61. return data.containsKey(line.toLowerCase());
  62. }
  63. public void execute(InputHandler handler, Response response, String line) throws Exception {
  64. response.setInheritFollows(true);
  65. response.sendMessage("the " + line.toLowerCase() + " of issue "
  66. + data.get("id") + " is: " + data.get(line.toLowerCase()));
  67. }
  68. }
  69. }