| 1 |
package compose |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"os" |
| 9 |
"os/exec" |
| 10 |
"path/filepath" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"git.sr.ht/~rjarry/aerc/app" |
| 14 |
"git.sr.ht/~rjarry/aerc/commands" |
| 15 |
"git.sr.ht/~rjarry/aerc/config" |
| 16 |
"git.sr.ht/~rjarry/aerc/lib" |
| 17 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 18 |
"git.sr.ht/~rjarry/aerc/lib/ui" |
| 19 |
"git.sr.ht/~rjarry/aerc/lib/xdg" |
| 20 |
"github.com/pkg/errors" |
| 21 |
) |
| 22 |
|
| 23 |
type Attach struct { |
| 24 |
Menu bool `opt:"-m" desc:"Select files from file-picker-cmd."` |
| 25 |
Name string `opt:"-r" desc:"<name> <cmd...>: Generate attachment from command output."` |
| 26 |
Path string `opt:"path" required:"false" complete:"CompletePath" desc:"Attachment file path."` |
| 27 |
Args string `opt:"..." required:"false"` |
| 28 |
} |
| 29 |
|
| 30 |
func init() { |
| 31 |
commands.Register(Attach{}) |
| 32 |
} |
| 33 |
|
| 34 |
func (Attach) Description() string { |
| 35 |
return "Attach the file at the given path to the email." |
| 36 |
} |
| 37 |
|
| 38 |
func (Attach) Context() commands.CommandContext { |
| 39 |
return commands.COMPOSE_EDIT | commands.COMPOSE_REVIEW |
| 40 |
} |
| 41 |
|
| 42 |
func (Attach) Aliases() []string { |
| 43 |
return []string{"attach"} |
| 44 |
} |
| 45 |
|
| 46 |
func (*Attach) CompletePath(arg string) []string { |
| 47 |
return commands.CompletePath(arg, false) |
| 48 |
} |
| 49 |
|
| 50 |
func (a Attach) Execute(args []string) error { |
| 51 |
if a.Menu && a.Name != "" { |
| 52 |
return errors.New("-m and -r are mutually exclusive") |
| 53 |
} |
| 54 |
switch { |
| 55 |
case a.Menu: |
| 56 |
return a.openMenu() |
| 57 |
case a.Name != "": |
| 58 |
if a.Path == "" { |
| 59 |
return errors.New("command is required") |
| 60 |
} |
| 61 |
return a.readCommand() |
| 62 |
default: |
| 63 |
if a.Args != "" { |
| 64 |
return errors.New("only a single path is supported") |
| 65 |
} |
| 66 |
return a.addPath(a.Path) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func (a Attach) addPath(path string) error { |
| 71 |
path = xdg.ExpandHome(path) |
| 72 |
attachments, err := filepath.Glob(path) |
| 73 |
if err != nil && errors.Is(err, filepath.ErrBadPattern) { |
| 74 |
log.Warnf("failed to parse as globbing pattern: %v", err) |
| 75 |
attachments = []string{path} |
| 76 |
} |
| 77 |
|
| 78 |
if !strings.HasPrefix(path, ".") && !strings.Contains(path, "/.") { |
| 79 |
log.Debugf("removing hidden files from glob results") |
| 80 |
for i := len(attachments) - 1; i >= 0; i-- { |
| 81 |
if strings.HasPrefix(filepath.Base(attachments[i]), ".") { |
| 82 |
if i == len(attachments)-1 { |
| 83 |
attachments = attachments[:i] |
| 84 |
continue |
| 85 |
} |
| 86 |
attachments = append(attachments[:i], attachments[i+1:]...) |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
composer, _ := app.SelectedTabContent().(*app.Composer) |
| 92 |
for _, attach := range attachments { |
| 93 |
log.Debugf("attaching '%s'", attach) |
| 94 |
|
| 95 |
pathinfo, err := os.Stat(attach) |
| 96 |
if err != nil { |
| 97 |
log.Errorf("failed to stat file: %v", err) |
| 98 |
app.PushError(err.Error()) |
| 99 |
return err |
| 100 |
} else if pathinfo.IsDir() && len(attachments) == 1 { |
| 101 |
app.PushError("Attachment must be a file, not a directory") |
| 102 |
return nil |
| 103 |
} |
| 104 |
|
| 105 |
composer.AddAttachment(attach) |
| 106 |
} |
| 107 |
|
| 108 |
if len(attachments) == 1 { |
| 109 |
app.PushSuccess(fmt.Sprintf("Attached %s", path)) |
| 110 |
} else { |
| 111 |
app.PushSuccess(fmt.Sprintf("Attached %d files", len(attachments))) |
| 112 |
} |
| 113 |
|
| 114 |
return nil |
| 115 |
} |
| 116 |
|
| 117 |
func (a Attach) openMenu() error { |
| 118 |
filePickerCmd := config.Compose().FilePickerCmd |
| 119 |
if filePickerCmd == "" { |
| 120 |
return fmt.Errorf("no file-picker-cmd defined") |
| 121 |
} |
| 122 |
|
| 123 |
if strings.Contains(filePickerCmd, "%s") { |
| 124 |
filePickerCmd = strings.ReplaceAll(filePickerCmd, "%s", a.Path) |
| 125 |
} |
| 126 |
|
| 127 |
picks, err := os.CreateTemp(config.General().TempDir, "aerc-filepicker-*") |
| 128 |
if err != nil { |
| 129 |
return err |
| 130 |
} |
| 131 |
|
| 132 |
var filepicker *exec.Cmd |
| 133 |
if strings.Contains(filePickerCmd, "%f") { |
| 134 |
filePickerCmd = strings.ReplaceAll(filePickerCmd, "%f", picks.Name()) |
| 135 |
filepicker = exec.Command("sh", "-c", filePickerCmd) |
| 136 |
} else { |
| 137 |
filepicker = exec.Command("sh", "-c", filePickerCmd+" >&3") |
| 138 |
filepicker.ExtraFiles = append(filepicker.ExtraFiles, picks) |
| 139 |
} |
| 140 |
|
| 141 |
t, err := app.NewTerminal(filepicker) |
| 142 |
if err != nil { |
| 143 |
return err |
| 144 |
} |
| 145 |
t.Focus(true) |
| 146 |
t.OnClose = func(err error) { |
| 147 |
defer func() { |
| 148 |
if err := picks.Close(); err != nil { |
| 149 |
log.Errorf("error closing file: %v", err) |
| 150 |
} |
| 151 |
if err := os.Remove(picks.Name()); err != nil { |
| 152 |
log.Errorf("could not remove tmp file: %v", err) |
| 153 |
} |
| 154 |
}() |
| 155 |
|
| 156 |
app.CloseDialog() |
| 157 |
|
| 158 |
if err != nil { |
| 159 |
log.Errorf("terminal closed with error: %v", err) |
| 160 |
return |
| 161 |
} |
| 162 |
|
| 163 |
_, err = picks.Seek(0, io.SeekStart) |
| 164 |
if err != nil { |
| 165 |
log.Errorf("seek failed: %v", err) |
| 166 |
return |
| 167 |
} |
| 168 |
|
| 169 |
scanner := bufio.NewScanner(picks) |
| 170 |
for scanner.Scan() { |
| 171 |
f := strings.TrimSpace(scanner.Text()) |
| 172 |
if _, err := os.Stat(f); err != nil { |
| 173 |
continue |
| 174 |
} |
| 175 |
log.Tracef("File picker attaches: %v", f) |
| 176 |
err := a.addPath(f) |
| 177 |
if err != nil { |
| 178 |
log.Errorf("attach failed for file %s: %v", f, err) |
| 179 |
} |
| 180 |
|
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
app.AddDialog(app.DefaultDialog( |
| 185 |
ui.NewBox(t, "File Picker", "", app.SelectedAccountUiConfig()), |
| 186 |
)) |
| 187 |
|
| 188 |
return nil |
| 189 |
} |
| 190 |
|
| 191 |
func (a Attach) readCommand() error { |
| 192 |
cmd := exec.Command("sh", "-c", a.Path+" "+a.Args) |
| 193 |
|
| 194 |
data, err := cmd.Output() |
| 195 |
if err != nil { |
| 196 |
return errors.Wrap(err, "Output") |
| 197 |
} |
| 198 |
|
| 199 |
reader := bufio.NewReader(bytes.NewReader(data)) |
| 200 |
|
| 201 |
mimeType, mimeParams, err := lib.FindMimeType(a.Name, reader) |
| 202 |
if err != nil { |
| 203 |
return errors.Wrap(err, "FindMimeType") |
| 204 |
} |
| 205 |
|
| 206 |
mimeParams["name"] = a.Name |
| 207 |
|
| 208 |
composer, _ := app.SelectedTabContent().(*app.Composer) |
| 209 |
err = composer.AddPartAttachment(a.Name, mimeType, mimeParams, reader) |
| 210 |
if err != nil { |
| 211 |
return errors.Wrap(err, "AddPartAttachment") |
| 212 |
} |
| 213 |
|
| 214 |
app.PushSuccess(fmt.Sprintf("Attached %s", a.Name)) |
| 215 |
|
| 216 |
return nil |
| 217 |
} |