about summary refs log blame commit diff stats
path: root/commands/msgview/open.go
blob: d1b323802d574323fac29a9f49998d3baa558c92 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16















                                          

                  
             








                                                                    

 
                                                                







































                                                                                           
package msgview

import (
	"encoding/base64"
	"errors"
	"io"
	"io/ioutil"
	"mime/quotedprintable"
	"os"
	"os/exec"
	"strings"
	"time"

	"git.sr.ht/~sircmpwn/aerc/widgets"
)

type Open struct{}

func init() {
	register(Open{})
}

func (_ Open) Aliases() []string {
	return []string{"open"}
}

func (_ Open) Complete(aerc *widgets.Aerc, args []string) []string {
	return nil
}

func (_ Open) Execute(aerc *widgets.Aerc, args []string) error {
	if len(args) != 1 {
		return errors.New("Usage: open")
	}

	mv := aerc.SelectedTab().(*widgets.MessageViewer)
	p := mv.CurrentPart()

	p.Store.FetchBodyPart(p.Msg.Uid, p.Index, func(reader io.Reader) {
		// email parts are encoded as 7bit (plaintext), quoted-printable, or base64

		if strings.EqualFold(p.Part.Encoding, "base64") {
			reader = base64.NewDecoder(base64.StdEncoding, reader)
		} else if strings.EqualFold(p.Part.Encoding, "quoted-printable") {
			reader = quotedprintable.NewReader(reader)
		}

		tmpFile, err := ioutil.TempFile(os.TempDir(), "aerc-")
		if err != nil {
			aerc.PushError(" " + err.Error())
			return
		}
		defer tmpFile.Close()

		_, err = io.Copy(tmpFile, reader)
		if err != nil {
			aerc.PushError(" " + err.Error())
			return
		}

		cmd := exec.Command("xdg-open", tmpFile.Name())
		err = cmd.Run()
		if err != nil {
			aerc.PushError(" " + err.Error())
		}

		aerc.PushStatus("Opened", 10*time.Second)
	})

	return nil
}
KEY_CTRL_S 0x13 #define TB_KEY_CTRL_T 0x14 #define TB_KEY_CTRL_U 0x15 #define TB_KEY_CTRL_V 0x16 #define TB_KEY_CTRL_W 0x17 #define TB_KEY_CTRL_X 0x18 #define TB_KEY_CTRL_Y 0x19 #define TB_KEY_CTRL_Z 0x1A #define TB_KEY_ESC 0x1B #define TB_KEY_CTRL_LSQ_BRACKET 0x1B /* clash with 'ESC' */ #define TB_KEY_CTRL_3 0x1B /* clash with 'ESC' */ #define TB_KEY_CTRL_4 0x1C #define TB_KEY_CTRL_BACKSLASH 0x1C /* clash with 'CTRL_4' */ #define TB_KEY_CTRL_5 0x1D #define TB_KEY_CTRL_RSQ_BRACKET 0x1D /* clash with 'CTRL_5' */ #define TB_KEY_CTRL_6 0x1E #define TB_KEY_CTRL_7 0x1F #define TB_KEY_CTRL_SLASH 0x1F /* clash with 'CTRL_7' */ #define TB_KEY_CTRL_UNDERSCORE 0x1F /* clash with 'CTRL_7' */ #define TB_KEY_SPACE 0x20 #define TB_KEY_BACKSPACE2 0x7F #define TB_KEY_CTRL_8 0x7F /* clash with 'DELETE' */ /* These are non-existing ones. * * #define TB_KEY_CTRL_1 clash with '1' * #define TB_KEY_CTRL_9 clash with '9' * #define TB_KEY_CTRL_0 clash with '0' */ /* Wait for an event up to 'timeout' milliseconds and fill the 'event' * structure with it, when the event is available. Returns the type of the * event (one of TB_EVENT_* constants) or -1 if there was an error or 0 in case * there were no event during 'timeout' period. */ int tb_peek_event(struct tb_event *event, int timeout); /* Wait for an event forever and fill the 'event' structure with it, when the * event is available. Returns the type of the event (one of TB_EVENT_* * constants) or -1 if there was an error. */ int tb_poll_event(struct tb_event *event); /*** 3. Utility utf8 functions. */ #define TB_EOF -1 int tb_utf8_char_length(char c); int tb_utf8_char_to_unicode(uint32_t *out, const char *c); int tb_utf8_unicode_to_char(char *out, uint32_t c); #ifdef __cplusplus } #endif