52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
|
package threema
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"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{}
|
||
|
|
||
|
switch request {
|
||
|
case "generate":
|
||
|
|
||
|
// test if account already exists
|
||
|
account := t.getAccount(from)
|
||
|
if account != nil {
|
||
|
return fmt.Sprintf("you already has the threema account with id: %s", string(account.TID))
|
||
|
}
|
||
|
|
||
|
// create account
|
||
|
id, err := server.CreateIdentity()
|
||
|
if err != nil {
|
||
|
return fmt.Sprintf("failed to create a threema account: %s", err)
|
||
|
}
|
||
|
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
|
||
|
account = t.getAccount(from)
|
||
|
if account != nil {
|
||
|
return fmt.Sprintf("threema account with id: %s", string(account.TID))
|
||
|
}
|
||
|
return "failed to create a threema account"
|
||
|
}
|
||
|
return "command not supported"
|
||
|
}
|