# HG changeset patch # User Jaroslav Tulach # Date 1259519762 -3600 # Node ID 0d90e4cb917504ee31b46d3f9bbc20574dce162d # Parent e3d6e326eac14629c10b7b823123bdb2d284027f# Parent 0e8b21fcaeb0ae72d0f5413f2d6233273f3ee9c8 Merging the scala-emailer to default branch as things seem to work OK. diff -r e3d6e326eac1 -r 0d90e4cb9175 emailer/all-zip.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/emailer/all-zip.xml Sun Nov 29 19:36:02 2009 +0100 @@ -0,0 +1,19 @@ + + + all + + zip + + + + false + lib + + + + + target/emailer-${version}.jar + / + + + diff -r e3d6e326eac1 -r 0d90e4cb9175 emailer/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/emailer/pom.xml Sun Nov 29 19:36:02 2009 +0100 @@ -0,0 +1,128 @@ + + + 4.0.0 + + all-quoridor + org.apidesign + 1.0 + + org.apidesign + emailer + 1.0 + 2008 + + src/main/scala + src/test/scala + + + org.scala-tools + maven-scala-plugin + + + + compile + testCompile + + + + + ${scala.version} + + -target:jvm-1.5 + + + + + maven-assembly-plugin + 2.2-beta-2 + + + create-executable-jar + package + + single + + + + all-zip.xml + + emailer-${version} + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + true + lib/ + cz.xelfi.quoridor.emailer.Main + + + + + + + + + scala-tools.org + Scala-Tools Maven2 Repository + http://scala-tools.org/repo-releases + + + + + scala-tools.org + Scala-Tools Maven2 Repository + http://scala-tools.org/repo-releases + + + + + org.scala-lang + scala-library + ${scala.version} + + + junit + junit + 4.4 + test + + + org.specs + specs + 1.2.5 + test + + + javax.mail + mail + 1.4.1 + + + + + + org.scala-tools + maven-scala-plugin + + ${scala.version} + + + + + + 2.7.0 + + Emails lazy players + jar + Obtains list of players without any activity for a while and sends them an email. + + + + diff -r e3d6e326eac1 -r 0d90e4cb9175 emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala Sun Nov 29 19:36:02 2009 +0100 @@ -0,0 +1,133 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Jaroslav Tulach + */ + +package cz.xelfi.quoridor.emailer + +import scala.xml._ +import java.net.URL +import scala.io._ +import scala.collection.Map +import scala.collection.mutable.HashMap +import javax.mail.internet._ +import javax.mail.Message +import javax.mail.Transport + +object Main { + def main(args: Array[String]) { + if (args == null || args.size != 4) { + Console.println("Usage: emailer.jar ") + return + } + + val api = new URL(args(0)); + + val id = login(api, args(1), args(2)) + + val ret = allPlayers( + api, + 3600 * 1000 * 24 + ) + + val props = java.lang.System.getProperties + props.put("mail.smtp.host", args(3)); + + val session = javax.mail.Session.getDefaultInstance(props, null) + + + for (val address <- emails(api, id, ret.keySet)) { + Console.println("Sending message to " + address) + + val message = new MimeMessage(session) + message.setFrom(new InternetAddress("quoridor@xelfi.cz")) + message.addRecipient(Message.RecipientType.TO, new InternetAddress(address)) + message.setSubject("Play Quoridor!") + message.setText("Visit http://quoridor.xelfi.cz") + + Transport.send(message) + } + } + + def login(url : URL, name : String, password : String) : String = { + val u = new URL(url, "login?name=" + name + "&password=" + password); + val conn = u.openConnection.asInstanceOf[java.net.HttpURLConnection] + conn.setRequestMethod("PUT") + conn.setDoInput(true) + conn.connect + val reply = Source.fromInputStream(conn.getInputStream) + return reply.mkString("") + } + + def allPlayers(url : URL, okDelay : Long): Map[String,List[Node]] = { + val u = new URL(url, "games"); + val conn = u.openConnection + conn.setRequestProperty("Accept", "text/xml") + + val ret = new HashMap[String,List[Node]] + + val before = System.currentTimeMillis - okDelay + val games = XML.load(conn.getInputStream) + for (val g : Node <- games \\ "gameId") { + val modified = (g \\ "@modified").text.toLong + if (modified < before) { + val status = (g \\ "@status").text; + val name = + if (status == "blackMove") { + (g \\ "@black").text + } else if (status == "whiteMove") { + (g \\ "@white").text + } else { + null + } + if (name != null) { + val prev = ret.getOrElse(name, Nil) + val next = g :: prev + ret.put(name, next); + } + } + } + return ret; + } + + def emails(url : URL, loginID : String, ids : Collection[String]): List[String] = { + var ret : List[String] = Nil; + for (val p <- ids) { + val u = new URL(url, "users/" + p + "?loginID=" + loginID) + val conn = u.openConnection + conn.setRequestProperty("Accept", "text/xml") + val xml = XML.load(conn.getInputStream) + val res = (xml \\ "property").filter({ + n => n.attribute("name") match { + case Some(attr) => attr.text == "email" + case _ => false + } + }) + if (!res.isEmpty) { + ret = res(0).text :: ret; + } + } + return ret + } +} diff -r e3d6e326eac1 -r 0d90e4cb9175 emailer/src/test/scala/cz/xelfi/quoridor/emailer/MainTest.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/emailer/src/test/scala/cz/xelfi/quoridor/emailer/MainTest.scala Sun Nov 29 19:36:02 2009 +0100 @@ -0,0 +1,41 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Jaroslav Tulach + */ + +package cz.xelfi.quoridor.emailer + +import org.junit._ +import Assert._ + +@Test +class MainTest { + + @Test + def testCheckLazyPlayers() = { + Main.main(null) + } +} + + diff -r e3d6e326eac1 -r 0d90e4cb9175 pom.xml --- a/pom.xml Sun Nov 29 09:12:55 2009 +0100 +++ b/pom.xml Sun Nov 29 19:36:02 2009 +0100 @@ -38,6 +38,7 @@ visidor webidor freemarkerdor + emailer Quoridor related projects Master project that agregates all quoridor related functionality.