Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
Дмитрий Оленёв committed Mar 5, 2010
0 parents commit 3c29105
Show file tree
Hide file tree
Showing 5 changed files with 358 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.iml
*.ipr
*.iws
target
.project
.classpath
147 changes: 147 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<name>ScalaBot</name>
<modelVersion>4.0.0</modelVersion>
<groupId>kz.dema</groupId>
<artifactId>scalabot</artifactId>
<version>1.0-SNAPSHOT</version>
<inceptionYear>2009</inceptionYear>
<properties>
<scala.version>2.7.7</scala.version>
</properties>

<repositories>
<repository>
<id>scala-tools.org</id>
<name>Scala-Tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-releases</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>scala-tools.org</id>
<name>Scala-Tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-releases</url>
</pluginRepository>
</pluginRepositories>

<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.specs</groupId>
<artifactId>specs</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jivesoftware</groupId>
<artifactId>smack</artifactId>
<version>3.1.0</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>jivesoftware</groupId>
<artifactId>smackx</artifactId>
<version>3.1.0</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>

</dependencies>

<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<!-- put your configurations here -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>jackofall</shadedClassifierName>
<!-- Any name that makes sense -->
</configuration>
</execution>
</executions>

</plugin>

<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
<args>
<arg>-target:jvm-1.5</arg>
</args>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<buildcommands>
<buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand>
</buildcommands>
<additionalProjectnatures>
<projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature>
</additionalProjectnatures>
<classpathContainers>
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
<classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer>
</classpathContainers>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
172 changes: 172 additions & 0 deletions src/main/scala/kz/dema/scalabot/App.scala
Original file line number Diff line number Diff line change
@@ -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("[email protected]"))

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)
}
}
}

16 changes: 16 additions & 0 deletions src/test/scala/kz/dema/scalabot/AppTest.scala
Original file line number Diff line number Diff line change
@@ -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)

}


17 changes: 17 additions & 0 deletions src/test/scala/kz/dema/scalabot/MySpec.scala
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit 3c29105

Please sign in to comment.