2 * Quoridor server and related libraries
3 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://www.gnu.org/licenses/.
18 package cz.xelfi.quoridor.emailer
22 import java.net.HttpURLConnection
23 import java.io.BufferedReader
24 import java.io.InputStreamReader
26 import scala.collection.Map
27 import scala.collection.mutable.HashMap
28 import javax.mail.internet._
29 import javax.mail.Message
30 import javax.mail.Transport
33 def main(args: Array[String]) {
34 if (args == null || args.size != 4) {
35 Console.println("Usage: emailer.jar <quoridor API URL> <login name> <password> <smtp server IP>")
39 val api = new URL(args(0));
41 val loginID = login(api, args(1), args(2))
43 val okDelay = 3600 * 1000 * 24
44 val props = java.lang.System.getProperties
45 props.put("mail.smtp.host", args(3));
47 val session = if (args(3).equals("print")) null else javax.mail.Session.getDefaultInstance(props, null)
49 val notify = new Notify() {
50 protected def allPlayers(): Map[String,List[Info]] = {
51 val u = new URL(api, "games");
52 val conn = u.openConnection
53 conn.setRequestProperty("Accept", "text/xml")
55 val ret = new HashMap[String,List[Info]]
57 val before = System.currentTimeMillis - okDelay
58 val games = XML.load(conn.getInputStream)
59 for (val g : Node <- games \\ "gameId") {
60 val modified = (g \\ "@modified").text.toLong
61 if (modified < before) {
62 val status = (g \\ "@status").text;
64 if (status == "blackMove") {
66 } else if (status == "whiteMove") {
72 val prev = ret.getOrElse(name, Nil)
73 val id = (g \\ "@id").text
74 val info = new Info(modified, id, name)
75 val next = info :: prev
83 protected def emails(ids : Collection[String]): Map[String,String] = {
84 var ret = new HashMap[String,String]
86 val u = new URL(api, "users/" + p + "?loginID=" + loginID)
87 val conn = u.openConnection
88 conn.setRequestProperty("Accept", "text/xml")
89 val xml = XML.load(conn.getInputStream)
90 val res = (xml \\ "property").filter({
91 n => n.attribute("name") match {
92 case Some(attr) => attr.text == "email"
97 ret.put(p, res(0).text)
103 protected def sendEmail(address : String, subject : String, text : String) : Unit = {
104 Console.println("Sending message to")
105 Console.println(" email: " + address.split('@')(0) + "@... ")
106 Console.println(" subject: " + subject)
107 Console.println(" " + text)
108 if (session == null) {
112 val message = new MimeMessage(session)
113 message.setFrom(new InternetAddress("quoridor@xelfi.cz"))
114 message.addRecipient(Message.RecipientType.TO, new InternetAddress(address))
115 message.setSubject(subject)
116 message.setText(text)
118 Transport.send(message)
121 protected def resignGame(id : Info): Unit = {
122 val u = new URL(api, "games/" + id.gameId + "?loginID=" + loginID + "&player=" + id.toMove + "&move=RESIGN")
123 val c = u.openConnection.asInstanceOf[HttpURLConnection]
124 c.setRequestMethod("PUT")
126 val is = c.getInputStream
127 val r = new BufferedReader(new InputStreamReader(is))
143 def login(url : URL, name : String, password : String) : String = {
144 val u = new URL(url, "login?name=" + name + "&password=" + password);
145 val conn = u.openConnection.asInstanceOf[java.net.HttpURLConnection]
146 conn.setRequestMethod("PUT")
147 conn.setDoInput(true)
149 val reply = Source.fromInputStream(conn.getInputStream)
150 return reply.mkString("")