added IRC bot frontend

This commit is contained in:
A_D 2018-11-17 20:39:54 +02:00
parent 119f91102e
commit 14023d09d0
No known key found for this signature in database
GPG Key ID: C242F3DD220FA945
6 changed files with 189 additions and 4 deletions

79
src/bot.go Normal file
View File

@ -0,0 +1,79 @@
package main
import (
"bufio"
"github.com/goshuirc/irc-go/ircmsg"
"log"
"os"
)
type Bot struct {
Nick string
Ident string
Realname string
Server Server
ChansToJoin []string
NSPasswd string
NSNick string
Logger log.Logger
}
type BotConfig struct {
Nick string
Ident string
Realname string
ChansToJoin []string
NSNick string
NSPasswd string
}
func newBot(config BotConfig, host, port, connectionName string, ssl bool) *Bot {
logger := *log.New(os.Stdout, "", log.LstdFlags)
return &Bot{
Nick: config.Nick,
Ident: config.Ident,
Realname: config.Realname,
NSPasswd: config.NSPasswd,
NSNick: config.NSNick,
ChansToJoin: config.ChansToJoin,
Server: *NewServer(host, port, connectionName, ssl, logger),
Logger: logger,
}
}
func (b *Bot) run() {
err := b.Server.Connect()
if err != nil {
b.Logger.Fatal(err)
}
b.sendRaw(MakeMessage("NICK", b.Nick))
b.sendRaw(MakeMessage("USER", b.Ident, "0", "0", b.Realname))
b.readLoop()
}
func (b *Bot) sendRaw(message ircmsg.IrcMessage) {
line, err := message.LineBytes()
if err != nil {
b.Logger.Printf("could not convert IrcMessage to string: %v", err)
return
}
b.Logger.Printf(">> %s", line)
b.Server.Conn.Write(line)
}
func (b *Bot) readLoop() {
scanner := bufio.NewScanner(b.Server.Conn)
for scanner.Scan() {
b.HandleLine(scanner.Text())
}
}
func (b *Bot) joinChannel(channelName string) {
b.sendRaw(MakeMessage("JOIN", channelName))
}
func (b *Bot) sendPrivMessage(target, message string) {
b.sendRaw(MakeMessage("PRIVMSG", message))
}

38
src/botLineHandlers.go Normal file
View File

@ -0,0 +1,38 @@
package main
import (
"fmt"
"github.com/goshuirc/irc-go/ircmsg"
)
func createPong(ping ircmsg.IrcMessage) ircmsg.IrcMessage {
return ircmsg.MakeMessage(nil, "", "PONG", ping.Params[0])
}
func (b *Bot) HandleLine(lineIn string) {
line, err := ircmsg.ParseLine(lineIn)
if err != nil {
b.Logger.Printf("could not parse IRC line: %v", err)
return
}
b.Logger.Printf("<< %s", line.SourceLine)
switch line.Command {
case "PING":
b.sendRaw(createPong(line))
case "001":
b.OnWelcome()
}
}
func (b *Bot) OnWelcome() {
if b.NSNick != "" && b.NSPasswd != "" {
b.sendPrivMessage("NickServ", fmt.Sprintf("IDENTIFY %s %s", b.NSNick, b.NSPasswd))
}
for _, channel := range b.ChansToJoin {
b.joinChannel(channel)
}
}

View File

@ -7,9 +7,15 @@ import (
) )
type Config struct { type Config struct {
Secret string Secret string `json:"secret"`
BindHost string `json:"bind_host"` BindHost string `json:"bind_host"`
BindPort string `json:"bind_port"` BindPort string `json:"bind_port"`
Nick string `json:"nick"`
Ident string `json:"ident"`
Realname string `json:"realname"`
NsNick string `json:"ns_nick"`
NsPasswd string `json:"ns_passwd"`
ChansToJoin []string `json:"chans_to_join"`
} }
func getConfig() Config { func getConfig() Config {

7
src/ircUtil.go Normal file
View File

@ -0,0 +1,7 @@
package main
import "github.com/goshuirc/irc-go/ircmsg"
func MakeMessage(command string, params... string) ircmsg.IrcMessage{
return ircmsg.MakeMessage(nil, "", command, params...)
}

View File

@ -1,15 +1,26 @@
package main package main
import "fmt" import "fmt"
const configPrint = ` const configPrint = `
CONFIG: CONFIG:
Secret: %q Secret: %q
BindHost: %q BindHost: %q
BindPort: %q BindPort: %q
` `
func main() {
func main() {
config := getConfig() config := getConfig()
fmt.Printf(configPrint, config.Secret, config.BindHost, config.BindPort) fmt.Printf(configPrint, config.Secret, config.BindHost, config.BindPort)
bc := BotConfig{
Nick: config.Nick,
Ident: config.Ident,
Realname: config.Realname,
ChansToJoin: config.ChansToJoin,
}
bot := newBot(bc, "chimera.snoonet.org", "6697", "snoonet", true)
go bot.run()
ListenForWebHook(config) ListenForWebHook(config)
} }

44
src/server.go Normal file
View File

@ -0,0 +1,44 @@
package main
import (
"crypto/tls"
"fmt"
"log"
"net"
)
type Server struct {
Host string
Port string
Name string
SSL bool
Conn net.Conn
Logger log.Logger
}
func NewServer(host, port, name string, ssl bool, logger log.Logger) *Server {
return &Server{
host,
port,
name,
ssl,
nil,
logger,
}
}
func (s *Server) Connect() error {
var conn net.Conn
var err error
if s.SSL {
conn, err = tls.Dial("tcp", fmt.Sprintf("%s:%s", s.Host, s.Port), nil)
} else {
conn, err = net.Dial("tcp", fmt.Sprintf("%s:%s", s.Host, s.Port))
}
if err != nil {
return err
}
s.Conn = conn
return nil
}