emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala
changeset 228 3ea04696a115
parent 158 0e8b21fcaeb0
child 240 29287b0a1a0b
     1.1 --- a/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala	Sun Nov 29 19:35:08 2009 +0100
     1.2 +++ b/emailer/src/main/scala/cz/xelfi/quoridor/emailer/Main.scala	Tue Feb 16 01:22:36 2010 +0100
     1.3 @@ -44,30 +44,78 @@
     1.4  
     1.5      val api = new URL(args(0));
     1.6  
     1.7 -    val id = login(api, args(1), args(2))
     1.8 -    
     1.9 -    val ret = allPlayers(
    1.10 -      api,
    1.11 -      3600 * 1000 * 24
    1.12 -    )
    1.13 +    val loginID = login(api, args(1), args(2))
    1.14  
    1.15 +    val okDelay = 3600 * 1000 * 24
    1.16      val props = java.lang.System.getProperties
    1.17      props.put("mail.smtp.host", args(3));
    1.18  
    1.19      val session = javax.mail.Session.getDefaultInstance(props, null)
    1.20  
    1.21 +    val notify = new Notify() {
    1.22 +      protected def allPlayers(): Map[String,List[Node]] = {
    1.23 +        val u = new URL(api, "games");
    1.24 +        val conn = u.openConnection
    1.25 +        conn.setRequestProperty("Accept", "text/xml")
    1.26  
    1.27 -    for (val address <- emails(api, id, ret.keySet)) {
    1.28 -        Console.println("Sending message to " + address)
    1.29 +        val ret = new HashMap[String,List[Node]]
    1.30  
    1.31 +        val before = System.currentTimeMillis - okDelay
    1.32 +        val games = XML.load(conn.getInputStream)
    1.33 +        for (val g : Node <- games \\ "gameId") {
    1.34 +          val modified = (g \\ "@modified").text.toLong
    1.35 +          if (modified < before) {
    1.36 +            val status = (g \\ "@status").text;
    1.37 +            val name =
    1.38 +              if (status == "blackMove") {
    1.39 +                (g \\ "@black").text
    1.40 +              } else if (status == "whiteMove") {
    1.41 +                (g \\ "@white").text
    1.42 +              } else {
    1.43 +                null
    1.44 +              }
    1.45 +            if (name != null) {
    1.46 +              val prev = ret.getOrElse(name, Nil)
    1.47 +              val next = g :: prev
    1.48 +              ret.put(name, next);
    1.49 +            }
    1.50 +          }
    1.51 +        }
    1.52 +        return ret;
    1.53 +      }
    1.54 +
    1.55 +      protected def emails(ids : Collection[String]): List[String] = {
    1.56 +        var ret : List[String] = Nil;
    1.57 +        for (val p <- ids) {
    1.58 +          val u = new URL(api, "users/" + p + "?loginID=" + loginID)
    1.59 +          val conn = u.openConnection
    1.60 +          conn.setRequestProperty("Accept", "text/xml")
    1.61 +          val xml = XML.load(conn.getInputStream)
    1.62 +          val res = (xml \\ "property").filter({
    1.63 +              n => n.attribute("name") match {
    1.64 +                case Some(attr) => attr.text == "email"
    1.65 +                case _ => false
    1.66 +              }
    1.67 +          })
    1.68 +          if (!res.isEmpty) {
    1.69 +            ret = res(0).text :: ret;
    1.70 +          }
    1.71 +        }
    1.72 +        return ret
    1.73 +      }
    1.74 +
    1.75 +      protected def sendEmail(address : String, subject : String, text : String) : Unit = {
    1.76          val message = new MimeMessage(session)
    1.77          message.setFrom(new InternetAddress("quoridor@xelfi.cz"))
    1.78          message.addRecipient(Message.RecipientType.TO, new InternetAddress(address))
    1.79 -        message.setSubject("Play Quoridor!")
    1.80 -        message.setText("Visit http://quoridor.xelfi.cz")
    1.81 +        message.setSubject(subject)
    1.82 +        message.setText(text)
    1.83  
    1.84          Transport.send(message)
    1.85 +      }
    1.86      }
    1.87 +
    1.88 +    notify.process()
    1.89    }
    1.90  
    1.91    def login(url : URL, name : String, password : String) : String = {
    1.92 @@ -79,55 +127,4 @@
    1.93      val reply = Source.fromInputStream(conn.getInputStream)
    1.94      return reply.mkString("")
    1.95    }
    1.96 -
    1.97 -  def allPlayers(url : URL, okDelay : Long): Map[String,List[Node]] = {
    1.98 -    val u = new URL(url, "games");
    1.99 -    val conn = u.openConnection
   1.100 -    conn.setRequestProperty("Accept", "text/xml")
   1.101 -
   1.102 -    val ret = new HashMap[String,List[Node]]
   1.103 -
   1.104 -    val before = System.currentTimeMillis - okDelay
   1.105 -    val games = XML.load(conn.getInputStream)
   1.106 -    for (val g : Node <- games \\ "gameId") {
   1.107 -      val modified = (g \\ "@modified").text.toLong
   1.108 -      if (modified < before) {
   1.109 -        val status = (g \\ "@status").text;
   1.110 -        val name =
   1.111 -          if (status == "blackMove") {
   1.112 -            (g \\ "@black").text
   1.113 -          } else if (status == "whiteMove") {
   1.114 -            (g \\ "@white").text
   1.115 -          } else {
   1.116 -            null
   1.117 -          }
   1.118 -        if (name != null) {
   1.119 -          val prev = ret.getOrElse(name, Nil)
   1.120 -          val next = g :: prev
   1.121 -          ret.put(name, next);
   1.122 -        }
   1.123 -      }
   1.124 -    }
   1.125 -    return ret;
   1.126 -  }
   1.127 -
   1.128 -  def emails(url : URL, loginID : String, ids : Collection[String]): List[String] = {
   1.129 -    var ret : List[String] = Nil;
   1.130 -    for (val p <- ids) {
   1.131 -      val u = new URL(url, "users/" + p + "?loginID=" + loginID)
   1.132 -      val conn = u.openConnection
   1.133 -      conn.setRequestProperty("Accept", "text/xml")
   1.134 -      val xml = XML.load(conn.getInputStream)
   1.135 -      val res = (xml \\ "property").filter({
   1.136 -          n => n.attribute("name") match {
   1.137 -            case Some(attr) => attr.text == "email"
   1.138 -            case _ => false
   1.139 -          }
   1.140 -      })
   1.141 -      if (!res.isEmpty) {
   1.142 -        ret = res(0).text :: ret;
   1.143 -      }
   1.144 -    }
   1.145 -    return ret
   1.146 -  }
   1.147  }