2009-11-18 15:03:14 +00:00
|
|
|
// Copyright 2009 Thomas Jager <mail@jager.no> All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2009-11-18 00:28:12 +00:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2010-01-06 18:32:35 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"bufio"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
VERSION = "GolangBOT v1.0"
|
2009-11-18 00:28:12 +00:00
|
|
|
)
|
|
|
|
|
2010-09-23 16:54:34 +00:00
|
|
|
var error bool
|
|
|
|
|
2009-11-18 00:28:12 +00:00
|
|
|
|
|
|
|
func reader(irc *IRCConnection) {
|
2010-01-06 18:32:35 +00:00
|
|
|
br := bufio.NewReader(irc.socket)
|
2010-09-23 16:54:34 +00:00
|
|
|
for !error {
|
2010-01-06 18:32:35 +00:00
|
|
|
msg, err := br.ReadString('\n')
|
2009-11-18 00:28:12 +00:00
|
|
|
if err != nil {
|
2010-01-06 18:32:35 +00:00
|
|
|
irc.Error <- err
|
2010-09-23 16:54:34 +00:00
|
|
|
break
|
2010-01-06 18:32:35 +00:00
|
|
|
}
|
|
|
|
irc.lastMessage = time.Seconds()
|
|
|
|
msg = msg[0 : len(msg)-2] //Remove \r\n
|
|
|
|
event := &IRCEvent{Raw: msg}
|
|
|
|
if msg[0] == ':' {
|
|
|
|
if i := strings.Index(msg, " "); i > -1 {
|
|
|
|
event.Source = msg[1:i]
|
|
|
|
msg = msg[i+1 : len(msg)]
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Misformed msg from server: %#s\n", msg)
|
|
|
|
}
|
|
|
|
if i, j := strings.Index(event.Source, "!"), strings.Index(event.Source, "@"); i > -1 && j > -1 {
|
|
|
|
event.Nick = event.Source[0:i]
|
|
|
|
event.User = event.Source[i+1 : j]
|
|
|
|
event.Host = event.Source[j+1 : len(event.Source)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args := strings.Split(msg, " :", 2)
|
|
|
|
if len(args) > 1 {
|
|
|
|
event.Message = args[1]
|
|
|
|
}
|
2010-08-06 10:49:56 +00:00
|
|
|
args = strings.Split(args[0], " ", -1)
|
2010-01-06 18:32:35 +00:00
|
|
|
event.Code = strings.ToUpper(args[0])
|
|
|
|
if len(args) > 1 {
|
|
|
|
event.Arguments = args[1:len(args)]
|
2009-11-18 00:28:12 +00:00
|
|
|
}
|
2010-01-06 18:32:35 +00:00
|
|
|
irc.RunCallbacks(event)
|
2009-11-18 00:28:12 +00:00
|
|
|
}
|
2010-09-23 16:54:34 +00:00
|
|
|
irc.syncreader <- true
|
2009-11-18 00:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func writer(irc *IRCConnection) {
|
2010-09-23 16:54:34 +00:00
|
|
|
for !error {
|
2010-03-01 16:09:03 +00:00
|
|
|
b := []byte(<-irc.pwrite)
|
2010-09-29 06:58:32 +00:00
|
|
|
if b == nil || irc.socket == nil {
|
2010-08-06 21:25:03 +00:00
|
|
|
return
|
|
|
|
}
|
2010-01-06 18:32:35 +00:00
|
|
|
_, err := irc.socket.Write(b)
|
2009-11-18 00:28:12 +00:00
|
|
|
if err != nil {
|
2010-01-06 18:32:35 +00:00
|
|
|
fmt.Printf("%s\n", err)
|
|
|
|
irc.Error <- err
|
2010-09-23 16:54:34 +00:00
|
|
|
break
|
2009-11-25 19:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-23 16:54:34 +00:00
|
|
|
irc.syncwriter <- true
|
2009-11-25 19:21:47 +00:00
|
|
|
}
|
|
|
|
|
2010-01-06 18:32:35 +00:00
|
|
|
//Pings the server if we have not recived any messages for 5 minutes
|
|
|
|
func pinger(i *IRCConnection) {
|
|
|
|
i.ticker = time.Tick(1000 * 1000 * 1000 * 60 * 4) //Every 4 minutes
|
|
|
|
i.ticker2 = time.Tick(1000 * 1000 * 1000 * 60 * 15) //Every 15 minutes
|
2009-11-18 00:28:12 +00:00
|
|
|
for {
|
|
|
|
select {
|
2010-01-06 18:32:35 +00:00
|
|
|
case <-i.ticker:
|
|
|
|
if time.Seconds()-i.lastMessage > 60*4 {
|
|
|
|
i.SendRaw(fmt.Sprintf("PING %d", time.Nanoseconds()))
|
2009-11-18 00:28:12 +00:00
|
|
|
}
|
2010-01-06 18:32:35 +00:00
|
|
|
case <-i.ticker2:
|
|
|
|
i.SendRaw(fmt.Sprintf("PING %d", time.Nanoseconds()))
|
2009-11-18 00:28:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRCConnection) Join(channel string) {
|
|
|
|
irc.pwrite <- fmt.Sprintf("JOIN %s\r\n", channel)
|
|
|
|
}
|
|
|
|
|
2009-11-18 15:26:23 +00:00
|
|
|
func (irc *IRCConnection) Notice(target, message string) {
|
|
|
|
irc.pwrite <- fmt.Sprintf("NOTICE %s :%s\r\n", target, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRCConnection) Privmsg(target, message string) {
|
|
|
|
irc.pwrite <- fmt.Sprintf("PRIVMSG %s :%s\r\n", target, message)
|
|
|
|
}
|
|
|
|
|
2010-01-06 18:32:35 +00:00
|
|
|
func (irc *IRCConnection) SendRaw(message string) {
|
|
|
|
fmt.Printf("--> %s\n", message)
|
|
|
|
irc.pwrite <- fmt.Sprintf("%s\r\n", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IRCConnection) Reconnect() os.Error {
|
2010-09-23 16:54:34 +00:00
|
|
|
<-i.syncreader
|
|
|
|
<-i.syncwriter
|
2010-01-06 18:32:35 +00:00
|
|
|
for {
|
|
|
|
fmt.Printf("Reconnecting to %s\n", i.server)
|
|
|
|
var err os.Error
|
|
|
|
i.socket, err = net.Dial("tcp", "", i.server)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fmt.Printf("Error: %s\n", err)
|
|
|
|
}
|
2010-09-23 16:54:34 +00:00
|
|
|
error = false
|
2010-01-06 18:32:35 +00:00
|
|
|
fmt.Printf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr())
|
|
|
|
go reader(i)
|
|
|
|
go writer(i)
|
|
|
|
i.pwrite <- fmt.Sprintf("NICK %s\r\n", i.nick)
|
|
|
|
i.pwrite <- fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", i.user, i.user)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IRCConnection) Loop() {
|
|
|
|
for {
|
2010-09-23 16:54:34 +00:00
|
|
|
e := <-i.Error
|
|
|
|
fmt.Printf("Error: %s\n", e)
|
|
|
|
error = true
|
2010-01-06 18:32:35 +00:00
|
|
|
i.Reconnect()
|
2009-11-25 18:17:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-06 18:32:35 +00:00
|
|
|
func (i *IRCConnection) Connect(server string) os.Error {
|
|
|
|
i.server = server
|
|
|
|
fmt.Printf("Connecting to %s\n", i.server)
|
|
|
|
var err os.Error
|
|
|
|
i.socket, err = net.Dial("tcp", "", i.server)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2009-11-25 19:21:47 +00:00
|
|
|
}
|
2010-01-06 18:32:35 +00:00
|
|
|
fmt.Printf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr())
|
|
|
|
i.pread = make(chan string, 100)
|
|
|
|
i.pwrite = make(chan string, 100)
|
|
|
|
i.Error = make(chan os.Error, 10)
|
2010-09-23 16:54:34 +00:00
|
|
|
i.syncreader = make(chan bool)
|
|
|
|
i.syncwriter = make(chan bool)
|
2010-01-06 18:32:35 +00:00
|
|
|
go reader(i)
|
|
|
|
go writer(i)
|
|
|
|
go pinger(i)
|
|
|
|
i.pwrite <- fmt.Sprintf("NICK %s\r\n", i.nick)
|
|
|
|
i.pwrite <- fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", i.user, i.user)
|
2010-09-29 06:58:32 +00:00
|
|
|
if len(i.password) > 0 {
|
|
|
|
i.pwrite <- fmt.Sprintf("PASS %s\r\n", i.password)
|
|
|
|
}
|
2010-01-06 18:32:35 +00:00
|
|
|
return nil
|
2009-11-25 19:21:47 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 06:58:32 +00:00
|
|
|
func IRC(nick, user, password string) *IRCConnection {
|
2010-01-06 18:32:35 +00:00
|
|
|
irc := new(IRCConnection)
|
|
|
|
irc.registered = false
|
|
|
|
irc.pread = make(chan string, 100)
|
|
|
|
irc.pwrite = make(chan string, 100)
|
|
|
|
irc.Error = make(chan os.Error)
|
|
|
|
irc.nick = nick
|
|
|
|
irc.user = user
|
2010-09-29 06:58:32 +00:00
|
|
|
irc.password = password
|
2010-01-06 18:32:35 +00:00
|
|
|
irc.setupCallbacks()
|
|
|
|
return irc
|
2009-11-18 00:28:12 +00:00
|
|
|
}
|