| 1 |
//go:build notmuch |
| 2 |
// +build notmuch |
| 3 |
|
| 4 |
package notmuch |
| 5 |
|
| 6 |
/* |
| 7 |
#cgo LDFLAGS: -lnotmuch |
| 8 |
|
| 9 |
#include <stdlib.h> |
| 10 |
#include <notmuch.h> |
| 11 |
|
| 12 |
*/ |
| 13 |
import "C" |
| 14 |
|
| 15 |
import ( |
| 16 |
"time" |
| 17 |
"unsafe" |
| 18 |
) |
| 19 |
|
| 20 |
type Message struct { |
| 21 |
message *C.notmuch_message_t |
| 22 |
} |
| 23 |
|
| 24 |
// Close frees resources associated with the message |
| 25 |
func (m *Message) Close() { |
| 26 |
C.notmuch_message_destroy(m.message) |
| 27 |
} |
| 28 |
|
| 29 |
// ID returns the message ID |
| 30 |
func (m *Message) ID() string { |
| 31 |
cID := C.notmuch_message_get_message_id(m.message) |
| 32 |
return C.GoString(cID) |
| 33 |
} |
| 34 |
|
| 35 |
// ThreadID returns the thread ID of the message |
| 36 |
func (m *Message) ThreadID() string { |
| 37 |
cID := C.notmuch_message_get_thread_id(m.message) |
| 38 |
return C.GoString(cID) |
| 39 |
} |
| 40 |
|
| 41 |
func (m *Message) Replies() Messages { |
| 42 |
cMessages := C.notmuch_message_get_replies(m.message) |
| 43 |
return Messages{ |
| 44 |
messages: cMessages, |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func (m *Message) TotalFiles() int { |
| 49 |
return int(C.notmuch_message_count_files(m.message)) |
| 50 |
} |
| 51 |
|
| 52 |
// Filename returns a single filename associated with the message. If the |
| 53 |
// message has multiple filenames, the return value will be arbitrarily chosen |
| 54 |
func (m *Message) Filename() string { |
| 55 |
cFilename := C.notmuch_message_get_filename(m.message) |
| 56 |
return C.GoString(cFilename) |
| 57 |
} |
| 58 |
|
| 59 |
func (m *Message) Filenames() []string { |
| 60 |
cFilenames := C.notmuch_message_get_filenames(m.message) |
| 61 |
defer C.notmuch_filenames_destroy(cFilenames) |
| 62 |
|
| 63 |
filenames := []string{} |
| 64 |
for C.notmuch_filenames_valid(cFilenames) > 0 { |
| 65 |
filename := C.notmuch_filenames_get(cFilenames) |
| 66 |
filenames = append(filenames, C.GoString(filename)) |
| 67 |
C.notmuch_filenames_move_to_next(cFilenames) |
| 68 |
} |
| 69 |
return filenames |
| 70 |
} |
| 71 |
|
| 72 |
// TODO is this needed? |
| 73 |
// func (m *Message) Reindex() error { |
| 74 |
// |
| 75 |
// } |
| 76 |
|
| 77 |
type Flag int |
| 78 |
|
| 79 |
const ( |
| 80 |
MESSAGE_FLAG_MATCH Flag = iota |
| 81 |
MESSAGE_FLAG_EXCLUDED |
| 82 |
MESSAGE_FLAG_GHOST |
| 83 |
) |
| 84 |
|
| 85 |
func (m *Message) Flag(flag Flag) (bool, error) { |
| 86 |
var ok C.notmuch_bool_t |
| 87 |
cFlag := C.notmuch_message_flag_t(flag) |
| 88 |
err := errorWrap(C.notmuch_message_get_flag_st(m.message, cFlag, &ok)) |
| 89 |
if err != nil { |
| 90 |
return false, err |
| 91 |
} |
| 92 |
if ok == 0 { |
| 93 |
return false, nil |
| 94 |
} |
| 95 |
return true, nil |
| 96 |
} |
| 97 |
|
| 98 |
// TODO why does this exist?? |
| 99 |
// func (m *Message) SetFlag(flag Flag) { |
| 100 |
// |
| 101 |
// } |
| 102 |
|
| 103 |
func (m *Message) Date() time.Time { |
| 104 |
cTime := C.notmuch_message_get_date(m.message) |
| 105 |
return time.Unix(int64(cTime), 0) |
| 106 |
} |
| 107 |
|
| 108 |
func (m *Message) Header(field string) string { |
| 109 |
cField := C.CString(field) |
| 110 |
defer C.free(unsafe.Pointer(cField)) |
| 111 |
cHeader := C.notmuch_message_get_header(m.message, cField) |
| 112 |
return C.GoString(cHeader) |
| 113 |
} |
| 114 |
|
| 115 |
func (m *Message) Tags() []string { |
| 116 |
cTags := C.notmuch_message_get_tags(m.message) |
| 117 |
defer C.notmuch_tags_destroy(cTags) |
| 118 |
|
| 119 |
tags := []string{} |
| 120 |
for C.notmuch_tags_valid(cTags) > 0 { |
| 121 |
tag := C.notmuch_tags_get(cTags) |
| 122 |
tags = append(tags, C.GoString(tag)) |
| 123 |
C.notmuch_tags_move_to_next(cTags) |
| 124 |
} |
| 125 |
return tags |
| 126 |
} |
| 127 |
|
| 128 |
func (m *Message) AddTag(tag string) error { |
| 129 |
cTag := C.CString(tag) |
| 130 |
defer C.free(unsafe.Pointer(cTag)) |
| 131 |
|
| 132 |
return errorWrap(C.notmuch_message_add_tag(m.message, cTag)) |
| 133 |
} |
| 134 |
|
| 135 |
func (m *Message) RemoveTag(tag string) error { |
| 136 |
cTag := C.CString(tag) |
| 137 |
defer C.free(unsafe.Pointer(cTag)) |
| 138 |
|
| 139 |
return errorWrap(C.notmuch_message_remove_tag(m.message, cTag)) |
| 140 |
} |
| 141 |
|
| 142 |
func (m *Message) RemoveAllTags() error { |
| 143 |
return errorWrap(C.notmuch_message_remove_all_tags(m.message)) |
| 144 |
} |
| 145 |
|
| 146 |
// SyncTagsToMaildirFlags adds/removes the appropriate tags to the maildir |
| 147 |
// filename |
| 148 |
func (m *Message) SyncTagsToMaildirFlags() error { |
| 149 |
return errorWrap(C.notmuch_message_tags_to_maildir_flags(m.message)) |
| 150 |
} |
| 151 |
|
| 152 |
// SyncMaildirFlagsToTags syncs the current maildir flags to the notmuch tags |
| 153 |
func (m *Message) SyncMaildirFlagsToTags() error { |
| 154 |
return errorWrap(C.notmuch_message_maildir_flags_to_tags(m.message)) |
| 155 |
} |
| 156 |
|
| 157 |
func (m *Message) HasMaildirFlag(flag rune) (bool, error) { |
| 158 |
var ok C.notmuch_bool_t |
| 159 |
err := errorWrap(C.notmuch_message_has_maildir_flag_st(m.message, C.char(flag), &ok)) |
| 160 |
if err != nil { |
| 161 |
return false, err |
| 162 |
} |
| 163 |
if ok == 0 { |
| 164 |
return false, nil |
| 165 |
} |
| 166 |
return true, nil |
| 167 |
} |
| 168 |
|
| 169 |
func (m *Message) Freeze() error { |
| 170 |
return errorWrap(C.notmuch_message_freeze(m.message)) |
| 171 |
} |
| 172 |
|
| 173 |
func (m *Message) Thaw() error { |
| 174 |
return errorWrap(C.notmuch_message_thaw(m.message)) |
| 175 |
} |
| 176 |
|
| 177 |
func (m *Message) Property(key string) (string, error) { |
| 178 |
var ( |
| 179 |
cKey *C.char |
| 180 |
cValue *C.char |
| 181 |
) |
| 182 |
defer C.free(unsafe.Pointer(cKey)) |
| 183 |
defer C.free(unsafe.Pointer(cValue)) |
| 184 |
cKey = C.CString(key) |
| 185 |
err := errorWrap(C.notmuch_message_get_property(m.message, cKey, &cValue)) //nolint:gocritic // see note in notmuch.go |
| 186 |
if err != nil { |
| 187 |
return "", err |
| 188 |
} |
| 189 |
return C.GoString(cValue), nil |
| 190 |
} |
| 191 |
|
| 192 |
func (m *Message) AddProperty(key string, value string) error { |
| 193 |
var ( |
| 194 |
cKey *C.char |
| 195 |
cValue *C.char |
| 196 |
) |
| 197 |
defer C.free(unsafe.Pointer(cKey)) |
| 198 |
defer C.free(unsafe.Pointer(cValue)) |
| 199 |
cKey = C.CString(key) |
| 200 |
cValue = C.CString(value) |
| 201 |
return errorWrap(C.notmuch_message_add_property(m.message, cKey, cValue)) |
| 202 |
} |
| 203 |
|
| 204 |
func (m *Message) RemoveProperty(key string, value string) error { |
| 205 |
var ( |
| 206 |
cKey *C.char |
| 207 |
cValue *C.char |
| 208 |
) |
| 209 |
defer C.free(unsafe.Pointer(cKey)) |
| 210 |
defer C.free(unsafe.Pointer(cValue)) |
| 211 |
cKey = C.CString(key) |
| 212 |
cValue = C.CString(value) |
| 213 |
return errorWrap(C.notmuch_message_remove_property(m.message, cKey, cValue)) |
| 214 |
} |
| 215 |
|
| 216 |
func (m *Message) RemoveAllProperties(key string) error { |
| 217 |
var cKey *C.char |
| 218 |
defer C.free(unsafe.Pointer(cKey)) |
| 219 |
cKey = C.CString(key) |
| 220 |
return errorWrap(C.notmuch_message_remove_all_properties(m.message, cKey)) |
| 221 |
} |
| 222 |
|
| 223 |
func (m *Message) RemoveAllPropertiesWithPrefix(prefix string) error { |
| 224 |
var cPrefix *C.char |
| 225 |
defer C.free(unsafe.Pointer(cPrefix)) |
| 226 |
cPrefix = C.CString(prefix) |
| 227 |
return errorWrap(C.notmuch_message_remove_all_properties_with_prefix(m.message, cPrefix)) |
| 228 |
} |
| 229 |
|
| 230 |
func (m *Message) Properties(key string, exact bool) *Properties { |
| 231 |
var ( |
| 232 |
cKey *C.char |
| 233 |
cExact C.int |
| 234 |
) |
| 235 |
defer C.free(unsafe.Pointer(cKey)) |
| 236 |
if exact { |
| 237 |
cExact = 1 |
| 238 |
} |
| 239 |
|
| 240 |
cKey = C.CString(key) |
| 241 |
props := C.notmuch_message_get_properties(m.message, cKey, cExact) |
| 242 |
|
| 243 |
return &Properties{ |
| 244 |
properties: props, |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
func (m *Message) CountProperties(key string) (int, error) { |
| 249 |
var ( |
| 250 |
cKey *C.char |
| 251 |
cCount C.uint |
| 252 |
) |
| 253 |
defer C.free(unsafe.Pointer(cKey)) |
| 254 |
cKey = C.CString(key) |
| 255 |
err := errorWrap(C.notmuch_message_count_properties(m.message, cKey, &cCount)) |
| 256 |
if err != nil { |
| 257 |
return 0, err |
| 258 |
} |
| 259 |
return int(cCount), nil |
| 260 |
} |