go-ircevent/irc.go

223 lines
5.2 KiB
Go
Raw Normal View History

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 (
2011-11-07 00:26:12 +00:00
"bufio"
"fmt"
"net"
"strings"
"time"
"github.com/lye/tls"
)
const (
VERSION = "GolangBOT v1.0"
2009-11-18 00:28:12 +00:00
)
2011-11-07 00:26:12 +00:00
var error_ bool
2009-11-18 00:28:12 +00:00
func reader(irc *IRCConnection) {
br := bufio.NewReader(irc.socket)
2011-11-07 00:26:12 +00:00
for !error_ {
msg, err := br.ReadString('\n')
2009-11-18 00:28:12 +00:00
if err != nil {
irc.Error <- err
break
}
irc.lastMessage = time.Now()
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.SplitN(msg, " :", 2)
if len(args) > 1 {
event.Message = args[1]
}
args = strings.Split(args[0], " ")
event.Code = strings.ToUpper(args[0])
if len(args) > 1 {
event.Arguments = args[1:len(args)]
2009-11-18 00:28:12 +00:00
}
irc.RunCallbacks(event)
2009-11-18 00:28:12 +00:00
}
irc.syncreader <- true
2009-11-18 00:28:12 +00:00
}
func writer(irc *IRCConnection) {
b, ok := <-irc.pwrite
2011-11-07 00:26:12 +00:00
for !error_ && ok {
if b == "" || irc.socket == nil {
2010-10-11 19:17:17 +00:00
break
2010-08-06 21:25:03 +00:00
}
_, err := irc.socket.Write([]byte(b))
2009-11-18 00:28:12 +00:00
if err != nil {
fmt.Printf("%s\n", err)
irc.Error <- err
break
2009-11-25 19:21:47 +00:00
}
b, ok = <-irc.pwrite
2009-11-25 19:21:47 +00:00
}
irc.syncwriter <- true
2009-11-25 19:21:47 +00:00
}
//Pings the server if we have not recived any messages for 5 minutes
func pinger(i *IRCConnection) {
i.ticker = time.Tick(1 * time.Minute) //Tick every minute.
i.ticker2 = time.Tick(15 * time.Minute) //Tick every 15 minutes.
2009-11-18 00:28:12 +00:00
for {
select {
case <-i.ticker:
//Ping if we haven't recived anything from the server within 4 minutes
if time.Since(i.lastMessage) >= (4 * time.Minute) {
i.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano()))
2009-11-18 00:28:12 +00:00
}
case <-i.ticker2:
//Ping every 15 minutes.
i.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano()))
2011-02-03 00:16:13 +00:00
//Try to recapture nickname if it's not as configured.
if i.nick != i.nickcurrent {
i.nickcurrent = i.nick
2011-02-03 00:16:13 +00:00
i.SendRaw(fmt.Sprintf("NICK %s", i.nick))
}
2009-11-18 00:28:12 +00:00
}
}
}
func (irc *IRCConnection) Cycle() {
irc.SendRaw("QUIT")
irc.Reconnect()
}
func (irc *IRCConnection) Quit() {
irc.quitting = true
irc.SendRaw("QUIT")
}
2009-11-18 00:28:12 +00:00
func (irc *IRCConnection) Join(channel string) {
irc.pwrite <- fmt.Sprintf("JOIN %s\r\n", channel)
}
2010-10-19 12:12:17 +00:00
func (irc *IRCConnection) Part(channel string) {
irc.pwrite <- fmt.Sprintf("PART %s\r\n", channel)
2010-10-19 12:12:17 +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)
}
func (irc *IRCConnection) SendRaw(message string) {
fmt.Printf("--> %s\n", message)
irc.pwrite <- fmt.Sprintf("%s\r\n", message)
}
2011-11-07 00:26:12 +00:00
func (i *IRCConnection) Reconnect() error {
2010-10-11 19:17:17 +00:00
close(i.pwrite)
close(i.pread)
<-i.syncreader
<-i.syncwriter
for {
fmt.Printf("Reconnecting to %s\n", i.server)
2011-11-07 00:26:12 +00:00
var err error
i.socket, err = net.Dial("tcp", i.server)
if err == nil {
break
}
fmt.Printf("Error: %s\n", err)
}
2011-11-07 00:26:12 +00:00
error_ = false
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 !i.quitting {
e := <-i.Error
if i.quitting {
break
}
fmt.Printf("Error: %s\n", e)
2011-11-07 00:26:12 +00:00
error_ = true
i.Reconnect()
2009-11-25 18:17:08 +00:00
}
close(i.pwrite)
close(i.pread)
<-i.syncreader
<-i.syncwriter
2009-11-25 18:17:08 +00:00
}
2012-03-18 21:51:13 +00:00
func (i *IRCConnection) postConnect() error {
i.pread = make(chan string, 100)
i.pwrite = make(chan string, 100)
2011-11-07 00:26:12 +00:00
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)
2010-09-29 06:58:32 +00:00
}
return nil
2009-11-25 19:21:47 +00:00
}
2012-03-18 21:51:13 +00:00
func (i *IRCConnection) Connect(server string) error {
i.server = server
fmt.Printf("Connecting to %s\n", i.server)
var err error
i.socket, err = net.Dial("tcp", i.server)
if err != nil {
return err
}
fmt.Printf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr())
return i.postConnect()
}
func (i *IRCConnection) ConnectSSL(server string) error {
i.server = server
fmt.Printf("Connecting to %s over SSL\n", i.server)
var err error
2012-03-18 22:08:05 +00:00
i.socket, err = tls.Dial("tcp", i.server, i.SSLConfig)
2012-03-18 21:51:13 +00:00
if err != nil {
return err
}
fmt.Printf("Connected to %s (%s) over SSL\n", i.server, i.socket.RemoteAddr())
return i.postConnect()
}
func IRC(nick, user string) *IRCConnection {
irc := new(IRCConnection)
irc.registered = false
irc.pread = make(chan string, 100)
irc.pwrite = make(chan string, 100)
2011-11-07 00:26:12 +00:00
irc.Error = make(chan error)
irc.nick = nick
irc.user = user
irc.VerboseCallbackHandler = true
irc.setupCallbacks()
return irc
2009-11-18 00:28:12 +00:00
}