| 1 |
package models |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"slices" |
| 8 |
"strings" |
| 9 |
"time" |
| 10 |
|
| 11 |
"git.sr.ht/~rjarry/aerc/lib/parse" |
| 12 |
"github.com/emersion/go-message/mail" |
| 13 |
) |
| 14 |
|
| 15 |
// Flags is an abstraction around the different flags which can be present in |
| 16 |
// different email backends and represents a flag that we use in the UI. |
| 17 |
type Flags uint32 |
| 18 |
|
| 19 |
const ( |
| 20 |
// SeenFlag marks a message as having been seen previously |
| 21 |
SeenFlag Flags = 1 << iota |
| 22 |
|
| 23 |
// RecentFlag marks a message as being recent |
| 24 |
RecentFlag |
| 25 |
|
| 26 |
// AnsweredFlag marks a message as having been replied to |
| 27 |
AnsweredFlag |
| 28 |
|
| 29 |
// ForwardedFlag marks a message as having been forwarded |
| 30 |
ForwardedFlag |
| 31 |
|
| 32 |
// DeletedFlag marks a message as having been deleted |
| 33 |
DeletedFlag |
| 34 |
|
| 35 |
// FlaggedFlag marks a message with a user flag |
| 36 |
FlaggedFlag |
| 37 |
|
| 38 |
// DraftFlag marks a message as a draft |
| 39 |
DraftFlag |
| 40 |
) |
| 41 |
|
| 42 |
func (f Flags) Has(flags Flags) bool { |
| 43 |
return f&flags == flags |
| 44 |
} |
| 45 |
|
| 46 |
type Role string |
| 47 |
|
| 48 |
var Roles = map[string]Role{ |
| 49 |
"all": AllRole, |
| 50 |
"archive": ArchiveRole, |
| 51 |
"drafts": DraftsRole, |
| 52 |
"inbox": InboxRole, |
| 53 |
"junk": JunkRole, |
| 54 |
"sent": SentRole, |
| 55 |
"trash": TrashRole, |
| 56 |
"query": QueryRole, |
| 57 |
} |
| 58 |
|
| 59 |
const ( |
| 60 |
AllRole Role = "all" |
| 61 |
ArchiveRole Role = "archive" |
| 62 |
DraftsRole Role = "drafts" |
| 63 |
InboxRole Role = "inbox" |
| 64 |
JunkRole Role = "junk" |
| 65 |
SentRole Role = "sent" |
| 66 |
TrashRole Role = "trash" |
| 67 |
// Custom aerc roles |
| 68 |
QueryRole Role = "query" |
| 69 |
// virtual node created by the directory tree |
| 70 |
VirtualRole Role = "virtual" |
| 71 |
) |
| 72 |
|
| 73 |
type Directory struct { |
| 74 |
Name string |
| 75 |
// Uid is a unique identifier for this directory that changes when |
| 76 |
// the directory contents become invalid (e.g. IMAP UIDVALIDITY change). |
| 77 |
// For IMAP: UIDVALIDITY as string, empty for other backends. |
| 78 |
Uid string |
| 79 |
// Exists messages in the Directory |
| 80 |
Exists int |
| 81 |
// Recent messages in the Directory |
| 82 |
Recent int |
| 83 |
// Unseen messages in the Directory |
| 84 |
Unseen int |
| 85 |
// IANA role |
| 86 |
Role Role |
| 87 |
} |
| 88 |
|
| 89 |
type DirectoryInfo struct { |
| 90 |
Name string |
| 91 |
// Uid is a unique identifier for this directory that changes when |
| 92 |
// the directory contents become invalid (e.g. IMAP UIDVALIDITY change). |
| 93 |
// For IMAP: UIDVALIDITY as string, empty for other backends. |
| 94 |
Uid string |
| 95 |
// The total number of messages in this mailbox. |
| 96 |
Exists int |
| 97 |
// The number of messages not seen since the last time the mailbox was opened. |
| 98 |
Recent int |
| 99 |
// The number of unread messages |
| 100 |
Unseen int |
| 101 |
} |
| 102 |
|
| 103 |
// Capabilities provides the backend capabilities |
| 104 |
type Capabilities struct { |
| 105 |
Sort bool |
| 106 |
Thread bool |
| 107 |
Extensions []string |
| 108 |
} |
| 109 |
|
| 110 |
func (c *Capabilities) Has(s string) bool { |
| 111 |
return slices.Contains(c.Extensions, s) |
| 112 |
} |
| 113 |
|
| 114 |
type UID string |
| 115 |
|
| 116 |
// A MessageInfo holds information about the structure of a message |
| 117 |
type MessageInfo struct { |
| 118 |
BodyStructure *BodyStructure |
| 119 |
Envelope *Envelope |
| 120 |
Flags Flags |
| 121 |
Directory string |
| 122 |
Labels []string |
| 123 |
Filenames []string |
| 124 |
InternalDate time.Time |
| 125 |
RFC822Headers *mail.Header |
| 126 |
Refs []string |
| 127 |
Size uint32 |
| 128 |
Uid UID |
| 129 |
Index *int // 0-based index for insertion; nil means append |
| 130 |
Error error |
| 131 |
} |
| 132 |
|
| 133 |
func (mi *MessageInfo) MsgId() (msgid string, err error) { |
| 134 |
if mi == nil { |
| 135 |
return "", errors.New("msg is nil") |
| 136 |
} |
| 137 |
if mi.Envelope == nil { |
| 138 |
return "", errors.New("envelope is nil") |
| 139 |
} |
| 140 |
return mi.Envelope.MessageId, nil |
| 141 |
} |
| 142 |
|
| 143 |
func (mi *MessageInfo) InReplyTo() (msgid string, err error) { |
| 144 |
if mi == nil { |
| 145 |
return "", errors.New("msg is nil") |
| 146 |
} |
| 147 |
if mi.Envelope != nil && mi.Envelope.InReplyTo != "" { |
| 148 |
return mi.Envelope.InReplyTo, nil |
| 149 |
} |
| 150 |
if mi.RFC822Headers == nil { |
| 151 |
return "", errors.New("header is nil") |
| 152 |
} |
| 153 |
list := parse.MsgIDList(mi.RFC822Headers, "In-Reply-To") |
| 154 |
if len(list) == 0 { |
| 155 |
return "", errors.New("no results") |
| 156 |
} |
| 157 |
return list[0], err |
| 158 |
} |
| 159 |
|
| 160 |
func (mi *MessageInfo) References() ([]string, error) { |
| 161 |
if mi == nil { |
| 162 |
return []string{}, errors.New("msg is nil") |
| 163 |
} |
| 164 |
if mi.Refs != nil { |
| 165 |
return mi.Refs, nil |
| 166 |
} |
| 167 |
if mi.RFC822Headers == nil { |
| 168 |
return []string{}, errors.New("header is nil") |
| 169 |
} |
| 170 |
list := parse.MsgIDList(mi.RFC822Headers, "References") |
| 171 |
if len(list) == 0 { |
| 172 |
return []string{}, errors.New("no results") |
| 173 |
} |
| 174 |
return list, nil |
| 175 |
} |
| 176 |
|
| 177 |
// A MessageBodyPart can be displayed in the message viewer |
| 178 |
type MessageBodyPart struct { |
| 179 |
Reader io.Reader |
| 180 |
Uid UID |
| 181 |
} |
| 182 |
|
| 183 |
// A FullMessage is the entire message |
| 184 |
type FullMessage struct { |
| 185 |
Reader io.Reader |
| 186 |
Uid UID |
| 187 |
} |
| 188 |
|
| 189 |
type BodyStructure struct { |
| 190 |
MIMEType string |
| 191 |
MIMESubType string |
| 192 |
Params map[string]string |
| 193 |
Description string |
| 194 |
Encoding string |
| 195 |
Parts []*BodyStructure |
| 196 |
Disposition string |
| 197 |
DispositionParams map[string]string |
| 198 |
ContentID string |
| 199 |
} |
| 200 |
|
| 201 |
// PartAtIndex returns the BodyStructure at the requested index |
| 202 |
func (bs *BodyStructure) PartAtIndex(index []int) (*BodyStructure, error) { |
| 203 |
if len(index) == 0 { |
| 204 |
return bs, nil |
| 205 |
} |
| 206 |
cur := index[0] |
| 207 |
rest := index[1:] |
| 208 |
// passed indexes are 1 based, we need to convert back to actual indexes |
| 209 |
curidx := cur - 1 |
| 210 |
if curidx < 0 { |
| 211 |
return nil, fmt.Errorf("invalid index, expected 1 based input") |
| 212 |
} |
| 213 |
|
| 214 |
// no children, base case |
| 215 |
if len(bs.Parts) == 0 { |
| 216 |
if len(rest) != 0 { |
| 217 |
return nil, fmt.Errorf("more index levels given than available") |
| 218 |
} |
| 219 |
if cur == 1 { |
| 220 |
return bs, nil |
| 221 |
} else { |
| 222 |
return nil, fmt.Errorf("invalid index %v for non multipart", cur) |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
if cur > len(bs.Parts) { |
| 227 |
return nil, fmt.Errorf("invalid index %v, only have %v children", |
| 228 |
cur, len(bs.Parts)) |
| 229 |
} |
| 230 |
|
| 231 |
return bs.Parts[curidx].PartAtIndex(rest) |
| 232 |
} |
| 233 |
|
| 234 |
func (bs *BodyStructure) FullMIMEType() string { |
| 235 |
mime := fmt.Sprintf("%s/%s", bs.MIMEType, bs.MIMESubType) |
| 236 |
return strings.ToLower(mime) |
| 237 |
} |
| 238 |
|
| 239 |
func (bs *BodyStructure) FileName() string { |
| 240 |
if filename, ok := bs.DispositionParams["filename"]; ok { |
| 241 |
return filename |
| 242 |
} else if filename, ok := bs.Params["name"]; ok { |
| 243 |
// workaround golang not supporting RFC2231 besides ASCII and UTF8 |
| 244 |
return filename |
| 245 |
} |
| 246 |
return "" |
| 247 |
} |
| 248 |
|
| 249 |
type Envelope struct { |
| 250 |
Date time.Time |
| 251 |
Subject string |
| 252 |
From []*mail.Address |
| 253 |
ReplyTo []*mail.Address |
| 254 |
Sender []*mail.Address |
| 255 |
To []*mail.Address |
| 256 |
Cc []*mail.Address |
| 257 |
Bcc []*mail.Address |
| 258 |
MessageId string |
| 259 |
InReplyTo string |
| 260 |
} |
| 261 |
|
| 262 |
// OriginalMail is helper struct used for reply/forward |
| 263 |
type OriginalMail struct { |
| 264 |
Date time.Time |
| 265 |
From string |
| 266 |
Text string |
| 267 |
MIMEType string |
| 268 |
RFC822Headers *mail.Header |
| 269 |
Folder string |
| 270 |
} |
| 271 |
|
| 272 |
type SignatureValidity int32 |
| 273 |
|
| 274 |
const ( |
| 275 |
UnknownValidity SignatureValidity = iota |
| 276 |
Valid |
| 277 |
InvalidSignature |
| 278 |
UnknownEntity |
| 279 |
UnsupportedMicalg |
| 280 |
MicalgMismatch |
| 281 |
) |
| 282 |
|
| 283 |
type MessageDetails struct { |
| 284 |
IsEncrypted bool |
| 285 |
IsSigned bool |
| 286 |
SignedBy string // Primary identity of signing key |
| 287 |
SignedByKeyId uint64 |
| 288 |
SignatureValidity SignatureValidity |
| 289 |
SignatureError string |
| 290 |
DecryptedWith string // Primary Identity of decryption key |
| 291 |
DecryptedWithKeyId uint64 // Public key id of decryption key |
| 292 |
Body io.Reader |
| 293 |
Micalg string |
| 294 |
} |