Kaynağa Gözat

NotifyMyAndroid plugin

Add slf4j API jar

Change-Id: I56f1f80196a64c5c34b6824d0056de6bf289f6d8
Reviewed-on: http://gerrit.dmdirc.com/2319
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.7rc1
Chris Smith 12 yıl önce
ebeveyn
işleme
c4b0529ec0

BIN
lib/slf4j-api-1.6.4.jar Dosyayı Görüntüle


+ 107
- 0
src/com/dmdirc/addons/nma/NotifyMyAndroidClient.java Dosyayı Görüntüle

@@ -0,0 +1,107 @@
1
+/*
2
+ * Copyright (c) 2006-2012 DMDirc Developers
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.dmdirc.addons.nma;
24
+
25
+import com.dmdirc.util.io.Downloader;
26
+
27
+import java.io.IOException;
28
+import java.util.Arrays;
29
+import java.util.Collection;
30
+import java.util.HashMap;
31
+import java.util.List;
32
+import java.util.Map;
33
+
34
+import lombok.RequiredArgsConstructor;
35
+import lombok.extern.slf4j.Slf4j;
36
+
37
+/**
38
+ * Simple client for sending events to NotifyMyAndroid.
39
+ */
40
+@Slf4j
41
+@RequiredArgsConstructor
42
+public class NotifyMyAndroidClient {
43
+
44
+    /** The base URL for the NMA API. */
45
+    private static final String BASE_URL = "https://www.notifymyandroid.com";
46
+    /** The method to call to send a notification. */
47
+    private static final String NOTIFY_PATH = "/publicapi/notify";
48
+
49
+    /** The API keys to deliver to. */
50
+    private final Collection<String> apiKeys;
51
+
52
+    /** The application to report ourselves as. */
53
+    private final String application;
54
+
55
+    /**
56
+     * Creates a new instance of {@link NotifyMyAndroidClient} with a single
57
+     * API key.
58
+     *
59
+     * @param apiKey The API key to use when connecting to NMA
60
+     * @param application The application string to report to NMA.
61
+     */
62
+    public NotifyMyAndroidClient(final String apiKey, final String application) {
63
+        this(Arrays.asList(new String[] { apiKey }), application);
64
+    }
65
+
66
+    /**
67
+     * Sends a notification to NotifyMyAndroid. At present return status and
68
+     * text is ignored.
69
+     *
70
+     * @param event The name of the event to send (max 1,000 chars).
71
+     * @param description The description of the event (max 10,000 chars).
72
+     * @throws IOException If the NMA service couldn't be reached
73
+     */
74
+    public void notify(final String event, final String description) throws IOException {
75
+        final Map<String, String> arguments = new HashMap<String, String>();
76
+        arguments.put("apikey", getApiKeys());
77
+        arguments.put("application", application);
78
+        arguments.put("event", event);
79
+        arguments.put("description", description);
80
+
81
+        log.info("Sending notification to NMA for event '{}'", event);
82
+        log.debug("Arguments: {}", arguments);
83
+
84
+        final List<String> response = Downloader.getPage(BASE_URL + NOTIFY_PATH, arguments);
85
+        log.debug("Response: {}", response);
86
+    }
87
+
88
+    /**
89
+     * Returns a comma-separated list of API keys.
90
+     *
91
+     * @return A string representation of API keys.
92
+     */
93
+    private String getApiKeys() {
94
+        final StringBuilder builder = new StringBuilder();
95
+
96
+        for (String apiKey : apiKeys) {
97
+            if (builder.length() > 0) {
98
+                builder.append(',');
99
+            }
100
+
101
+            builder.append(apiKey);
102
+        }
103
+
104
+        return builder.toString();
105
+    }
106
+
107
+}

+ 79
- 0
src/com/dmdirc/addons/nma/NotifyMyAndroidCommand.java Dosyayı Görüntüle

@@ -0,0 +1,79 @@
1
+/*
2
+ * Copyright (c) 2006-2012 DMDirc Developers
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.dmdirc.addons.nma;
24
+
25
+import com.dmdirc.FrameContainer;
26
+import com.dmdirc.commandparser.BaseCommandInfo;
27
+import com.dmdirc.commandparser.CommandArguments;
28
+import com.dmdirc.commandparser.CommandType;
29
+import com.dmdirc.commandparser.commands.Command;
30
+import com.dmdirc.commandparser.commands.context.CommandContext;
31
+
32
+import java.io.IOException;
33
+
34
+import lombok.Setter;
35
+import lombok.extern.slf4j.Slf4j;
36
+
37
+/**
38
+ * Command to raise notifications with NotifyMyAndroid.
39
+ */
40
+@Slf4j
41
+public class NotifyMyAndroidCommand extends Command {
42
+
43
+    /** A command info object for this command. */
44
+    public static final BaseCommandInfo INFO = new BaseCommandInfo(
45
+            "notifymyandroid",
46
+            "notifymyandroid <title> -- <body>"
47
+                + " - Sends notification to NotifyMyAndroid",
48
+            CommandType.TYPE_GLOBAL);
49
+
50
+    /** The configuration domain to retrieve settings from. */
51
+    @Setter
52
+    private String configDomain;
53
+
54
+    /** {@inheritDoc} */
55
+    @Override
56
+    public void execute(final FrameContainer origin,
57
+            final CommandArguments args, final CommandContext context) {
58
+        final String[] parts = args.getArgumentsAsString().split("\\s+--\\s+", 2);
59
+        log.trace("Split input: {}", parts);
60
+
61
+        if (parts.length != 2) {
62
+            showUsage(origin, args.isSilent(), INFO.getName(), INFO.getHelp());
63
+        }
64
+
65
+        log.trace("Retrieving settings from domain '{}'", configDomain);
66
+        final NotifyMyAndroidClient client = new NotifyMyAndroidClient(
67
+                origin.getConfigManager().getOption(configDomain, "apikey"),
68
+                origin.getConfigManager().getOption(configDomain, "application"));
69
+
70
+        try {
71
+            client.notify(parts[0], parts[1]);
72
+            sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Notification sent");
73
+        } catch (IOException ex) {
74
+            log.info("Exception when trying to notify NMA", ex);
75
+            sendLine(origin, args.isSilent(), FORMAT_ERROR, "Unable to send: " + ex.getMessage());
76
+        }
77
+    }
78
+
79
+}

+ 83
- 0
src/com/dmdirc/addons/nma/NotifyMyAndroidPlugin.java Dosyayı Görüntüle

@@ -0,0 +1,83 @@
1
+/*
2
+ * Copyright (c) 2006-2012 DMDirc Developers
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.dmdirc.addons.nma;
24
+
25
+import com.dmdirc.config.prefs.PluginPreferencesCategory;
26
+import com.dmdirc.config.prefs.PreferencesCategory;
27
+import com.dmdirc.config.prefs.PreferencesDialogModel;
28
+import com.dmdirc.config.prefs.PreferencesSetting;
29
+import com.dmdirc.config.prefs.PreferencesType;
30
+import com.dmdirc.plugins.BasePlugin;
31
+import com.dmdirc.plugins.PluginInfo;
32
+
33
+/**
34
+ * Plugin to allow pushing notifications to NotifyMyAndroid.
35
+ */
36
+public class NotifyMyAndroidPlugin extends BasePlugin {
37
+
38
+    /** The command to register. */
39
+    private final NotifyMyAndroidCommand command = new NotifyMyAndroidCommand();
40
+
41
+    /** Our info object. */
42
+    private final PluginInfo pluginInfo;
43
+
44
+    /**
45
+     * Creates a new instance of the {@link NotifyMyAndroidPlugin}.
46
+     *
47
+     * @param pluginInfo The plugin info object for this plugin.
48
+     */
49
+    public NotifyMyAndroidPlugin(final PluginInfo pluginInfo) {
50
+        registerCommand(command, NotifyMyAndroidCommand.INFO);
51
+
52
+        this.pluginInfo = pluginInfo;
53
+    }
54
+
55
+    /** {@inheritDoc} */
56
+    @Override
57
+    public void setDomain(final String newDomain) {
58
+        super.setDomain(newDomain);
59
+        command.setConfigDomain(newDomain);
60
+    }
61
+
62
+    /** {@inheritDoc} */
63
+    @Override
64
+    public void showConfig(final PreferencesDialogModel manager) {
65
+        final PreferencesCategory category = new PluginPreferencesCategory(
66
+                pluginInfo, "Notify My Android",
67
+                "General configuration for Notify My Android plugin.");
68
+
69
+        category.addSetting(new PreferencesSetting(
70
+                PreferencesType.TEXT, getDomain(), "apikey",
71
+                "API Key", "Comma-separated list of NotifyMyAndroid API keys"
72
+                + " to be notified when the command is used.",
73
+                manager.getConfigManager(), manager.getIdentity()));
74
+        category.addSetting(new PreferencesSetting(
75
+                PreferencesType.TEXT, getDomain(), "application",
76
+                "Application", "Name of the application to report to "
77
+                + "NotifyMyAndroid",
78
+                manager.getConfigManager(), manager.getIdentity()));
79
+
80
+        manager.getCategory("Plugins").addSubCategory(category);
81
+    }
82
+
83
+}

+ 30
- 0
src/com/dmdirc/addons/nma/plugin.config Dosyayı Görüntüle

@@ -0,0 +1,30 @@
1
+# This is a DMDirc configuration file.
2
+
3
+# This section indicates which sections below take key/value
4
+# pairs, rather than a simple list. It should be placed above
5
+# any sections that take key/values.
6
+keysections:
7
+  metadata
8
+  version
9
+  defaults
10
+  updates
11
+
12
+metadata:
13
+  author=Chris <chris@dmdirc.com>
14
+  mainclass=com.dmdirc.addons.nma.NotifyMyAndroidPlugin
15
+  description=Facilitates sending notifications to NotifyMyAndroid
16
+  name=nma
17
+  nicename=Notify My Android plugin
18
+
19
+updates:
20
+  id=70
21
+
22
+version:
23
+  friendly=0.1
24
+
25
+provides:
26
+  notifymyandroid command
27
+
28
+defaults:
29
+  apikey=
30
+  application=DMDirc

Loading…
İptal
Kaydet