mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-06-28 09:41:02 +00:00
Fix svc user (bot) so it actually starts
This commit is contained in:
parent
3f025d1ebb
commit
368577c99e
5
.gitignore
vendored
5
.gitignore
vendored
@ -17,4 +17,7 @@
|
|||||||
/cmd/salty-chat/salty-chat
|
/cmd/salty-chat/salty-chat
|
||||||
|
|
||||||
/data/*.key
|
/data/*.key
|
||||||
/data/.wellt-known
|
/data/.well-known
|
||||||
|
|
||||||
|
/echobot.sh
|
||||||
|
/execbot.sh
|
||||||
|
22
client.go
22
client.go
@ -3,7 +3,6 @@ package saltyim
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -85,27 +84,6 @@ func NewClient(me *Addr, options ...IdentityOption) (*Client, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateOrLoadClient creates a Client by creating or loading an existing identity
|
|
||||||
// from the given identity file and name of the client's user address
|
|
||||||
func CreateOrLoadClient(fn, name string) (*Client, error) {
|
|
||||||
me, err := ParseAddr(name)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if _, err := os.Stat(fn); err == nil {
|
|
||||||
return NewClient(me, WithIdentityPath(fn))
|
|
||||||
} else if !errors.Is(err, os.ErrNotExist) {
|
|
||||||
return nil, fmt.Errorf("failed to load client: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
identity, err := CreateIdentity(WithIdentityPath(fn), WithIdentityAddr(me))
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to create identity: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return NewClient(me, WithIdentityBytes(identity.Contents()))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *Client) getAddr(user string) (*Addr, error) {
|
func (cli *Client) getAddr(user string) (*Addr, error) {
|
||||||
addr, ok := cli.cache[user]
|
addr, ok := cli.cache[user]
|
||||||
if ok {
|
if ok {
|
||||||
|
21
identity.go
21
identity.go
@ -91,6 +91,10 @@ func GetIdentity(options ...IdentityOption) (*Identity, error) {
|
|||||||
option(ident)
|
option(ident)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ident.key != nil && ident.addr != nil {
|
||||||
|
return ident, nil
|
||||||
|
}
|
||||||
|
|
||||||
if ident.path != "" {
|
if ident.path != "" {
|
||||||
fn := ident.path
|
fn := ident.path
|
||||||
// Handle unix home with `~`
|
// Handle unix home with `~`
|
||||||
@ -123,6 +127,15 @@ func GetIdentity(options ...IdentityOption) (*Identity, error) {
|
|||||||
return ident, err
|
return ident, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetOrCreateIdentity ...
|
||||||
|
func GetOrCreateIdentity(options ...IdentityOption) (*Identity, error) {
|
||||||
|
ident, err := GetIdentity(options...)
|
||||||
|
if err != nil {
|
||||||
|
ident, err = CreateIdentity(options...)
|
||||||
|
}
|
||||||
|
return ident, err
|
||||||
|
}
|
||||||
|
|
||||||
// Identity allows interaction with CreateIdentity, GetIdentity, and NewClient
|
// Identity allows interaction with CreateIdentity, GetIdentity, and NewClient
|
||||||
type Identity struct {
|
type Identity struct {
|
||||||
// path where identity is read or written to
|
// path where identity is read or written to
|
||||||
@ -165,6 +178,14 @@ func WithIdentityAddr(addr *Addr) IdentityOption {
|
|||||||
return func(i *Identity) { i.addr = addr }
|
return func(i *Identity) { i.addr = addr }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithIdentity indicates that an identity should be passed in
|
||||||
|
func WithIdentity(ident *Identity) IdentityOption {
|
||||||
|
return func(i *Identity) {
|
||||||
|
i.key = ident.key
|
||||||
|
i.addr = ident.addr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WithIdentityPath indicates that an identity should be read / written from a file path
|
// WithIdentityPath indicates that an identity should be read / written from a file path
|
||||||
func WithIdentityPath(path string) IdentityOption {
|
func WithIdentityPath(path string) IdentityOption {
|
||||||
return func(i *Identity) { i.path = path }
|
return func(i *Identity) { i.path = path }
|
||||||
|
@ -222,15 +222,39 @@ func (s *Server) setupCronJobs() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) setupSvcUser() {
|
func (s *Server) setupSvcUser() {
|
||||||
time.Sleep(time.Second * 5)
|
|
||||||
|
|
||||||
log.Infof("starting service user %s", s.config.SvcUser)
|
log.Infof("starting service user %s", s.config.SvcUser)
|
||||||
|
|
||||||
// create or load client for services user
|
// create our addr
|
||||||
identity := filepath.Join(s.config.Data, servicesIdentity)
|
me, err := saltyim.ParseAddr(s.config.SvcUser)
|
||||||
cli, err := saltyim.CreateOrLoadClient(identity, s.config.SvcUser)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Errorf("error creating or loading service user")
|
log.WithError(err).Error("error oarsing scv user addr: %w", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// create or load client for services user
|
||||||
|
fn := filepath.Join(s.config.Data, servicesIdentity)
|
||||||
|
ident, err := saltyim.GetOrCreateIdentity(
|
||||||
|
saltyim.WithIdentityPath(fn),
|
||||||
|
saltyim.WithIdentityAddr(me),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Error("error getting or creating svc user's identity")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !FileExists(fn) {
|
||||||
|
if err := CreateConfig(s.config, me.Hash(), ident.Key().ID().String()); err != nil {
|
||||||
|
log.WithError(err).Error("error creating service config")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for things to settle
|
||||||
|
time.Sleep(time.Second * 5)
|
||||||
|
|
||||||
|
cli, err := saltyim.NewClient(me, saltyim.WithIdentity(ident))
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Error("error creating svc user's client")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
svc, err := saltyim.NewService(cli)
|
svc, err := saltyim.NewService(cli)
|
||||||
@ -240,12 +264,6 @@ func (s *Server) setupSvcUser() {
|
|||||||
}
|
}
|
||||||
s.svc = svc
|
s.svc = svc
|
||||||
|
|
||||||
err = CreateConfig(s.config, cli.Me().Hash(), cli.Me().Key().ID().String())
|
|
||||||
if err != nil {
|
|
||||||
log.WithError(err).Error("error creating service config")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
svc.TextFunc("register", func(ctx context.Context, bot *saltyim.Service, key *keys.EdX25519PublicKey, msg *lextwt.SaltyText) error {
|
svc.TextFunc("register", func(ctx context.Context, bot *saltyim.Service, key *keys.EdX25519PublicKey, msg *lextwt.SaltyText) error {
|
||||||
addr, err := saltyim.ParseAddr(msg.User.String())
|
addr, err := saltyim.ParseAddr(msg.User.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
13
internal/utils.go
Normal file
13
internal/utils.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package internal
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
|
||||||
|
// FileExists returns true if the given file exists
|
||||||
|
func FileExists(name string) bool {
|
||||||
|
if _, err := os.Stat(name); err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user