From de5a57e46b02dbf7c02df23c281a7594eb11223b Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Fri, 31 May 2019 10:07:40 +0200 Subject: [PATCH] Initial commit --- README.md | 4 ++ cmd/root.go | 26 +++++++++++ cmd/serve.go | 95 ++++++++++++++++++++++++++++++++++++++ config.toml | 9 ++++ main.go | 7 +++ models/account_threema.go | 17 +++++++ models/account_xmpp.go | 17 +++++++ thrempp.db | Bin 0 -> 24576 bytes 8 files changed, 175 insertions(+) create mode 100644 README.md create mode 100644 cmd/root.go create mode 100644 cmd/serve.go create mode 100644 config.toml create mode 100644 main.go create mode 100644 models/account_threema.go create mode 100644 models/account_xmpp.go create mode 100644 thrempp.db 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 0000000000000000000000000000000000000000..57f01e11ebd2174f201099dd945addf3b2ef3571 GIT binary patch literal 24576 zcmeI(&uSAv90%~3Y?7s78xbT2Jq!!MghWl1w&11K8n76eKUUDwI?0Z9Np_QNCM87> zQV)XOMDV0Ogl7*u>l1j;7wA!M9-Q4Ic9Ytkq=oPuW|QB}?9A+ZW-r4g_bWwP3ihPs zIb5rQ99itRgfq#q=9R)Oc~)HzJNt_y=I4IQJ(%@Why(!$ zKmY;|fB*y_009U<;A8}z#q{KAiuNzst|fZ5)iZg$-s-y2>^beWX^Ezg!ZNuG7spqt zg_Vs0TU)A_8g zO9PQ^M|cL?=3af1d+F;73z?h|RH*Xp$tsrV>zbZi%hLYkL%?#=6T;#DFdo)Kk7PdF zbWD0!J&s+D#FN=9eH2i4wwh|MYj(s|SGaXCajK1hPgJH|uHaP5OutBVzsudy*zyfW zAFbfZjBL}erhKmY;|fB*y_009U< z00Izz00d4$fK#0;FOy5fd_F&aCCDtFzZv9g@Ofm#QhBL*Z*j5u;pN`zpBHH4+n2A6 zQswTeYcln5@3~uf^Y;7h=TDJ$gZ=+LN$j7-1TiKAAOHafKmY;|fB*y_009U<00RG8 z;5^l`GZVj1IHLxkiQhzJBnUtN0uX=z1Rwwb2tWV=5P$##rcglB$c+E~U;XJH2?7v+ z00bZa0SG_<0uX=z1Rwx`DHibG|6~6@#S4t7K>z{}fB*y_009U<00Izz00fl4AMzPS ASO5S3 literal 0 HcmV?d00001