|
@@ -0,0 +1,140 @@
|
|
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
|
+
|
|
23
|
+package com.md87.charliebravo.commands;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.util.Downloader;
|
|
26
|
+import com.md87.charliebravo.Command;
|
|
27
|
+import com.md87.charliebravo.Followup;
|
|
28
|
+import com.md87.charliebravo.InputHandler;
|
|
29
|
+import com.md87.charliebravo.Response;
|
|
30
|
+import java.net.URLEncoder;
|
|
31
|
+import java.nio.charset.Charset;
|
|
32
|
+import java.util.ArrayList;
|
|
33
|
+import java.util.List;
|
|
34
|
+import java.util.regex.Matcher;
|
|
35
|
+import java.util.regex.Pattern;
|
|
36
|
+
|
|
37
|
+/**
|
|
38
|
+ *
|
|
39
|
+ * @author chris
|
|
40
|
+ */
|
|
41
|
+public class IssueSearchCommand implements Command {
|
|
42
|
+
|
|
43
|
+ public void execute(InputHandler handler, Response response, String line) throws Exception {
|
|
44
|
+ final List<String> result = Downloader.getPage("http://jira.dmdirc.com/secure/IssueNavigator.jspa?reset=true&view=rss&jqlQuery="
|
|
45
|
+ + URLEncoder.encode(line, Charset.defaultCharset().name()));
|
|
46
|
+ final StringBuilder builder = new StringBuilder();
|
|
47
|
+
|
|
48
|
+ for (String resline : result) {
|
|
49
|
+ builder.append(resline);
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ final Pattern pattern = Pattern.compile("<item>(.*?)</item>", Pattern.DOTALL);
|
|
53
|
+ final Matcher matcher = pattern.matcher(builder);
|
|
54
|
+ final List<String[]> matches = new ArrayList<String[]>();
|
|
55
|
+
|
|
56
|
+ while (matcher.find()) {
|
|
57
|
+ final String content = matcher.group(1);
|
|
58
|
+
|
|
59
|
+ final Pattern titlePattern = Pattern.compile("<title>(.*?)</title>");
|
|
60
|
+ final Matcher titleMatcher = titlePattern.matcher(content);
|
|
61
|
+ titleMatcher.find();
|
|
62
|
+ final String title = titleMatcher.group(1);
|
|
63
|
+
|
|
64
|
+ final Pattern linkPattern = Pattern.compile("<link>(.*?)</link>");
|
|
65
|
+ final Matcher linkMatcher = linkPattern.matcher(content);
|
|
66
|
+ linkMatcher.find();
|
|
67
|
+ final String link = linkMatcher.group(1);
|
|
68
|
+
|
|
69
|
+ final Pattern keyPattern = Pattern.compile("<key.*?>(.*?)</key>");
|
|
70
|
+ final Matcher keyMatcher = keyPattern.matcher(content);
|
|
71
|
+ keyMatcher.find();
|
|
72
|
+ final String key = keyMatcher.group(1);
|
|
73
|
+
|
|
74
|
+ matches.add(new String[] { title, link, key });
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ response.sendMessage("there "
|
|
78
|
+ + (matches.size() == 1 ? "was 1 result" : "were " + matches.size() + " results")
|
|
79
|
+ + " for that query."
|
|
80
|
+ + (matches.isEmpty() ? "" :
|
|
81
|
+ (matches.size() == 1 ? " It is: " : " The first result is: ")
|
|
82
|
+ + formatResult(matches.get(0))));
|
|
83
|
+
|
|
84
|
+ if (!matches.isEmpty()) {
|
|
85
|
+ response.addFollowup(new NextFollowup(matches, 1));
|
|
86
|
+ response.addFollowup(new DetailsFollowup(matches.get(0)[2]));
|
|
87
|
+ }
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ protected static String formatResult(final String[] args) {
|
|
91
|
+ return args[0] + " (" + args[1] + ")";
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ protected static class NextFollowup implements Followup {
|
|
95
|
+
|
|
96
|
+ private final int offset;
|
|
97
|
+ private final List<String[]> data;
|
|
98
|
+
|
|
99
|
+ public NextFollowup(List<String[]> data, int offset) {
|
|
100
|
+ this.data = data;
|
|
101
|
+ this.offset = offset;
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ public boolean matches(String line) {
|
|
105
|
+ return "next".equalsIgnoreCase(line);
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ public void execute(InputHandler handler, Response response, String line) throws Exception {
|
|
109
|
+ if (offset >= data.size()) {
|
|
110
|
+ response.setInheritFollows(true);
|
|
111
|
+ response.sendMessage("There are no more results", true);
|
|
112
|
+ } else {
|
|
113
|
+ response.sendMessage("result " + (1 + offset) + " is: " + formatResult(data.get(offset)));
|
|
114
|
+ response.addFollowup(new DetailsFollowup(data.get(offset)[2]));
|
|
115
|
+ response.addFollowup(new NextFollowup(data, offset + 1));
|
|
116
|
+ }
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ }
|
|
120
|
+
|
|
121
|
+ protected static class DetailsFollowup implements Followup {
|
|
122
|
+
|
|
123
|
+ private final String key;
|
|
124
|
+
|
|
125
|
+ public DetailsFollowup(String key) {
|
|
126
|
+ this.key = key;
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ public boolean matches(String line) {
|
|
130
|
+ return "details".equalsIgnoreCase(line);
|
|
131
|
+ }
|
|
132
|
+
|
|
133
|
+ public void execute(InputHandler handler, Response response, String line) throws Exception {
|
|
134
|
+ response.setInheritFollows(true);
|
|
135
|
+ new IssueCommand().execute(handler, response, key);
|
|
136
|
+ }
|
|
137
|
+
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+}
|