emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 26 Nov 2009 20:20:33 +0100
branchscala-emailer
changeset 154 d9d5376d5f23
parent 153 ac818c32c6e9
child 158 0e8b21fcaeb0
permissions -rw-r--r--
Really trying to send the emails
     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     val api = new URL("http://quoridor.xelfi.cz/api/");
    41 
    42     val id = login(api, "emailer", "otravuj")
    43     
    44     val ret = allPlayers(
    45       api,
    46       3600 * 1000 * 24
    47     )
    48 
    49     val props = java.lang.System.getProperties
    50     props.put("mail.smtp.host", "192.168.1.1");
    51 
    52     val session = javax.mail.Session.getDefaultInstance(props, null)
    53 
    54 
    55     for (val address <- emails(api, id, ret.keySet)) {
    56         Console.println("Sending message to " + address)
    57 
    58         val message = new MimeMessage(session)
    59         message.setFrom(new InternetAddress("quoridor@xelfi.cz"))
    60         message.addRecipient(Message.RecipientType.TO, new InternetAddress(address))
    61         message.setSubject("Play Quoridor!")
    62         message.setText("Visit http://quoridor.xelfi.cz")
    63 
    64         Transport.send(message)
    65     }
    66   }
    67 
    68   def login(url : URL, name : String, password : String) : String = {
    69     val u = new URL(url, "login?name=" + name + "&password=" + password);
    70     val conn = u.openConnection.asInstanceOf[java.net.HttpURLConnection]
    71     conn.setRequestMethod("PUT")
    72     conn.setDoInput(true)
    73     conn.connect
    74     val reply = Source.fromInputStream(conn.getInputStream)
    75     return reply.mkString("")
    76   }
    77 
    78   def allPlayers(url : URL, okDelay : Long): Map[String,List[Node]] = {
    79     val u = new URL(url, "games");
    80     val conn = u.openConnection
    81     conn.setRequestProperty("Accept", "text/xml")
    82 
    83     val ret = new HashMap[String,List[Node]]
    84 
    85     val before = System.currentTimeMillis - okDelay
    86     val games = XML.load(conn.getInputStream)
    87     for (val g : Node <- games \\ "gameId") {
    88       val modified = (g \\ "@modified").text.toLong
    89       if (modified < before) {
    90         val status = (g \\ "@status").text;
    91         val name =
    92           if (status == "blackMove") {
    93             (g \\ "@black").text
    94           } else if (status == "whiteMove") {
    95             (g \\ "@white").text
    96           } else {
    97             null
    98           }
    99         if (name != null) {
   100           val prev = ret.getOrElse(name, Nil)
   101           val next = g :: prev
   102           ret.put(name, next);
   103         }
   104       }
   105     }
   106     return ret;
   107   }
   108 
   109   def emails(url : URL, loginID : String, ids : Collection[String]): List[String] = {
   110     var ret : List[String] = Nil;
   111     for (val p <- ids) {
   112       val u = new URL(url, "users/" + p + "?loginID=" + loginID)
   113       val conn = u.openConnection
   114       conn.setRequestProperty("Accept", "text/xml")
   115       val xml = XML.load(conn.getInputStream)
   116       val res = (xml \\ "property").filter({
   117           n => n.attribute("name") match {
   118             case Some(attr) => attr.text == "email"
   119             case _ => false
   120           }
   121       })
   122       if (!res.isEmpty) {
   123         ret = res(0).text :: ret;
   124       }
   125     }
   126     return ret
   127   }
   128 }