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