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