mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-06-30 18:51:03 +00:00
131 lines
3.1 KiB
Go
131 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
"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 <user> [broker-url]",
|
|
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/.config/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
|
|
|
|
If the -u/--endpoint flag or <endpoint> argument is passed this is used as the broker
|
|
endpoint for constructing the Well-Known Config, if neither is used the broker is
|
|
assumed to be the domain part of the nick@domain <user> argument.
|
|
|
|
NOTE: The <endpoint> argument will override and -u/--endpoint flag set.`,
|
|
Args: cobra.RangeArgs(1, 2),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
identity := viper.GetString("identity")
|
|
|
|
endpoint, err := cmd.Flags().GetString("endpoint")
|
|
if err != nil {
|
|
log.WithError(err).Fatal("error getting -e/--endpoint flag")
|
|
}
|
|
if len(args) == 2 {
|
|
endpoint = args[1]
|
|
}
|
|
|
|
me := &saltyim.Addr{}
|
|
if sp := strings.Split(args[0], "@"); len(sp) > 1 {
|
|
me.User = sp[0]
|
|
me.Domain = sp[1]
|
|
}
|
|
// XXX: What if me.IsZero()
|
|
|
|
makeuser(me, identity, endpoint)
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(makeuserCmd)
|
|
|
|
makeuserCmd.Flags().StringP(
|
|
"endpoint", "e", "",
|
|
"use a custom broker endpoint",
|
|
)
|
|
}
|
|
|
|
func makeuser(me *saltyim.Addr, identity, endpoint string) {
|
|
u, err := url.Parse(endpoint)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "error parsing endpoint %s: %s\n", endpoint, err)
|
|
os.Exit(2)
|
|
}
|
|
ulid := saltyim.MustGenerateULID()
|
|
if strings.HasSuffix(u.Path, "/") {
|
|
u.Path += fmt.Sprintf("%s", ulid)
|
|
} else {
|
|
u.Path += fmt.Sprintf("/%s", ulid)
|
|
}
|
|
|
|
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 err := saltyim.CreateIdentity(identity, me.String()); err != nil {
|
|
fmt.Fprintf(os.Stderr, "error creating identity %q for %s: %s\n", identity, me, err)
|
|
os.Exit(2)
|
|
}
|
|
|
|
key, _, err := saltyim.GetIdentity(identity)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "error reading identity %s for %s: %s\n", identity, me, err)
|
|
os.Exit(2)
|
|
}
|
|
|
|
ctx := setupCtx{
|
|
Config: saltyim.Config{
|
|
Endpoint: u.String(),
|
|
Key: key.PublicKey().ID().String(),
|
|
},
|
|
Addr: me,
|
|
}
|
|
|
|
fmt.Println(mustRenderString(postSetupInstructions, ctx))
|
|
}
|