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/chat.go
xuu 7ccd59efc0 feat: make endpoint uniform. add profiles (#30)
Co-authored-by: Jon Lundy <jon@xuu.cc>
Reviewed-on: https://git.mills.io/prologic/saltyim/pulls/30
Co-authored-by: xuu <xuu@noreply@mills.io>
Co-committed-by: xuu <xuu@noreply@mills.io>
2022-03-21 00:31:59 +00:00

80 lines
1.8 KiB
Go

package main
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.mills.io/saltyim"
)
var chatCmd = &cobra.Command{
Use: "chat <user>",
Short: "Creates a chat with a specific user",
Long: `This command creates a chat with the specified user by subscribing
to your default inbox (normally $USER) and prompts for input and sends encrypted
messages to the user via their endpoint.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
user := viper.GetString("user")
endpoint := viper.GetString("endpoint")
identity := viper.GetString("identity")
var profiles []profile
viper.UnmarshalKey("profiles", &profiles)
for _, p := range profiles {
if user == p.User {
endpoint = p.Endpoint
identity = p.Identity
}
}
var me saltyim.Addr
if sp := strings.Split(user, "@"); len(sp) > 1 {
me.User = sp[0]
me.Domain = sp[1]
}
chat(me, identity, endpoint, args[0])
},
}
func init() {
rootCmd.AddCommand(chatCmd)
}
func chat(me saltyim.Addr, identity, endpoint, user string) {
key, m, err := saltyim.GetIdentity(identity)
if err != nil {
fmt.Fprintf(os.Stderr, "error opening identity: %q\n", identity)
os.Exit(2)
}
if me.IsZero() {
me = m
}
if me.IsZero() {
fmt.Fprintf(os.Stderr, "unable to find your user addressn in %q\n", identity)
fmt.Fprintln(os.Stderr, "tip: try adding # user: nick@domain to your identity")
os.Exit(2)
}
// Initialize necessary channels.
inCh := make(chan string)
outCh := make(chan string)
// Initialize client.
cc, err := saltyim.NewChatClient(key, me, endpoint, user)
if err != nil {
fmt.Fprintf(os.Stderr, "error creating chat: %s", err)
os.Exit(2)
}
saltyim.SetTerminalTitle("Salty IM with %s", user)
go cc.RunChat(inCh, outCh)
cc.SetScreen(inCh, outCh)
}