| 1 |
package state |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"os" |
| 9 |
"os/exec" |
| 10 |
"strings" |
| 11 |
"time" |
| 12 |
|
| 13 |
"git.sr.ht/~rjarry/aerc/config" |
| 14 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 15 |
"git.sr.ht/~rjarry/aerc/lib/ui" |
| 16 |
"git.sr.ht/~rjarry/aerc/lib/xdg" |
| 17 |
"git.sr.ht/~rjarry/aerc/models" |
| 18 |
"github.com/danwakefield/fnmatch" |
| 19 |
sortthread "github.com/emersion/go-imap-sortthread" |
| 20 |
"github.com/emersion/go-message/mail" |
| 21 |
) |
| 22 |
|
| 23 |
type Composer interface { |
| 24 |
AddAttachment(string) |
| 25 |
} |
| 26 |
|
| 27 |
type DataSetter interface { |
| 28 |
Data() models.TemplateData |
| 29 |
SetHeaders(*mail.Header, *models.OriginalMail) |
| 30 |
SetInfo(*models.MessageInfo, int, bool) |
| 31 |
SetBody(string) |
| 32 |
SetVisual(bool) |
| 33 |
SetThreading(ThreadInfo) |
| 34 |
SetComposer(Composer) |
| 35 |
SetAccount(*config.AccountConfig) |
| 36 |
SetFolder(*models.Directory) |
| 37 |
SetRUE([]string, func(string) (int, int, int)) |
| 38 |
SetState(s *AccountState) |
| 39 |
SetPendingKeys([]config.KeyStroke) |
| 40 |
SetHasNew(bool) |
| 41 |
SetBell(bool) |
| 42 |
SetTitle(string) |
| 43 |
} |
| 44 |
|
| 45 |
type ThreadInfo struct { |
| 46 |
SameSubject bool |
| 47 |
Prefix string |
| 48 |
Count int |
| 49 |
Unread int |
| 50 |
Folded bool |
| 51 |
Context bool |
| 52 |
Orphan bool |
| 53 |
} |
| 54 |
|
| 55 |
type templateData struct { |
| 56 |
// only available when composing/replying/forwarding |
| 57 |
headers *mail.Header |
| 58 |
// only available when replying with a quote |
| 59 |
parent *models.OriginalMail |
| 60 |
// only available for the message list |
| 61 |
info *models.MessageInfo |
| 62 |
|
| 63 |
// only available when composing |
| 64 |
body string |
| 65 |
marked bool |
| 66 |
msgNum int |
| 67 |
visual bool |
| 68 |
hasNew bool |
| 69 |
|
| 70 |
// message list threading |
| 71 |
threadInfo ThreadInfo |
| 72 |
|
| 73 |
// selected account |
| 74 |
account *config.AccountConfig |
| 75 |
myAddresses map[string]bool |
| 76 |
folder *models.Directory // selected folder |
| 77 |
folders []string |
| 78 |
getRUEcount func(string) (int, int, int) |
| 79 |
|
| 80 |
state *AccountState |
| 81 |
pendingKeys []config.KeyStroke |
| 82 |
|
| 83 |
composer Composer |
| 84 |
|
| 85 |
bell bool |
| 86 |
title string |
| 87 |
} |
| 88 |
|
| 89 |
func NewDataSetter() DataSetter { |
| 90 |
return &templateData{} |
| 91 |
} |
| 92 |
|
| 93 |
// Data returns the template data |
| 94 |
func (d *templateData) Data() models.TemplateData { |
| 95 |
return d |
| 96 |
} |
| 97 |
|
| 98 |
// only used for compose/reply/forward |
| 99 |
func (d *templateData) SetHeaders(h *mail.Header, o *models.OriginalMail) { |
| 100 |
d.headers = h |
| 101 |
d.parent = o |
| 102 |
} |
| 103 |
|
| 104 |
// only used for message list templates |
| 105 |
func (d *templateData) SetInfo(info *models.MessageInfo, num int, marked bool, |
| 106 |
) { |
| 107 |
d.info = info |
| 108 |
d.msgNum = num |
| 109 |
d.marked = marked |
| 110 |
} |
| 111 |
|
| 112 |
func (d *templateData) SetBody(body string) { |
| 113 |
d.body = body |
| 114 |
} |
| 115 |
|
| 116 |
func (d *templateData) SetVisual(visual bool) { |
| 117 |
d.visual = visual |
| 118 |
} |
| 119 |
|
| 120 |
func (d *templateData) SetThreading(info ThreadInfo) { |
| 121 |
d.threadInfo = info |
| 122 |
} |
| 123 |
|
| 124 |
func (d *templateData) SetAccount(acct *config.AccountConfig) { |
| 125 |
d.account = acct |
| 126 |
d.myAddresses = make(map[string]bool) |
| 127 |
if acct != nil { |
| 128 |
d.myAddresses[acct.From.Address] = true |
| 129 |
for _, addr := range acct.Aliases { |
| 130 |
d.myAddresses[addr.Address] = true |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
func (d *templateData) SetFolder(folder *models.Directory) { |
| 136 |
d.folder = folder |
| 137 |
} |
| 138 |
|
| 139 |
func (d *templateData) SetComposer(c Composer) { |
| 140 |
d.composer = c |
| 141 |
} |
| 142 |
|
| 143 |
func (d *templateData) SetHasNew(hasNew bool) { |
| 144 |
d.hasNew = hasNew |
| 145 |
} |
| 146 |
|
| 147 |
func (d *templateData) SetRUE(folders []string, |
| 148 |
cb func(string) (int, int, int), |
| 149 |
) { |
| 150 |
d.folders = folders |
| 151 |
d.getRUEcount = cb |
| 152 |
} |
| 153 |
|
| 154 |
func (d *templateData) SetState(state *AccountState) { |
| 155 |
d.state = state |
| 156 |
} |
| 157 |
|
| 158 |
func (d *templateData) SetPendingKeys(keys []config.KeyStroke) { |
| 159 |
d.pendingKeys = keys |
| 160 |
} |
| 161 |
|
| 162 |
func (d *templateData) Attach(s string) string { |
| 163 |
if d.composer != nil { |
| 164 |
d.composer.AddAttachment(s) |
| 165 |
return "" |
| 166 |
} |
| 167 |
return fmt.Sprintf("Failed to attach: %s", s) |
| 168 |
} |
| 169 |
|
| 170 |
func (d *templateData) Account() string { |
| 171 |
if d.account != nil { |
| 172 |
return d.account.Name |
| 173 |
} |
| 174 |
return "" |
| 175 |
} |
| 176 |
|
| 177 |
func (d *templateData) AccountBackend() string { |
| 178 |
if d.account != nil { |
| 179 |
return d.account.Backend |
| 180 |
} |
| 181 |
return "" |
| 182 |
} |
| 183 |
|
| 184 |
func (d *templateData) AccountFrom() *mail.Address { |
| 185 |
if d.account != nil { |
| 186 |
return d.account.From |
| 187 |
} |
| 188 |
return nil |
| 189 |
} |
| 190 |
|
| 191 |
func (d *templateData) Folder() string { |
| 192 |
if d.folder != nil { |
| 193 |
return d.folder.Name |
| 194 |
} |
| 195 |
return "" |
| 196 |
} |
| 197 |
|
| 198 |
func (d *templateData) Role() string { |
| 199 |
if d.folder != nil { |
| 200 |
return string(d.folder.Role) |
| 201 |
} |
| 202 |
return "" |
| 203 |
} |
| 204 |
|
| 205 |
func (d *templateData) ui() *config.UIConfig { |
| 206 |
return config.Ui().ForAccount(d.Account()).ForFolder(d.Folder()) |
| 207 |
} |
| 208 |
|
| 209 |
func (d *templateData) To() []*mail.Address { |
| 210 |
var to []*mail.Address |
| 211 |
switch { |
| 212 |
case d.info != nil && d.info.Envelope != nil: |
| 213 |
to = d.info.Envelope.To |
| 214 |
case d.headers != nil: |
| 215 |
to, _ = d.headers.AddressList("to") |
| 216 |
} |
| 217 |
return to |
| 218 |
} |
| 219 |
|
| 220 |
func (d *templateData) Cc() []*mail.Address { |
| 221 |
var cc []*mail.Address |
| 222 |
switch { |
| 223 |
case d.info != nil && d.info.Envelope != nil: |
| 224 |
cc = d.info.Envelope.Cc |
| 225 |
case d.headers != nil: |
| 226 |
cc, _ = d.headers.AddressList("cc") |
| 227 |
} |
| 228 |
return cc |
| 229 |
} |
| 230 |
|
| 231 |
func (d *templateData) Bcc() []*mail.Address { |
| 232 |
var bcc []*mail.Address |
| 233 |
switch { |
| 234 |
case d.info != nil && d.info.Envelope != nil: |
| 235 |
bcc = d.info.Envelope.Bcc |
| 236 |
case d.headers != nil: |
| 237 |
bcc, _ = d.headers.AddressList("bcc") |
| 238 |
} |
| 239 |
return bcc |
| 240 |
} |
| 241 |
|
| 242 |
func (d *templateData) From() []*mail.Address { |
| 243 |
var from []*mail.Address |
| 244 |
switch { |
| 245 |
case d.info != nil && d.info.Envelope != nil: |
| 246 |
from = d.info.Envelope.From |
| 247 |
case d.headers != nil: |
| 248 |
from, _ = d.headers.AddressList("from") |
| 249 |
} |
| 250 |
return from |
| 251 |
} |
| 252 |
|
| 253 |
func (d *templateData) Peer() []*mail.Address { |
| 254 |
var from, to []*mail.Address |
| 255 |
switch { |
| 256 |
case d.info != nil && d.info.Envelope != nil: |
| 257 |
from = d.info.Envelope.From |
| 258 |
to = d.info.Envelope.To |
| 259 |
case d.headers != nil: |
| 260 |
from, _ = d.headers.AddressList("from") |
| 261 |
to, _ = d.headers.AddressList("to") |
| 262 |
} |
| 263 |
for _, addr := range from { |
| 264 |
for myAddr := range d.myAddresses { |
| 265 |
if fnmatch.Match(myAddr, addr.Address, 0) { |
| 266 |
return to |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
return from |
| 271 |
} |
| 272 |
|
| 273 |
func (d *templateData) ReplyTo() []*mail.Address { |
| 274 |
var replyTo []*mail.Address |
| 275 |
switch { |
| 276 |
case d.info != nil && d.info.Envelope != nil: |
| 277 |
replyTo = d.info.Envelope.ReplyTo |
| 278 |
case d.headers != nil: |
| 279 |
replyTo, _ = d.headers.AddressList("reply-to") |
| 280 |
} |
| 281 |
return replyTo |
| 282 |
} |
| 283 |
|
| 284 |
func (d *templateData) Date() time.Time { |
| 285 |
var date time.Time |
| 286 |
switch { |
| 287 |
case d.info != nil && d.info.Envelope != nil: |
| 288 |
date = d.info.Envelope.Date |
| 289 |
case d.info != nil: |
| 290 |
date = d.info.InternalDate |
| 291 |
default: |
| 292 |
date = time.Now() |
| 293 |
} |
| 294 |
return date |
| 295 |
} |
| 296 |
|
| 297 |
func (d *templateData) DateAutoFormat(date time.Time) string { |
| 298 |
if date.IsZero() { |
| 299 |
return "" |
| 300 |
} |
| 301 |
ui := d.ui() |
| 302 |
year := date.Year() |
| 303 |
day := date.YearDay() |
| 304 |
now := time.Now() |
| 305 |
thisYear := now.Year() |
| 306 |
thisDay := now.YearDay() |
| 307 |
fmt := ui.TimestampFormat |
| 308 |
if year == thisYear { |
| 309 |
switch { |
| 310 |
case day == thisDay && ui.ThisDayTimeFormat != "": |
| 311 |
fmt = ui.ThisDayTimeFormat |
| 312 |
case day > thisDay-7 && ui.ThisWeekTimeFormat != "": |
| 313 |
fmt = ui.ThisWeekTimeFormat |
| 314 |
case ui.ThisYearTimeFormat != "": |
| 315 |
fmt = ui.ThisYearTimeFormat |
| 316 |
} |
| 317 |
} |
| 318 |
return date.Format(fmt) |
| 319 |
} |
| 320 |
|
| 321 |
func (d *templateData) Header(name string) string { |
| 322 |
var h *mail.Header |
| 323 |
switch { |
| 324 |
case d.headers != nil: |
| 325 |
h = d.headers |
| 326 |
case d.info != nil && d.info.RFC822Headers != nil: |
| 327 |
h = d.info.RFC822Headers |
| 328 |
default: |
| 329 |
return "" |
| 330 |
} |
| 331 |
text, err := h.Text(name) |
| 332 |
if err != nil { |
| 333 |
text = h.Get(name) |
| 334 |
} |
| 335 |
return text |
| 336 |
} |
| 337 |
|
| 338 |
func (d *templateData) ThreadPrefix() string { |
| 339 |
return d.threadInfo.Prefix |
| 340 |
} |
| 341 |
|
| 342 |
func (d *templateData) ThreadCount() int { |
| 343 |
return d.threadInfo.Count |
| 344 |
} |
| 345 |
|
| 346 |
func (d *templateData) ThreadUnread() int { |
| 347 |
return d.threadInfo.Unread |
| 348 |
} |
| 349 |
|
| 350 |
func (d *templateData) ThreadFolded() bool { |
| 351 |
return d.threadInfo.Folded |
| 352 |
} |
| 353 |
|
| 354 |
func (d *templateData) ThreadContext() bool { |
| 355 |
return d.threadInfo.Context |
| 356 |
} |
| 357 |
|
| 358 |
func (d *templateData) ThreadOrphan() bool { |
| 359 |
return d.threadInfo.Orphan |
| 360 |
} |
| 361 |
|
| 362 |
func (d *templateData) Subject() string { |
| 363 |
var subject string |
| 364 |
switch { |
| 365 |
case d.info != nil && d.info.Envelope != nil: |
| 366 |
subject = d.info.Envelope.Subject |
| 367 |
case d.headers != nil: |
| 368 |
subject = d.Header("subject") |
| 369 |
} |
| 370 |
if d.threadInfo.SameSubject { |
| 371 |
subject = "" |
| 372 |
} else if subject == "" { |
| 373 |
subject = config.Ui().EmptySubject |
| 374 |
} |
| 375 |
return subject |
| 376 |
} |
| 377 |
|
| 378 |
func (d *templateData) Body() string { |
| 379 |
return d.body |
| 380 |
} |
| 381 |
|
| 382 |
func (d *templateData) SubjectBase() string { |
| 383 |
var subject string |
| 384 |
switch { |
| 385 |
case d.info != nil && d.info.Envelope != nil: |
| 386 |
subject = d.info.Envelope.Subject |
| 387 |
case d.headers != nil: |
| 388 |
subject = d.Header("subject") |
| 389 |
} |
| 390 |
base, _ := sortthread.GetBaseSubject(subject) |
| 391 |
return base |
| 392 |
} |
| 393 |
|
| 394 |
func (d *templateData) Number() int { |
| 395 |
return d.msgNum |
| 396 |
} |
| 397 |
|
| 398 |
func (d *templateData) Labels() []string { |
| 399 |
if d.info == nil { |
| 400 |
return nil |
| 401 |
} |
| 402 |
return d.info.Labels |
| 403 |
} |
| 404 |
|
| 405 |
func (d *templateData) Filename() string { |
| 406 |
if d.info == nil { |
| 407 |
return "" |
| 408 |
} |
| 409 |
if (d.info.Filenames != nil) && len(d.info.Filenames) > 0 { |
| 410 |
return d.info.Filenames[0] |
| 411 |
} |
| 412 |
return "" |
| 413 |
} |
| 414 |
|
| 415 |
func (d *templateData) Filenames() []string { |
| 416 |
if d.info == nil { |
| 417 |
return nil |
| 418 |
} |
| 419 |
return d.info.Filenames |
| 420 |
} |
| 421 |
|
| 422 |
func (d *templateData) Flags() []string { |
| 423 |
var flags []string |
| 424 |
if d.info == nil { |
| 425 |
return flags |
| 426 |
} |
| 427 |
|
| 428 |
switch { |
| 429 |
case d.info.Flags.Has(models.SeenFlag | models.AnsweredFlag): |
| 430 |
flags = append(flags, d.ui().IconReplied) // message has been replied to |
| 431 |
case d.info.Flags.Has(models.SeenFlag): |
| 432 |
break |
| 433 |
case d.info.Flags.Has(models.RecentFlag): |
| 434 |
flags = append(flags, d.ui().IconNew) // message is unread and new |
| 435 |
default: |
| 436 |
flags = append(flags, d.ui().IconOld) // message is unread and old |
| 437 |
} |
| 438 |
if d.info.Flags.Has(models.DraftFlag) { |
| 439 |
flags = append(flags, d.ui().IconDraft) |
| 440 |
} |
| 441 |
if d.info.Flags.Has(models.DeletedFlag) { |
| 442 |
flags = append(flags, d.ui().IconDeleted) |
| 443 |
} |
| 444 |
if d.info.Flags.Has(models.ForwardedFlag) { |
| 445 |
flags = append(flags, d.ui().IconForwarded) |
| 446 |
} |
| 447 |
if d.info.BodyStructure != nil { |
| 448 |
for _, bS := range d.info.BodyStructure.Parts { |
| 449 |
if strings.ToLower(bS.Disposition) == "attachment" { |
| 450 |
flags = append(flags, d.ui().IconAttachment) |
| 451 |
break |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
if d.info.Flags.Has(models.FlaggedFlag) { |
| 456 |
flags = append(flags, d.ui().IconFlagged) |
| 457 |
} |
| 458 |
if d.marked { |
| 459 |
flags = append(flags, d.ui().IconMarked) |
| 460 |
} |
| 461 |
return flags |
| 462 |
} |
| 463 |
|
| 464 |
func (d *templateData) IsReplied() bool { |
| 465 |
if d.info != nil && d.info.Flags.Has(models.AnsweredFlag) { |
| 466 |
return true |
| 467 |
} |
| 468 |
return false |
| 469 |
} |
| 470 |
|
| 471 |
func (d *templateData) IsForwarded() bool { |
| 472 |
if d.info != nil && d.info.Flags.Has(models.ForwardedFlag) { |
| 473 |
return true |
| 474 |
} |
| 475 |
return false |
| 476 |
} |
| 477 |
|
| 478 |
func (d *templateData) HasAttachment() bool { |
| 479 |
if d.info != nil && d.info.BodyStructure != nil { |
| 480 |
for _, bS := range d.info.BodyStructure.Parts { |
| 481 |
if strings.ToLower(bS.Disposition) == "attachment" { |
| 482 |
return true |
| 483 |
} |
| 484 |
} |
| 485 |
} |
| 486 |
return false |
| 487 |
} |
| 488 |
|
| 489 |
func (d *templateData) IsRecent() bool { |
| 490 |
if d.info != nil && d.info.Flags.Has(models.RecentFlag) { |
| 491 |
return true |
| 492 |
} |
| 493 |
return false |
| 494 |
} |
| 495 |
|
| 496 |
func (d *templateData) IsUnread() bool { |
| 497 |
if d.info != nil && !d.info.Flags.Has(models.SeenFlag) { |
| 498 |
return true |
| 499 |
} |
| 500 |
return false |
| 501 |
} |
| 502 |
|
| 503 |
func (d *templateData) IsFlagged() bool { |
| 504 |
if d.info != nil && d.info.Flags.Has(models.FlaggedFlag) { |
| 505 |
return true |
| 506 |
} |
| 507 |
return false |
| 508 |
} |
| 509 |
|
| 510 |
func (d *templateData) IsDraft() bool { |
| 511 |
if d.info != nil && d.info.Flags.Has(models.DraftFlag) { |
| 512 |
return true |
| 513 |
} |
| 514 |
return false |
| 515 |
} |
| 516 |
|
| 517 |
func (d *templateData) IsMarked() bool { |
| 518 |
return d.marked |
| 519 |
} |
| 520 |
|
| 521 |
func (d *templateData) MessageId() string { |
| 522 |
if d.info == nil || d.info.Envelope == nil { |
| 523 |
return "" |
| 524 |
} |
| 525 |
return d.info.Envelope.MessageId |
| 526 |
} |
| 527 |
|
| 528 |
func (d *templateData) Size() int { |
| 529 |
if d.info == nil || d.info.Envelope == nil { |
| 530 |
return 0 |
| 531 |
} |
| 532 |
return int(d.info.Size) |
| 533 |
} |
| 534 |
|
| 535 |
func (d *templateData) OriginalText() string { |
| 536 |
if d.parent == nil { |
| 537 |
return "" |
| 538 |
} |
| 539 |
return d.parent.Text |
| 540 |
} |
| 541 |
|
| 542 |
func (d *templateData) OriginalDate() time.Time { |
| 543 |
if d.parent == nil { |
| 544 |
return time.Time{} |
| 545 |
} |
| 546 |
return d.parent.Date |
| 547 |
} |
| 548 |
|
| 549 |
func (d *templateData) OriginalFrom() []*mail.Address { |
| 550 |
if d.parent == nil || d.parent.RFC822Headers == nil { |
| 551 |
return nil |
| 552 |
} |
| 553 |
from, _ := d.parent.RFC822Headers.AddressList("from") |
| 554 |
return from |
| 555 |
} |
| 556 |
|
| 557 |
func (d *templateData) OriginalMIMEType() string { |
| 558 |
if d.parent == nil { |
| 559 |
return "" |
| 560 |
} |
| 561 |
return d.parent.MIMEType |
| 562 |
} |
| 563 |
|
| 564 |
func (d *templateData) OriginalHeader(name string) string { |
| 565 |
if d.parent == nil || d.parent.RFC822Headers == nil { |
| 566 |
return "" |
| 567 |
} |
| 568 |
text, err := d.parent.RFC822Headers.Text(name) |
| 569 |
if err != nil { |
| 570 |
text = d.parent.RFC822Headers.Get(name) |
| 571 |
} |
| 572 |
return text |
| 573 |
} |
| 574 |
|
| 575 |
func (d *templateData) rue(folders ...string) (int, int, int) { |
| 576 |
var recent, unread, exists int |
| 577 |
if d.getRUEcount != nil { |
| 578 |
if len(folders) == 0 { |
| 579 |
folders = d.folders |
| 580 |
} |
| 581 |
for _, dir := range folders { |
| 582 |
r, u, e := d.getRUEcount(dir) |
| 583 |
recent += r |
| 584 |
unread += u |
| 585 |
exists += e |
| 586 |
} |
| 587 |
} |
| 588 |
return recent, unread, exists |
| 589 |
} |
| 590 |
|
| 591 |
func (d *templateData) Recent(folders ...string) int { |
| 592 |
r, _, _ := d.rue(folders...) |
| 593 |
return r |
| 594 |
} |
| 595 |
|
| 596 |
func (d *templateData) Unread(folders ...string) int { |
| 597 |
_, u, _ := d.rue(folders...) |
| 598 |
return u |
| 599 |
} |
| 600 |
|
| 601 |
func (d *templateData) HasNew() bool { |
| 602 |
return d.hasNew |
| 603 |
} |
| 604 |
|
| 605 |
func (d *templateData) Exists(folders ...string) int { |
| 606 |
_, _, e := d.rue(folders...) |
| 607 |
return e |
| 608 |
} |
| 609 |
|
| 610 |
func (d *templateData) RUE(folders ...string) string { |
| 611 |
r, u, e := d.rue(folders...) |
| 612 |
switch { |
| 613 |
case r > 0: |
| 614 |
return fmt.Sprintf("%d/%d/%d", r, u, e) |
| 615 |
case u > 0: |
| 616 |
return fmt.Sprintf("%d/%d", u, e) |
| 617 |
case e > 0: |
| 618 |
return fmt.Sprintf("%d", e) |
| 619 |
} |
| 620 |
return "" |
| 621 |
} |
| 622 |
|
| 623 |
func (d *templateData) Connected() bool { |
| 624 |
if d.state != nil { |
| 625 |
return d.state.Connected |
| 626 |
} |
| 627 |
return false |
| 628 |
} |
| 629 |
|
| 630 |
func (d *templateData) ConnectionInfo() string { |
| 631 |
switch { |
| 632 |
case d.state == nil: |
| 633 |
return "" |
| 634 |
case d.state.connActivity != "": |
| 635 |
return d.state.connActivity |
| 636 |
case d.state.Connected: |
| 637 |
return texter().Connected() |
| 638 |
default: |
| 639 |
return texter().Disconnected() |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
func (d *templateData) ContentInfo() string { |
| 644 |
if d.state == nil { |
| 645 |
return "" |
| 646 |
} |
| 647 |
var content []string |
| 648 |
fldr := d.state.folderState(d.Folder()) |
| 649 |
if fldr.FilterActivity != "" { |
| 650 |
content = append(content, fldr.FilterActivity) |
| 651 |
} else if fldr.Filter != "" { |
| 652 |
content = append(content, texter().FormatFilter(fldr.Filter)) |
| 653 |
} |
| 654 |
if fldr.Search != "" { |
| 655 |
content = append(content, texter().FormatSearch(fldr.Search)) |
| 656 |
} |
| 657 |
return strings.Join(content, config.Statusline().Separator) |
| 658 |
} |
| 659 |
|
| 660 |
func (d *templateData) StatusInfo() string { |
| 661 |
stat := d.ConnectionInfo() |
| 662 |
if content := d.ContentInfo(); content != "" { |
| 663 |
stat += config.Statusline().Separator + content |
| 664 |
} |
| 665 |
return stat |
| 666 |
} |
| 667 |
|
| 668 |
func (d *templateData) TrayInfo() string { |
| 669 |
if d.state == nil { |
| 670 |
return "" |
| 671 |
} |
| 672 |
var tray []string |
| 673 |
fldr := d.state.folderState(d.Folder()) |
| 674 |
if fldr.Sorting { |
| 675 |
tray = append(tray, texter().Sorting()) |
| 676 |
} |
| 677 |
if fldr.Threading { |
| 678 |
tray = append(tray, texter().Threading()) |
| 679 |
} |
| 680 |
if d.state.passthrough { |
| 681 |
tray = append(tray, texter().Passthrough()) |
| 682 |
} |
| 683 |
if d.visual { |
| 684 |
tray = append(tray, texter().Visual()) |
| 685 |
} |
| 686 |
return strings.Join(tray, config.Statusline().Separator) |
| 687 |
} |
| 688 |
|
| 689 |
func (d *templateData) PendingKeys() string { |
| 690 |
return config.FormatKeyStrokes(d.pendingKeys) |
| 691 |
} |
| 692 |
|
| 693 |
func (d *templateData) Style(content, name string) string { |
| 694 |
cfg := config.Ui().ForAccount(d.Account()) |
| 695 |
style := cfg.GetUserStyle(name) |
| 696 |
return ui.ApplyStyle(style, content) |
| 697 |
} |
| 698 |
|
| 699 |
func (d *templateData) StyleSwitch(content string, cases ...models.Case) string { |
| 700 |
for _, c := range cases { |
| 701 |
if c.Matches(content) { |
| 702 |
cfg := config.Ui().ForAccount(d.Account()) |
| 703 |
style := cfg.GetUserStyle(c.Value()) |
| 704 |
return ui.ApplyStyle(style, content) |
| 705 |
} |
| 706 |
} |
| 707 |
return content |
| 708 |
} |
| 709 |
|
| 710 |
func (d *templateData) StyleMap(elems []string, cases ...models.Case) []string { |
| 711 |
mapped := make([]string, 0, len(elems)) |
| 712 |
top: |
| 713 |
for _, e := range elems { |
| 714 |
for _, c := range cases { |
| 715 |
if c.Matches(e) { |
| 716 |
if c.Skip() { |
| 717 |
continue top |
| 718 |
} |
| 719 |
cfg := config.Ui().ForAccount(d.Account()) |
| 720 |
style := cfg.GetUserStyle(c.Value()) |
| 721 |
e = ui.ApplyStyle(style, e) |
| 722 |
break |
| 723 |
} |
| 724 |
} |
| 725 |
mapped = append(mapped, e) |
| 726 |
} |
| 727 |
return mapped |
| 728 |
} |
| 729 |
|
| 730 |
func (d *templateData) Signature() string { |
| 731 |
if d.account == nil { |
| 732 |
return "" |
| 733 |
} |
| 734 |
var signature []byte |
| 735 |
if d.account.SignatureCmd != "" { |
| 736 |
var err error |
| 737 |
signature, err = d.readSignatureFromCmd() |
| 738 |
if err != nil { |
| 739 |
var execErr *exec.ExitError |
| 740 |
if errors.As(err, &execErr) { |
| 741 |
log.Warnf("signature command failed with error (%d): %s", execErr.ExitCode(), execErr.Stderr) |
| 742 |
} |
| 743 |
signature = d.readSignatureFromFile() |
| 744 |
} |
| 745 |
} else { |
| 746 |
signature = d.readSignatureFromFile() |
| 747 |
} |
| 748 |
if len(bytes.TrimSpace(signature)) == 0 { |
| 749 |
return "" |
| 750 |
} |
| 751 |
signature = d.ensureSignatureDelimiter(signature) |
| 752 |
return string(signature) |
| 753 |
} |
| 754 |
|
| 755 |
func (d *templateData) readSignatureFromCmd() ([]byte, error) { |
| 756 |
sigCmd := d.account.SignatureCmd |
| 757 |
cmd := exec.Command("sh", "-c", sigCmd) |
| 758 |
env := os.Environ() |
| 759 |
if d.account != nil { |
| 760 |
env = append(env, fmt.Sprintf("AERC_ACCOUNT=%s", d.account.Name)) |
| 761 |
} |
| 762 |
if d.folder != nil { |
| 763 |
env = append(env, fmt.Sprintf("AERC_FOLDER=%s", d.folder.Name)) |
| 764 |
} |
| 765 |
cmd.Env = env |
| 766 |
signature, err := cmd.Output() |
| 767 |
if err != nil { |
| 768 |
return nil, err |
| 769 |
} |
| 770 |
return signature, nil |
| 771 |
} |
| 772 |
|
| 773 |
func (d *templateData) readSignatureFromFile() []byte { |
| 774 |
sigFile := d.account.SignatureFile |
| 775 |
if sigFile == "" { |
| 776 |
return nil |
| 777 |
} |
| 778 |
sigFile = xdg.ExpandHome(sigFile) |
| 779 |
signature, err := os.ReadFile(sigFile) |
| 780 |
if err != nil { |
| 781 |
log.Errorf(" Error loading signature from file: %v", sigFile) |
| 782 |
return nil |
| 783 |
} |
| 784 |
return signature |
| 785 |
} |
| 786 |
|
| 787 |
func (d *templateData) ensureSignatureDelimiter(signature []byte) []byte { |
| 788 |
buf := bytes.NewBuffer(signature) |
| 789 |
scanner := bufio.NewScanner(buf) |
| 790 |
for scanner.Scan() { |
| 791 |
line := scanner.Text() |
| 792 |
if line == "-- " { |
| 793 |
// signature contains standard delimiter, we're good |
| 794 |
return signature |
| 795 |
} |
| 796 |
} |
| 797 |
// signature does not contain standard delimiter, prepend one |
| 798 |
sig := "\n\n-- \n" + strings.TrimLeft(string(signature), " \t\r\n") |
| 799 |
return []byte(sig) |
| 800 |
} |
| 801 |
|
| 802 |
func (d *templateData) Bell() bool { |
| 803 |
return d.bell |
| 804 |
} |
| 805 |
|
| 806 |
func (d *templateData) Title() string { |
| 807 |
return d.title |
| 808 |
} |
| 809 |
|
| 810 |
func (d *templateData) SetBell(bell bool) { |
| 811 |
d.bell = bell |
| 812 |
} |
| 813 |
|
| 814 |
func (d *templateData) SetTitle(title string) { |
| 815 |
d.title = title |
| 816 |
} |