Cleanup old files

This commit is contained in:
Thomas Jager 2012-11-05 23:41:04 +01:00
parent 4e254c18e1
commit ddf0508cdb
8 changed files with 2 additions and 422 deletions

View File

@ -1,6 +0,0 @@
include $(GOROOT)/src/Make.inc
TARG=irc
GOFILES=irc.go irc_struct.go irc_callback.go
include $(GOROOT)/src/Make.pkg

View File

@ -14,14 +14,11 @@ Features
Install
----------
$ git clone git://github.com/thoj/Go-IRC-Client-Library.git
$ cd Go-IRC-Client-Library
$ make
$ make install
$ go get github.com/thoj/go-ircevent
Example
----------
See example/test.go
See test/irc_test.go
Events for callbacks
---------

View File

@ -1,13 +0,0 @@
include $(GOROOT)/src/Make.inc
OBJS := $(patsubst %.go,%.$O,$(wildcard *.go))
OUT := $(patsubst %.$O,%,$(OBJS))
all: $(OBJS)
%.$O: %.go
$(GC) $<
$(LD) -o $(patsubst %.$O,%,$@) $@
clean:
rm -f *.$O $(OBJS) $(OUT)

View File

@ -1,31 +0,0 @@
package main
import (
// irc "github.com/thoj/Go-IRC-Client-Library"
"fmt"
"os"
"irc"
)
func main() {
irccon := irc.IRC("testgo", "testgo")
err := irccon.Connect("irc.efnet.net:6667")
if err != nil {
fmt.Printf("%s\n", err)
fmt.Printf("%#v\n", irccon)
os.Exit(1)
}
irccon.AddCallback("001", func(e *irc.Event) { irccon.Join("#testgo1") })
irccon.AddCallback("001", func(e *irc.Event) { irccon.Join("#testgo2") })
irccon.AddCallback("001", func(e *irc.Event) { irccon.Join("#testgo3") })
irccon.AddCallback("001", func(e *irc.Event) { irccon.Join("#testgo4") })
irccon.AddCallback("001", func(e *irc.Event) { irccon.Join("#testgo5") })
irccon.AddCallback("001", func(e *irc.Event) { irccon.Join("#testgo6") })
irccon.ReplaceCallback("001", 0, func(e *irc.Event) { irccon.Join("#testgo01") })
irccon.ReplaceCallback("001", 1, func(e *irc.Event) { irccon.Join("#testgo02") })
irccon.ReplaceCallback("001", 2, func(e *irc.Event) { irccon.Join("#testgo03") })
irccon.ReplaceCallback("001", 3, func(e *irc.Event) { irccon.Join("#testgo04") })
irccon.ReplaceCallback("001", 4, func(e *irc.Event) { irccon.Join("#testgo05") })
irccon.ReplaceCallback("001", 6, func(e *irc.Event) { irccon.Join("#testgo06") })
irccon.Loop()
}

205
irc.go
View File

@ -1,205 +0,0 @@
// 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"
"fmt"
"net"
"strings"
"time"
)
const (
VERSION = "GolangBOT v1.0"
)
var error_ bool
func reader(irc *Connection) {
br := bufio.NewReader(irc.socket)
for !error_ {
msg, err := br.ReadString('\n')
if err != nil {
irc.Error <- err
break
}
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)]
} 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)]
}
irc.RunCallbacks(event)
}
irc.syncreader <- true
}
func writer(irc *Connection) {
b, ok := <-irc.pwrite
for !error_ && ok {
if b == "" || irc.socket == nil {
break
}
_, err := irc.socket.Write([]byte(b))
if err != nil {
fmt.Printf("%s\n", err)
irc.Error <- err
break
}
b, ok = <-irc.pwrite
}
irc.syncwriter <- true
}
//Pings the server if we have not recived any messages for 5 minutes
func pinger(i *Connection) {
i.ticker = time.Tick(1 * time.Minute) //Tick every minute.
i.ticker2 = time.Tick(15 * time.Minute) //Tick every 15 minutes.
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()))
}
case <-i.ticker2:
//Ping every 15 minutes.
i.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano()))
//Try to recapture nickname if it's not as configured.
if i.nick != i.nickcurrent {
i.nickcurrent = i.nick
i.SendRaw(fmt.Sprintf("NICK %s", i.nick))
}
}
}
}
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)
}
func (irc *Connection) Privmsg(target, message string) {
irc.pwrite <- fmt.Sprintf("PRIVMSG %s :%s\r\n", target, message)
}
func (irc *Connection) SendRaw(message string) {
fmt.Printf("--> %s\n", message)
irc.pwrite <- fmt.Sprintf("%s\r\n", message)
}
func (i *Connection) Reconnect() error {
close(i.pwrite)
close(i.pread)
<-i.syncreader
<-i.syncwriter
for {
fmt.Printf("Reconnecting to %s\n", i.server)
var err error
i.socket, err = net.Dial("tcp", i.server)
if err == nil {
break
}
fmt.Printf("Error: %s\n", err)
}
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 *Connection) Loop() {
for !i.quitting {
e := <-i.Error
if i.quitting {
break
}
fmt.Printf("Error: %s\n", e)
error_ = true
i.Reconnect()
}
close(i.pwrite)
close(i.pread)
<-i.syncreader
<-i.syncwriter
}
func (i *Connection) 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())
i.pread = make(chan string, 100)
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)
}
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 = true
irc.setupCallbacks()
return irc
}

View File

@ -1,118 +0,0 @@
package irc
import (
"fmt"
"strconv"
"strings"
"time"
)
func (irc *Connection) AddCallback(eventcode string, callback func(*Event)) {
eventcode = strings.ToUpper(eventcode)
if _, ok := irc.events[eventcode]; ok {
irc.events[eventcode] = append(irc.events[eventcode], callback)
} else {
irc.events[eventcode] = make([]func(*Event), 1)
irc.events[eventcode][0] = callback
}
}
func (irc *Connection) ReplaceCallback(eventcode string, i int, callback func(*Event)) {
eventcode = strings.ToUpper(eventcode)
if event, ok := irc.events[eventcode]; ok {
if i < len(event) {
event[i] = callback
return
}
fmt.Printf("Event found, but no callback found at index %d. Use AddCallback\n", i)
return
}
fmt.Printf("Event not found. Use AddCallBack\n")
}
func (irc *Connection) RunCallbacks(event *Event) {
if event.Code == "PRIVMSG" && len(event.Message) > 0 && event.Message[0] == '\x01' {
event.Code = "CTCP" //Unknown CTCP
if i := strings.LastIndex(event.Message, "\x01"); i > -1 {
event.Message = event.Message[1:i]
}
if event.Message == "VERSION" {
event.Code = "CTCP_VERSION"
} else if event.Message == "TIME" {
event.Code = "CTCP_TIME"
} else if event.Message[0:4] == "PING" {
event.Code = "CTCP_PING"
} else if event.Message == "USERINFO" {
event.Code = "CTCP_USERINFO"
} else if event.Message == "CLIENTINFO" {
event.Code = "CTCP_CLIENTINFO"
}
}
if callbacks, ok := irc.events[event.Code]; ok {
if irc.VerboseCallbackHandler {
fmt.Printf("%v (%v) >> %#v\n", event.Code, len(callbacks), event)
}
for _, callback := range callbacks {
go callback(event)
}
} else if irc.VerboseCallbackHandler {
fmt.Printf("%v (0) >> %#v\n", event.Code, event)
}
}
func (irc *Connection) setupCallbacks() {
irc.events = make(map[string][]func(*Event))
//Handle ping events
irc.AddCallback("PING", func(e *Event) { irc.SendRaw("PONG :" + e.Message) })
//Version handler
irc.AddCallback("CTCP_VERSION", func(e *Event) {
irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01VERSION %s\x01", e.Nick, VERSION))
})
irc.AddCallback("CTCP_USERINFO", func(e *Event) {
irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01USERINFO %s\x01", e.Nick, irc.user))
})
irc.AddCallback("CTCP_CLIENTINFO", func(e *Event) {
irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01CLIENTINFO PING VERSION TIME USERINFO CLIENTINFO\x01", e.Nick))
})
irc.AddCallback("CTCP_TIME", func(e *Event) {
ltime := time.Now()
irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01TIME %s\x01", e.Nick, ltime.String()))
})
irc.AddCallback("CTCP_PING", func(e *Event) { irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01%s\x01", e.Nick, e.Message)) })
irc.AddCallback("437", func(e *Event) {
irc.nickcurrent = irc.nickcurrent + "_"
irc.SendRaw(fmt.Sprintf("NICK %s", irc.nickcurrent))
})
irc.AddCallback("433", func(e *Event) {
if len(irc.nickcurrent) > 8 {
irc.nickcurrent = "_" + irc.nickcurrent
} else {
irc.nickcurrent = irc.nickcurrent + "_"
}
irc.SendRaw(fmt.Sprintf("NICK %s", irc.nickcurrent))
})
irc.AddCallback("PONG", func(e *Event) {
ns, _ := strconv.ParseInt(e.Message, 10, 64)
delta := time.Duration(time.Now().UnixNano() - ns)
fmt.Printf("Lag: %vs\n", delta)
})
irc.AddCallback("NICK", func(e *Event) {
if e.Nick == irc.nick {
irc.nickcurrent = e.Arguments[0]
}
})
irc.AddCallback("001", func(e *Event) {
irc.nickcurrent = e.Arguments[0]
})
}

View File

@ -1,44 +0,0 @@
// 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 (
"net"
"time"
)
type Connection struct {
socket net.Conn
pread, pwrite chan string
Error chan error
syncreader, syncwriter chan bool
nick string //The nickname we want.
nickcurrent string //The nickname we currently have.
user string
registered bool
server string
Password string
events map[string][]func(*Event)
lastMessage time.Time
ticker <-chan time.Time
ticker2 <-chan time.Time
VerboseCallbackHandler bool
quitting bool
}
type Event struct {
Code string
Message string
Raw string
Nick string //<nick>
Host string //<nick>!<usr>@<host>
Source string //<host>
User string //<usr>
Arguments []string
}