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@154
|
53 |
val session = javax.mail.Session.getDefaultInstance(props, null)
|
jtulach@154
|
54 |
|
jtulach@228
|
55 |
val notify = new Notify() {
|
jtulach@228
|
56 |
protected def allPlayers(): Map[String,List[Node]] = {
|
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@228
|
61 |
val ret = new HashMap[String,List[Node]]
|
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@228
|
79 |
val next = g :: prev
|
jtulach@228
|
80 |
ret.put(name, next);
|
jtulach@228
|
81 |
}
|
jtulach@228
|
82 |
}
|
jtulach@228
|
83 |
}
|
jtulach@228
|
84 |
return ret;
|
jtulach@228
|
85 |
}
|
jtulach@228
|
86 |
|
jtulach@228
|
87 |
protected def emails(ids : Collection[String]): List[String] = {
|
jtulach@228
|
88 |
var ret : List[String] = Nil;
|
jtulach@228
|
89 |
for (val p <- ids) {
|
jtulach@228
|
90 |
val u = new URL(api, "users/" + p + "?loginID=" + loginID)
|
jtulach@228
|
91 |
val conn = u.openConnection
|
jtulach@228
|
92 |
conn.setRequestProperty("Accept", "text/xml")
|
jtulach@228
|
93 |
val xml = XML.load(conn.getInputStream)
|
jtulach@228
|
94 |
val res = (xml \\ "property").filter({
|
jtulach@228
|
95 |
n => n.attribute("name") match {
|
jtulach@228
|
96 |
case Some(attr) => attr.text == "email"
|
jtulach@228
|
97 |
case _ => false
|
jtulach@228
|
98 |
}
|
jtulach@228
|
99 |
})
|
jtulach@228
|
100 |
if (!res.isEmpty) {
|
jtulach@228
|
101 |
ret = res(0).text :: ret;
|
jtulach@228
|
102 |
}
|
jtulach@228
|
103 |
}
|
jtulach@228
|
104 |
return ret
|
jtulach@228
|
105 |
}
|
jtulach@228
|
106 |
|
jtulach@228
|
107 |
protected def sendEmail(address : String, subject : String, text : String) : Unit = {
|
jtulach@154
|
108 |
val message = new MimeMessage(session)
|
jtulach@154
|
109 |
message.setFrom(new InternetAddress("quoridor@xelfi.cz"))
|
jtulach@154
|
110 |
message.addRecipient(Message.RecipientType.TO, new InternetAddress(address))
|
jtulach@228
|
111 |
message.setSubject(subject)
|
jtulach@228
|
112 |
message.setText(text)
|
jtulach@154
|
113 |
|
jtulach@154
|
114 |
Transport.send(message)
|
jtulach@228
|
115 |
}
|
jtulach@153
|
116 |
}
|
jtulach@228
|
117 |
|
jtulach@228
|
118 |
notify.process()
|
jtulach@153
|
119 |
}
|
jtulach@153
|
120 |
|
jtulach@153
|
121 |
def login(url : URL, name : String, password : String) : String = {
|
jtulach@153
|
122 |
val u = new URL(url, "login?name=" + name + "&password=" + password);
|
jtulach@153
|
123 |
val conn = u.openConnection.asInstanceOf[java.net.HttpURLConnection]
|
jtulach@153
|
124 |
conn.setRequestMethod("PUT")
|
jtulach@153
|
125 |
conn.setDoInput(true)
|
jtulach@153
|
126 |
conn.connect
|
jtulach@153
|
127 |
val reply = Source.fromInputStream(conn.getInputStream)
|
jtulach@153
|
128 |
return reply.mkString("")
|
jtulach@153
|
129 |
}
|
jtulach@153
|
130 |
}
|