emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 25 Apr 2010 22:46:08 +0200
changeset 240 29287b0a1a0b
parent 228 3ea04696a115
child 244 32dd025e4b76
permissions -rw-r--r--
Adding description of individual lonely games. More verbose subject added too.
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
jtulach@228
    47
    val loginID = login(api, args(1), args(2))
jtulach@154
    48
jtulach@228
    49
    val okDelay = 3600 * 1000 * 24
jtulach@154
    50
    val props = java.lang.System.getProperties
jaroslav@158
    51
    props.put("mail.smtp.host", args(3));
jtulach@154
    52
jtulach@240
    53
    val session = if (args(3).equals("print")) null else javax.mail.Session.getDefaultInstance(props, null)
jtulach@154
    54
jtulach@228
    55
    val notify = new Notify() {
jtulach@240
    56
      protected def allPlayers(): Map[String,List[Info]] = {
jtulach@228
    57
        val u = new URL(api, "games");
jtulach@228
    58
        val conn = u.openConnection
jtulach@228
    59
        conn.setRequestProperty("Accept", "text/xml")
jtulach@154
    60
jtulach@240
    61
        val ret = new HashMap[String,List[Info]]
jtulach@154
    62
jtulach@228
    63
        val before = System.currentTimeMillis - okDelay
jtulach@228
    64
        val games = XML.load(conn.getInputStream)
jtulach@228
    65
        for (val g : Node <- games \\ "gameId") {
jtulach@228
    66
          val modified = (g \\ "@modified").text.toLong
jtulach@228
    67
          if (modified < before) {
jtulach@228
    68
            val status = (g \\ "@status").text;
jtulach@228
    69
            val name =
jtulach@228
    70
              if (status == "blackMove") {
jtulach@228
    71
                (g \\ "@black").text
jtulach@228
    72
              } else if (status == "whiteMove") {
jtulach@228
    73
                (g \\ "@white").text
jtulach@228
    74
              } else {
jtulach@228
    75
                null
jtulach@228
    76
              }
jtulach@228
    77
            if (name != null) {
jtulach@228
    78
              val prev = ret.getOrElse(name, Nil)
jtulach@240
    79
              val id = (g \\ "@id").text
jtulach@240
    80
              val info = new Info(modified, id)
jtulach@240
    81
              val next = info :: prev
jtulach@228
    82
              ret.put(name, next);
jtulach@228
    83
            }
jtulach@228
    84
          }
jtulach@228
    85
        }
jtulach@228
    86
        return ret;
jtulach@228
    87
      }
jtulach@228
    88
jtulach@240
    89
      protected def emails(ids : Collection[String]): Map[String,String] = {
jtulach@240
    90
        var ret = new HashMap[String,String]
jtulach@228
    91
        for (val p <- ids) {
jtulach@228
    92
          val u = new URL(api, "users/" + p + "?loginID=" + loginID)
jtulach@228
    93
          val conn = u.openConnection
jtulach@228
    94
          conn.setRequestProperty("Accept", "text/xml")
jtulach@228
    95
          val xml = XML.load(conn.getInputStream)
jtulach@228
    96
          val res = (xml \\ "property").filter({
jtulach@228
    97
              n => n.attribute("name") match {
jtulach@228
    98
                case Some(attr) => attr.text == "email"
jtulach@228
    99
                case _ => false
jtulach@228
   100
              }
jtulach@228
   101
          })
jtulach@228
   102
          if (!res.isEmpty) {
jtulach@240
   103
            ret.put(p, res(0).text)
jtulach@228
   104
          }
jtulach@228
   105
        }
jtulach@228
   106
        return ret
jtulach@228
   107
      }
jtulach@228
   108
jtulach@228
   109
      protected def sendEmail(address : String, subject : String, text : String) : Unit = {
jtulach@240
   110
        Console.println("Sending message to")
jtulach@240
   111
        Console.println("  email: " + address.split('@')(0) + "@... ")
jtulach@240
   112
        Console.println("  subject: " + subject)
jtulach@240
   113
        Console.println("  " + text)
jtulach@240
   114
        if (session == null) {
jtulach@240
   115
          return;
jtulach@240
   116
        }
jtulach@240
   117
jtulach@154
   118
        val message = new MimeMessage(session)
jtulach@154
   119
        message.setFrom(new InternetAddress("quoridor@xelfi.cz"))
jtulach@154
   120
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(address))
jtulach@228
   121
        message.setSubject(subject)
jtulach@228
   122
        message.setText(text)
jtulach@154
   123
jtulach@154
   124
        Transport.send(message)
jtulach@228
   125
      }
jtulach@153
   126
    }
jtulach@228
   127
jtulach@228
   128
    notify.process()
jtulach@153
   129
  }
jtulach@153
   130
jtulach@153
   131
  def login(url : URL, name : String, password : String) : String = {
jtulach@153
   132
    val u = new URL(url, "login?name=" + name + "&password=" + password);
jtulach@153
   133
    val conn = u.openConnection.asInstanceOf[java.net.HttpURLConnection]
jtulach@153
   134
    conn.setRequestMethod("PUT")
jtulach@153
   135
    conn.setDoInput(true)
jtulach@153
   136
    conn.connect
jtulach@153
   137
    val reply = Source.fromInputStream(conn.getInputStream)
jtulach@153
   138
    return reply.mkString("")
jtulach@153
   139
  }
jtulach@153
   140
}