emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 244 32dd025e4b76
permissions -rw-r--r--
Changing headers to GPLv3
     1 /*
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 package cz.xelfi.quoridor.emailer
    19 
    20 import scala.xml._
    21 import java.net.URL
    22 import java.net.HttpURLConnection
    23 import java.io.BufferedReader
    24 import java.io.InputStreamReader
    25 import scala.io._
    26 import scala.collection.Map
    27 import scala.collection.mutable.HashMap
    28 import javax.mail.internet._
    29 import javax.mail.Message
    30 import javax.mail.Transport
    31 
    32 object Main {
    33   def main(args: Array[String]) {
    34     if (args == null || args.size != 4) {
    35       Console.println("Usage: emailer.jar <quoridor API URL> <login name> <password> <smtp server IP>")
    36       return
    37     }
    38 
    39     val api = new URL(args(0));
    40 
    41     val loginID = login(api, args(1), args(2))
    42 
    43     val okDelay = 3600 * 1000 * 24
    44     val props = java.lang.System.getProperties
    45     props.put("mail.smtp.host", args(3));
    46 
    47     val session = if (args(3).equals("print")) null else javax.mail.Session.getDefaultInstance(props, null)
    48 
    49     val notify = new Notify() {
    50       protected def allPlayers(): Map[String,List[Info]] = {
    51         val u = new URL(api, "games");
    52         val conn = u.openConnection
    53         conn.setRequestProperty("Accept", "text/xml")
    54 
    55         val ret = new HashMap[String,List[Info]]
    56 
    57         val before = System.currentTimeMillis - okDelay
    58         val games = XML.load(conn.getInputStream)
    59         for (val g : Node <- games \\ "gameId") {
    60           val modified = (g \\ "@modified").text.toLong
    61           if (modified < before) {
    62             val status = (g \\ "@status").text;
    63             val name =
    64               if (status == "blackMove") {
    65                 (g \\ "@black").text
    66               } else if (status == "whiteMove") {
    67                 (g \\ "@white").text
    68               } else {
    69                 null
    70               }
    71             if (name != null) {
    72               val prev = ret.getOrElse(name, Nil)
    73               val id = (g \\ "@id").text
    74               val info = new Info(modified, id, name)
    75               val next = info :: prev
    76               ret.put(name, next);
    77             }
    78           }
    79         }
    80         return ret;
    81       }
    82 
    83       protected def emails(ids : Collection[String]): Map[String,String] = {
    84         var ret = new HashMap[String,String]
    85         for (val p <- ids) {
    86           val u = new URL(api, "users/" + p + "?loginID=" + loginID)
    87           val conn = u.openConnection
    88           conn.setRequestProperty("Accept", "text/xml")
    89           val xml = XML.load(conn.getInputStream)
    90           val res = (xml \\ "property").filter({
    91               n => n.attribute("name") match {
    92                 case Some(attr) => attr.text == "email"
    93                 case _ => false
    94               }
    95           })
    96           if (!res.isEmpty) {
    97             ret.put(p, res(0).text)
    98           }
    99         }
   100         return ret
   101       }
   102 
   103       protected def sendEmail(address : String, subject : String, text : String) : Unit = {
   104         Console.println("Sending message to")
   105         Console.println("  email: " + address.split('@')(0) + "@... ")
   106         Console.println("  subject: " + subject)
   107         Console.println("  " + text)
   108         if (session == null) {
   109           return;
   110         }
   111 
   112         val message = new MimeMessage(session)
   113         message.setFrom(new InternetAddress("quoridor@xelfi.cz"))
   114         message.addRecipient(Message.RecipientType.TO, new InternetAddress(address))
   115         message.setSubject(subject)
   116         message.setText(text)
   117 
   118         Transport.send(message)
   119       }
   120 
   121       protected def resignGame(id : Info): Unit = {
   122         val u = new URL(api, "games/" + id.gameId + "?loginID=" + loginID + "&player=" + id.toMove + "&move=RESIGN")
   123         val c = u.openConnection.asInstanceOf[HttpURLConnection]
   124         c.setRequestMethod("PUT")
   125         c.connect
   126         val is = c.getInputStream
   127         val r = new BufferedReader(new InputStreamReader(is))
   128         var goOn = true;
   129         while (goOn) {
   130           val s = r.readLine()
   131           if (s == null) {
   132             goOn = false;
   133           } else {
   134             Console.println(s);
   135           }
   136         }
   137       }
   138     }
   139 
   140     notify.process()
   141   }
   142 
   143   def login(url : URL, name : String, password : String) : String = {
   144     val u = new URL(url, "login?name=" + name + "&password=" + password);
   145     val conn = u.openConnection.asInstanceOf[java.net.HttpURLConnection]
   146     conn.setRequestMethod("PUT")
   147     conn.setDoInput(true)
   148     conn.connect
   149     val reply = Source.fromInputStream(conn.getInputStream)
   150     return reply.mkString("")
   151   }
   152 }