From a6be64b6b56bdcf63ed63c3b3607a1fd9431760f Mon Sep 17 00:00:00 2001 From: "yakumo.izuru" Date: Tue, 2 Apr 2024 01:22:38 +0000 Subject: [PATCH] It seems I'd either have to somehow fix the Python thing, or create a whole XMPP bot framework in Golang, should I rewrite it Signed-off-by: Izuru Yakumo git-svn-id: https://svn.yakumo.dev/yakumo.izuru/chen/trunk@40 32723744-9b23-0b4a-b1da-9b2e968f9461 --- cmd/chen/main.go | 33 ++++++++++++++++++++++++-------- go.mod | 10 ++++++++-- go.sum | 4 ++++ main.py | 20 ++++++++----------- version.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 22 deletions(-) create mode 100644 version.go diff --git a/cmd/chen/main.go b/cmd/chen/main.go index b8826e5..97609c1 100644 --- a/cmd/chen/main.go +++ b/cmd/chen/main.go @@ -1,10 +1,15 @@ package main import ( + "flag" + "log" "gopkg.in/ini.v1" + + "git.chaotic.ninja/yakumo_izuru/chen" + "github.com/jbuchbinder/go-xmppbot" ) -var chen struct { +var config struct { jid string password string prefix string @@ -12,6 +17,12 @@ var chen struct { autojoin string } +var confFile string + +func init() { + flag.StringVar(&confFile, "f", "", "Configuration file") +} + func readConf(file string) error { cfg, err := ini.Load(file) @@ -19,16 +30,22 @@ func readConf(file string) error { return err } - chen.jid = cfg.Section("chen").Key("jid").String() - chen.password = cfg.Section("chen").Key("password").String() - chen.prefix = cfg.Section("chen").Key("prefix").String() - chen.nick = cfg.Section("chen").Key("nick").String() - chen.autojoin = cfg.Section("chen").Key("autojoin").String() + config.jid = cfg.Section("chen").Key("jid").String() + config.password = cfg.Section("chen").Key("password").String() + config.prefix = cfg.Section("chen").Key("prefix").String() + config.nick = cfg.Section("chen").Key("nick").String() + config.autojoin = cfg.Section("chen").Key("autojoin").String() return nil } func main() { - readConf(chen.ini) - // Do whatever here + flag.Parse() + if confFile != "" { + log.Println("Loading configuration file for Chen") + readConf(confFile) + } else { + log.Fatal("No configuration file has been provided") + } + log.Printf("Chen v%s starting with config %s\n", chen.FullVersion(), confFile) } diff --git a/go.mod b/go.mod index 095e444..f694f4f 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,12 @@ module git.chaotic.ninja/yakumo_izuru/chen go 1.20 -require gopkg.in/ini.v1 v1.67.0 +require ( + github.com/jbuchbinder/go-xmppbot v0.0.0-20201202211837-603b6f40c1d1 + gopkg.in/ini.v1 v1.67.0 +) -require github.com/stretchr/testify v1.9.0 // indirect +require ( + github.com/mattn/go-xmpp v0.0.0-20190124093244-6093f50721ed // indirect + github.com/stretchr/testify v1.9.0 // indirect +) diff --git a/go.sum b/go.sum index eb1ea6f..393874a 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/jbuchbinder/go-xmppbot v0.0.0-20201202211837-603b6f40c1d1 h1:D/19k3fV0YtcD2fot8lEqgPbY4xtRD+rck/IgWE23Z4= +github.com/jbuchbinder/go-xmppbot v0.0.0-20201202211837-603b6f40c1d1/go.mod h1:eWgqRsgB8cZqUd3VM03fsFR5Xmzqto5148/GO6WLPBk= +github.com/mattn/go-xmpp v0.0.0-20190124093244-6093f50721ed h1:A1hEQg5M0b3Wg06pm3q/B0wdZsPjVQ/a2IgauQ8wCZo= +github.com/mattn/go-xmpp v0.0.0-20190124093244-6093f50721ed/go.mod h1:Cs5mF0OsrRRmhkyOod//ldNPOwJsrBvJ+1WRspv0xoc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= diff --git a/main.py b/main.py index c0b03e6..9c1c86f 100644 --- a/main.py +++ b/main.py @@ -43,16 +43,13 @@ html_files = ( "application/xhtml+xml", ) - class Lifo(list): """ Limited size LIFO array to store messages and urls """ - def __init__(self, size): super().__init__() self.size = size - def add(self, item): self.insert(0, item) if len(self) > self.size: @@ -269,6 +266,10 @@ class ChenBot(ClientXMPP): if cm in self.commands: self.commands[cm](msg, ctx, sender) + @self.word + def SOURCE(msg, ctx, sender): + if ctx return: bot.send_message(mto=sender, mbody=f"`git clone git://git.chaotic.ninja/yakumo_izuru/chen`", mtype="chat") + async def muc_message(self, msg): """Process a groupchat message.""" if msg["type"] in ("groupchat", "normal"): @@ -290,15 +291,10 @@ class ChenBot(ClientXMPP): if cm in self.muc_commands: self.muc_commands[cm](msg, ctx, sender) - @self.muc_command - def repo(msg, ctx, sender): - if ctx: - return - bot.send_message( - mto=sender, - mbody=f"{msg['mucnick']}: https://git.chaotic.ninja/usr/yakumo_izuru/chen", - mtype="groupchat", - ) + @self.muc_word + def SOURCE(msg, ctx, sender): + if ctx return: bot.send_message(mto=sender, mbody=f"{msg['mucnick']}: `git clone git://git.chaotic.ninja/yakumo_izuru/chen`", mtype="groupchat") + if __name__ == "__main__": config = configparser.ConfigParser() diff --git a/version.go b/version.go new file mode 100644 index 0000000..f7b20e8 --- /dev/null +++ b/version.go @@ -0,0 +1,50 @@ +package chen + +import ( + "fmt" + "runtime/debug" + "strings" +) + +const ( + defaultVersion = "0.0.0" + defaultCommit = "HEAD" + defaultBuild = "0000-01-01:00:00+00:00" +) + +var ( + // Version is the tagged release version in the form .. + // following semantic versioning and is overwritten by the build system. + Version = defaultVersion + + // Commit is the commit sha of the build (normally from Git) and is overwritten + // by the build system. + Commit = defaultCommit + + // Build is the date and time of the build as an RFC3339 formatted string + // and is overwritten by the build system. + Build = defaultBuild +) + +// FullVersion display the full version and build +func FullVersion() string { + var sb strings.Builder + + isDefault := Version == defaultVersion && Commit == defaultCommit && Build == defaultBuild + + if !isDefault { + sb.WriteString(fmt.Sprintf("%s@%s %s", Version, Commit, Build)) + } + + if info, ok := debug.ReadBuildInfo(); ok { + if isDefault { + sb.WriteString(fmt.Sprintf(" %s", info.Main.Version)) + } + sb.WriteString(fmt.Sprintf(" %s", info.GoVersion)) + if info.Main.Sum != "" { + sb.WriteString(fmt.Sprintf(" %s", info.Main.Sum)) + } + } + + return sb.String() +}