6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-30 18:51:03 +00:00
prologic-saltyim/cmd/salty-chat/makeuser.go
2022-03-20 23:54:54 +10:00

100 lines
2.4 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.mills.io/saltyim"
)
const (
postSetupInstructions = `Create this file and place it on your web server
so that it is accessible at a top-level domain or sub-domain at the URL:
{{ .Addr.HashURI }}
mkdir -p .well-known/salty
cat > .well-known/salty/{{ .Addr.Hash }}.json << EOF
{
"endpoint": "{{ .Config.Endpoint }}",
"key": "{{ .Config.Key }}"
}
EOF
To verify you have done this correctly:
$ salty-chat lookup {{ .Addr }}`
)
type setupCtx struct {
Config saltyim.Config
Addr saltyim.Addr
}
var makeuserCmd = &cobra.Command{
Use: "make-user nick@domain",
Aliases: []string{"mkuser", "setup"},
Short: "Creates a new Salty User",
Long: `This command creates a new Salty User and Key Pair storing the
Private Key in either $XDG_CONFIG_HOME/salty/ or $HOME/.salty/ as well as
information on how to setup the Well-Known Config and URI for Discovery by
others Salty IM Users.
A valid top-level domain or sub-domain is required and the <user> is in the form of:
username@domain`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
identity := viper.GetString("identity")
uri := viper.GetString("broker-uri")
user := args[0]
makeuser(identity, uri, user)
},
}
func init() {
rootCmd.AddCommand(makeuserCmd)
}
func makeuser(identity, uri, user string) {
if _, err := saltyim.ParseAddr(user); err != nil {
fmt.Fprintf(os.Stderr, "error parsing user %q: %s\n", user, err)
os.Exit(2)
}
dir := filepath.Dir(identity)
if err := os.MkdirAll(dir, 0700); err != nil {
fmt.Fprintf(os.Stderr, "error creating configuration directory %s: %s\n", dir, err)
os.Exit(2)
}
if saltyim.FileExists(identity) {
fmt.Fprintf(os.Stderr, "error identity %q already exists!\n", identity)
}
if err := saltyim.CreateIdentity(identity, user); err != nil {
fmt.Fprintf(os.Stderr, "error creating identity %q for %s: %s\n", identity, user, err)
os.Exit(2)
}
key, me, err := saltyim.GetIdentity(identity)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading identity %s for %s: %s\n", identity, user, err)
os.Exit(2)
}
ctx := setupCtx{
Config: saltyim.Config{
Endpoint: fmt.Sprintf("%s/%s", uri, me.User),
Key: key.PublicKey().ID().String(),
},
Addr: me,
}
fmt.Println(saltyim.MustRenderString(postSetupInstructions, ctx))
}