Minor formatting changes
This commit is contained in:
parent
a008509e20
commit
a910f6d47d
27
src/irc.go
27
src/irc.go
@ -10,26 +10,29 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"github.com/lye/tls"
|
"tls"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VERSION = "GolangBOT v1.0"
|
VERSION = "cleanirc v1.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
var error_ bool
|
var error_ bool
|
||||||
|
|
||||||
func reader(irc *IRCConnection) {
|
func reader(irc *IRCConnection) {
|
||||||
br := bufio.NewReader(irc.socket)
|
br := bufio.NewReader(irc.socket)
|
||||||
|
|
||||||
for !error_ {
|
for !error_ {
|
||||||
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 := &IRCEvent{Raw: msg}
|
event := &IRCEvent{Raw: msg}
|
||||||
|
|
||||||
if msg[0] == ':' {
|
if msg[0] == ':' {
|
||||||
if i := strings.Index(msg, " "); i > -1 {
|
if i := strings.Index(msg, " "); i > -1 {
|
||||||
event.Source = msg[1:i]
|
event.Source = msg[1:i]
|
||||||
@ -43,32 +46,39 @@ func reader(irc *IRCConnection) {
|
|||||||
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)]
|
||||||
}
|
}
|
||||||
|
|
||||||
irc.RunCallbacks(event)
|
irc.RunCallbacks(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
irc.syncreader <- true
|
irc.syncreader <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
func writer(irc *IRCConnection) {
|
func writer(irc *IRCConnection) {
|
||||||
b, ok := <-irc.pwrite
|
b, ok := <-irc.pwrite
|
||||||
|
|
||||||
for !error_ && ok {
|
for !error_ && 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 {
|
||||||
fmt.Printf("%s\n", err)
|
fmt.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
|
||||||
@ -78,6 +88,7 @@ func writer(irc *IRCConnection) {
|
|||||||
func pinger(i *IRCConnection) {
|
func pinger(i *IRCConnection) {
|
||||||
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 {
|
||||||
select {
|
select {
|
||||||
case <-i.ticker:
|
case <-i.ticker:
|
||||||
@ -85,6 +96,7 @@ func pinger(i *IRCConnection) {
|
|||||||
if time.Since(i.lastMessage) >= (4 * time.Minute) {
|
if time.Since(i.lastMessage) >= (4 * time.Minute) {
|
||||||
i.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano()))
|
i.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano()))
|
||||||
}
|
}
|
||||||
|
|
||||||
case <-i.ticker2:
|
case <-i.ticker2:
|
||||||
//Ping every 15 minutes.
|
//Ping every 15 minutes.
|
||||||
i.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano()))
|
i.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano()))
|
||||||
@ -131,8 +143,10 @@ func (irc *IRCConnection) SendRaw(message string) {
|
|||||||
func (i *IRCConnection) Reconnect() error {
|
func (i *IRCConnection) Reconnect() error {
|
||||||
close(i.pwrite)
|
close(i.pwrite)
|
||||||
close(i.pread)
|
close(i.pread)
|
||||||
|
|
||||||
<-i.syncreader
|
<-i.syncreader
|
||||||
<-i.syncwriter
|
<-i.syncwriter
|
||||||
|
|
||||||
for {
|
for {
|
||||||
fmt.Printf("Reconnecting to %s\n", i.server)
|
fmt.Printf("Reconnecting to %s\n", i.server)
|
||||||
var err error
|
var err error
|
||||||
@ -142,27 +156,36 @@ func (i *IRCConnection) Reconnect() error {
|
|||||||
}
|
}
|
||||||
fmt.Printf("Error: %s\n", err)
|
fmt.Printf("Error: %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
error_ = false
|
error_ = false
|
||||||
|
|
||||||
fmt.Printf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr())
|
fmt.Printf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr())
|
||||||
|
|
||||||
go reader(i)
|
go reader(i)
|
||||||
go writer(i)
|
go writer(i)
|
||||||
|
|
||||||
i.pwrite <- fmt.Sprintf("NICK %s\r\n", i.nick)
|
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)
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IRCConnection) Loop() {
|
func (i *IRCConnection) Loop() {
|
||||||
for !i.quitting {
|
for !i.quitting {
|
||||||
e := <-i.Error
|
e := <-i.Error
|
||||||
|
|
||||||
if i.quitting {
|
if i.quitting {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Error: %s\n", e)
|
fmt.Printf("Error: %s\n", e)
|
||||||
error_ = true
|
error_ = true
|
||||||
i.Reconnect()
|
i.Reconnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
close(i.pwrite)
|
close(i.pwrite)
|
||||||
close(i.pread)
|
close(i.pread)
|
||||||
|
|
||||||
<-i.syncreader
|
<-i.syncreader
|
||||||
<-i.syncwriter
|
<-i.syncwriter
|
||||||
}
|
}
|
||||||
|
@ -9,8 +9,10 @@ import (
|
|||||||
|
|
||||||
func (irc *IRCConnection) AddCallback(eventcode string, callback func(*IRCEvent)) {
|
func (irc *IRCConnection) AddCallback(eventcode string, callback func(*IRCEvent)) {
|
||||||
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(*IRCEvent), 1)
|
irc.events[eventcode] = make([]func(*IRCEvent), 1)
|
||||||
irc.events[eventcode][0] = callback
|
irc.events[eventcode][0] = callback
|
||||||
@ -19,42 +21,54 @@ func (irc *IRCConnection) AddCallback(eventcode string, callback func(*IRCEvent)
|
|||||||
|
|
||||||
func (irc *IRCConnection) ReplaceCallback(eventcode string, i int, callback func(*IRCEvent)) {
|
func (irc *IRCConnection) ReplaceCallback(eventcode string, i int, callback func(*IRCEvent)) {
|
||||||
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
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Event found, but no callback found at index %d. Use AddCallback\n", i)
|
fmt.Printf("Event found, but no callback found at index %d. Use AddCallback\n", i)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Event not found. Use AddCallBack\n")
|
fmt.Printf("Event not found. Use AddCallBack\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (irc *IRCConnection) RunCallbacks(event *IRCEvent) {
|
func (irc *IRCConnection) RunCallbacks(event *IRCEvent) {
|
||||||
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 {
|
||||||
fmt.Printf("%v (%v) >> %#v\n", event.Code, len(callbacks), event)
|
fmt.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 {
|
||||||
fmt.Printf("%v (0) >> %#v\n", event.Code, event)
|
fmt.Printf("%v (0) >> %#v\n", event.Code, event)
|
||||||
}
|
}
|
||||||
@ -84,7 +98,9 @@ func (irc *IRCConnection) setupCallbacks() {
|
|||||||
irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01TIME %s\x01", e.Nick, ltime.String()))
|
irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01TIME %s\x01", e.Nick, ltime.String()))
|
||||||
})
|
})
|
||||||
|
|
||||||
irc.AddCallback("CTCP_PING", func(e *IRCEvent) { irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01%s\x01", e.Nick, e.Message)) })
|
irc.AddCallback("CTCP_PING", func(e *IRCEvent) {
|
||||||
|
irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01%s\x01", e.Nick, e.Message))
|
||||||
|
})
|
||||||
|
|
||||||
irc.AddCallback("437", func(e *IRCEvent) {
|
irc.AddCallback("437", func(e *IRCEvent) {
|
||||||
irc.nickcurrent = irc.nickcurrent + "_"
|
irc.nickcurrent = irc.nickcurrent + "_"
|
||||||
@ -94,9 +110,11 @@ func (irc *IRCConnection) setupCallbacks() {
|
|||||||
irc.AddCallback("433", func(e *IRCEvent) {
|
irc.AddCallback("433", func(e *IRCEvent) {
|
||||||
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 + "_"
|
||||||
}
|
}
|
||||||
|
|
||||||
irc.SendRaw(fmt.Sprintf("NICK %s", irc.nickcurrent))
|
irc.SendRaw(fmt.Sprintf("NICK %s", irc.nickcurrent))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ package irc
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
"github.com/lye/tls"
|
"tls"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IRCConnection struct {
|
type IRCConnection struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user