Merge branch 'master' of git://github.com/lye/cleanirc

Conflicts:
	irc.go
	irc_callback.go
	irc_struct.go

Merge Interesting changes from fork
This commit is contained in:
Thomas Jager 2012-11-06 00:38:20 +01:00
commit be3afcc796
3 changed files with 94 additions and 54 deletions

89
irc.go
View File

@ -12,22 +12,25 @@ import (
"os" "os"
"strings" "strings"
"time" "time"
"crypto/tls"
) )
const ( const (
VERSION = "GolangBOT v1.0" VERSION = "cleanirc v1.0"
) )
var error_ bool var error_ bool
func reader(irc *Connection) { func readLoop(irc *Connection) {
br := bufio.NewReader(irc.socket) br := bufio.NewReader(irc.socket)
for !error_ {
for !irc.reconnecting {
msg, err := br.ReadString('\n') msg, err := br.ReadString('\n')
if err != nil { if err != nil {
irc.Error <-err irc.Error <-err
break break
} }
irc.lastMessage = time.Now() irc.lastMessage = time.Now()
msg = msg[0 : len(msg)-2] //Remove \r\n msg = msg[0 : len(msg)-2] //Remove \r\n
event := &Event{Raw: msg} event := &Event{Raw: msg}
@ -35,48 +38,58 @@ func reader(irc *Connection) {
if i := strings.Index(msg, " "); i > -1 { if i := strings.Index(msg, " "); i > -1 {
event.Source = msg[1:i] event.Source = msg[1:i]
msg = msg[i+1 : len(msg)] msg = msg[i+1 : len(msg)]
} else { } else {
irc.log.Printf("Misformed msg from server: %#s\n", msg) irc.log.Printf("Misformed msg from server: %#s\n", msg)
} }
if i, j := strings.Index(event.Source, "!"), strings.Index(event.Source, "@"); i > -1 && j > -1 { if i, j := strings.Index(event.Source, "!"), strings.Index(event.Source, "@"); i > -1 && j > -1 {
event.Nick = event.Source[0:i] event.Nick = event.Source[0:i]
event.User = event.Source[i+1 : j] event.User = event.Source[i+1 : j]
event.Host = event.Source[j+1 : len(event.Source)] event.Host = event.Source[j+1 : len(event.Source)]
} }
} }
args := strings.SplitN(msg, " :", 2) args := strings.SplitN(msg, " :", 2)
if len(args) > 1 { if len(args) > 1 {
event.Message = args[1] event.Message = args[1]
} }
args = strings.Split(args[0], " ") args = strings.Split(args[0], " ")
event.Code = strings.ToUpper(args[0]) event.Code = strings.ToUpper(args[0])
if len(args) > 1 { if len(args) > 1 {
event.Arguments = args[1:len(args)] event.Arguments = args[1:len(args)]
} }
/* XXX: len(args) == 0: args should be empty */
irc.RunCallbacks(event) irc.RunCallbacks(event)
} }
irc.syncreader <-true irc.syncreader <-true
} }
func writer(irc *Connection) { func writeLoop(irc *Connection) {
b, ok := <-irc.pwrite b, ok := <-irc.pwrite
for !error_ && ok { for !irc.reconnecting && ok {
if b == "" || irc.socket == nil { if b == "" || irc.socket == nil {
break break
} }
_, err := irc.socket.Write([]byte(b)) _, err := irc.socket.Write([]byte(b))
if err != nil { if err != nil {
irc.log.Printf("%s\n", err) irc.log.Printf("%s\n", err)
irc.Error <- err irc.Error <- err
break break
} }
b, ok = <-irc.pwrite b, ok = <-irc.pwrite
} }
irc.syncwriter <-true irc.syncwriter <-true
} }
//Pings the server if we have not recived any messages for 5 minutes //Pings the server if we have not recived any messages for 5 minutes
func pinger(i *Connection) { func pingLoop(i *Connection) {
i.ticker = time.Tick(1 * time.Minute) //Tick every minute. i.ticker = time.Tick(1 * time.Minute) //Tick every minute.
i.ticker2 = time.Tick(15 * time.Minute) //Tick every 15 minutes. i.ticker2 = time.Tick(15 * time.Minute) //Tick every 15 minutes.
for { for {
@ -86,7 +99,7 @@ func pinger(i *Connection) {
if time.Since(i.lastMessage) >= (4 * time.Minute) { if time.Since(i.lastMessage) >= (4 * time.Minute) {
i.SendRawf("PING %d", time.Now().UnixNano()) i.SendRawf("PING %d", time.Now().UnixNano())
} }
case <-i.ticker2: case <-irc.ticker2:
//Ping every 15 minutes. //Ping every 15 minutes.
i.SendRawf("PING %d", time.Now().UnixNano()) i.SendRawf("PING %d", time.Now().UnixNano())
//Try to recapture nickname if it's not as configured. //Try to recapture nickname if it's not as configured.
@ -145,18 +158,13 @@ func (i *Connection) Reconnect() error {
for { for {
i.log.Printf("Reconnecting to %s\n", i.server) i.log.Printf("Reconnecting to %s\n", i.server)
var err error var err error
i.socket, err = net.Dial("tcp", i.server) irc.Connect(irc.server)
if err == nil { if err == nil {
break break
} }
i.log.Printf("Error: %s\n", err) i.log.Printf("Error: %s\n", err)
} }
error_ = false error_ = false
i.log.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 return nil
} }
@ -170,34 +178,47 @@ func (i *Connection) Loop() {
error_ = true error_ = true
i.Reconnect() i.Reconnect()
} }
close(i.pwrite)
close(i.pread) close(irc.pwrite)
<-i.syncreader close(irc.pread)
<-i.syncwriter
<-irc.syncreader
<-irc.syncwriter
} }
func (i *Connection) Connect(server string) error {
i.server = server func (irc *Connection) Connect(server string) error {
i.log.Printf("Connecting to %s\n", i.server) irc.server = server
irc.log.Printf("Connecting to %s\n", i.server)
var err error var err error
i.socket, err = net.Dial("tcp", i.server) if irc.UseSSL {
irc.socket, err = tls.Dial("tcp", irc.server, irc.SSLConfig)
} else {
irc.socket, err = net.Dial("tcp", irc.server)
}
if err != nil { if err != nil {
return err return err
} }
i.log.Printf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr()) irc.log.Printf("Connected to %s (%s)\n", irc.server, irc.socket.RemoteAddr())
i.pread = make(chan string, 100) return irc.postConnect()
i.pwrite = make(chan string, 100)
i.Error = make(chan error, 10)
i.syncreader = make(chan bool)
i.syncwriter = make(chan bool)
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)
if len(i.Password) > 0 {
i.pwrite <- fmt.Sprintf("PASS %s\r\n", i.Password)
} }
func (irc *Connection) postConnect() error {
irc.pread = make(chan string, 100)
irc.pwrite = make(chan string, 100)
irc.Error = make(chan error, 10)
irc.syncreader = make(chan bool)
irc.syncwriter = make(chan bool)
go irc.readLoop()
go irc.writeLoop()
go irc.pingLoop()
if len(irc.Password) > 0 {
irc.pwrite <-fmt.Sprintf("PASS %s\r\n", irc.Password)
}
irc.pwrite <-fmt.Sprintf("NICK %s\r\n", irc.nick)
irc.pwrite <-fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", irc.user, irc.user)
return nil return nil
} }

View File

@ -8,8 +8,10 @@ import (
func (irc *Connection) AddCallback(eventcode string, callback func(*Event)) { func (irc *Connection) AddCallback(eventcode string, callback func(*Event)) {
eventcode = strings.ToUpper(eventcode) eventcode = strings.ToUpper(eventcode)
if _, ok := irc.events[eventcode]; ok { if _, ok := irc.events[eventcode]; ok {
irc.events[eventcode] = append(irc.events[eventcode], callback) irc.events[eventcode] = append(irc.events[eventcode], callback)
} else { } else {
irc.events[eventcode] = make([]func(*Event), 1) irc.events[eventcode] = make([]func(*Event), 1)
irc.events[eventcode][0] = callback irc.events[eventcode][0] = callback
@ -18,6 +20,7 @@ func (irc *Connection) AddCallback(eventcode string, callback func(*Event)) {
func (irc *Connection) ReplaceCallback(eventcode string, i int, callback func(*Event)) { func (irc *Connection) ReplaceCallback(eventcode string, i int, callback func(*Event)) {
eventcode = strings.ToUpper(eventcode) eventcode = strings.ToUpper(eventcode)
if event, ok := irc.events[eventcode]; ok { if event, ok := irc.events[eventcode]; ok {
if i < len(event) { if i < len(event) {
event[i] = callback event[i] = callback
@ -32,28 +35,37 @@ func (irc *Connection) ReplaceCallback(eventcode string, i int, callback func(*E
func (irc *Connection) RunCallbacks(event *Event) { func (irc *Connection) RunCallbacks(event *Event) {
if event.Code == "PRIVMSG" && len(event.Message) > 0 && event.Message[0] == '\x01' { if event.Code == "PRIVMSG" && len(event.Message) > 0 && event.Message[0] == '\x01' {
event.Code = "CTCP" //Unknown CTCP event.Code = "CTCP" //Unknown CTCP
if i := strings.LastIndex(event.Message, "\x01"); i > -1 { if i := strings.LastIndex(event.Message, "\x01"); i > -1 {
event.Message = event.Message[1:i] event.Message = event.Message[1:i]
} }
if event.Message == "VERSION" { if event.Message == "VERSION" {
event.Code = "CTCP_VERSION" event.Code = "CTCP_VERSION"
} else if event.Message == "TIME" { } else if event.Message == "TIME" {
event.Code = "CTCP_TIME" event.Code = "CTCP_TIME"
} else if event.Message[0:4] == "PING" { } else if event.Message[0:4] == "PING" {
event.Code = "CTCP_PING" event.Code = "CTCP_PING"
} else if event.Message == "USERINFO" { } else if event.Message == "USERINFO" {
event.Code = "CTCP_USERINFO" event.Code = "CTCP_USERINFO"
} else if event.Message == "CLIENTINFO" { } else if event.Message == "CLIENTINFO" {
event.Code = "CTCP_CLIENTINFO" event.Code = "CTCP_CLIENTINFO"
} }
} }
if callbacks, ok := irc.events[event.Code]; ok { if callbacks, ok := irc.events[event.Code]; ok {
if irc.VerboseCallbackHandler { if irc.VerboseCallbackHandler {
irc.log.Printf("%v (%v) >> %#v\n", event.Code, len(callbacks), event) irc.log.Printf("%v (%v) >> %#v\n", event.Code, len(callbacks), event)
} }
for _, callback := range callbacks { for _, callback := range callbacks {
go callback(event) go callback(event)
} }
} else if irc.VerboseCallbackHandler { } else if irc.VerboseCallbackHandler {
irc.log.Printf("%v (0) >> %#v\n", event.Code, event) irc.log.Printf("%v (0) >> %#v\n", event.Code, event)
} }
@ -93,6 +105,7 @@ func (irc *Connection) setupCallbacks() {
irc.AddCallback("433", func(e *Event) { irc.AddCallback("433", func(e *Event) {
if len(irc.nickcurrent) > 8 { if len(irc.nickcurrent) > 8 {
irc.nickcurrent = "_" + irc.nickcurrent irc.nickcurrent = "_" + irc.nickcurrent
} else { } else {
irc.nickcurrent = irc.nickcurrent + "_" irc.nickcurrent = irc.nickcurrent + "_"
} }

View File

@ -5,23 +5,30 @@
package irc package irc
import ( import (
"crypto/tls"
"log" "log"
"net" "net"
"time" "time"
) )
type Connection struct { type Connection struct {
Error chan error
Log chan string
Password string
UseSSL bool
SSLConfig *tls.Config
socket net.Conn socket net.Conn
pread, pwrite chan string pread, pwrite chan string
Error chan error
syncreader, syncwriter chan bool syncreader, syncwriter chan bool
reconnecting bool
nick string //The nickname we want. nick string //The nickname we want.
nickcurrent string //The nickname we currently have. nickcurrent string //The nickname we currently have.
user string user string
registered bool registered bool
server string server string
Password string events map[string][]func(*IRCEvent)
events map[string][]func(*Event)
lastMessage time.Time lastMessage time.Time
ticker <-chan time.Time ticker <-chan time.Time
@ -41,6 +48,5 @@ type Event struct {
Host string //<nick>!<usr>@<host> Host string //<nick>!<usr>@<host>
Source string //<host> Source string //<host>
User string //<usr> User string //<usr>
Arguments []string Arguments []string
} }