Rename IRC{Connection,Event} -> {Connection,Event}
I don't feel it's necessary to tag the types with IRC, as a client would tag it with the module name anyway. Example: var conn irc.IRCConnection // ... vs. var conn irc.Connection // ...
This commit is contained in:
parent
67c1c92623
commit
670fd99fb4
@ -15,17 +15,17 @@ func main() {
|
||||
fmt.Printf("%#v\n", irccon)
|
||||
os.Exit(1)
|
||||
}
|
||||
irccon.AddCallback("001", func(e *irc.IRCEvent) { irccon.Join("#testgo1") })
|
||||
irccon.AddCallback("001", func(e *irc.IRCEvent) { irccon.Join("#testgo2") })
|
||||
irccon.AddCallback("001", func(e *irc.IRCEvent) { irccon.Join("#testgo3") })
|
||||
irccon.AddCallback("001", func(e *irc.IRCEvent) { irccon.Join("#testgo4") })
|
||||
irccon.AddCallback("001", func(e *irc.IRCEvent) { irccon.Join("#testgo5") })
|
||||
irccon.AddCallback("001", func(e *irc.IRCEvent) { irccon.Join("#testgo6") })
|
||||
irccon.ReplaceCallback("001", 0, func(e *irc.IRCEvent) { irccon.Join("#testgo01") })
|
||||
irccon.ReplaceCallback("001", 1, func(e *irc.IRCEvent) { irccon.Join("#testgo02") })
|
||||
irccon.ReplaceCallback("001", 2, func(e *irc.IRCEvent) { irccon.Join("#testgo03") })
|
||||
irccon.ReplaceCallback("001", 3, func(e *irc.IRCEvent) { irccon.Join("#testgo04") })
|
||||
irccon.ReplaceCallback("001", 4, func(e *irc.IRCEvent) { irccon.Join("#testgo05") })
|
||||
irccon.ReplaceCallback("001", 6, func(e *irc.IRCEvent) { irccon.Join("#testgo06") })
|
||||
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()
|
||||
}
|
||||
|
32
irc.go
32
irc.go
@ -18,7 +18,7 @@ const (
|
||||
|
||||
var error_ bool
|
||||
|
||||
func reader(irc *IRCConnection) {
|
||||
func reader(irc *Connection) {
|
||||
br := bufio.NewReader(irc.socket)
|
||||
for !error_ {
|
||||
msg, err := br.ReadString('\n')
|
||||
@ -28,7 +28,7 @@ func reader(irc *IRCConnection) {
|
||||
}
|
||||
irc.lastMessage = time.Now()
|
||||
msg = msg[0 : len(msg)-2] //Remove \r\n
|
||||
event := &IRCEvent{Raw: msg}
|
||||
event := &Event{Raw: msg}
|
||||
if msg[0] == ':' {
|
||||
if i := strings.Index(msg, " "); i > -1 {
|
||||
event.Source = msg[1:i]
|
||||
@ -56,7 +56,7 @@ func reader(irc *IRCConnection) {
|
||||
irc.syncreader <- true
|
||||
}
|
||||
|
||||
func writer(irc *IRCConnection) {
|
||||
func writer(irc *Connection) {
|
||||
b, ok := <-irc.pwrite
|
||||
for !error_ && ok {
|
||||
if b == "" || irc.socket == nil {
|
||||
@ -74,7 +74,7 @@ func writer(irc *IRCConnection) {
|
||||
}
|
||||
|
||||
//Pings the server if we have not recived any messages for 5 minutes
|
||||
func pinger(i *IRCConnection) {
|
||||
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 {
|
||||
@ -96,38 +96,38 @@ func pinger(i *IRCConnection) {
|
||||
}
|
||||
}
|
||||
|
||||
func (irc *IRCConnection) Cycle() {
|
||||
func (irc *Connection) Cycle() {
|
||||
irc.SendRaw("QUIT")
|
||||
irc.Reconnect()
|
||||
}
|
||||
|
||||
func (irc *IRCConnection) Quit() {
|
||||
func (irc *Connection) Quit() {
|
||||
irc.quitting = true
|
||||
irc.SendRaw("QUIT")
|
||||
}
|
||||
|
||||
func (irc *IRCConnection) Join(channel string) {
|
||||
func (irc *Connection) Join(channel string) {
|
||||
irc.pwrite <- fmt.Sprintf("JOIN %s\r\n", channel)
|
||||
}
|
||||
|
||||
func (irc *IRCConnection) Part(channel string) {
|
||||
func (irc *Connection) Part(channel string) {
|
||||
irc.pwrite <- fmt.Sprintf("PART %s\r\n", channel)
|
||||
}
|
||||
|
||||
func (irc *IRCConnection) Notice(target, message string) {
|
||||
func (irc *Connection) Notice(target, message string) {
|
||||
irc.pwrite <- fmt.Sprintf("NOTICE %s :%s\r\n", target, message)
|
||||
}
|
||||
|
||||
func (irc *IRCConnection) Privmsg(target, message string) {
|
||||
func (irc *Connection) Privmsg(target, message string) {
|
||||
irc.pwrite <- fmt.Sprintf("PRIVMSG %s :%s\r\n", target, message)
|
||||
}
|
||||
|
||||
func (irc *IRCConnection) SendRaw(message string) {
|
||||
func (irc *Connection) SendRaw(message string) {
|
||||
fmt.Printf("--> %s\n", message)
|
||||
irc.pwrite <- fmt.Sprintf("%s\r\n", message)
|
||||
}
|
||||
|
||||
func (i *IRCConnection) Reconnect() error {
|
||||
func (i *Connection) Reconnect() error {
|
||||
close(i.pwrite)
|
||||
close(i.pread)
|
||||
<-i.syncreader
|
||||
@ -150,7 +150,7 @@ func (i *IRCConnection) Reconnect() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *IRCConnection) Loop() {
|
||||
func (i *Connection) Loop() {
|
||||
for !i.quitting {
|
||||
e := <-i.Error
|
||||
if i.quitting {
|
||||
@ -166,7 +166,7 @@ func (i *IRCConnection) Loop() {
|
||||
<-i.syncwriter
|
||||
}
|
||||
|
||||
func (i *IRCConnection) Connect(server string) error {
|
||||
func (i *Connection) Connect(server string) error {
|
||||
i.server = server
|
||||
fmt.Printf("Connecting to %s\n", i.server)
|
||||
var err error
|
||||
@ -191,8 +191,8 @@ func (i *IRCConnection) Connect(server string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func IRC(nick, user string) *IRCConnection {
|
||||
irc := new(IRCConnection)
|
||||
func IRC(nick, user string) *Connection {
|
||||
irc := new(Connection)
|
||||
irc.registered = false
|
||||
irc.pread = make(chan string, 100)
|
||||
irc.pwrite = make(chan string, 100)
|
||||
|
@ -7,17 +7,17 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func (irc *IRCConnection) AddCallback(eventcode string, callback func(*IRCEvent)) {
|
||||
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(*IRCEvent), 1)
|
||||
irc.events[eventcode] = make([]func(*Event), 1)
|
||||
irc.events[eventcode][0] = callback
|
||||
}
|
||||
}
|
||||
|
||||
func (irc *IRCConnection) ReplaceCallback(eventcode string, i int, callback func(*IRCEvent)) {
|
||||
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) {
|
||||
@ -30,7 +30,7 @@ func (irc *IRCConnection) ReplaceCallback(eventcode string, i int, callback func
|
||||
fmt.Printf("Event not found. Use AddCallBack\n")
|
||||
}
|
||||
|
||||
func (irc *IRCConnection) RunCallbacks(event *IRCEvent) {
|
||||
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 {
|
||||
@ -60,38 +60,38 @@ func (irc *IRCConnection) RunCallbacks(event *IRCEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
func (irc *IRCConnection) setupCallbacks() {
|
||||
irc.events = make(map[string][]func(*IRCEvent))
|
||||
func (irc *Connection) setupCallbacks() {
|
||||
irc.events = make(map[string][]func(*Event))
|
||||
|
||||
//Handle ping events
|
||||
irc.AddCallback("PING", func(e *IRCEvent) { irc.SendRaw("PONG :" + e.Message) })
|
||||
irc.AddCallback("PING", func(e *Event) { irc.SendRaw("PONG :" + e.Message) })
|
||||
|
||||
//Version handler
|
||||
irc.AddCallback("CTCP_VERSION", func(e *IRCEvent) {
|
||||
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 *IRCEvent) {
|
||||
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 *IRCEvent) {
|
||||
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 *IRCEvent) {
|
||||
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 *IRCEvent) { irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01%s\x01", e.Nick, e.Message)) })
|
||||
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 *IRCEvent) {
|
||||
irc.AddCallback("437", func(e *Event) {
|
||||
irc.nickcurrent = irc.nickcurrent + "_"
|
||||
irc.SendRaw(fmt.Sprintf("NICK %s", irc.nickcurrent))
|
||||
})
|
||||
|
||||
irc.AddCallback("433", func(e *IRCEvent) {
|
||||
irc.AddCallback("433", func(e *Event) {
|
||||
if len(irc.nickcurrent) > 8 {
|
||||
irc.nickcurrent = "_" + irc.nickcurrent
|
||||
} else {
|
||||
@ -100,19 +100,19 @@ func (irc *IRCConnection) setupCallbacks() {
|
||||
irc.SendRaw(fmt.Sprintf("NICK %s", irc.nickcurrent))
|
||||
})
|
||||
|
||||
irc.AddCallback("PONG", func(e *IRCEvent) {
|
||||
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 *IRCEvent) {
|
||||
irc.AddCallback("NICK", func(e *Event) {
|
||||
if e.Nick == irc.nick {
|
||||
irc.nickcurrent = e.Arguments[0]
|
||||
}
|
||||
})
|
||||
|
||||
irc.AddCallback("001", func(e *IRCEvent) {
|
||||
irc.AddCallback("001", func(e *Event) {
|
||||
irc.nickcurrent = e.Arguments[0]
|
||||
})
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type IRCConnection struct {
|
||||
type Connection struct {
|
||||
socket net.Conn
|
||||
pread, pwrite chan string
|
||||
Error chan error
|
||||
@ -20,7 +20,7 @@ type IRCConnection struct {
|
||||
registered bool
|
||||
server string
|
||||
Password string
|
||||
events map[string][]func(*IRCEvent)
|
||||
events map[string][]func(*Event)
|
||||
|
||||
lastMessage time.Time
|
||||
ticker <-chan time.Time
|
||||
@ -31,7 +31,7 @@ type IRCConnection struct {
|
||||
quitting bool
|
||||
}
|
||||
|
||||
type IRCEvent struct {
|
||||
type Event struct {
|
||||
Code string
|
||||
Message string
|
||||
Raw string
|
||||
|
Loading…
Reference in New Issue
Block a user