now with config files

This commit is contained in:
Horscchtey 2021-11-24 00:50:01 +00:00
commit 844578f217
6 changed files with 71 additions and 86 deletions

BIN
boddle.db

Binary file not shown.

View file

@ -27,6 +27,7 @@ var err error
func checkErr(err error) {
if err != nil {
fmt.Printf("::::ERROR:::: %s\n", err)
panic(err)
}
}
@ -193,12 +194,12 @@ func parsemsg(msg irc_msg, foo bot) bool {
}
return false
}
LOG_WARN.Printf("line was parsed, continue with evaluation.\n")
if c.cmd == "" {
LOG_WARN.Printf("no command provided.\n")
return false
}
if c.cmd == "pepe" && msg.author == "spaghetto" {
printPepe(msg.author, foo)
if c.cmd == "help" {
return false
}
@ -211,6 +212,7 @@ func parsemsg(msg irc_msg, foo bot) bool {
var name_allow int
var has_groups int
sorter_row.Scan(&sorter_id, &tag_id, &groups_allow, &name_allow, &has_groups)
sorter_row.Close()
if groups_allow != 0 {
sendmsg(foo.conn, msg.channel, "This command does not allow groups. :(")
@ -218,28 +220,20 @@ func parsemsg(msg irc_msg, foo bot) bool {
}
if name_allow != 0 && !c.add {
// warning: not allowed
sendmsg(foo.conn, msg.channel, "This command ignores provided names. :(")
c.suffix = ""
}
c.cmd = tag_id
var sorter_groups []string
if has_groups == 0 {
sorter_groups_row, err := db.Query(fmt.Sprintf("select group_id from sorter_groups where sorter_id = '%s'", sorter_id))
checkErr(err)
if sorter_groups_row.Next() {
var group_id int
sorter_groups_row.Scan(&group_id)
if c.add {
sorter_groups = append(sorter_groups, fmt.Sprintf("%d", group_id))
} else {
c.groups = append(c.groups, fmt.Sprintf("%d", group_id))
}
}
sorter_groups_row.Close()
}
sorter_row.Close()
if c.add {
var tag_name string
tag_name_row, err := db.Query(fmt.Sprintf("select name from tag where id = %d", tag_id))
@ -248,9 +242,11 @@ func parsemsg(msg irc_msg, foo bot) bool {
tag_name_row.Scan(&tag_name)
}
tag_name_row.Close()
sendmsg(foo.conn, msg.channel, "You're using an alias. Please try again with 'add" + tag_name + " [" + strings.Join(sorter_groups, " ") + "]. Thanks!")
LOG_WARN.Printf("Alias used. Tell the user about that.\n")
sendmsg(foo.conn, msg.channel, "You're using an alias. Please try again with 'add " + tag_name + " [" + strings.Join(c.groups, " ") + "]. Thanks!")
return false
}
}
} else {
LOG_WARN.Printf("no sorter entry, no translation possible\n")
sendmsg(foo.conn, msg.channel, "No entry found, sorry...")
@ -262,15 +258,14 @@ func parsemsg(msg irc_msg, foo bot) bool {
return false;
}
c.suffix = strings.Replace(c.suffix,"\"","'", -1)
fmt.Printf(c.suffix)
// insert into database
statement, err := db.Prepare("insert into line (content, author) values (?,?)")
LOG_WARN.Printf("adding new stuff: %s from %s.\n", c.suffix, msg.author)
res, err := db.Exec("insert into line (content, author) values (?,?)", c.suffix, msg.author)
checkErr(err)
_, err = statement.Exec(c.suffix, msg.author)
checkErr(err)
defer statement.Close()
LOG_WARN.Printf("added line to table.\n")
// get ID of content...
/*
task := fmt.Sprintf("select l.id from line l where l.content = \"%s\"", c.suffix) // strings.Replace(c.suffix, "'", "''", -1))
fmt.Printf(task)
row, err := db.Query(task)
@ -278,13 +273,17 @@ func parsemsg(msg irc_msg, foo bot) bool {
line_id := 0
if row.Next() { row.Scan(&line_id) }
row.Close()
LOG_INFO.Printf(c.cmd)
LOG_WARN.Printf(c.cmd)
if line_id == 0 {
LOG_WARN.Printf("no entry for adding")
sendmsg(foo.conn, msg.channel, "internal server error.")
return false
}
*/
line_id, err := res.LastInsertId();
checkErr(err)
LOG_WARN.Printf("line_id: %d, tag_id: %d, groups: %s\n", line_id, c.cmd, strings.Join(c.groups, "-"))
// for tag and all groups
if len(c.groups) == 0 {
stat, err := db.Prepare("insert into brain(line_id, tag_id) values (?,?)")
@ -329,44 +328,3 @@ func parsemsg(msg irc_msg, foo bot) bool {
return true
}
/*
func test() {
db, err = sql.Open("sqlite3", "./boddle.db")
checkErr(err)
var msg irc_msg
msg.channel = "testchannel"
msg.author = "max mustermann"
// == invalid commands ==
// empty cmd
msg.msg = ">add [test me bro hahahaha] blob"
parsemsg(msg)
msg.msg = "> [test me bro hahahaha]"
parsemsg(msg)
msg.msg = ">[test me bro hahahaha] meow "
parsemsg(msg)
msg.msg = ">add [test me bro hahahaha] meow "
parsemsg(msg)
// non-existent groups
msg.msg = ">ohai [test me bro hahahaha]"
parsemsg(msg)
// add command...? not implemented yet!
msg.msg = ">add jokes blob"
parsemsg(msg)
msg.msg = ">add test [test me bro hahahaha] meow "
parsemsg(msg)
// ok command \o/
msg.msg = ">flirt [sweet] meow "
parsemsg(msg)
msg.msg = ">jokes [flach] meow "
parsemsg(msg)
msg.msg = ">hate [sweet] blobfisch "
parsemsg(msg)
msg.msg = ">radschlag [science]"
parsemsg(msg)
}
*/

28
bot.go
View file

@ -2,31 +2,41 @@ package main
import (
"os"
"log"
"fmt"
"net"
"time"
configo "github.com/distributedio/configo"
_ "github.com/mattn/go-sqlite3"
)
var dead = true
type Config struct {
Name string `cfg:"name; boddle; printableascii; IRC nick of bot"`
Channels []string `cfg:"channels; required; printableascii; Channel list to join to"`
Server string `cfg:"server; required; netaddr; Server name to connect to"`
Database string `cfg:"database; ./boddle.db; path; Path to database"`
}
type bot struct {
name string
channel []string
port int
server string
Conf Config
conn net.Conn
}
func main() {
LOG_init()
boddle := bot {
name: "boddle",
channel: []string{"#faui2k16", "#fau", "#sigbike", "#sigfreibad"},
port: 6667,
server: "irc.fau.de",
var boddle bot
if err := configo.Load("./boddle.toml", &boddle.Conf); err != nil {
log.Fatal(err)
}
// boddle := bot {
// name: "boddle",
// channel: []string{"#faui2k16", "#fau", "#sigbike", "#sigfreibad"},
// port: 6667,
// server: "irc.fau.de",
// }
LOG_INFO.Println("bot started!")

11
deploy.sh Executable file
View file

@ -0,0 +1,11 @@
#!/bin/bash
if ! [[ "$UID" -eq 0 ]] ; then
echo "Please run as sudo $0 $*" >&2
exit 1
fi
declare -r target="${1:-/var/lib/boddle}"
install -o boddle -s -m 0500 -T bot "${target}/executable"
systemctl restart boddle

View file

@ -1,9 +1,15 @@
#!/bin/bash
export GOPATH=/home/horscchtey/go
export CC=arm-linux-gnueabihf-gcc
export CXX=arm-linux-gnueabihf-g++
export CGO_ENABLED=1
export GOOS=linux
export GOARCH=arm
export GOARM=7
env CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ \
CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7 \
go build bot.go boddle.go logging.go ircfoo.go pepe.go
declare -a src
src+=(bot.go boddle.go logging.go ircfoo.go)
declare -i nproc="$(nproc)"
mv bot boddle_executable
go build -p ${nproc} "${src[@]}"

View file

@ -38,7 +38,7 @@ func scanline_privmsg (msg string) irc_msg {
func listenToIRC (foo bot) <-chan []byte {
c := make(chan []byte)
reader := bufio.NewReader(foo.conn)
db, err = sql.Open("sqlite3", "./boddle.db")
db, err = sql.Open("sqlite3", foo.Conf.Database)
if err != nil {
LOG_ERR.Printf("opening database failed")
return nil
@ -77,7 +77,7 @@ func listenToIRC (foo bot) <-chan []byte {
}
func connect(boddle bot) <-chan []byte {
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", boddle.server, boddle.port))
conn, err := net.Dial("tcp", boddle.Conf.Server)
if err != nil {
fmt.Fprintf(os.Stderr, "Error while dialing server.")
@ -88,9 +88,9 @@ func connect(boddle bot) <-chan []byte {
boddle.conn = conn
/* connect to irc */
fmt.Fprintf(boddle.conn, "NICK %s\r\n", boddle.name)
fmt.Fprintf(boddle.conn, "USER %s 1 1 1:%s\r\n", boddle.name, boddle.name)
for _, channel := range(boddle.channel) {
fmt.Fprintf(boddle.conn, "NICK %s\r\n", boddle.Conf.Name)
fmt.Fprintf(boddle.conn, "USER %s 1 1 1:%s\r\n", boddle.Conf.Name, boddle.Conf.Name)
for _, channel := range(boddle.Conf.Channels) {
fmt.Fprintf(boddle.conn, "JOIN %s\r\n", channel)
}