jtulach@153: /* jtulach@153: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. jtulach@153: * jtulach@153: * The contents of this file are subject to the terms of either the GNU jtulach@153: * General Public License Version 2 only ("GPL") or the Common jtulach@153: * Development and Distribution License("CDDL") (collectively, the jtulach@153: * "License"). You may not use this file except in compliance with the jtulach@153: * License. You can obtain a copy of the License at jtulach@153: * http://www.netbeans.org/cddl-gplv2.html jtulach@153: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the jtulach@153: * specific language governing permissions and limitations under the jtulach@153: * License. When distributing the software, include this License Header jtulach@153: * Notice in each file and include the License file at jtulach@153: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this jtulach@153: * particular file as subject to the "Classpath" exception as provided jtulach@153: * by Sun in the GPL Version 2 section of the License file that jtulach@153: * accompanied this code. If applicable, add the following below the jtulach@153: * License Header, with the fields enclosed by brackets [] replaced by jtulach@153: * your own identifying information: jtulach@153: * "Portions Copyrighted [year] [name of copyright owner]" jtulach@153: * jtulach@153: * Contributor(s): jtulach@153: * jtulach@153: * Portions Copyrighted 2009 Jaroslav Tulach jtulach@153: */ jtulach@153: jtulach@153: package cz.xelfi.quoridor.emailer jtulach@153: jtulach@153: import scala.xml._ jtulach@153: import java.net.URL jtulach@153: import scala.io._ jtulach@153: import scala.collection.Map jtulach@153: import scala.collection.mutable.HashMap jtulach@154: import javax.mail.internet._ jtulach@154: import javax.mail.Message jtulach@154: import javax.mail.Transport jtulach@153: jtulach@153: object Main { jtulach@153: def main(args: Array[String]) { jaroslav@158: if (args == null || args.size != 4) { jaroslav@158: Console.println("Usage: emailer.jar ") jaroslav@158: return jaroslav@158: } jtulach@153: jaroslav@158: val api = new URL(args(0)); jaroslav@158: jaroslav@158: val id = login(api, args(1), args(2)) jtulach@153: jtulach@153: val ret = allPlayers( jtulach@153: api, jtulach@153: 3600 * 1000 * 24 jtulach@153: ) jtulach@154: jtulach@154: val props = java.lang.System.getProperties jaroslav@158: props.put("mail.smtp.host", args(3)); jtulach@154: jtulach@154: val session = javax.mail.Session.getDefaultInstance(props, null) jtulach@154: jtulach@154: jtulach@153: for (val address <- emails(api, id, ret.keySet)) { jtulach@154: Console.println("Sending message to " + address) jtulach@154: jtulach@154: val message = new MimeMessage(session) jtulach@154: message.setFrom(new InternetAddress("quoridor@xelfi.cz")) jtulach@154: message.addRecipient(Message.RecipientType.TO, new InternetAddress(address)) jtulach@154: message.setSubject("Play Quoridor!") jtulach@154: message.setText("Visit http://quoridor.xelfi.cz") jtulach@154: jtulach@154: Transport.send(message) jtulach@153: } jtulach@153: } jtulach@153: jtulach@153: def login(url : URL, name : String, password : String) : String = { jtulach@153: val u = new URL(url, "login?name=" + name + "&password=" + password); jtulach@153: val conn = u.openConnection.asInstanceOf[java.net.HttpURLConnection] jtulach@153: conn.setRequestMethod("PUT") jtulach@153: conn.setDoInput(true) jtulach@153: conn.connect jtulach@153: val reply = Source.fromInputStream(conn.getInputStream) jtulach@153: return reply.mkString("") jtulach@153: } jtulach@153: jtulach@153: def allPlayers(url : URL, okDelay : Long): Map[String,List[Node]] = { jtulach@153: val u = new URL(url, "games"); jtulach@153: val conn = u.openConnection jtulach@153: conn.setRequestProperty("Accept", "text/xml") jtulach@153: jtulach@153: val ret = new HashMap[String,List[Node]] jtulach@153: jtulach@153: val before = System.currentTimeMillis - okDelay jtulach@153: val games = XML.load(conn.getInputStream) jtulach@153: for (val g : Node <- games \\ "gameId") { jtulach@153: val modified = (g \\ "@modified").text.toLong jtulach@153: if (modified < before) { jtulach@153: val status = (g \\ "@status").text; jtulach@153: val name = jtulach@153: if (status == "blackMove") { jtulach@153: (g \\ "@black").text jtulach@153: } else if (status == "whiteMove") { jtulach@153: (g \\ "@white").text jtulach@153: } else { jtulach@153: null jtulach@153: } jtulach@153: if (name != null) { jtulach@153: val prev = ret.getOrElse(name, Nil) jtulach@153: val next = g :: prev jtulach@153: ret.put(name, next); jtulach@153: } jtulach@153: } jtulach@153: } jtulach@153: return ret; jtulach@153: } jtulach@153: jtulach@153: def emails(url : URL, loginID : String, ids : Collection[String]): List[String] = { jtulach@153: var ret : List[String] = Nil; jtulach@153: for (val p <- ids) { jtulach@153: val u = new URL(url, "users/" + p + "?loginID=" + loginID) jtulach@153: val conn = u.openConnection jtulach@153: conn.setRequestProperty("Accept", "text/xml") jtulach@153: val xml = XML.load(conn.getInputStream) jtulach@153: val res = (xml \\ "property").filter({ jtulach@153: n => n.attribute("name") match { jtulach@153: case Some(attr) => attr.text == "email" jtulach@153: case _ => false jtulach@153: } jtulach@153: }) jtulach@153: if (!res.isEmpty) { jtulach@153: ret = res(0).text :: ret; jtulach@153: } jtulach@153: } jtulach@153: return ret jtulach@153: } jtulach@153: }