commit de5a57e46b02dbf7c02df23c281a7594eb11223b Author: Martin/Geno Date: Fri May 31 10:07:40 2019 +0200 Initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..c286048 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# Threempp +Threema XMPP - Transport + + diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..ec76178 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,26 @@ +package cmd + +import ( + "os" + + "github.com/bdlm/log" + "github.com/spf13/cobra" +) + +var ( + timestamps bool +) + +// RootCmd represents the base command when called without any subcommands +var RootCmd = &cobra.Command{ + Use: "thrempp", +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + if err := RootCmd.Execute(); err != nil { + log.Fatal(err) + os.Exit(1) + } +} diff --git a/cmd/serve.go b/cmd/serve.go new file mode 100644 index 0000000..5489f8c --- /dev/null +++ b/cmd/serve.go @@ -0,0 +1,95 @@ +package cmd + +import ( + "fmt" + + "github.com/bdlm/log" + "github.com/bdlm/std/logger" + "github.com/spf13/cobra" + + "github.com/o3ma/o3" + + "dev.sum7.eu/genofire/golang-lib/database" + "dev.sum7.eu/genofire/golang-lib/file" + + // for init database + "dev.sum7.eu/genofire/thrempp/models" +) + +type Config struct { + LogLevel logger.Level `toml:"log_level"` + Database database.Config `toml:"database"` +} + +var configPath string + +// serveCmd represents the serve command +var serveCmd = &cobra.Command{ + Use: "serve", + Short: "Run xmpp transport", + Example: "yanic serve --config /etc/thrempp.toml", + Run: func(cmd *cobra.Command, args []string) { + config := &Config{} + if err := file.ReadTOML(configPath, config); err != nil { + log.Panicf("open config file: %s", err) + } + + log.SetLevel(config.LogLevel) + + if err := database.Open(config.Database); err != nil { + log.Panicf("no database connection: %s", err) + } + defer database.Close() + + server := o3.ThreemaRest{} + + var thrAccount models.AccountThreema + if err := database.Read.First(&thrAccount).Error; err != nil { + id, _ := server.CreateIdentity() + thrAccount.TID = make([]byte, len(id.ID)) + thrAccount.LSK = make([]byte, len(id.LSK)) + copy(thrAccount.TID, id.ID[:]) + copy(thrAccount.LSK, id.LSK[:]) + database.Write.Create(&thrAccount) + } + + log.Warnf("%s", thrAccount.TID) + var lsk [32]byte + copy(lsk[:], thrAccount.LSK[:]) + tid, err := o3.NewThreemaID(string(thrAccount.TID), lsk, o3.AddressBook{}) + tid.Nick = o3.NewPubNick("xmpp:geno@fireorbit.de") + + ctx := o3.NewSessionContext(tid) + + // let the session begin + log.Info("Starting session") + sendMsgChan, receiveMsgChan, err := ctx.Run() + if err != nil { + log.Fatal(err) + } + // handle incoming messages + for receivedMessage := range receiveMsgChan { + if receivedMessage.Err != nil { + log.Errorf("Error Receiving Message: %s\n", receivedMessage.Err) + continue + } + switch msg := receivedMessage.Msg.(type) { + case o3.TextMessage: + if tid.String() == msg.Sender().String() { + continue + } + qoute := fmt.Sprintf("> %s: %s\n%s", msg.Sender(), msg.Text(), "Exactly!") + err = ctx.SendTextMessage(msg.Sender().String(), qoute, sendMsgChan) + if err != nil { + log.Fatal(err) + } + + } + } + }, +} + +func init() { + RootCmd.AddCommand(serveCmd) + serveCmd.Flags().StringVarP(&configPath, "config", "c", "config.toml", "Path to configuration file") +} diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..5568587 --- /dev/null +++ b/config.toml @@ -0,0 +1,9 @@ +log_level = 50 + +[database] +type = "sqlite3" +logging = true +connection = "./thrempp.db" +# For Master-Slave cluster +# read_connection = "" + diff --git a/main.go b/main.go new file mode 100644 index 0000000..d54f0b9 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "dev.sum7.eu/genofire/thrempp/cmd" + +func main() { + cmd.Execute() +} diff --git a/models/account_threema.go b/models/account_threema.go new file mode 100644 index 0000000..047516d --- /dev/null +++ b/models/account_threema.go @@ -0,0 +1,17 @@ +package models + +import ( + "github.com/jinzhu/gorm" + + "dev.sum7.eu/genofire/golang-lib/database" +) + +type AccountThreema struct { + gorm.Model + TID []byte + LSK []byte +} + +func init() { + database.AddModel(&AccountThreema{}) +} diff --git a/models/account_xmpp.go b/models/account_xmpp.go new file mode 100644 index 0000000..7f98b66 --- /dev/null +++ b/models/account_xmpp.go @@ -0,0 +1,17 @@ +package models + +import ( + "github.com/jinzhu/gorm" + + "dev.sum7.eu/genofire/golang-lib/database" +) + +type AccountXMPP struct { + gorm.Model + XMPPUser string + XMPPServer string +} + +func init() { + database.AddModel(&AccountXMPP{}) +} diff --git a/thrempp.db b/thrempp.db new file mode 100644 index 0000000..57f01e1 Binary files /dev/null and b/thrempp.db differ