Initial commit
This commit is contained in:
commit
de5a57e46b
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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")
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
log_level = 50
|
||||
|
||||
[database]
|
||||
type = "sqlite3"
|
||||
logging = true
|
||||
connection = "./thrempp.db"
|
||||
# For Master-Slave cluster
|
||||
# read_connection = ""
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package main
|
||||
|
||||
import "dev.sum7.eu/genofire/thrempp/cmd"
|
||||
|
||||
func main() {
|
||||
cmd.Execute()
|
||||
}
|
|
@ -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{})
|
||||
}
|
|
@ -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{})
|
||||
}
|
Binary file not shown.
Reference in New Issue