This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
thrempp/component/threema/bot.go

62 lines
1.4 KiB
Go
Raw Normal View History

2019-05-31 18:26:15 +02:00
package threema
import (
"fmt"
"github.com/bdlm/log"
"github.com/o3ma/o3"
"dev.sum7.eu/genofire/golang-lib/database"
"dev.sum7.eu/genofire/thrempp/models"
)
func (t *Threema) Bot(from *models.JID, request string) string {
server := o3.ThreemaRest{}
logger := log.WithFields(map[string]interface{}{
"type": "threema",
"jid": from.String(),
})
switch request {
case "generate":
// test if account already exists
2019-06-01 15:21:49 +02:00
account, err := t.getAccount(from)
if err == nil {
2019-05-31 18:26:15 +02:00
return fmt.Sprintf("you already has the threema account with id: %s", string(account.TID))
}
// create account
id, err := server.CreateIdentity()
if err != nil {
logger.Warnf("failed to generate: %s", err)
return fmt.Sprintf("failed to create a threema account: %s", err)
}
//TODO works it
if err := database.Read.Where(from).First(from); err != nil {
database.Write.Create(from)
}
// store account
a := models.AccountThreema{}
a.XMPPID = from.ID
a.TID = make([]byte, len(id.ID))
a.LSK = make([]byte, len(id.LSK))
copy(a.TID, id.ID[:])
copy(a.LSK, id.LSK[:])
database.Write.Create(&a)
// fetch account and connect
2019-06-01 15:21:49 +02:00
account, err = t.getAccount(from)
2019-05-31 18:26:15 +02:00
tid := string(account.TID)
if tid != "" {
logger.WithField("threema", tid).Info("generate")
return fmt.Sprintf("threema account with id: %s", tid)
}
logger.Warn("failed to generate")
return "failed to create a threema account"
}
return "command not supported"
}