-
Notifications
You must be signed in to change notification settings - Fork 0
/
idlewatch.go
169 lines (144 loc) · 3.27 KB
/
idlewatch.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
package main
import (
"crypto/tls"
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"time"
"github.com/rakoo/go-ini"
"code.google.com/p/go-imap/go1/imap"
)
var (
mailbox = flag.String("mailbox", "[Gmail]/All Mail", "The mailbox to watch")
)
func main() {
flag.Parse()
email, password := getCredentials()
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt, os.Kill)
go func() {
<-quit
os.Exit(0)
}()
connect := func(server string) (c *imap.Client, err error) {
c, err = imap.DialTLS(server, &tls.Config{})
if err != nil {
return
}
_, err = c.Auth(imap.PlainAuth(email, password, ""))
if err != nil {
return
}
log.Println("Successfully authed")
cmd, err := c.Select(*mailbox, true)
if err != nil {
log.Println("Error selecting mailbox: ", err)
return
}
_, err = cmd.Result(imap.OK)
if err != nil {
return
}
log.Println("Successfully selected ", *mailbox)
_, err = c.Idle()
if err != nil {
return
}
log.Println("Starting idle...")
c.Data = nil
return
}
loop:
for {
if err := cmd(); err != nil {
log.Printf("Error running sync on new loop: %s\n", err)
continue
}
c, err := connect("imap.gmail.com")
if err != nil {
continue
}
wait := true
for wait {
err = c.Recv(15 * time.Minute)
switch err {
case nil:
fallthrough
case io.EOF:
// We received content from server -- sync mails
wait = false
case imap.ErrTimeout:
// after the timeout, wakeup the connection
if _, err := c.IdleTerm(); err != nil {
log.Println("Error finishing idle:: ", err)
continue loop
}
if _, err := imap.Wait(c.Noop()); err != nil {
log.Println("Error nooping: ", err)
continue loop
}
log.Println("Keeping it alive !")
if _, err := c.Idle(); err != nil {
log.Println("Error re-idling: ", err)
continue loop
}
default:
log.Println("Error while receiving content: ", err)
continue loop
}
}
for _, rsp := range c.Data {
if rsp.Label == "EXISTS" {
log.Println("New message, running sync...")
if err := cmd(); err != nil {
log.Printf("Error running sync: %s\n", err)
}
log.Println("Ran sync")
}
}
c.Data = nil
}
}
func getCredentials() (email, password string) {
cfg, err := ini.LoadFile(filepath.Join(os.Getenv("HOME"),
".offlineimaprc"))
if err != nil {
log.Fatal(err)
}
accounts, ok := cfg.Get("general", "accounts")
if !ok {
log.Fatal("No general/accounts string")
}
// Only take first one
account := strings.Split(accounts, ",")[0]
remoterepo, ok := cfg.Get("Account "+account, "remoterepository")
if !ok {
log.Fatal(fmt.Sprintf("No section 'Account %s'/remoterepository",
account))
}
typ, ok := cfg.Get("Repository "+remoterepo, "type")
if !ok {
log.Fatal("No type in repo")
}
if typ != "Gmail" {
log.Fatal("I can only manage Gmail repos; curren type is ", typ)
}
user, okuser := cfg.Get("Repository "+remoterepo, "remoteuser")
password, okpass := cfg.Get("Repository "+remoterepo, "remotepass")
if !okuser || !okpass {
log.Fatal("Invalid config")
}
return user + "@gmail.com", password
}
func cmd() error {
cmd := exec.Command("offlineimap", "-u", "Quiet")
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
return cmd.Run()
}