| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"cmp" |
| 5 |
"context" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"os" |
| 9 |
"runtime" |
| 10 |
"slices" |
| 11 |
"strings" |
| 12 |
"sync" |
| 13 |
"time" |
| 14 |
|
| 15 |
"git.sr.ht/~rjarry/go-opt/v2" |
| 16 |
|
| 17 |
"git.sr.ht/~rjarry/aerc/app" |
| 18 |
"git.sr.ht/~rjarry/aerc/commands" |
| 19 |
"git.sr.ht/~rjarry/aerc/config" |
| 20 |
"git.sr.ht/~rjarry/aerc/lib" |
| 21 |
"git.sr.ht/~rjarry/aerc/lib/crypto" |
| 22 |
"git.sr.ht/~rjarry/aerc/lib/hooks" |
| 23 |
"git.sr.ht/~rjarry/aerc/lib/ipc" |
| 24 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 25 |
"git.sr.ht/~rjarry/aerc/lib/pinentry" |
| 26 |
"git.sr.ht/~rjarry/aerc/lib/templates" |
| 27 |
"git.sr.ht/~rjarry/aerc/lib/ui" |
| 28 |
"git.sr.ht/~rjarry/aerc/models" |
| 29 |
|
| 30 |
_ "git.sr.ht/~rjarry/aerc/commands/account" |
| 31 |
_ "git.sr.ht/~rjarry/aerc/commands/compose" |
| 32 |
_ "git.sr.ht/~rjarry/aerc/commands/msg" |
| 33 |
_ "git.sr.ht/~rjarry/aerc/commands/msgview" |
| 34 |
_ "git.sr.ht/~rjarry/aerc/commands/patch" |
| 35 |
) |
| 36 |
|
| 37 |
func execCommand( |
| 38 |
cmdline string, |
| 39 |
acct *config.AccountConfig, msg *models.MessageInfo, |
| 40 |
) error { |
| 41 |
cmdline, cmd, err := commands.ResolveCommand(cmdline, acct, msg) |
| 42 |
if err != nil { |
| 43 |
return err |
| 44 |
} |
| 45 |
err = commands.ExecuteCommand(cmd, cmdline) |
| 46 |
if errors.As(err, new(commands.ErrorExit)) { |
| 47 |
ui.Exit() |
| 48 |
return nil |
| 49 |
} |
| 50 |
return err |
| 51 |
} |
| 52 |
|
| 53 |
func getCompletions(ctx context.Context, cmdline string) ([]opt.Completion, string) { |
| 54 |
// complete template terms |
| 55 |
if options, prefix, ok := commands.GetTemplateCompletion(cmdline); ok { |
| 56 |
slices.Sort(options) |
| 57 |
completions := make([]opt.Completion, 0, len(options)) |
| 58 |
for _, o := range options { |
| 59 |
completions = append(completions, opt.Completion{ |
| 60 |
Value: o, |
| 61 |
Description: "Template", |
| 62 |
}) |
| 63 |
} |
| 64 |
return completions, prefix |
| 65 |
} |
| 66 |
|
| 67 |
args := opt.LexArgs(cmdline) |
| 68 |
|
| 69 |
if args.Count() < 2 && args.TrailingSpace() == "" { |
| 70 |
// complete command names |
| 71 |
var completions []opt.Completion |
| 72 |
for _, cmd := range commands.ActiveCommands() { |
| 73 |
for _, alias := range cmd.Aliases() { |
| 74 |
if strings.HasPrefix(alias, cmdline) { |
| 75 |
completions = append(completions, opt.Completion{ |
| 76 |
Value: alias + " ", |
| 77 |
Description: cmd.Description(), |
| 78 |
}) |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
slices.SortFunc(completions, func(a, b opt.Completion) int { |
| 83 |
return cmp.Compare(a.Value, b.Value) |
| 84 |
}) |
| 85 |
return completions, "" |
| 86 |
} |
| 87 |
|
| 88 |
// complete command arguments |
| 89 |
_, cmd, err := commands.ExpandAbbreviations(args.Arg(0)) |
| 90 |
if err != nil { |
| 91 |
return nil, cmdline |
| 92 |
} |
| 93 |
return commands.GetCompletions(cmd, args) |
| 94 |
} |
| 95 |
|
| 96 |
// set at build time |
| 97 |
var ( |
| 98 |
Version string |
| 99 |
Date string |
| 100 |
) |
| 101 |
|
| 102 |
func buildInfo() string { |
| 103 |
info := Version |
| 104 |
if soVersion, hasNotmuch := lib.NotmuchVersion(); hasNotmuch { |
| 105 |
info += fmt.Sprintf(" +notmuch-%s", soVersion) |
| 106 |
} |
| 107 |
info += fmt.Sprintf(" (%s %s %s %s)", |
| 108 |
runtime.Version(), runtime.GOARCH, runtime.GOOS, Date) |
| 109 |
return info |
| 110 |
} |
| 111 |
|
| 112 |
type Opts struct { |
| 113 |
Help bool `opt:"-h,--help" action:"ShowHelp"` |
| 114 |
Version bool `opt:"-v,--version" action:"ShowVersion"` |
| 115 |
Accounts []string `opt:"-a,--account" action:"ParseAccounts" metavar:"<name>"` |
| 116 |
ConfAerc string `opt:"-C,--aerc-conf" metavar:"<file>"` |
| 117 |
ConfAccounts string `opt:"-A,--accounts-conf" metavar:"<file>"` |
| 118 |
ConfBinds string `opt:"-B,--binds-conf" metavar:"<file>"` |
| 119 |
NoIPC bool `opt:"-I,--no-ipc"` |
| 120 |
Command []string `opt:"..." required:"false" metavar:"mailto:<address> | mbox:<file> | :<command...>"` |
| 121 |
} |
| 122 |
|
| 123 |
func (o *Opts) ShowHelp(arg string) error { |
| 124 |
fmt.Println("Usage: " + opt.NewCmdSpec(os.Args[0], o).Usage()) |
| 125 |
fmt.Print(` |
| 126 |
Aerc is an email client for your terminal. |
| 127 |
|
| 128 |
Options: |
| 129 |
|
| 130 |
-h, --help Show this help message and exit. |
| 131 |
-v, --version Print version information. |
| 132 |
-a <name>, --account <name> |
| 133 |
Load only the named account, as opposed to all configured |
| 134 |
accounts. It can also be a comma separated list of names. |
| 135 |
This option may be specified multiple times. The account |
| 136 |
order will be preserved. |
| 137 |
-C <file>, --aerc-conf <file> |
| 138 |
Path to configuration file to be used instead of the default. |
| 139 |
-A <file>, --accounts-conf <file> |
| 140 |
Path to configuration file to be used instead of the default. |
| 141 |
-B <file>, --binds-conf <file> |
| 142 |
Path to configuration file to be used instead of the default. |
| 143 |
-I, --no-ipc Run any commands in this aerc instance, and don't create a |
| 144 |
socket for other aerc instances to communicate with this one. |
| 145 |
mailto:<address> Open the composer with the address(es) in the To field. |
| 146 |
If aerc is already running, the composer is started in |
| 147 |
this instance, otherwise aerc will be started. |
| 148 |
mbox:<file> Open the specified mbox file as a virtual temporary account. |
| 149 |
:<command...> Run an aerc command as you would in Ex-Mode. |
| 150 |
`) |
| 151 |
os.Exit(0) |
| 152 |
return nil |
| 153 |
} |
| 154 |
|
| 155 |
func (o *Opts) ShowVersion(arg string) error { |
| 156 |
fmt.Println("aerc " + log.BuildInfo) |
| 157 |
os.Exit(0) |
| 158 |
return nil |
| 159 |
} |
| 160 |
|
| 161 |
func (o *Opts) ParseAccounts(arg string) error { |
| 162 |
o.Accounts = append(o.Accounts, strings.Split(arg, ",")...) |
| 163 |
return nil |
| 164 |
} |
| 165 |
|
| 166 |
func die(format string, args ...any) { |
| 167 |
fmt.Fprintf(os.Stderr, "error: "+format+"\n", args...) |
| 168 |
os.Exit(1) |
| 169 |
} |
| 170 |
|
| 171 |
func main() { |
| 172 |
defer log.PanicHandler() |
| 173 |
log.BuildInfo = buildInfo() |
| 174 |
|
| 175 |
var opts Opts |
| 176 |
|
| 177 |
args := opt.QuoteArgs(os.Args...) |
| 178 |
err := opt.ArgsToStruct(args, &opts) |
| 179 |
if err != nil { |
| 180 |
die("%s", err) |
| 181 |
} |
| 182 |
switch { |
| 183 |
case len(opts.Command) == 0: |
| 184 |
break |
| 185 |
case strings.HasPrefix(opts.Command[0], ":"): |
| 186 |
case strings.HasPrefix(opts.Command[0], "mailto:"): |
| 187 |
case strings.HasPrefix(opts.Command[0], "mbox:"): |
| 188 |
break |
| 189 |
default: |
| 190 |
die("unknown argument: %s", opts.Command[0]) |
| 191 |
} |
| 192 |
|
| 193 |
err = config.LoadConfigFromFile( |
| 194 |
nil, opts.Accounts, opts.ConfAerc, opts.ConfBinds, opts.ConfAccounts, |
| 195 |
) |
| 196 |
if err != nil { |
| 197 |
die("%s", err) |
| 198 |
} |
| 199 |
|
| 200 |
noIPC := opts.NoIPC || config.General().DisableIPC |
| 201 |
|
| 202 |
if len(opts.Command) > 0 && !noIPC && |
| 203 |
!(config.General().DisableIPCMailto && strings.HasPrefix(opts.Command[0], "mailto:")) && |
| 204 |
!(config.General().DisableIPCMbox && strings.HasPrefix(opts.Command[0], "mbox:")) { |
| 205 |
|
| 206 |
response, err := ipc.ConnectAndExec(opts.Command) |
| 207 |
if err == nil { |
| 208 |
if response.Error != "" { |
| 209 |
fmt.Printf("response: %s\n", response.Error) |
| 210 |
} |
| 211 |
return // other aerc instance takes over |
| 212 |
} |
| 213 |
// continue with setting up a new aerc instance and retry after init |
| 214 |
} |
| 215 |
|
| 216 |
log.Infof("Starting up version %s", log.BuildInfo) |
| 217 |
|
| 218 |
deferLoop := make(chan struct{}) |
| 219 |
|
| 220 |
c := crypto.New() |
| 221 |
err = c.Init() |
| 222 |
if err != nil { |
| 223 |
log.Warnf("failed to initialise crypto interface: %v", err) |
| 224 |
} |
| 225 |
defer c.Close() |
| 226 |
|
| 227 |
app.Init(c, execCommand, getCompletions, &commands.CmdHistory, deferLoop) |
| 228 |
|
| 229 |
err = ui.Initialize(app.Drawable()) |
| 230 |
if err != nil { |
| 231 |
panic(err) |
| 232 |
} |
| 233 |
defer ui.Close() |
| 234 |
log.UICleanup = func() { |
| 235 |
ui.Close() |
| 236 |
} |
| 237 |
close(deferLoop) |
| 238 |
|
| 239 |
config.EnablePinentry = pinentry.Enable |
| 240 |
config.DisablePinentry = pinentry.Disable |
| 241 |
config.SetPinentryEnv = pinentry.SetCmdEnv |
| 242 |
|
| 243 |
startup, startupDone := context.WithCancel(context.Background()) |
| 244 |
|
| 245 |
if !noIPC { |
| 246 |
as, err := ipc.StartServer(app.IPCHandler(), startup) |
| 247 |
if err != nil { |
| 248 |
log.Warnf("Failed to start Unix server: %v", err) |
| 249 |
} else { |
| 250 |
defer as.Close() |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
// set the aerc version so that we can use it in the template funcs |
| 255 |
templates.SetVersion(Version) |
| 256 |
templates.SetExecPath(config.SearchDirs) |
| 257 |
|
| 258 |
endStartup := func() { |
| 259 |
startupDone() |
| 260 |
if len(opts.Command) == 0 { |
| 261 |
return |
| 262 |
} |
| 263 |
// Retry execution. Since IPC has already failed, we know no |
| 264 |
// other aerc instance is running (or IPC was explicitly |
| 265 |
// disabled); run the command directly. |
| 266 |
err := app.Command(opts.Command) |
| 267 |
if err != nil { |
| 268 |
// no other aerc instance is running, so let |
| 269 |
// this one stay running but show the error |
| 270 |
errMsg := fmt.Sprintf("Startup command (%s) failed: %s\n", |
| 271 |
strings.Join(opts.Command, " "), err) |
| 272 |
log.Errorf(errMsg) |
| 273 |
app.PushError(errMsg) |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
go func() { |
| 278 |
defer log.PanicHandler() |
| 279 |
err := hooks.RunHook(&hooks.AercStartup{Version: Version}) |
| 280 |
if err != nil { |
| 281 |
msg := fmt.Sprintf("aerc-startup hook: %s", err) |
| 282 |
app.PushError(msg) |
| 283 |
} |
| 284 |
}() |
| 285 |
defer func(start time.Time) { |
| 286 |
err := hooks.RunHook( |
| 287 |
&hooks.AercShutdown{Lifetime: time.Since(start)}, |
| 288 |
) |
| 289 |
if err != nil { |
| 290 |
log.Errorf("aerc-shutdown hook: %s", err) |
| 291 |
} |
| 292 |
}(time.Now()) |
| 293 |
var once sync.Once |
| 294 |
loop: |
| 295 |
for { |
| 296 |
select { |
| 297 |
case event := <-ui.Events: |
| 298 |
ui.HandleEvent(event) |
| 299 |
case msg := <-app.WorkerMessages: |
| 300 |
app.HandleMessage(msg) |
| 301 |
// XXX: The app may not be 100% ready at this point. |
| 302 |
// The issue is that there is no real way to tell when |
| 303 |
// it will be ready. And in some cases, it may never be. |
| 304 |
// At least, we can be confident that accepting IPC |
| 305 |
// commands will not crash the whole process. |
| 306 |
once.Do(endStartup) |
| 307 |
case callback := <-ui.Callbacks: |
| 308 |
callback() |
| 309 |
case <-ui.Redraw: |
| 310 |
ui.Render() |
| 311 |
case <-ui.SuspendQueue: |
| 312 |
err = ui.Suspend() |
| 313 |
if err != nil { |
| 314 |
app.PushError(fmt.Sprintf("suspend: %s", err)) |
| 315 |
} |
| 316 |
case <-ui.Quit: |
| 317 |
err = app.CloseBackends() |
| 318 |
if err != nil { |
| 319 |
log.Warnf("failed to close backends: %v", err) |
| 320 |
} |
| 321 |
break loop |
| 322 |
} |
| 323 |
} |
| 324 |
} |