~bfiedler/go-threema

5fff32c55e285f6d7786e4885525570fc0cf0915 — Ben Fiedler 1 year, 6 months ago 2784584
tcli: Add qr command to display client's qr code
1 files changed, 52 insertions(+), 22 deletions(-)

M cmd/tcli/main.go
M cmd/tcli/main.go => cmd/tcli/main.go +52 -22
@@ 8,6 8,8 @@ import (
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"strings"

	"github.com/spf13/pflag"



@@ 141,6 143,8 @@ func parseCommand() (command, error) {
	switch c {
	case "credits":
		cmd = &creditsCommand{}
	case "qr":
		cmd = &qrCommand{}
	case "lookup":
		cmd = &lookupCommand{arg: args[1]}
	case "send":


@@ 153,6 157,8 @@ func expectedArgs(command string) (int, error) {
	switch command {
	case "credits":
		return 1, nil
	case "qr":
		return 1, nil
	case "lookup":
		return 2, nil
	case "send":


@@ 162,33 168,47 @@ func expectedArgs(command string) (int, error) {
	}
}

type sendCommand struct {
	recipient string
}
type creditsCommand struct{}

type qrCommand struct{}

type lookupCommand struct {
	arg string
}

type creditsCommand struct{}

func (cmd sendCommand) run(c client.Client) error {
	var msg []byte
	fmt.Fprintf(os.Stderr, "reading message from stdin\n")
type sendCommand struct {
	recipient string
}

	msg, err := ioutil.ReadAll(os.Stdin)
func (creditsCommand) run(c client.Client) error {
	n, err := c.GetRemainingCredits()
	if err != nil {
		log.Fatalf("could not read message: %v", err)
		return err
	}
	fmt.Printf("remaining credits: %d\n", n)
	return nil
}

	if *email {
		err = c.SendTextMessageToEmail(cmd.recipient, string(msg))
	} else if *phone {
		err = c.SendTextMessageToPhone(cmd.recipient, string(msg))
	} else {
		err = c.SendTextMessage(cmd.recipient, string(msg))
func (qrCommand) run(c client.Client) error {
	e2eClient, ok := c.(*client.E2EClient)
	if !ok {
		return errors.New("The 'qr' command only makes sense for E2E mode, as only an E2E client has a private key.")
	}
	return err

	qrencode, err := exec.LookPath("qrencode")
	if err != nil {
		return errors.New("The shell command 'qrencode' ist required for this to work.")
	}

	// 3mid == Threema ID I guess?
	inp := fmt.Sprintf("3mid:%s,%s", c.ID(), e2eClient.PublicKey())
	fmt.Println(inp)

	cmd := exec.Command(qrencode, "-t", "ANSI")
	cmd.Stdin = strings.NewReader(inp)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	return cmd.Run()
}

func (cmd lookupCommand) run(c client.Client) error {


@@ 211,11 231,21 @@ func (cmd lookupCommand) run(c client.Client) error {
	return err
}

func (creditsCommand) run(c client.Client) error {
	n, err := c.GetRemainingCredits()
func (cmd sendCommand) run(c client.Client) error {
	var msg []byte
	fmt.Fprintf(os.Stderr, "reading message from stdin\n")

	msg, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		return err
		log.Fatalf("could not read message: %v", err)
	}
	fmt.Printf("remaining credits: %d\n", n)
	return nil

	if *email {
		err = c.SendTextMessageToEmail(cmd.recipient, string(msg))
	} else if *phone {
		err = c.SendTextMessageToPhone(cmd.recipient, string(msg))
	} else {
		err = c.SendTextMessage(cmd.recipient, string(msg))
	}
	return err
}