-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
200 lines (181 loc) · 4.78 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// notmuch-mtag tags a bunch of messages, a la 'notmuch tag'
package main
import (
// stdlib imports
"encoding/json"
"flag"
"fmt"
stdlog "log"
"os"
"path"
"strings"
// 3rd-party imports
"github.com/kless/goconfig/config"
"notmuch"
)
type Tag struct {
Cmd string
Query string
}
func build_tag_cmd(cmd string) (cmds []string, err error) {
cmds = strings.Split(cmd, " ")
if len(cmds) <= 0 {
err = fmt.Errorf("invalid tag-cmd [%s]", cmd)
return nil, err
}
for idx,c := range cmds {
if len(c) <= 1 {
err = fmt.Errorf("invalid tag-cmd at index #%d: [%s]", idx, c)
return nil, err
}
switch c[0] {
case '+', '-':
// ok.
default:
err = fmt.Errorf("invalid tag-cmd at index #%d: [%s]", idx, c)
return nil, err
}
}
return cmds, err
}
var g_verbose = flag.Bool("verbose", false, "enable verbose output")
func main() {
var cfg *config.Config
var err error
log := stdlog.New(
os.Stderr,
"[notmuch-mtag] ",
stdlog.Flags())
flag.Parse()
log.Printf(":: notmuch-mtag...\n")
// honor NOTMUCH_CONFIG
home := os.Getenv("NOTMUCH_CONFIG")
if home == "" {
home = os.Getenv("HOME")
}
if cfg, err = config.ReadDefault(path.Join(home, ".notmuch-config")); err != nil {
log.Fatalf("error loading config file:", err)
}
db_path, err := cfg.String("database", "path")
if err != nil {
log.Fatalf("no field 'path' in section 'database'")
}
tag_fname, err := cfg.String("notmuch-mtag", "script")
if err != nil {
log.Fatalf("no field 'script' in section 'notmuch-mtag'")
}
log.Printf("verbose: [%v]\n", *g_verbose)
log.Printf("db_path: [%s]\n", db_path)
log.Printf("tag_fname: [%s]\n", tag_fname)
// open the database
db, status := notmuch.OpenDatabase(
db_path,
notmuch.DATABASE_MODE_READ_WRITE)
if status != notmuch.STATUS_SUCCESS {
log.Fatalf("Failed to open the database: %v\n", status)
}
defer db.Close()
// open the tag commands
tagfile, err := os.Open(tag_fname)
if err != nil {
log.Fatalf("Failed to open the tag-commands file: %v\n", err)
}
tagcmds := []Tag{}
{
dec := json.NewDecoder(tagfile)
if dec == nil {
log.Fatalf("Failed to create a new json-decoder\n")
}
err = dec.Decode(&tagcmds)
if dec == nil {
log.Fatalf("Failed to decode the tag-commands file: %v\n", err)
}
}
{
// remove phony messages (before 1980)
query := db.CreateQuery("tag:new AND tag:inbox AND ..-0")
msgs := query.SearchMessages()
for _, msg := range msgs {
for _,tag := range []string{"new", "inbox", "unread"} {
if msg.Freeze() != notmuch.STATUS_SUCCESS {
log.Printf("could not freeze message [%v]\n", msg.GetMessageId())
}
if msg.RemoveTag(tag) != notmuch.STATUS_SUCCESS {
log.Printf("could not apply tag -%s to message id=%v\n",
tag,
msg.GetMessageId())
}
if msg.Thaw() != notmuch.STATUS_SUCCESS {
log.Printf("could not freeze message [%v]\n", msg.GetMessageId())
}
}
}
}
new_msg_tag_fmt := "(tag:new AND tag:inbox AND NOT (%s)) AND (%s)"
for _, tag := range tagcmds {
tag_cmds, err := build_tag_cmd(tag.Cmd)
if err != nil {
log.Printf("error: %v\n", err)
continue
}
tag_cmd_str := make([]string, len(tag_cmds))
for i,tag_cmd_i := range tag_cmds {
tag_cmd_str[i] = "tag:"+tag_cmd_i[1:]
}
query_str := fmt.Sprintf(
new_msg_tag_fmt,
strings.Join(tag_cmd_str, " AND "),
tag.Query,
)
// look at all new messages in the inbox
query := db.CreateQuery(query_str)
msgs := query.SearchMessages()
if len(msgs) > 0 {
log.Printf(">> [%s]\n", query_str)
log.Printf(">> got: [%v] new messages...\n", len(msgs))
}
for _, msg := range msgs {
if *g_verbose {
log.Printf("==> %s\n", msg.GetMessageId())
}
if msg.Freeze() != notmuch.STATUS_SUCCESS {
log.Printf("could not freeze message [%v]\n", msg.GetMessageId())
}
tags := msg.GetTags()
for _,tag_cmd := range tag_cmds {
switch tag_cmd[0] {
case '+':
if msg.AddTag(tag_cmd[1:]) != notmuch.STATUS_SUCCESS {
log.Printf("**error**\n")
continue
}
case '-':
if msg.RemoveTag(tag_cmd[1:]) != notmuch.STATUS_SUCCESS {
log.Printf("**error**\n")
continue
}
}
}
if *g_verbose {
tags = msg.GetTags()
log.Printf("==> tags: %v\n", tags)
}
if msg.Thaw() != notmuch.STATUS_SUCCESS {
log.Printf("could not freeze message [%v]\n", msg.GetMessageId())
}
}
} //> tag cmds
// remove the 'new' tag
query := db.CreateQuery("tag:new AND tag:inbox")
msgs := query.SearchMessages()
log.Printf(">> applying tag -new...\n")
log.Printf(">> got: [%v] new messages...\n", len(msgs))
for _, msg := range msgs {
if msg.RemoveTag("new") != notmuch.STATUS_SUCCESS {
log.Printf("could not 'un-new' message id=%v\n", msg.GetMessageId())
}
}
log.Printf(">> applying tag -new... [done]\n")
log.Printf(":: notmuch-mtag...[done]\n")
return
}