From 3c29105d04a7e89d62bccc843c6e0bdd3bd60eae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=9E=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=91=D0=B2?= Date: Fri, 5 Mar 2010 13:20:34 +0600 Subject: [PATCH] Initial import --- .gitignore | 6 + pom.xml | 147 +++++++++++++++ src/main/scala/kz/dema/scalabot/App.scala | 172 ++++++++++++++++++ src/test/scala/kz/dema/scalabot/AppTest.scala | 16 ++ src/test/scala/kz/dema/scalabot/MySpec.scala | 17 ++ 5 files changed, 358 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/scala/kz/dema/scalabot/App.scala create mode 100644 src/test/scala/kz/dema/scalabot/AppTest.scala create mode 100644 src/test/scala/kz/dema/scalabot/MySpec.scala diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5900c29 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.iml +*.ipr +*.iws +target +.project +.classpath diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b2e3766 --- /dev/null +++ b/pom.xml @@ -0,0 +1,147 @@ + + ScalaBot + 4.0.0 + kz.dema + scalabot + 1.0-SNAPSHOT + 2009 + + 2.7.7 + + + + + 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 + + + jivesoftware + smack + 3.1.0 + + + javax.servlet + servlet-api + + + + + jivesoftware + smackx + 3.1.0 + + + javax.servlet + servlet-api + + + + + + + + src/main/scala + src/test/scala + + + org.apache.maven.plugins + maven-shade-plugin + 1.2.1 + + + + + + package + + shade + + + true + jackofall + + + + + + + + + org.scala-tools + maven-scala-plugin + + + + compile + testCompile + + + + + ${scala.version} + + -target:jvm-1.5 + + + + + org.apache.maven.plugins + maven-eclipse-plugin + + true + + ch.epfl.lamp.sdt.core.scalabuilder + + + ch.epfl.lamp.sdt.core.scalanature + + + org.eclipse.jdt.launching.JRE_CONTAINER + ch.epfl.lamp.sdt.launching.SCALA_CONTAINER + + + + + + + + + org.scala-tools + maven-scala-plugin + + ${scala.version} + + + + + diff --git a/src/main/scala/kz/dema/scalabot/App.scala b/src/main/scala/kz/dema/scalabot/App.scala new file mode 100644 index 0000000..af2fbda --- /dev/null +++ b/src/main/scala/kz/dema/scalabot/App.scala @@ -0,0 +1,172 @@ +package kz.dema.scalabot + +import org.jivesoftware.smack._ +import org.jivesoftware.smack.filter._ +import org.jivesoftware.smackx.muc._ + +import packet.{Presence, Message, Packet} +import proxy.ProxyInfo +import scala.actors._ +import scala.actors.Actor._ +import scala.collection.mutable.HashMap +import scala.collection.jcl.Conversions._ +import java.util.{Timer, Date} +import java.util.TimerTask +import java.util.ArrayList + +case class BotConfiguration(serviceName: String, host: String, port: Option[Int], username: String, password: String, rooms: List[String]) + +sealed abstract class ManagerActorMessage + +case class PrivateChatMessage(packet: Packet) +case class GroupChatMessage(packet: Packet) + + +case class JoinMUC(name: String) extends ManagerActorMessage +case class LeaveMUC(name: String) extends ManagerActorMessage +case class SendResponse(to: String, body: String) extends ManagerActorMessage +case class IncomingMessage(from: String, body: String) extends ManagerActorMessage +case class UnBan(who:String) extends ManagerActorMessage +case class GetBannedPersons() extends ManagerActorMessage + +/** + * Hello world! + * + */ +object App extends Application { + try { + var configuration = BotConfiguration("jabber.ru", + "77.88.57.181", + Some(58128), + "JABBER_USER", + "PASSWORD", + List("testroom1@conference.jabber.ru")) + + val cconf = new ConnectionConfiguration(configuration.host, configuration.port.getOrElse(5222), + configuration.serviceName, + ProxyInfo.forHttpProxy("10.1.164.13", 5865, null, null)); + + val connection = new XMPPConnection(cconf); + SASLAuthentication.supportSASLMechanism("PLAIN", 0); + + connection.connect(); + connection.login(configuration.username, configuration.password); + + JabberManagerActor.start(connection) + + configuration.rooms.foreach(JabberManagerActor ! JoinMUC(_)) + + val collector: PacketCollector = connection.createPacketCollector(new PacketFilter() { + def accept(p: Packet) = true + }) + + while (true) { + try { + val packet = collector.nextResult + JabberManagerActor ! packet + } catch { + case _ => () + } + } + } catch { + case e => { + println(e) + } + } +} +object MUCEventsListener { +} + +object BanListManager extends Actor { + val timer = new Timer() + case class CheckForBanExpiration + def act = loop { + react { + case CheckForBanExpiration => { + check() + //TimerTask({}), 600 * 1000 + timer.schedule(new TimerTask(){ + def run(){ + BanListManager ! CheckForBanExpiration + } + }, 600 * 1000) + } + } + } + + def check() { + /* val banned: List[(String, List[(String, String)])] = JabberManagerActor !? JabberManagerActor.GetBannedPersons + for{item <- banned + user <- item._2 + } { + if (banExpired(user)) { + JabberManagerActor ! UnBan(item._1, user) + } + }*/ + } + +} + +object StatusProcessor extends Actor { + val statuses = HashMap[String, (Date, Int)]() //JID, Дата последней смены статуса, количество смен статуса + def act = loop { + react{ + case s=> + } + } +} +object JabberManagerActor extends Actor { + case class GetBannedPersons + + val rooms = HashMap[String, MultiUserChat]() + var connection: XMPPConnection = null + + def start(connection: XMPPConnection) { + this.connection = connection + super.start + } + + def act = loop { + react { + + case GetBannedPersons => { + for{room <- rooms.keySet} { + val chat = rooms.get(room) + (room, chat.map(c=>new ArrayList[Affiliate](c.getOutcasts).toList.map(_.getJid()))) + } + } + + case JoinMUC(roomName) => if (rooms.contains(roomName)) () else { + val muc = new MultiUserChat(connection, roomName) + muc.join("ScalaBot") + + rooms.put(roomName, muc) + } + + case LeaveMUC(room) => rooms.get(room) match { + case Some(muc) => muc.leave + case _ => () + } + case SendResponse(to, body) => () + + case IncomingMessage(from, body) => MessageProcessor ! (from, body) + + case p: Packet => processPacket(p) + } + } + + def processPacket(p: Packet) = p match { + case msg: Message => {println(List(msg.getFrom, msg.getType, msg.getBody).mkString(","))} + case p: Presence => {println(List(p.getFrom, p.getType, p.getMode, p.getStatus).mkString(","))} + case m => println(m.getClass) + } + +} +object MessageProcessor extends Actor { + def act = loop { + react { + case a => println(a) + } + } +} + diff --git a/src/test/scala/kz/dema/scalabot/AppTest.scala b/src/test/scala/kz/dema/scalabot/AppTest.scala new file mode 100644 index 0000000..2e80abd --- /dev/null +++ b/src/test/scala/kz/dema/scalabot/AppTest.scala @@ -0,0 +1,16 @@ +package kz.dema.scalabot + +import org.junit._ +import Assert._ + +@Test +class AppTest { + @Test + def testOK() = assertTrue(true) + + // @Test + // def testKO() = assertTrue(false) + +} + + diff --git a/src/test/scala/kz/dema/scalabot/MySpec.scala b/src/test/scala/kz/dema/scalabot/MySpec.scala new file mode 100644 index 0000000..b47bb8e --- /dev/null +++ b/src/test/scala/kz/dema/scalabot/MySpec.scala @@ -0,0 +1,17 @@ +package kz.dema.scalabot + +import org.specs._ +import org.specs.runner.{ConsoleRunner, JUnit4} + +class MySpecTest extends JUnit4(MySpec) +//class MySpecSuite extends ScalaTestSuite(MySpec) +object MySpecRunner extends ConsoleRunner(MySpec) + +object MySpec extends Specification { + "This wonderful system" should { + "save the world" in { + val list = Nil + list must beEmpty + } + } +}