2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
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]"
24 * Portions Copyrighted 2009 Jaroslav Tulach
27 package cz.xelfi.quoridor.emailer
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
39 def main(args: Array[String]) {
40 val api = new URL("http://quoridor.xelfi.cz/api/");
42 val id = login(api, "emailer", "otravuj")
49 val props = java.lang.System.getProperties
50 props.put("mail.smtp.host", "192.168.1.1");
52 val session = javax.mail.Session.getDefaultInstance(props, null)
55 for (val address <- emails(api, id, ret.keySet)) {
56 Console.println("Sending message to " + address)
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")
64 Transport.send(message)
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")
74 val reply = Source.fromInputStream(conn.getInputStream)
75 return reply.mkString("")
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")
83 val ret = new HashMap[String,List[Node]]
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;
92 if (status == "blackMove") {
94 } else if (status == "whiteMove") {
100 val prev = ret.getOrElse(name, Nil)
109 def emails(url : URL, loginID : String, ids : Collection[String]): List[String] = {
110 var ret : List[String] = Nil;
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"
123 ret = res(0).text :: ret;