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) { func checkErr(err error) {
if err != nil { if err != nil {
fmt.Printf("::::ERROR:::: %s\n", err)
panic(err) panic(err)
} }
} }
@ -193,12 +194,12 @@ func parsemsg(msg irc_msg, foo bot) bool {
} }
return false return false
} }
LOG_WARN.Printf("line was parsed, continue with evaluation.\n")
if c.cmd == "" { if c.cmd == "" {
LOG_WARN.Printf("no command provided.\n") LOG_WARN.Printf("no command provided.\n")
return false return false
} }
if c.cmd == "pepe" && msg.author == "spaghetto" { if c.cmd == "help" {
printPepe(msg.author, foo)
return false return false
} }
@ -211,6 +212,7 @@ func parsemsg(msg irc_msg, foo bot) bool {
var name_allow int var name_allow int
var has_groups int var has_groups int
sorter_row.Scan(&sorter_id, &tag_id, &groups_allow, &name_allow, &has_groups) sorter_row.Scan(&sorter_id, &tag_id, &groups_allow, &name_allow, &has_groups)
sorter_row.Close()
if groups_allow != 0 { if groups_allow != 0 {
sendmsg(foo.conn, msg.channel, "This command does not allow groups. :(") sendmsg(foo.conn, msg.channel, "This command does not allow groups. :(")
@ -218,38 +220,32 @@ func parsemsg(msg irc_msg, foo bot) bool {
} }
if name_allow != 0 && !c.add { if name_allow != 0 && !c.add {
// warning: not allowed // warning: not allowed
sendmsg(foo.conn, msg.channel, "This command ignores provided names. :(")
c.suffix = "" c.suffix = ""
} }
c.cmd = tag_id c.cmd = tag_id
var sorter_groups []string
if has_groups == 0 { if has_groups == 0 {
sorter_groups_row, err := db.Query(fmt.Sprintf("select group_id from sorter_groups where sorter_id = '%s'", sorter_id)) sorter_groups_row, err := db.Query(fmt.Sprintf("select group_id from sorter_groups where sorter_id = '%s'", sorter_id))
checkErr(err) checkErr(err)
if sorter_groups_row.Next() { if sorter_groups_row.Next() {
var group_id int var group_id int
sorter_groups_row.Scan(&group_id) sorter_groups_row.Scan(&group_id)
if c.add { c.groups = append(c.groups, fmt.Sprintf("%d", group_id))
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_groups_row.Close()
} if c.add {
sorter_row.Close() var tag_name string
if c.add { tag_name_row, err := db.Query(fmt.Sprintf("select name from tag where id = %d", tag_id))
var tag_name string checkErr(err)
tag_name_row, err := db.Query(fmt.Sprintf("select name from tag where id = %d", tag_id)) if tag_name_row.Next() {
checkErr(err) tag_name_row.Scan(&tag_name)
if tag_name_row.Next() { }
tag_name_row.Scan(&tag_name) tag_name_row.Close()
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
} }
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!")
return false
} }
} else { } else {
LOG_WARN.Printf("no sorter entry, no translation possible\n") LOG_WARN.Printf("no sorter entry, no translation possible\n")
@ -262,15 +258,14 @@ func parsemsg(msg irc_msg, foo bot) bool {
return false; return false;
} }
c.suffix = strings.Replace(c.suffix,"\"","'", -1) c.suffix = strings.Replace(c.suffix,"\"","'", -1)
fmt.Printf(c.suffix) LOG_WARN.Printf("adding new stuff: %s from %s.\n", c.suffix, msg.author)
// insert into database
statement, err := db.Prepare("insert into line (content, author) values (?,?)") res, err := db.Exec("insert into line (content, author) values (?,?)", c.suffix, msg.author)
checkErr(err) checkErr(err)
_, err = statement.Exec(c.suffix, msg.author) LOG_WARN.Printf("added line to table.\n")
checkErr(err)
defer statement.Close()
// get ID of content... // get ID of content...
/*
task := fmt.Sprintf("select l.id from line l where l.content = \"%s\"", c.suffix) // strings.Replace(c.suffix, "'", "''", -1)) task := fmt.Sprintf("select l.id from line l where l.content = \"%s\"", c.suffix) // strings.Replace(c.suffix, "'", "''", -1))
fmt.Printf(task) fmt.Printf(task)
row, err := db.Query(task) row, err := db.Query(task)
@ -278,13 +273,17 @@ func parsemsg(msg irc_msg, foo bot) bool {
line_id := 0 line_id := 0
if row.Next() { row.Scan(&line_id) } if row.Next() { row.Scan(&line_id) }
row.Close() row.Close()
LOG_INFO.Printf(c.cmd) LOG_WARN.Printf(c.cmd)
if line_id == 0 { if line_id == 0 {
LOG_WARN.Printf("no entry for adding") LOG_WARN.Printf("no entry for adding")
sendmsg(foo.conn, msg.channel, "internal server error.") sendmsg(foo.conn, msg.channel, "internal server error.")
return false 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 // for tag and all groups
if len(c.groups) == 0 { if len(c.groups) == 0 {
stat, err := db.Prepare("insert into brain(line_id, tag_id) values (?,?)") 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 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 ( import (
"os" "os"
"log"
"fmt" "fmt"
"net" "net"
"time" "time"
configo "github.com/distributedio/configo"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
var dead = true 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 { type bot struct {
name string Conf Config
channel []string
port int
server string
conn net.Conn conn net.Conn
} }
func main() { func main() {
LOG_init() LOG_init()
boddle := bot { var boddle bot
name: "boddle", if err := configo.Load("./boddle.toml", &boddle.Conf); err != nil {
channel: []string{"#faui2k16", "#fau", "#sigbike", "#sigfreibad"}, log.Fatal(err)
port: 6667,
server: "irc.fau.de",
} }
// boddle := bot {
// name: "boddle",
// channel: []string{"#faui2k16", "#fau", "#sigbike", "#sigfreibad"},
// port: 6667,
// server: "irc.fau.de",
// }
LOG_INFO.Println("bot started!") 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 #!/bin/bash
export GOPATH=/home/horscchtey/go 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++ \ declare -a src
CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7 \ src+=(bot.go boddle.go logging.go ircfoo.go)
go build bot.go boddle.go logging.go ircfoo.go pepe.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 { func listenToIRC (foo bot) <-chan []byte {
c := make(chan []byte) c := make(chan []byte)
reader := bufio.NewReader(foo.conn) reader := bufio.NewReader(foo.conn)
db, err = sql.Open("sqlite3", "./boddle.db") db, err = sql.Open("sqlite3", foo.Conf.Database)
if err != nil { if err != nil {
LOG_ERR.Printf("opening database failed") LOG_ERR.Printf("opening database failed")
return nil return nil
@ -77,7 +77,7 @@ func listenToIRC (foo bot) <-chan []byte {
} }
func connect(boddle 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 { if err != nil {
fmt.Fprintf(os.Stderr, "Error while dialing server.") fmt.Fprintf(os.Stderr, "Error while dialing server.")
@ -88,9 +88,9 @@ func connect(boddle bot) <-chan []byte {
boddle.conn = conn boddle.conn = conn
/* connect to irc */ /* connect to irc */
fmt.Fprintf(boddle.conn, "NICK %s\r\n", boddle.name) fmt.Fprintf(boddle.conn, "NICK %s\r\n", boddle.Conf.Name)
fmt.Fprintf(boddle.conn, "USER %s 1 1 1:%s\r\n", boddle.name, boddle.name) fmt.Fprintf(boddle.conn, "USER %s 1 1 1:%s\r\n", boddle.Conf.Name, boddle.Conf.Name)
for _, channel := range(boddle.channel) { for _, channel := range(boddle.Conf.Channels) {
fmt.Fprintf(boddle.conn, "JOIN %s\r\n", channel) fmt.Fprintf(boddle.conn, "JOIN %s\r\n", channel)
} }