Commits

raichoo committed a0f3c39

some fixes and extensions

* messages that are too long for irc are splitted
* connectionlistener observes connectionstatus
* first connectionwatchdog implementation

Comments (0)

Files changed (9)

src/sakura/AnonConnector.scala

       return
     val id = ids.pop
     val cf = new ConnectionConfiguration(service, 5222)
-    cf.setTruststorePassword("changeme")
+    cf.setTruststorePassword("changeit")
+    cf.setReconnectionAllowed(false)
     val connection =  new XMPPConnection(cf)
     SASLAuthentication.supportSASLMechanism("DIGEST-MD5", 0)
     connection.connect
     try {
       connection.login("warpzone"+ id, password)
+      connection.addConnectionListener(new SakuraConnectionListener)
     } catch {
       case e: Exception => {
           connection.disconnect
       }
     }
   }
-
+  
 }

src/sakura/ConnectionWatchdog.scala

+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package sakura
+
+import java.net.InetAddress
+import scala.actors.Actor
+import scala.actors.Actor._
+import scala.actors.TIMEOUT
+
+object ConnectionWatchdog extends Actor {
+
+  val service = Main.config \ "connectors" \ "anon" \ "service" text
+
+  val xmpp_server = InetAddress.getByName(service)
+
+  java.security.Security.setProperty("networkaddress.cache.ttl", "0")
+  System.setProperty("networkaddress.cache.ttl", "0")
+
+  def act(): Unit = {
+    loop {
+      receiveWithin(60000) {
+        case TIMEOUT =>
+          {
+            val check_addr = InetAddress.getByName(service)
+            println("XMPP:  " + xmpp_server.toString)
+            println("CHECK: " + check_addr.toString)
+
+            if (check_addr.toString != xmpp_server.toString) {
+              println("DISCONNECTED")
+              System.exit(0)
+            }
+            println("CONNECTED TO XMPP SERVER")
+          }
+      }
+    }
+  }
+
+}
+

src/sakura/Connector.scala

 import scala.actors.Actor
 
 trait Connector extends Actor {
-  
+
 }

src/sakura/IRCConnector.scala

   val Nick    = new Regex(""":(.+)!~?.+\s+NICK\s+:(.+)""")
   val cmd352  = new Regex("""(?i):.+ 352 .+ #(\w+) .+ .+ .+ (.+) \w@? :0.*""")
   val cmd315  = new Regex("""(?i):.+ 315 .+ #(\w+) :.*""")
+  val cmd353  = new Regex(""".+ 353 .+ = #(\w+) :(.*)""")
 
   // send necessary commands to login
   val room = login
             try {
               println(processInput(input.readLine))
             } catch {
-              case e: Exception => {}
+              case e: Exception => {
+                  println(e.toString)
+              }
             }
           }
         case _ => {}
           AnonConnector.join(to, roomname)
       case cmd352(room, nick) =>
         AnonConnector.join(nick, room)
+      case cmd353(room, nicks) => {
+          (nicks.split(' ').toList) map { nick => AnonConnector.join(nick, room) }
+      }
       case _ => {}
     }
     input

src/sakura/IRCRoom.scala

    * Split long text into tokens so IRC can swallow it
    *
    * @param text Text to be tokenized
+   * 
    */
   private def tokenizeText(text: String): List[String] = {
     if ((text size) < 480) {
       List(text)
     } else {
       val (left, right) = text splitAt(480)
-      List(left) ++ this.tokenizeText(right)
+      List(left + "…") ++ this.tokenizeText("…" + right)
     }
   }
 

src/sakura/Main.scala

     IRCConnector.start
     ZoneStatusObserver.start
     AnonConnector.start
+    //ConnectionWatchdog.start
   }
 
 }

src/sakura/SakuraConnectionListener.scala

+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package sakura
+
+import org.jivesoftware.smack.ConnectionListener
+
+class SakuraConnectionListener extends ConnectionListener {
+
+  def connectionClosedOnError(e: Exception): Unit = {
+    println("CONNECTION TERMINATED")
+//    System.exit(0)
+  }
+
+  def connectionClosed(): Unit = {}
+  def reconnectingIn(seconds: Int): Unit = {}
+  def reconnectionFailed(e: Exception): Unit = {}
+  def reconnectionSuccessful(): Unit = {}
+}

src/sakura/XMPPConnector.scala

    */
   private def login(): XMPPConnection = {
     val cf = new ConnectionConfiguration(service, 5222)
-    cf.setTruststorePassword("changeme")
+    cf.setReconnectionAllowed(false)
+    cf.setTruststorePassword("changeit")
     val connection = new XMPPConnection(cf)
     connection.connect
     SASLAuthentication.supportSASLMechanism("PLAIN", 0)
     connection.login(XMPPConnector.user, XMPPConnector.password)
+    connection.addConnectionListener(new SakuraConnectionListener)
     connection
   }
 

src/sakura/ZoneStatusObserver.scala

   val request = "GET /ecmd?io%20get%20pin%200 HTTP/1.1\n"
   
   val provider = Main.config \ "plugins" \ "zonestatus" \ "provider" text
-  val room     = Main.config \ "plugins" \ "zonestatus" \ "room" text
-  val topic    = Main.config \ "plugins" \ "zonestatus" \ "topic" text
+  val room     = Main.config \ "plugins" \ "zonestatus" \ "room"     text
+  val topic    = Main.config \ "plugins" \ "zonestatus" \ "topic"    text
 
   private var zonestatus = isOpen