Skip to content
Browse files

found the missing crlf

  • Loading branch information...
1 parent f18172f commit 4e98702b3d561712af12be9e6a15ee61ac5f888b Anders Conbere committed Feb 25, 2013
View
2 killit.sh
@@ -0,0 +1,2 @@
+#!/bin/sh
+ps aux | grep java | grep Main | awk '{print }' | xargs kill
View
81 src/main/scala/Bot.scala
@@ -1,78 +1,17 @@
package org.conbere.irc
-import java.net.InetSocketAddress
-import com.typesafe.scalalogging.log4j.Logging
-
-import akka.actor._
-import akka.util.{ ByteString, ByteStringBuilder }
-
-import Tokens._
-import ControlChars._
import Messages._
+import Tokens._
-object Bot extends Logging {
- def start(serverName:String, port:Int, responder:BotResponder) = {
- ActorSystem().actorOf(Props(new Bot(serverName, port, responder)))
- }
-}
-
-class Bot(serverName:String, port:Int, responder:BotResponder)
-extends Actor with Logging {
- val state = IO.IterateeRef.Map.async[IO.Handle]()(context.dispatcher)
- val address = new InetSocketAddress(serverName, port)
-
- def utf8(bytes:ByteString) =
- bytes.decodeString("UTF-8").trim
-
- override def preStart {
- println("Connecting to: " + address)
- IOManager(context.system).connect(address)
- }
-
- def parseMessage(str:String) =
- Parser.apply(str) match {
- case Parser.Success(message, _) =>
- Some(message)
- case _ =>
- logger.error("Could not parse: " + str)
- None
- }
-
- def writeResponseSocket(socket:IO.SocketHandle)(response:Option[Response]) = {
- response match {
- case Some(response) =>
- println("Response: " + utf8(response.byteString))
- socket.write(response.byteString)
- case _ =>
- // no response
- }
- }
-
- def receive = {
- case IO.Connected(socket, address) =>
- val writeResponse = writeResponseSocket(socket) _
-
- writeResponse(responder.onConnect)
-
- state(socket).flatMap(_ =>
- IO.repeat {
- IO.takeUntil(CRLF).map { in =>
- parseMessage(utf8(in)) match {
- case Some(message) =>
- if (responder.respondTo.isDefinedAt(message))
- writeResponse(responder.respondTo(message))
- case None =>
- // do nothing
- }
- }
- }
- )
-
- case IO.Read(socket, bytes) =>
- state(socket)(IO.Chunk(bytes))
+trait Bot {
+ val rooms:List[Room] = List()
+ val respondTo:PartialFunction[Message,Option[Response]]
+ val onConnect:Option[Response] = None
- case IO.Closed(socket, cause) =>
- state(socket)(IO.EOF)
- state -= socket
+ val defaultResponse:PartialFunction[Message,Option[Response]] = {
+ case Ping(from) =>
+ Some(Pong(from))
+ case msg@Mode(params) if !rooms.isEmpty =>
+ Some(Join(rooms))
}
}
View
20 src/main/scala/BotResponder.scala
@@ -1,20 +0,0 @@
-package org.conbere.irc
-
-import Messages._
-import Tokens._
-
-trait BotResponder {
- val rooms:List[Room] = List()
- val respondTo:PartialFunction[Message,Option[Response]]
- val onConnect:Option[Response] = None
-
- val defaultResponse:PartialFunction[Message,Option[Response]] = {
- case Ping(from) =>
- println("Ping: " + from)
- Some(Pong(from))
- case msg@Mode(params) if !rooms.isEmpty =>
- Some(Join(rooms))
- }
-
-}
-
View
84 src/main/scala/Client.scala
@@ -1,18 +1,80 @@
package org.conbere.irc
-import ControlChars._
+import java.net.InetSocketAddress
+import com.typesafe.scalalogging.log4j.Logging
+
+import akka.actor._
+import akka.util.{ ByteString, ByteStringBuilder }
+
import Tokens._
+import ControlChars._
import Messages._
-/* I'm not really happy with this class, it kind of represents
- All the necessary config data to initiate a connection
- but that responsibility seems overly broad and weird.
-*/
-class Client( val domainName:String
- , val port:Int
- , val userName:String
- , val password:String
- , val nickName:String
- , val realName:String) {
+object Client extends Logging {
+ def start(serverName:String, port:Int, responder:Bot) = {
+ ActorSystem().actorOf(Props(new Client(serverName, port, responder)))
+ }
}
+class Client(serverName:String, port:Int, responder:Bot)
+extends Actor with Logging {
+ val state = IO.IterateeRef.Map.async[IO.Handle]()(context.dispatcher)
+ val address = new InetSocketAddress(serverName, port)
+
+ def utf8(bytes:ByteString) =
+ bytes.decodeString("UTF-8").trim
+
+ override def preStart {
+ println("Connecting to: " + address)
+ IOManager(context.system).connect(address)
+ }
+
+ def parseMessage(str:String) =
+ Parser.apply(str) match {
+ case Parser.Success(message, _) =>
+ println("Received: " + message)
+ Some(message)
+ case _ =>
+ logger.error("Could not parse: " + str)
+ None
+ }
+
+ def writeResponseSocket(socket:IO.SocketHandle)(response:Option[Response]) = {
+ response match {
+ case Some(response) =>
+ println("Response: " + utf8(response.byteString))
+ socket.write((new ByteStringBuilder ++= response.byteString ++= CRLF).result)
+ case _ =>
+ // no response
+ }
+ }
+
+ def receive = {
+ case IO.Connected(socket, address) =>
+ val writeResponse = writeResponseSocket(socket) _
+
+ writeResponse(responder.onConnect)
+
+ state(socket).flatMap(_ =>
+ IO.repeat {
+ IO.takeUntil(CRLF).map { in =>
+ parseMessage(utf8(in)) match {
+ case Some(message) =>
+ if (responder.respondTo.isDefinedAt(message)) {
+ writeResponse(responder.respondTo(message))
+ }
+ case None =>
+ // do nothing
+ }
+ }
+ }
+ )
+
+ case IO.Read(socket, bytes) =>
+ state(socket)(IO.Chunk(bytes))
+
+ case IO.Closed(socket, cause) =>
+ state(socket)(IO.EOF)
+ state -= socket
+ }
+}
View
44 src/main/scala/ExampleBot.scala
@@ -5,13 +5,13 @@ import Messages._
import akka.actor._
import com.typesafe.scalalogging.log4j.Logging
-class Responder( val serverName:String
- , val nickName:String
- , val userName:String
- , val password:String
- , val realName:String
- , override val rooms:List[Room])
-extends BotResponder with Logging {
+class ExampleBot( val serverName:String
+ , val nickName:String
+ , val userName:String
+ , val password:String
+ , val realName:String
+ , override val rooms:List[Room])
+extends Bot with Logging {
val hostName = java.net.InetAddress.getLocalHost.getHostName
override val onConnect =
@@ -20,30 +20,30 @@ extends BotResponder with Logging {
User(userName, hostName, serverName, realName))
val respondTo = defaultResponse.orElse[Message,Option[Response]] {
- case PrivMsg(to, from, text) =>
- println(to + ", " + from + ", " + text)
+ case PrivMsg(from, `nickName`, text) =>
+ Some(PrivMsg(from, text))
+ case PrivMsg(from, to, text) =>
None
- case msg@Message(_, _, _) =>
- println(msg)
+ case _ =>
None
}
}
object Main extends Logging {
def main(args:Array[String]) = {
- val rooms = List(Room("#testroom", None),
- Room("#etsynomics", None))
+ val rooms = List(Room("#testroom", None))
- val responder = new Responder("irc.ny4dev.etsy.com",
- "abcdefg",
- "abcdefg",
- "all_hail_etsy",
- "Test Bot",
- rooms)
+ val server = "irc.server.com"
+ val port = 6667
- val server = Bot.start("irc.ny4dev.etsy.com", 6667, responder)
+ val responder = new ExampleBot(server,
+ "testbot",
+ "testbot",
+ "password",
+ "Test Bot",
+ rooms)
+
+ val actor = Client.start(server, port, responder)
}
}
-
-
View
16 src/main/scala/Messages.scala
@@ -38,7 +38,7 @@ object Messages {
def unapply(msg:Message) = {
msg match {
case Message(Some(Prefix(from, _, _)), Command("PRIVMSG"), List(to, text)) =>
- Some((to, from, text))
+ Some((from, to, text))
case _ =>
None
}
@@ -146,4 +146,18 @@ object Messages {
}
}
}
+
+ object Part {
+ def apply(channels:List[String]) =
+ Message(None, Command("PART"), List(channels.mkString(",")))
+
+ def unapply(msg:Message) = {
+ msg match {
+ case Message(_,Command("PART"), List(channels)) =>
+ Some(channels)
+ case _ =>
+ None
+ }
+ }
+ }
}
View
4 src/test/scala/TokensSuite.scala
@@ -1,6 +1,7 @@
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
+import akka.util.ByteString
import org.conbere.irc.Parser
import org.conbere.irc.Tokens._
@@ -9,7 +10,6 @@ import org.conbere.irc.Tokens._
class TokensSuite extends FunSuite {
test("produces messages suffixed with :") {
val userMessage = Message(None, Command("USER"), List("avibot", "opae", "opae", "Avi Bryant"))
- assert(userMessage.outputString === "USER avibot opae opae :Avi Bryant")
+ assert(userMessage.byteString === ByteString("USER avibot opae opae :Avi Bryant"))
}
}
-

0 comments on commit 4e98702

Please sign in to comment.
Something went wrong with that request. Please try again.