Skip to content
Browse files

Add IrcClient struct.

  • Loading branch information...
1 parent c3e881c commit 440a2821cf2d118b143e4541a0c3bf65cf93f1f9 @kentaro committed May 2, 2014
Showing with 78 additions and 5 deletions.
  1. +58 −0 lib/irc_client.go
  2. +8 −2 lib/message.go
  3. +10 −1 lib/message_test.go
  4. +2 −2 lib/queue_test.go
View
58 lib/irc_client.go
@@ -0,0 +1,58 @@
+package inkfish
+
+import (
+ "fmt"
+ "github.com/thoj/go-ircevent"
+ "log"
+)
+
+type IrcClient struct {
+ client *irc.Connection
+ option *Option
+}
+
+func NewIrcClient(option *Option) (ircClient *IrcClient) {
+ client := irc.IRC(option.Nickname, option.User)
+
+ if option.Keyword != "" {
+ client.Password = option.Keyword
+ }
+
+ ircClient = &IrcClient{client: client, option: option}
+ return
+}
+
+func (self *IrcClient) Run() {
+ err := self.client.Connect(fmt.Sprintf("%s:%d", self.option.Host, self.option.Port))
+ defer func() {
+ log.Println("INFO: Disconnect")
+ self.client.Disconnect()
+ }()
+
+ if err != nil {
+ log.Fatalf("ERROR: %s\n", err.Error())
+ }
+
+ go self.handleError()
+}
+
+func (self *IrcClient) join(message *Message) {
+ self.client.Join("#" + message.Channel)
+}
+
+func (self *IrcClient) leave(message *Message) {
+ self.client.Part("#" + message.Channel)
+}
+
+func (self *IrcClient) notice(message *Message) {
+ self.client.Notice("#"+message.Channel, message.Body)
+}
+
+func (self *IrcClient) privmsg(message *Message) {
+ self.client.Privmsg("#"+message.Channel, message.Body)
+}
+
+func (self *IrcClient) handleError() {
+ err := <-self.client.Error
+ log.Printf("ERROR: %s", err.Error())
+}
View
10 lib/message.go
@@ -2,9 +2,15 @@ package inkfish
type Message struct {
Command string
+ Channel string
Body string
}
-func NewMessage(command, body string) (message *Message) {
- return &Message{Command: command, Body: body}
+func NewMessage(command, channel, body string) (message *Message) {
+ message = &Message{
+ Command: command,
+ Channel: channel,
+ Body: body,
+ }
+ return
}
View
11 lib/message_test.go
@@ -5,7 +5,7 @@ import (
)
func TestMessage(t *testing.T) {
- message := NewMessage("join", "test")
+ message := NewMessage("join", "test", "test")
{
actual := message.Command
@@ -17,6 +17,15 @@ func TestMessage(t *testing.T) {
}
{
+ actual := message.Channel
+ expected := "test"
+
+ if actual != expected {
+ t.Fatalf("Message.Channel is not correctly set: %v", actual)
+ }
+ }
+
+ {
actual := message.Body
expected := "test"
View
4 lib/queue_test.go
@@ -9,9 +9,9 @@ func TestQueue(t *testing.T) {
queue := NewQueue()
{
- go func() { queue.Push(NewMessage("join", "test")) }()
+ go func() { queue.Push(NewMessage("join", "test", "test")) }()
actual := queue.Pop()
- expected := NewMessage("join", "test")
+ expected := NewMessage("join", "test", "test")
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Message is not correctly enqueued: %+v", actual)

0 comments on commit 440a282

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