~bfiedler/go-threema

6b3757df1fed352d640fb8f072ef03c3d381a790 — Ben Fiedler 1 year, 8 months ago 77df0f4
tcli: Implement sending images
2 files changed, 35 insertions(+), 5 deletions(-)

M README.md
M cmd/tcli/main.go
M README.md => README.md +1 -1
@@ 20,10 20,10 @@ The following features are supported:
 - [x] Sending text messages to Threema IDs / phone numbers / e-mail addresses
 - [x] Querying receiver capabilities
 - [x] Uploading blobs (prerequisite for sending images, videos and files)
 - [x] Sending images

The following features are not (yet) supported:

 - [ ] Sending images
 - [ ] Sending videos
 - [ ] Sending files
 - [ ] Receiving messages

M cmd/tcli/main.go => cmd/tcli/main.go +34 -4
@@ 33,10 33,11 @@ func usage() {
	fmt.Fprintf(os.Stderr, "Flags\n")
	pflag.PrintDefaults()
	fmt.Fprintf(os.Stderr, "Command list\n")
	fmt.Fprintf(os.Stderr, "  credits                   Prints the remaining credits for this account\n")
	fmt.Fprintf(os.Stderr, "  qr                        Displays the client's public key in Threema-compatible QR-form (E2E only). Requires qrencode to be installed.\n")
	fmt.Fprintf(os.Stderr, "  lookup <recipient>        Looks up recipient's Threema ID\n")
	fmt.Fprintf(os.Stderr, "  send   <recipient>        Reads a message from stdin and sends it to <recipient>\n")
	fmt.Fprintf(os.Stderr, "  credits                        Prints the remaining credits for this account\n")
	fmt.Fprintf(os.Stderr, "  qr                             Displays the client's public key in Threema-compatible QR-form (E2E only). Requires qrencode to be installed.\n")
	fmt.Fprintf(os.Stderr, "  lookup      <recipient>        Looks up recipient's Threema ID\n")
	fmt.Fprintf(os.Stderr, "  send        <recipient>        Reads a message from stdin and sends it to <recipient>\n")
	fmt.Fprintf(os.Stderr, "  sendimage   <recipient> <img>  Send image <img> to <recipient> (e2e only)\n")
	fmt.Fprintf(os.Stderr, "By default the recipient is addressed using his Threema ID, this can be controlled using the -p and -e flags.\n")
}



@@ 150,6 151,8 @@ func parseCommand() (command, error) {
		cmd = &lookupCommand{arg: args[1]}
	case "send":
		cmd = &sendCommand{recipient: args[1]}
	case "sendimage":
		cmd = &imageCommand{recipient: args[1], imgPath: args[2]}
	}
	return cmd, nil
}


@@ 164,6 167,8 @@ func expectedArgs(command string) (int, error) {
		return 2, nil
	case "send":
		return 2, nil
	case "sendimage":
		return 3, nil
	default:
		return 0, fmt.Errorf("unknown command '%s'", command)
	}


@@ 181,6 186,11 @@ type sendCommand struct {
	recipient string
}

type imageCommand struct {
	recipient string
	imgPath string
}

func (creditsCommand) run(c client.Client) error {
	n, err := c.GetRemainingCredits()
	if err != nil {


@@ 251,3 261,23 @@ func (cmd sendCommand) run(c client.Client) error {
	}
	return err
}

func (cmd imageCommand) run(c client.Client) error {
	img, err := os.Open(cmd.imgPath)
	if err != nil {
		log.Fatalf("could not read image: %v", err)
	}
	defer img.Close()

	id := cmd.recipient
	if *email {
		id, err = c.LookupIDByEmailHash(cmd.recipient)
	} else if *phone {
		id, err = c.LookupIDByPhoneHash(cmd.recipient)
	}
	if err != nil {
		return err
	}

	return c.(*client.E2EClient).SendImage(id, img)
}