2012-11-05 22:46:47 +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.
|
|
|
|
|
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2012-11-07 20:51:24 +00:00
|
|
|
"crypto/tls"
|
2012-11-05 22:46:47 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2012-11-07 20:51:24 +00:00
|
|
|
VERSION = "go-ircevent v2.0"
|
2012-11-05 22:46:47 +00:00
|
|
|
)
|
|
|
|
|
2012-11-05 23:39:31 +00:00
|
|
|
func (irc *Connection) readLoop() {
|
2012-11-05 22:46:47 +00:00
|
|
|
br := bufio.NewReader(irc.socket)
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-11-07 20:51:24 +00:00
|
|
|
for {
|
2012-11-05 22:46:47 +00:00
|
|
|
msg, err := br.ReadString('\n')
|
|
|
|
if err != nil {
|
2012-11-07 20:51:24 +00:00
|
|
|
irc.Error <- err
|
2012-11-05 22:46:47 +00:00
|
|
|
break
|
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
irc.lastMessage = time.Now()
|
|
|
|
msg = msg[0 : len(msg)-2] //Remove \r\n
|
|
|
|
event := &Event{Raw: msg}
|
|
|
|
if msg[0] == ':' {
|
|
|
|
if i := strings.Index(msg, " "); i > -1 {
|
|
|
|
event.Source = msg[1:i]
|
|
|
|
msg = msg[i+1 : len(msg)]
|
2012-03-22 04:08:21 +00:00
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
} else {
|
|
|
|
irc.log.Printf("Misformed msg from server: %#s\n", msg)
|
|
|
|
}
|
2012-03-22 04:08:21 +00:00
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
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)]
|
|
|
|
}
|
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
args := strings.SplitN(msg, " :", 2)
|
|
|
|
if len(args) > 1 {
|
|
|
|
event.Message = args[1]
|
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
args = strings.Split(args[0], " ")
|
|
|
|
event.Code = strings.ToUpper(args[0])
|
2012-03-22 04:08:21 +00:00
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
if len(args) > 1 {
|
|
|
|
event.Arguments = args[1:len(args)]
|
|
|
|
}
|
2012-03-22 04:08:21 +00:00
|
|
|
/* XXX: len(args) == 0: args should be empty */
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
irc.RunCallbacks(event)
|
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-11-07 20:51:24 +00:00
|
|
|
irc.syncreader <- true
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
|
|
|
|
2012-11-05 23:39:31 +00:00
|
|
|
func (irc *Connection) writeLoop() {
|
2012-11-05 22:46:47 +00:00
|
|
|
b, ok := <-irc.pwrite
|
2012-11-07 20:51:24 +00:00
|
|
|
for ok {
|
2012-11-05 22:46:47 +00:00
|
|
|
if b == "" || irc.socket == nil {
|
|
|
|
break
|
|
|
|
}
|
2012-11-11 09:37:52 +00:00
|
|
|
irc.log.Printf("--> %s\n", b)
|
2012-11-05 22:46:47 +00:00
|
|
|
_, err := irc.socket.Write([]byte(b))
|
|
|
|
if err != nil {
|
|
|
|
irc.log.Printf("%s\n", err)
|
|
|
|
irc.Error <- err
|
|
|
|
break
|
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
b, ok = <-irc.pwrite
|
|
|
|
}
|
2012-11-07 20:51:24 +00:00
|
|
|
irc.syncwriter <- true
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Pings the server if we have not recived any messages for 5 minutes
|
2012-11-05 23:39:31 +00:00
|
|
|
func (irc *Connection) pingLoop() {
|
2012-11-07 20:51:24 +00:00
|
|
|
irc.ticker = time.NewTicker(1 * time.Minute) //Tick every minute.
|
|
|
|
irc.ticker2 = time.NewTicker(15 * time.Minute) //Tick every 15 minutes.
|
2012-11-05 22:46:47 +00:00
|
|
|
for {
|
|
|
|
select {
|
2012-11-07 20:51:24 +00:00
|
|
|
case <-irc.ticker.C:
|
2012-11-05 22:46:47 +00:00
|
|
|
//Ping if we haven't recived anything from the server within 4 minutes
|
2012-11-05 23:39:31 +00:00
|
|
|
if time.Since(irc.lastMessage) >= (4 * time.Minute) {
|
|
|
|
irc.SendRawf("PING %d", time.Now().UnixNano())
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
2012-11-07 20:51:24 +00:00
|
|
|
case <-irc.ticker2.C:
|
2012-11-05 22:46:47 +00:00
|
|
|
//Ping every 15 minutes.
|
2012-11-05 23:39:31 +00:00
|
|
|
irc.SendRawf("PING %d", time.Now().UnixNano())
|
2012-11-05 22:46:47 +00:00
|
|
|
//Try to recapture nickname if it's not as configured.
|
2012-11-05 23:39:31 +00:00
|
|
|
if irc.nick != irc.nickcurrent {
|
|
|
|
irc.nickcurrent = irc.nick
|
|
|
|
irc.SendRawf("NICK %s", irc.nick)
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
2012-11-07 20:51:24 +00:00
|
|
|
case <-irc.endping:
|
|
|
|
irc.ticker.Stop()
|
|
|
|
irc.ticker2.Stop()
|
2012-11-11 09:51:14 +00:00
|
|
|
irc.syncpinger <- true
|
|
|
|
return
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *Connection) Cycle() {
|
|
|
|
irc.SendRaw("QUIT")
|
|
|
|
irc.Reconnect()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *Connection) Quit() {
|
|
|
|
irc.quitting = true
|
|
|
|
irc.SendRaw("QUIT")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *Connection) Join(channel string) {
|
|
|
|
irc.pwrite <- fmt.Sprintf("JOIN %s\r\n", channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *Connection) Part(channel string) {
|
|
|
|
irc.pwrite <- fmt.Sprintf("PART %s\r\n", channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *Connection) Notice(target, message string) {
|
|
|
|
irc.pwrite <- fmt.Sprintf("NOTICE %s :%s\r\n", target, message)
|
|
|
|
}
|
|
|
|
|
2012-11-11 09:37:52 +00:00
|
|
|
func (irc *Connection) Noticef(target, format string, a ...interface{}) {
|
|
|
|
irc.Notice(target, fmt.Sprintf(format, a...))
|
|
|
|
}
|
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
func (irc *Connection) Privmsg(target, message string) {
|
|
|
|
irc.pwrite <- fmt.Sprintf("PRIVMSG %s :%s\r\n", target, message)
|
|
|
|
}
|
|
|
|
|
2012-11-11 09:37:52 +00:00
|
|
|
func (irc *Connection) Privmsgf(target, format string, a ...interface{}) {
|
|
|
|
irc.Privmsg(target, fmt.Sprintf(format, a...))
|
|
|
|
}
|
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
func (irc *Connection) SendRaw(message string) {
|
|
|
|
irc.pwrite <- fmt.Sprintf("%s\r\n", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *Connection) SendRawf(format string, a ...interface{}) {
|
2012-11-07 20:51:24 +00:00
|
|
|
irc.SendRaw(fmt.Sprintf(format, a...))
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 11:53:08 +00:00
|
|
|
func (irc *Connection) Nick(n string) {
|
|
|
|
irc.nick = n
|
|
|
|
irc.SendRawf("NICK %s", n)
|
|
|
|
}
|
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
func (irc *Connection) GetNick() string {
|
2012-11-07 20:51:24 +00:00
|
|
|
return irc.nickcurrent
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
|
|
|
|
2012-11-05 23:39:31 +00:00
|
|
|
func (irc *Connection) Reconnect() error {
|
|
|
|
close(irc.pwrite)
|
|
|
|
close(irc.pread)
|
2012-11-07 20:51:24 +00:00
|
|
|
irc.endping <- true
|
|
|
|
irc.log.Printf("Syncing Threads\n")
|
|
|
|
irc.log.Printf("Syncing Reader\n")
|
2012-11-05 23:39:31 +00:00
|
|
|
<-irc.syncreader
|
2012-11-07 20:51:24 +00:00
|
|
|
irc.log.Printf("Syncing Writer\n")
|
2012-11-05 23:39:31 +00:00
|
|
|
<-irc.syncwriter
|
2012-11-07 20:51:24 +00:00
|
|
|
irc.log.Printf("Syncing Pinger\n")
|
|
|
|
<-irc.syncpinger
|
|
|
|
irc.log.Printf("Syncing Threads Done\n")
|
2012-11-05 22:46:47 +00:00
|
|
|
for {
|
2012-11-05 23:39:31 +00:00
|
|
|
irc.log.Printf("Reconnecting to %s\n", irc.server)
|
2012-11-05 22:46:47 +00:00
|
|
|
var err error
|
2012-11-05 23:38:20 +00:00
|
|
|
irc.Connect(irc.server)
|
2012-11-05 22:46:47 +00:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2012-11-05 23:39:31 +00:00
|
|
|
irc.log.Printf("Error: %s\n", err)
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-11-05 23:39:31 +00:00
|
|
|
func (irc *Connection) Loop() {
|
|
|
|
for !irc.quitting {
|
|
|
|
e := <-irc.Error
|
|
|
|
if irc.quitting {
|
2012-11-05 22:46:47 +00:00
|
|
|
break
|
|
|
|
}
|
2012-11-05 23:39:31 +00:00
|
|
|
irc.log.Printf("Error: %s\n", e)
|
|
|
|
irc.Reconnect()
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
close(irc.pwrite)
|
|
|
|
close(irc.pread)
|
2012-11-07 20:51:24 +00:00
|
|
|
irc.endping <- true
|
2012-11-11 09:51:14 +00:00
|
|
|
irc.log.Printf("Syncing Threads\n")
|
|
|
|
irc.log.Printf("Syncing Reader\n")
|
2012-03-22 04:02:29 +00:00
|
|
|
<-irc.syncreader
|
2012-11-11 09:51:14 +00:00
|
|
|
irc.log.Printf("Syncing Writer\n")
|
2012-03-22 04:02:29 +00:00
|
|
|
<-irc.syncwriter
|
2012-11-11 09:51:14 +00:00
|
|
|
irc.log.Printf("Syncing Pinger\n")
|
2012-11-07 20:51:24 +00:00
|
|
|
<-irc.syncpinger
|
2012-11-11 09:51:14 +00:00
|
|
|
irc.log.Printf("Syncing Threads Done\n")
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
|
|
|
|
2012-11-05 23:38:20 +00:00
|
|
|
func (irc *Connection) Connect(server string) error {
|
|
|
|
irc.server = server
|
2012-11-05 22:46:47 +00:00
|
|
|
var err error
|
2012-11-05 23:39:31 +00:00
|
|
|
irc.log.Printf("Connecting to %s\n", irc.server)
|
2012-11-07 20:55:33 +00:00
|
|
|
if irc.UseTLS {
|
|
|
|
irc.socket, err = tls.Dial("tcp", irc.server, irc.TLSConfig)
|
2012-11-05 23:38:20 +00:00
|
|
|
} else {
|
|
|
|
irc.socket, err = net.Dial("tcp", irc.server)
|
|
|
|
}
|
2012-11-05 22:46:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2012-11-05 23:38:20 +00:00
|
|
|
irc.log.Printf("Connected to %s (%s)\n", irc.server, irc.socket.RemoteAddr())
|
|
|
|
return irc.postConnect()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *Connection) postConnect() error {
|
2012-03-22 04:02:29 +00:00
|
|
|
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)
|
2012-11-07 20:51:24 +00:00
|
|
|
irc.syncpinger = make(chan bool)
|
|
|
|
irc.endping = make(chan bool)
|
2012-03-22 04:08:21 +00:00
|
|
|
go irc.readLoop()
|
|
|
|
go irc.writeLoop()
|
|
|
|
go irc.pingLoop()
|
2012-03-21 04:11:35 +00:00
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
if len(irc.Password) > 0 {
|
2012-11-07 20:51:24 +00:00
|
|
|
irc.pwrite <- fmt.Sprintf("PASS %s\r\n", irc.Password)
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
2012-11-07 20:51:24 +00:00
|
|
|
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)
|
2012-11-05 22:46:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func IRC(nick, user string) *Connection {
|
|
|
|
irc := new(Connection)
|
|
|
|
irc.registered = false
|
|
|
|
irc.pread = make(chan string, 100)
|
|
|
|
irc.pwrite = make(chan string, 100)
|
|
|
|
irc.Error = make(chan error)
|
|
|
|
irc.nick = nick
|
|
|
|
irc.user = user
|
|
|
|
irc.VerboseCallbackHandler = false
|
|
|
|
irc.log = log.New(os.Stdout, "", log.LstdFlags)
|
|
|
|
irc.setupCallbacks()
|
|
|
|
return irc
|
|
|
|
}
|