emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 16 Feb 2010 01:22:36 +0100
changeset 228 3ea04696a115
parent 158 0e8b21fcaeb0
child 240 29287b0a1a0b
permissions -rw-r--r--
Preparing for more testable 'business' logic - splitting the for cycles and REST queries.
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.emailer
    28 
    29 import scala.xml._
    30 import java.net.URL
    31 import scala.io._
    32 import scala.collection.Map
    33 import scala.collection.mutable.HashMap
    34 import javax.mail.internet._
    35 import javax.mail.Message
    36 import javax.mail.Transport
    37 
    38 object Main {
    39   def main(args: Array[String]) {
    40     if (args == null || args.size != 4) {
    41       Console.println("Usage: emailer.jar <quoridor API URL> <login name> <password> <smtp server IP>")
    42       return
    43     }
    44 
    45     val api = new URL(args(0));
    46 
    47     val loginID = login(api, args(1), args(2))
    48 
    49     val okDelay = 3600 * 1000 * 24
    50     val props = java.lang.System.getProperties
    51     props.put("mail.smtp.host", args(3));
    52 
    53     val session = javax.mail.Session.getDefaultInstance(props, null)
    54 
    55     val notify = new Notify() {
    56       protected def allPlayers(): Map[String,List[Node]] = {
    57         val u = new URL(api, "games");
    58         val conn = u.openConnection
    59         conn.setRequestProperty("Accept", "text/xml")
    60 
    61         val ret = new HashMap[String,List[Node]]
    62 
    63         val before = System.currentTimeMillis - okDelay
    64         val games = XML.load(conn.getInputStream)
    65         for (val g : Node <- games \\ "gameId") {
    66           val modified = (g \\ "@modified").text.toLong
    67           if (modified < before) {
    68             val status = (g \\ "@status").text;
    69             val name =
    70               if (status == "blackMove") {
    71                 (g \\ "@black").text
    72               } else if (status == "whiteMove") {
    73                 (g \\ "@white").text
    74               } else {
    75                 null
    76               }
    77             if (name != null) {
    78               val prev = ret.getOrElse(name, Nil)
    79               val next = g :: prev
    80               ret.put(name, next);
    81             }
    82           }
    83         }
    84         return ret;
    85       }
    86 
    87       protected def emails(ids : Collection[String]): List[String] = {
    88         var ret : List[String] = Nil;
    89         for (val p <- ids) {
    90           val u = new URL(api, "users/" + p + "?loginID=" + loginID)
    91           val conn = u.openConnection
    92           conn.setRequestProperty("Accept", "text/xml")
    93           val xml = XML.load(conn.getInputStream)
    94           val res = (xml \\ "property").filter({
    95               n => n.attribute("name") match {
    96                 case Some(attr) => attr.text == "email"
    97                 case _ => false
    98               }
    99           })
   100           if (!res.isEmpty) {
   101             ret = res(0).text :: ret;
   102           }
   103         }
   104         return ret
   105       }
   106 
   107       protected def sendEmail(address : String, subject : String, text : String) : Unit = {
   108         val message = new MimeMessage(session)
   109         message.setFrom(new InternetAddress("quoridor@xelfi.cz"))
   110         message.addRecipient(Message.RecipientType.TO, new InternetAddress(address))
   111         message.setSubject(subject)
   112         message.setText(text)
   113 
   114         Transport.send(message)
   115       }
   116     }
   117 
   118     notify.process()
   119   }
   120 
   121   def login(url : URL, name : String, password : String) : String = {
   122     val u = new URL(url, "login?name=" + name + "&password=" + password);
   123     val conn = u.openConnection.asInstanceOf[java.net.HttpURLConnection]
   124     conn.setRequestMethod("PUT")
   125     conn.setDoInput(true)
   126     conn.connect
   127     val reply = Source.fromInputStream(conn.getInputStream)
   128     return reply.mkString("")
   129   }
   130 }