etc/rhq-ircBot/pom.xml | 8 -
etc/rhq-ircBot/src/main/java/org/rhq/etc/ircbot/GDocParser.java | 79
+++++++++
etc/rhq-ircBot/src/main/java/org/rhq/etc/ircbot/RhqIrcBot.java | 33 +---
etc/rhq-ircBot/src/main/java/org/rhq/etc/ircbot/RhqIrcBotListener.java | 80
+---------
etc/rhq-ircBot/src/main/resources/org/rhq/etc/ircbot/rhq-ircbot.properties | 3
5 files changed, 114 insertions(+), 89 deletions(-)
New commits:
commit b223cd3fc08b8e9eed3e06359488a50da2ee6ed7
Author: Jirka Kremser <jkremser(a)redhat.com>
Date: Sat Dec 7 01:02:23 2013 +0100
irc-bot: fixing the !support command
diff --git a/etc/rhq-ircBot/pom.xml b/etc/rhq-ircBot/pom.xml
index adbab6a..75c9249 100644
--- a/etc/rhq-ircBot/pom.xml
+++ b/etc/rhq-ircBot/pom.xml
@@ -8,7 +8,7 @@
<groupId>org.rhq.etc</groupId>
<artifactId>rhq-ircbot</artifactId>
- <version>1.1.1</version>
+ <version>1.1.2</version>
<packaging>jar</packaging>
<name>RHQ IRC Bot</name>
@@ -55,6 +55,12 @@
<artifactId>jsoup</artifactId>
<version>1.7.2</version>
</dependency>
+
+ <dependency>
+ <groupId>com.google.gdata</groupId>
+ <artifactId>core</artifactId>
+ <version>1.47.1</version>
+ </dependency>
<dependency>
<groupId>commons-codec</groupId>
diff --git a/etc/rhq-ircBot/src/main/java/org/rhq/etc/ircbot/GDocParser.java
b/etc/rhq-ircBot/src/main/java/org/rhq/etc/ircbot/GDocParser.java
new file mode 100644
index 0000000..84db12e
--- /dev/null
+++ b/etc/rhq-ircBot/src/main/java/org/rhq/etc/ircbot/GDocParser.java
@@ -0,0 +1,79 @@
+/*
+ * RHQ Management Platform
+ * Copyright (C) 2005-2013 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package org.rhq.etc.ircbot;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.List;
+
+import com.google.gdata.client.spreadsheet.SpreadsheetService;
+import com.google.gdata.data.spreadsheet.CellFeed;
+import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
+import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
+import com.google.gdata.data.spreadsheet.WorksheetEntry;
+import com.google.gdata.data.spreadsheet.WorksheetFeed;
+import com.google.gdata.util.AuthenticationException;
+import com.google.gdata.util.ServiceException;
+
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+
+/**
+ * @author Jirka Kremser
+ *
+ */
+public class GDocParser {
+ public static String onSupport2() throws AuthenticationException,
MalformedURLException, IOException,
+ ServiceException, URISyntaxException {
+ SpreadsheetService service = new
SpreadsheetService("MySpreadsheetIntegration-foo");
+ URL url = new
URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full...;
+ SpreadsheetFeed feed = service.getFeed(url, SpreadsheetFeed.class);
+ List<SpreadsheetEntry> spreadsheets = feed.getEntries();
+ SpreadsheetEntry spreadsheet = spreadsheets.get(0);
+ WorksheetFeed worksheetFeed = service.getFeed(spreadsheet.getWorksheetFeedUrl(),
WorksheetFeed.class);
+ List<WorksheetEntry> worksheets = worksheetFeed.getEntries();
+ WorksheetEntry worksheet = worksheets.get(0);
+ // fetch A1 only
+ URL cellFeedUrl = new URI(worksheet.getCellFeedUrl().toString() +
"?min-row=1&max-row=1&min-col=1&max-col=1")
+ .toURL();
+ CellFeed cellFeed = service.getFeed(cellFeedUrl, CellFeed.class);
+ // Iterate through each cell, printing its value.
+ String onSupport = cellFeed.getEntries().get(0).getCell().getValue();
+ return onSupport;
+ }
+
+ public static String onSupport1() {
+ // the simplest way of accessing the publicly available gdoc (OAuth is not needed
here)
+ String url =
"https://docs.google.com/spreadsheet/pub?key=0AsqiOfOdbEhBdEtMZGRGUTVZNTNmYXlFdllqdU9Oamc&single=true&gid=1&output=txt";
+ Document doc = null;
+ try {
+ doc = Jsoup.connect(url).get();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return doc.body().html().trim();
+ }
+
+ public static void main(String[] args) {
+ System.out.println(GDocParser.onSupport1());
+ }
+}
diff --git a/etc/rhq-ircBot/src/main/java/org/rhq/etc/ircbot/RhqIrcBot.java
b/etc/rhq-ircBot/src/main/java/org/rhq/etc/ircbot/RhqIrcBot.java
index 998efa9..7358e18 100644
--- a/etc/rhq-ircBot/src/main/java/org/rhq/etc/ircbot/RhqIrcBot.java
+++ b/etc/rhq-ircBot/src/main/java/org/rhq/etc/ircbot/RhqIrcBot.java
@@ -19,7 +19,6 @@
package org.rhq.etc.ircbot;
import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
@@ -27,7 +26,6 @@ import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
-import java.util.Properties;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
@@ -45,7 +43,7 @@ public class RhqIrcBot extends PircBotX {
public RhqIrcBot(RhqIrcBotListener rhqBot) {
setName("rhq-bot");
- setVersion("1.1.1");
+ setVersion("1.1.2");
setFinger("RHQ IRC bot (source code in RHQ git under
etc/rhq-ircBot/)");
setVerbose(true);
@@ -74,24 +72,21 @@ public class RhqIrcBot extends PircBotX {
System.err.println("Provided property file [" + args[2] +
"] does not exist");
System.exit(2);
}
- Properties properties = new Properties();
- FileInputStream fis = new FileInputStream(propertyFile);
- properties.load(fis);
- String docspaceLogin = properties.getProperty("docspace_login");
- String docspacePassword =
properties.getProperty("docspace_password");
- if (docspaceLogin == null || docspaceLogin.isEmpty() || docspacePassword ==
null || docspacePassword.isEmpty()) {
- System.err.println("The property format has bad format");
- System.err.println("It must contain following key-value
pairs\n");
- System.err.println("docspace_login=X");
- System.err.println("docspace_password=Y");
- System.exit(3);
- }
- fis.close();
+// Properties properties = new Properties();
+// FileInputStream fis = new FileInputStream(propertyFile);
+// properties.load(fis);
+// String docspaceLogin = properties.getProperty("docspace_login");
+// String docspacePassword =
properties.getProperty("docspace_password");
+// if (docspaceLogin == null || docspaceLogin.isEmpty() || docspacePassword ==
null || docspacePassword.isEmpty()) {
+// System.err.println("The property format has bad format");
+// System.err.println("It must contain following key-value
pairs\n");
+// System.err.println("docspace_login=X");
+// System.err.println("docspace_password=Y");
+// System.exit(3);
+// }
+// fis.close();
setupTrustStore();
-
- rhqBotListener.setDocspaceLogin(docspaceLogin);
- rhqBotListener.setDocspacePassword(docspacePassword);
}
PircBotX bot = new RhqIrcBot(rhqBotListener);
diff --git a/etc/rhq-ircBot/src/main/java/org/rhq/etc/ircbot/RhqIrcBotListener.java
b/etc/rhq-ircBot/src/main/java/org/rhq/etc/ircbot/RhqIrcBotListener.java
index 3e3e6f9..1a70c3f 100644
--- a/etc/rhq-ircBot/src/main/java/org/rhq/etc/ircbot/RhqIrcBotListener.java
+++ b/etc/rhq-ircBot/src/main/java/org/rhq/etc/ircbot/RhqIrcBotListener.java
@@ -7,7 +7,6 @@ import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
-import java.util.Random;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -17,7 +16,6 @@ import com.j2bugzilla.base.BugzillaConnector;
import com.j2bugzilla.base.BugzillaException;
import com.j2bugzilla.rpc.GetBug;
-import org.apache.commons.codec.binary.Base64;
import org.apache.xmlrpc.XmlRpcException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
@@ -43,7 +41,6 @@ public class RhqIrcBotListener extends ListenerAdapter<RhqIrcBot>
{
private static final Pattern BUG_PATTERN = Pattern.compile("(?i)(bz|bug)[
]*(\\d{6,7})");
private static final Pattern COMMIT_PATTERN =
Pattern.compile("(?i)(\\!commit|cm)[ ]*([0-9a-f]{3,40})");
private static final Pattern ECHO_PATTERN = Pattern.compile("(?i)echo[
]+(.+)");
- private static final String SUPPORT_LINK =
"https://docspace.corp.redhat.com/docs/DOC-124477";
private static final String COMMIT_LINK =
"https://git.fedorahosted.org/cgit/rhq/rhq.git/commit/?id=%s";
private static final String PTO_LINK =
"https://mail.corp.redhat.com/home/ccrouch@redhat.com/JBoss%20ON%20OOO?fmt=rss&view=day&start=0day&end=0day";
private static final DateFormat monthFormat = new SimpleDateFormat("MMM");
@@ -93,8 +90,6 @@ public class RhqIrcBotListener extends ListenerAdapter<RhqIrcBot>
{
private final String server;
private final String channel;
private final boolean isRedHatChannel;
- private String docspaceLogin;
- private String docspacePassword;
private BugzillaConnector bzConnector = new BugzillaConnector();
private final Map<Integer, Long> bugLogTimestamps = new HashMap<Integer,
Long>();
private final Map<String, String> names = new HashMap<String, String>();
@@ -119,6 +114,10 @@ public class RhqIrcBotListener extends
ListenerAdapter<RhqIrcBot> {
@Override
public void onMessage(MessageEvent<RhqIrcBot> event) throws Exception {
+ if (event.getUser().getNick().toLowerCase().contains("bot")) {
+ return; // never talk with artificial forms of life
+ }
+
PircBotX bot = event.getBot();
if (!bot.getNick().equals(bot.getName())) {
bot.changeNick(bot.getName());
@@ -290,7 +289,7 @@ public class RhqIrcBotListener extends
ListenerAdapter<RhqIrcBot> {
switch (command) {
case SUPPORT:
if (isRedHatChannel)
- return whoIsOnSupport(SUPPORT_LINK);
+ return whoIsOnSupport();
case PTO:
if (isRedHatChannel)
return whoIsOnPto(PTO_LINK);
@@ -301,67 +300,23 @@ public class RhqIrcBotListener extends
ListenerAdapter<RhqIrcBot> {
return null;
}
- private String whoIsOnSupport(String link) {
- if (docspaceLogin == null || docspaceLogin.isEmpty() || docspacePassword == null
|| docspacePassword.isEmpty()) {
- return "This command is not supported.";
- }
+ private String whoIsOnSupport() {
String month = monthFormat.format(new Date());
String dayInMonth = dayInMonthFormat.format(new Date());
- String cachedValue = ptoCache.get(month + "#" + dayInMonth);
+ String cachedValue = supportCache.get(month + "#" + dayInMonth);
if (cachedValue != null) {
return cachedValue;
}
- int dayInMonthInt = Integer.parseInt(dayInMonth);
- try {
- boolean monthFound = false;
- String login = docspaceLogin + ":" + docspacePassword;
- String base64login = new String(Base64.encodeBase64(login.getBytes()));
- Document doc = Jsoup.connect(link).header("Authorization",
"Basic " + base64login).get();
- Elements cells = doc.select("tr td");
- for (Element cell : cells) {
- String cellText = cell.text().toLowerCase();
- if (cellText.startsWith(month.toLowerCase())) {
- monthFound = true;
- if (cellText.substring(cellText.length() - 1,
cellText.length()).equals(dayInMonth)) {
- String value = doNotNotify(cell.firstElementSibling().text() +
" is on support this week");
- ptoCache.put(month + "#" + dayInMonth, value);
- return value;
- }
- continue;
- }
- if (monthFound && cellText.equals(dayInMonth)) {
- String value = doNotNotify(cell.firstElementSibling().text() + "
is on support this week");
- ptoCache.put(month + "#" + dayInMonth, value);
- return value;
- } else if (monthFound) {
- if (cell.equals(cell.firstElementSibling()) ||
cell.equals(cell.lastElementSibling())) {
- continue; //the first row with name or the last row with a
comment
- }
- int day;
- try {
- day = Integer.parseInt(cellText);
- if (day > dayInMonthInt) {
- String value =
doNotNotify(cell.parent().previousElementSibling().child(0).text() + " is on support
this week");
- ptoCache.put(month + "#" + dayInMonth, value);
- return value;
- }
- } catch (NumberFormatException nfe) {
- break; // next month
- }
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- // fallback solution if SSL is not set correctly
- String randomDevel = JON_DEVS.toArray(new String[JON_DEVS.size()])[new
Random().nextInt(JON_DEVS.size())];
- return "404 Developer Not Found, selecting randomly " +
doNotNotify(randomDevel) + ". Check the " + SUPPORT_LINK;
+ String onSupport = GDocParser.onSupport1();
+ String value = doNotNotify(onSupport + " is on support this week");
+ supportCache.put(month + "#" + dayInMonth, value);
+ return value;
}
private String whoIsOnPto(String link) {
String month = monthFormat.format(new Date());
String dayInMonth = dayInMonthFormat.format(new Date());
- String cachedValue = supportCache.get(month + "#" + dayInMonth);
+ String cachedValue = ptoCache.get(month + "#" + dayInMonth);
if (cachedValue != null) {
return cachedValue;
}
@@ -374,7 +329,7 @@ public class RhqIrcBotListener extends
ListenerAdapter<RhqIrcBot> {
}
if (!onPto.isEmpty()) {
String value = doNotNotify(onPto.substring(0, onPto.length() - 2));
- supportCache.put(month + "#" + dayInMonth, value);
+ ptoCache.put(month + "#" + dayInMonth, value);
return value;
}
} catch (IOException e) {
@@ -387,13 +342,4 @@ public class RhqIrcBotListener extends
ListenerAdapter<RhqIrcBot> {
//replace all vowels with unicode chars that look same not to spam users with
notifications
return nick.toLowerCase().replaceFirst("a",
"\u0430").replaceFirst("e",
"\u0435").replaceFirst("i",
"\u0456").replaceFirst("o",
"\u043E").replaceFirst("u",
"\u222A").replaceFirst("y", "\u028F");
}
-
- public void setDocspaceLogin(String docspaceLogin) {
- this.docspaceLogin = docspaceLogin;
- }
-
- public void setDocspacePassword(String docspacePassword) {
- this.docspacePassword = docspacePassword;
- }
-
}
diff --git a/etc/rhq-ircBot/src/main/resources/org/rhq/etc/ircbot/rhq-ircbot.properties
b/etc/rhq-ircBot/src/main/resources/org/rhq/etc/ircbot/rhq-ircbot.properties
index 25e325c..8b13789 100644
--- a/etc/rhq-ircBot/src/main/resources/org/rhq/etc/ircbot/rhq-ircbot.properties
+++ b/etc/rhq-ircBot/src/main/resources/org/rhq/etc/ircbot/rhq-ircbot.properties
@@ -1,2 +1 @@
-docspace_login=
-docspace_password=
+