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"
|
2010-01-06 18:32:35 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2012-03-28 06:15:26 +00:00
|
|
|
"crypto/tls"
|
2010-01-06 18:32:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2012-03-22 03:50:21 +00:00
|
|
|
VERSION = "cleanirc v1.0"
|
2009-11-18 00:28:12 +00:00
|
|
|
)
|
|
|
|
|
2012-03-22 04:08:21 +00:00
|
|
|
func (irc *IRCConnection) readLoop() {
|
2010-01-06 18:32:35 +00:00
|
|
|
br := bufio.NewReader(irc.socket)
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-03-22 04:08:21 +00:00
|
|
|
for !irc.reconnecting {
|
2010-01-06 18:32:35 +00:00
|
|
|
msg, err := br.ReadString('\n')
|
2009-11-18 00:28:12 +00:00
|
|
|
if err != nil {
|
2012-03-22 03:57:35 +00:00
|
|
|
irc.Error <-err
|
2010-09-23 16:54:34 +00:00
|
|
|
break
|
2010-01-06 18:32:35 +00:00
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-02-25 08:52:19 +00:00
|
|
|
irc.lastMessage = time.Now()
|
2010-01-06 18:32:35 +00:00
|
|
|
msg = msg[0 : len(msg)-2] //Remove \r\n
|
|
|
|
event := &IRCEvent{Raw: msg}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2010-01-06 18:32:35 +00:00
|
|
|
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
|
|
|
|
2010-01-06 18:32:35 +00:00
|
|
|
} else {
|
2012-03-22 03:57:35 +00:00
|
|
|
irc.log(fmt.Sprintf("Misformed msg from server: %#s\n", msg))
|
2010-01-06 18:32:35 +00:00
|
|
|
}
|
2012-03-22 04:08:21 +00:00
|
|
|
|
2010-01-06 18:32:35 +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
|
|
|
|
2011-11-06 01:58:17 +00:00
|
|
|
args := strings.SplitN(msg, " :", 2)
|
2010-01-06 18:32:35 +00:00
|
|
|
if len(args) > 1 {
|
|
|
|
event.Message = args[1]
|
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2011-11-06 01:58:17 +00:00
|
|
|
args = strings.Split(args[0], " ")
|
2010-01-06 18:32:35 +00:00
|
|
|
event.Code = strings.ToUpper(args[0])
|
2012-03-22 04:08:21 +00:00
|
|
|
|
2010-01-06 18:32:35 +00:00
|
|
|
if len(args) > 1 {
|
|
|
|
event.Arguments = args[1:len(args)]
|
2009-11-18 00:28:12 +00:00
|
|
|
}
|
2012-03-22 04:08:21 +00:00
|
|
|
/* XXX: len(args) == 0: args should be empty */
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2010-01-06 18:32:35 +00:00
|
|
|
irc.RunCallbacks(event)
|
2009-11-18 00:28:12 +00:00
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-03-22 03:57:35 +00:00
|
|
|
irc.syncreader <-true
|
2009-11-18 00:28:12 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 04:08:21 +00:00
|
|
|
func (irc *IRCConnection) writeLoop() {
|
2011-04-05 08:37:17 +00:00
|
|
|
b, ok := <-irc.pwrite
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-03-22 04:08:21 +00:00
|
|
|
for !irc.reconnecting && ok {
|
2011-04-05 08:37:17 +00:00
|
|
|
if b == "" || irc.socket == nil {
|
2010-10-11 19:17:17 +00:00
|
|
|
break
|
2010-08-06 21:25:03 +00:00
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2011-04-05 08:37:17 +00:00
|
|
|
_, err := irc.socket.Write([]byte(b))
|
2009-11-18 00:28:12 +00:00
|
|
|
if err != nil {
|
2012-03-22 03:57:35 +00:00
|
|
|
irc.Error <-err
|
2010-09-23 16:54:34 +00:00
|
|
|
break
|
2009-11-25 19:21:47 +00:00
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2011-04-05 08:37:17 +00:00
|
|
|
b, ok = <-irc.pwrite
|
2009-11-25 19:21:47 +00:00
|
|
|
}
|
2012-03-22 03:57:35 +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
|
2012-03-22 04:08:21 +00:00
|
|
|
func (irc *IRCConnection) pingLoop() {
|
2012-03-22 04:02:29 +00:00
|
|
|
irc.ticker = time.Tick(1 * time.Minute) //Tick every minute.
|
|
|
|
irc.ticker2 = time.Tick(15 * time.Minute) //Tick every 15 minutes.
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2009-11-18 00:28:12 +00:00
|
|
|
for {
|
|
|
|
select {
|
2012-03-22 04:02:29 +00:00
|
|
|
case <-irc.ticker:
|
2012-03-22 04:08:21 +00:00
|
|
|
//Ping if we haven't received anything from the server within 4 minutes
|
2012-03-22 04:02:29 +00:00
|
|
|
if time.Since(irc.lastMessage) >= (4 * time.Minute) {
|
|
|
|
irc.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano()))
|
2009-11-18 00:28:12 +00:00
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
case <-irc.ticker2:
|
2010-10-16 18:43:59 +00:00
|
|
|
//Ping every 15 minutes.
|
2012-03-22 04:02:29 +00:00
|
|
|
irc.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.
|
2012-03-22 04:02:29 +00:00
|
|
|
if irc.nick != irc.nickcurrent {
|
|
|
|
irc.nickcurrent = irc.nick
|
|
|
|
irc.SendRaw(fmt.Sprintf("NICK %s", irc.nick))
|
2011-02-03 00:16:13 +00:00
|
|
|
}
|
2009-11-18 00:28:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-18 19:46:34 +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) {
|
2012-03-22 03:57:35 +00:00
|
|
|
irc.pwrite <-fmt.Sprintf("JOIN %s\r\n", channel)
|
2009-11-18 00:28:12 +00:00
|
|
|
}
|
|
|
|
|
2010-10-19 12:12:17 +00:00
|
|
|
func (irc *IRCConnection) Part(channel string) {
|
2012-03-22 03:57:35 +00:00
|
|
|
irc.pwrite <-fmt.Sprintf("PART %s\r\n", channel)
|
2010-10-19 12:12:17 +00:00
|
|
|
}
|
|
|
|
|
2009-11-18 15:26:23 +00:00
|
|
|
func (irc *IRCConnection) Notice(target, message string) {
|
2012-03-22 03:57:35 +00:00
|
|
|
irc.pwrite <-fmt.Sprintf("NOTICE %s :%s\r\n", target, message)
|
2009-11-18 15:26:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRCConnection) Privmsg(target, message string) {
|
2012-03-22 03:57:35 +00:00
|
|
|
irc.pwrite <-fmt.Sprintf("PRIVMSG %s :%s\r\n", target, message)
|
2009-11-18 15:26:23 +00:00
|
|
|
}
|
|
|
|
|
2010-01-06 18:32:35 +00:00
|
|
|
func (irc *IRCConnection) SendRaw(message string) {
|
2012-03-22 03:57:35 +00:00
|
|
|
irc.log(fmt.Sprintf("--> %s\n", message))
|
|
|
|
irc.pwrite <-fmt.Sprintf("%s\r\n", message)
|
2010-01-06 18:32:35 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
func (irc *IRCConnection) Reconnect() error {
|
2012-03-22 04:08:21 +00:00
|
|
|
irc.reconnecting = true
|
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
close(irc.pwrite)
|
|
|
|
close(irc.pread)
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
<-irc.syncreader
|
|
|
|
<-irc.syncwriter
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2010-01-06 18:32:35 +00:00
|
|
|
for {
|
2012-03-22 04:02:29 +00:00
|
|
|
irc.log(fmt.Sprintf("Reconnecting to %s\n", irc.server))
|
2012-03-22 03:57:35 +00:00
|
|
|
|
2011-11-07 00:26:12 +00:00
|
|
|
var err error
|
2012-03-22 04:02:29 +00:00
|
|
|
irc.socket, err = net.Dial("tcp", irc.server)
|
2010-01-06 18:32:35 +00:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2012-03-22 03:57:35 +00:00
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
irc.log(fmt.Sprintf("Error: %s\n", err))
|
2010-01-06 18:32:35 +00:00
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-03-22 04:08:21 +00:00
|
|
|
irc.reconnecting = false
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
irc.log(fmt.Sprintf("Connected to %s (%s)\n", irc.server, irc.socket.RemoteAddr()))
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-03-22 04:08:21 +00:00
|
|
|
go irc.readLoop()
|
|
|
|
go irc.writeLoop()
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-03-22 04:02:29 +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-03-22 03:50:21 +00:00
|
|
|
|
2010-01-06 18:32:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
func (irc *IRCConnection) Loop() {
|
|
|
|
for !irc.quitting {
|
|
|
|
e := <-irc.Error
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
if irc.quitting {
|
2010-10-18 19:46:34 +00:00
|
|
|
break
|
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
irc.log(fmt.Sprintf("Error: %s\n", e))
|
|
|
|
irc.Reconnect()
|
2009-11-25 18:17:08 +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-03-22 03:50:21 +00:00
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
<-irc.syncreader
|
|
|
|
<-irc.syncwriter
|
2009-11-25 18:17:08 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
func (irc *IRCConnection) 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)
|
2012-03-21 04:11:35 +00:00
|
|
|
|
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 {
|
|
|
|
irc.pwrite <-fmt.Sprintf("PASS %s\r\n", irc.Password)
|
2010-09-29 06:58:32 +00:00
|
|
|
}
|
2012-03-21 04:11:35 +00:00
|
|
|
|
2012-03-22 04:02:29 +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)
|
2010-01-06 18:32:35 +00:00
|
|
|
return nil
|
2009-11-25 19:21:47 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
func (irc *IRCConnection) Connect(server string) error {
|
|
|
|
irc.server = server
|
|
|
|
irc.log(fmt.Sprintf("Connecting to %s\n", irc.server))
|
2012-03-22 03:57:35 +00:00
|
|
|
|
2012-03-18 21:51:13 +00:00
|
|
|
var err error
|
2012-03-22 04:02:29 +00:00
|
|
|
irc.socket, err = net.Dial("tcp", irc.server)
|
2012-03-18 21:51:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2012-03-22 03:57:35 +00:00
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
irc.log(fmt.Sprintf("Connected to %s (%s)\n", irc.server, irc.socket.RemoteAddr()))
|
|
|
|
return irc.postConnect()
|
2012-03-18 21:51:13 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
func (irc *IRCConnection) ConnectSSL(server string) error {
|
|
|
|
irc.server = server
|
|
|
|
irc.log(fmt.Sprintf("Connecting to %s over SSL\n", irc.server))
|
2012-03-22 03:57:35 +00:00
|
|
|
|
2012-03-18 21:51:13 +00:00
|
|
|
var err error
|
2012-03-22 04:02:29 +00:00
|
|
|
irc.socket, err = tls.Dial("tcp", irc.server, irc.SSLConfig)
|
2012-03-22 03:57:35 +00:00
|
|
|
|
2012-03-18 21:51:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2012-03-22 03:57:35 +00:00
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
irc.log(fmt.Sprintf("Connected to %s (%s) over SSL\n", irc.server, irc.socket.RemoteAddr()))
|
2012-03-22 03:57:35 +00:00
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
return irc.postConnect()
|
2012-03-18 21:51:13 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 04:02:29 +00:00
|
|
|
func (irc *IRCConnection) log(msg string) {
|
|
|
|
if irc.Log != nil {
|
|
|
|
irc.Log <-msg
|
2012-03-22 03:57:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-22 04:08:21 +00:00
|
|
|
/* XXX: Change ctor name */
|
2010-09-29 07:52:43 +00:00
|
|
|
func IRC(nick, user 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)
|
2011-11-07 00:26:12 +00:00
|
|
|
irc.Error = make(chan error)
|
2010-01-06 18:32:35 +00:00
|
|
|
irc.nick = nick
|
|
|
|
irc.user = user
|
2011-05-22 04:06:22 +00:00
|
|
|
irc.VerboseCallbackHandler = true
|
2010-01-06 18:32:35 +00:00
|
|
|
irc.setupCallbacks()
|
|
|
|
return irc
|
2009-11-18 00:28:12 +00:00
|
|
|
}
|