[vlma-devel] commit: Improved version of the IRC notifier. (Adrien Grand )

git version control git at videolan.org
Tue Jun 17 18:49:20 CEST 2008


vlma | branch: master | Adrien Grand <jpountz at videolan.org> | Tue Jun 17 18:50:45 2008 +0200| [1ed17dcea67d200ed271af936535d1e2c0fbf368]

Improved version of the IRC notifier.

> http://git.videolan.org/gitweb.cgi/vlma.git/?a=commit;h=1ed17dcea67d200ed271af936535d1e2c0fbf368
---

 vlma-core/pom.xml                                  |    5 +
 .../org/videolan/vlma/notifier/IRCNotifier.java    |  138 ++++++++++++++++---
 2 files changed, 121 insertions(+), 22 deletions(-)

diff --git a/vlma-core/pom.xml b/vlma-core/pom.xml
index 1260acb..b59fed1 100644
--- a/vlma-core/pom.xml
+++ b/vlma-core/pom.xml
@@ -63,6 +63,11 @@
                 </exclusion>
             </exclusions>
         </dependency>
+       <dependency>
+           <groupId>org.schwering</groupId>
+           <artifactId>irclib</artifactId>
+           <version>1.10</version>
+        </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring</artifactId>
diff --git a/vlma-core/src/main/java/org/videolan/vlma/notifier/IRCNotifier.java b/vlma-core/src/main/java/org/videolan/vlma/notifier/IRCNotifier.java
index 2fa5e55..2f4a1d8 100644
--- a/vlma-core/src/main/java/org/videolan/vlma/notifier/IRCNotifier.java
+++ b/vlma-core/src/main/java/org/videolan/vlma/notifier/IRCNotifier.java
@@ -20,13 +20,13 @@
 
 package org.videolan.vlma.notifier;
 
-import java.io.BufferedReader;
 import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
-import java.io.PrintWriter;
-import java.net.Socket;
 
+import org.apache.log4j.Logger;
+import org.schwering.irc.lib.IRCConnection;
+import org.schwering.irc.lib.IRCEventListener;
+import org.schwering.irc.lib.IRCModeParser;
+import org.schwering.irc.lib.IRCUser;
 import org.videolan.vlma.VLMa;
 
 /**
@@ -36,26 +36,65 @@ import org.videolan.vlma.VLMa;
  */
 public class IRCNotifier extends Notifier {
 
-    private Socket ircSocket = new Socket();
-    private PrintWriter out;
+    public static final Logger logger = Logger.getLogger(IRCNotifier.class);
+
+    private IRCConnection conn;
+    private String nick;
     private String chan;
     private String host;
     private int port;
-    private String nick;
 
+    public IRCNotifier() {
+        if (isIRCEnabled()) {
+            try {
+                connect();
+            } catch (IOException e) {
+                logger.error("Unable to connect to the IRC server", e);
+            }
+        }
+    }
+
+    /**
+     * Connect to the IRC server and join the appropriate channel.
+     *
+     * @throws IOException
+     */
     private void connect() throws IOException {
         nick = VLMa.getInstance().getString("vlma.notification.irc.nick");
         host = VLMa.getInstance().getString("vlma.notification.irc.host");
         port = VLMa.getInstance().getInt("vlma.notification.irc.port");
         chan = VLMa.getInstance().getString("vlma.notification.irc.chan");
-        ircSocket = new Socket(host, port);
-        ircSocket.setKeepAlive(true);
-        out = new PrintWriter(new OutputStreamWriter(ircSocket.getOutputStream()),true);
-        out.println("NICK " + nick);
-        out.println("USER " + nick + " hostname " + host +" :Hallo");
-        out.println("JOIN " + chan);
+        conn = new IRCConnection(host, new int[] { port }, null, nick, null, null);
+        conn.setEncoding("UTF-8");
+        conn.addIRCEventListener(new Listener());
+        conn.setPong(true);
+        conn.setDaemon(true);
+        conn.connect();
+        /* Wait for 5 seconds before trying to join any chan.
+         * It may be necessary if the server is slow to initialise the connection.
+         */
+        try {
+            Thread.sleep(5000);
+        } catch (InterruptedException e) { }
+        conn.doJoin(chan);
     }
 
+    /**
+     * Reconnect to the IRC server if the connection has been broken.
+     *
+     * @throws IOException
+     */
+    private void checkConnection() throws IOException {
+        if (conn == null || !conn.isConnected()) {
+            connect();
+        }
+    }
+
+    /**
+     * Does the user want to be notified by IRC?
+     *
+     * @return true if and only if there is at least one channel provided in the configuration
+     */
     private boolean isIRCEnabled() {
         String tmp = VLMa.getInstance().getString("vlma.notification.irc.chan");
         return (tmp != null && tmp.length() > 0);
@@ -64,16 +103,71 @@ public class IRCNotifier extends Notifier {
     @Override
     public void sendNotification(String message) {
         if (isIRCEnabled()) {
-            if (!ircSocket.isConnected()) {
-                try {
-                    connect();
-                } catch (IOException e) {
-                    logger.error("Unable to connect.", e);
-                    return;
-                }
+            try {
+                checkConnection();
+                conn.doPrivmsg(chan, message);
+            } catch (IOException e) {
+                logger.error("IRC notifier cannot connect", e);
             }
-            out.println("PRIVMSG " + chan + " " + message);
         }
     }
 
+    /**
+     * A listener for the {@link IRCNotifier} class.
+     *
+     * @author jpountz
+     */
+    public class Listener implements IRCEventListener {
+
+        public void onDisconnected() {
+            logger.info("IRC notifier deconnected");
+        }
+
+        public void onError(String error) {
+            logger.error("IRC notifier encountered an error: " + error);
+        }
+
+        public void onError(int code, String error) {
+            logger.error("IRC notifier encountered an error: " + error);
+        }
+
+        public void onInvite(String chan, IRCUser user, String nick) { }
+
+        public void onJoin(String chan, IRCUser user) {
+            logger.info("IRC notifier joined " + chan);
+        }
+
+        public void onKick(String chan, IRCUser user, String nick, String msg) {
+            logger.info("IRC notifier was kicked by " + user.getNick());
+        }
+
+        public void onMode(String arg0, IRCUser arg1, IRCModeParser arg2) { }
+
+        public void onMode(IRCUser arg0, String arg1, String arg2) { }
+
+        public void onNick(IRCUser arg0, String arg1) { }
+
+        public void onNotice(String arg0, IRCUser arg1, String arg2) { }
+
+        public void onPart(String arg0, IRCUser arg1, String arg2) { }
+
+        public void onPing(String ping) {
+            logger.info("IRC notifier received ping " + ping);
+        }
+
+        public void onPrivmsg(String chan, IRCUser user, String msg) {
+            logger.debug("User " + user + " said " + msg);
+        }
+
+        public void onQuit(IRCUser arg0, String arg1) { }
+
+        public void onRegistered() { }
+
+        public void onReply(int arg0, String arg1, String arg2) { }
+
+        public void onTopic(String arg0, IRCUser arg1, String arg2) { }
+
+        public void unknown(String arg0, String arg1, String arg2, String arg3) { }
+    }
+
 }



More information about the vlma-devel mailing list