jtulach@153
|
1 |
/*
|
jaroslav@264
|
2 |
* Quoridor server and related libraries
|
jaroslav@264
|
3 |
* Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jtulach@153
|
4 |
*
|
jaroslav@264
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@264
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@264
|
7 |
* the Free Software Foundation, either version 3 of the License.
|
jtulach@153
|
8 |
*
|
jaroslav@264
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@264
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@264
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@264
|
12 |
* GNU General Public License for more details.
|
jtulach@153
|
13 |
*
|
jaroslav@264
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@264
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@264
|
16 |
* If not, see http://www.gnu.org/licenses/.
|
jtulach@153
|
17 |
*/
|
jtulach@153
|
18 |
package cz.xelfi.quoridor.emailer
|
jtulach@153
|
19 |
|
jtulach@153
|
20 |
import scala.xml._
|
jtulach@153
|
21 |
import java.net.URL
|
jaroslav@244
|
22 |
import java.net.HttpURLConnection
|
jaroslav@244
|
23 |
import java.io.BufferedReader
|
jaroslav@244
|
24 |
import java.io.InputStreamReader
|
jtulach@153
|
25 |
import scala.io._
|
jtulach@153
|
26 |
import scala.collection.Map
|
jtulach@153
|
27 |
import scala.collection.mutable.HashMap
|
jtulach@154
|
28 |
import javax.mail.internet._
|
jtulach@154
|
29 |
import javax.mail.Message
|
jtulach@154
|
30 |
import javax.mail.Transport
|
jtulach@153
|
31 |
|
jtulach@153
|
32 |
object Main {
|
jtulach@153
|
33 |
def main(args: Array[String]) {
|
jaroslav@158
|
34 |
if (args == null || args.size != 4) {
|
jaroslav@158
|
35 |
Console.println("Usage: emailer.jar <quoridor API URL> <login name> <password> <smtp server IP>")
|
jaroslav@158
|
36 |
return
|
jaroslav@158
|
37 |
}
|
jtulach@153
|
38 |
|
jaroslav@158
|
39 |
val api = new URL(args(0));
|
jaroslav@158
|
40 |
|
jtulach@228
|
41 |
val loginID = login(api, args(1), args(2))
|
jtulach@154
|
42 |
|
jtulach@228
|
43 |
val okDelay = 3600 * 1000 * 24
|
jtulach@154
|
44 |
val props = java.lang.System.getProperties
|
jaroslav@158
|
45 |
props.put("mail.smtp.host", args(3));
|
jtulach@154
|
46 |
|
jtulach@240
|
47 |
val session = if (args(3).equals("print")) null else javax.mail.Session.getDefaultInstance(props, null)
|
jtulach@154
|
48 |
|
jtulach@228
|
49 |
val notify = new Notify() {
|
jtulach@240
|
50 |
protected def allPlayers(): Map[String,List[Info]] = {
|
jtulach@228
|
51 |
val u = new URL(api, "games");
|
jtulach@228
|
52 |
val conn = u.openConnection
|
jtulach@228
|
53 |
conn.setRequestProperty("Accept", "text/xml")
|
jtulach@154
|
54 |
|
jtulach@240
|
55 |
val ret = new HashMap[String,List[Info]]
|
jtulach@154
|
56 |
|
jtulach@228
|
57 |
val before = System.currentTimeMillis - okDelay
|
jtulach@228
|
58 |
val games = XML.load(conn.getInputStream)
|
jtulach@228
|
59 |
for (val g : Node <- games \\ "gameId") {
|
jtulach@228
|
60 |
val modified = (g \\ "@modified").text.toLong
|
jtulach@228
|
61 |
if (modified < before) {
|
jtulach@228
|
62 |
val status = (g \\ "@status").text;
|
jtulach@228
|
63 |
val name =
|
jtulach@228
|
64 |
if (status == "blackMove") {
|
jtulach@228
|
65 |
(g \\ "@black").text
|
jtulach@228
|
66 |
} else if (status == "whiteMove") {
|
jtulach@228
|
67 |
(g \\ "@white").text
|
jtulach@228
|
68 |
} else {
|
jtulach@228
|
69 |
null
|
jtulach@228
|
70 |
}
|
jtulach@228
|
71 |
if (name != null) {
|
jtulach@228
|
72 |
val prev = ret.getOrElse(name, Nil)
|
jtulach@240
|
73 |
val id = (g \\ "@id").text
|
jaroslav@244
|
74 |
val info = new Info(modified, id, name)
|
jtulach@240
|
75 |
val next = info :: prev
|
jtulach@228
|
76 |
ret.put(name, next);
|
jtulach@228
|
77 |
}
|
jtulach@228
|
78 |
}
|
jtulach@228
|
79 |
}
|
jtulach@228
|
80 |
return ret;
|
jtulach@228
|
81 |
}
|
jtulach@228
|
82 |
|
jtulach@240
|
83 |
protected def emails(ids : Collection[String]): Map[String,String] = {
|
jtulach@240
|
84 |
var ret = new HashMap[String,String]
|
jtulach@228
|
85 |
for (val p <- ids) {
|
jtulach@228
|
86 |
val u = new URL(api, "users/" + p + "?loginID=" + loginID)
|
jtulach@228
|
87 |
val conn = u.openConnection
|
jtulach@228
|
88 |
conn.setRequestProperty("Accept", "text/xml")
|
jtulach@228
|
89 |
val xml = XML.load(conn.getInputStream)
|
jtulach@228
|
90 |
val res = (xml \\ "property").filter({
|
jtulach@228
|
91 |
n => n.attribute("name") match {
|
jtulach@228
|
92 |
case Some(attr) => attr.text == "email"
|
jtulach@228
|
93 |
case _ => false
|
jtulach@228
|
94 |
}
|
jtulach@228
|
95 |
})
|
jtulach@228
|
96 |
if (!res.isEmpty) {
|
jtulach@240
|
97 |
ret.put(p, res(0).text)
|
jtulach@228
|
98 |
}
|
jtulach@228
|
99 |
}
|
jtulach@228
|
100 |
return ret
|
jtulach@228
|
101 |
}
|
jtulach@228
|
102 |
|
jtulach@228
|
103 |
protected def sendEmail(address : String, subject : String, text : String) : Unit = {
|
jtulach@240
|
104 |
Console.println("Sending message to")
|
jtulach@240
|
105 |
Console.println(" email: " + address.split('@')(0) + "@... ")
|
jtulach@240
|
106 |
Console.println(" subject: " + subject)
|
jtulach@240
|
107 |
Console.println(" " + text)
|
jtulach@240
|
108 |
if (session == null) {
|
jtulach@240
|
109 |
return;
|
jtulach@240
|
110 |
}
|
jtulach@240
|
111 |
|
jtulach@154
|
112 |
val message = new MimeMessage(session)
|
jtulach@154
|
113 |
message.setFrom(new InternetAddress("quoridor@xelfi.cz"))
|
jtulach@154
|
114 |
message.addRecipient(Message.RecipientType.TO, new InternetAddress(address))
|
jtulach@228
|
115 |
message.setSubject(subject)
|
jtulach@228
|
116 |
message.setText(text)
|
jtulach@154
|
117 |
|
jtulach@154
|
118 |
Transport.send(message)
|
jtulach@228
|
119 |
}
|
jaroslav@244
|
120 |
|
jaroslav@244
|
121 |
protected def resignGame(id : Info): Unit = {
|
jaroslav@244
|
122 |
val u = new URL(api, "games/" + id.gameId + "?loginID=" + loginID + "&player=" + id.toMove + "&move=RESIGN")
|
jaroslav@244
|
123 |
val c = u.openConnection.asInstanceOf[HttpURLConnection]
|
jaroslav@244
|
124 |
c.setRequestMethod("PUT")
|
jaroslav@244
|
125 |
c.connect
|
jaroslav@244
|
126 |
val is = c.getInputStream
|
jaroslav@244
|
127 |
val r = new BufferedReader(new InputStreamReader(is))
|
jaroslav@244
|
128 |
var goOn = true;
|
jaroslav@244
|
129 |
while (goOn) {
|
jaroslav@244
|
130 |
val s = r.readLine()
|
jaroslav@244
|
131 |
if (s == null) {
|
jaroslav@244
|
132 |
goOn = false;
|
jaroslav@244
|
133 |
} else {
|
jaroslav@244
|
134 |
Console.println(s);
|
jaroslav@244
|
135 |
}
|
jaroslav@244
|
136 |
}
|
jaroslav@244
|
137 |
}
|
jtulach@153
|
138 |
}
|
jtulach@228
|
139 |
|
jtulach@228
|
140 |
notify.process()
|
jtulach@153
|
141 |
}
|
jtulach@153
|
142 |
|
jtulach@153
|
143 |
def login(url : URL, name : String, password : String) : String = {
|
jtulach@153
|
144 |
val u = new URL(url, "login?name=" + name + "&password=" + password);
|
jtulach@153
|
145 |
val conn = u.openConnection.asInstanceOf[java.net.HttpURLConnection]
|
jtulach@153
|
146 |
conn.setRequestMethod("PUT")
|
jtulach@153
|
147 |
conn.setDoInput(true)
|
jtulach@153
|
148 |
conn.connect
|
jtulach@153
|
149 |
val reply = Source.fromInputStream(conn.getInputStream)
|
jtulach@153
|
150 |
return reply.mkString("")
|
jtulach@153
|
151 |
}
|
jtulach@153
|
152 |
}
|