67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"github.com/lrstanley/girc"
|
|
)
|
|
|
|
type BotStatus struct {
|
|
Ready bool
|
|
Error string
|
|
Info string
|
|
}
|
|
|
|
func (b *IRCBroker) StartBot(nick string, server string, port int, ssl bool, channel string, incoming chan []byte) chan Status {
|
|
var client *girc.Client
|
|
var status chan Status
|
|
|
|
client = girc.New(girc.Config{
|
|
Server: server,
|
|
Port: port,
|
|
Nick: nick,
|
|
Name: "jupemail",
|
|
User: "jupemail",
|
|
SSL: ssl,
|
|
AllowFlood: true,
|
|
Version: "~ JupeMail v0.0101 ~",
|
|
})
|
|
|
|
client.Handlers.Add(girc.CONNECTED, func(c *girc.Client, e girc.Event) {
|
|
status <- &BotStatus{
|
|
Available: true,
|
|
Info: "[IRC] Successfully connected to: " + server + " joining channel...",
|
|
}
|
|
c.Cmd.Join(ircHome)
|
|
})
|
|
|
|
client.Handlers.Add(girc.ERR_CANNOTSENDTOCHAN, func(c *girc.Client, e girc.Event) {
|
|
status <- &BotStatus{
|
|
Available: false,
|
|
Error: "ERR_CANNOTSENDTOCHAN",
|
|
}
|
|
})
|
|
|
|
sslString := " "
|
|
if ircSSL == true {
|
|
sslString = "-ssl"
|
|
}
|
|
status <- &BotStatus{
|
|
Available: false,
|
|
Info: "[IRC] Connecting...",
|
|
}
|
|
go botLoop()
|
|
return status
|
|
}
|
|
|
|
func botLoop() {
|
|
for {
|
|
if err := client.Connect(); err != nil {
|
|
status <- &BotStatus{
|
|
Available: false,
|
|
Info: "[IRC] Reconnecting to " + ircHost + "/" + strconv.Itoa(ircPort) + " " + sslString,
|
|
Error: err.Error(),
|
|
}
|
|
time.Sleep(time.Duration(500) * time.Millisecond)
|
|
}
|
|
}
|
|
}
|