-
Notifications
You must be signed in to change notification settings - Fork 2
/
kobomail.go
231 lines (202 loc) · 5.92 KB
/
kobomail.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/BurntSushi/toml"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"github.com/emersion/go-message/mail"
)
//config struct
type KoboMailConfig struct {
IMAP_Host string `toml:"imap_host"`
IMAP_Port string `toml:"imap_port"`
IMAP_User string `toml:"imap_user"`
IMAP_Pwd string `toml:"imap_pwd"`
Email_Flag string `toml:"email_flag"`
Email_Unseen string `toml:"email_unseen"`
}
// chkErrFatal prints a message to the Kobo screen, then exits the program
func chkErrFatal(err error, usrMsg string, msgDuration int) {
if err != nil {
if usrMsg != "" {
// fbPrint(usrMsg)
time.Sleep(time.Duration(msgDuration) * time.Second)
}
log.Fatal(err)
}
}
// // logErrPrint is a convenience function for logging errors
// func logErrPrint(err error) {
// if err != nil {
// log.Print(err)
// }
// }
// nickelUSBplug simulates pugging in a USB cable
func nickelUSBplug() {
nickelHWstatusPipe := "/tmp/nickel-hardware-status"
nickelPipe, _ := os.OpenFile(nickelHWstatusPipe, os.O_RDWR, os.ModeNamedPipe)
nickelPipe.WriteString("usb plug add")
nickelPipe.Close()
}
// nickelUSBunplug simulates unplugging a USB cable
func nickelUSBunplug() {
nickelHWstatusPipe := "/tmp/nickel-hardware-status"
nickelPipe, _ := os.OpenFile(nickelHWstatusPipe, os.O_RDWR, os.ModeNamedPipe)
nickelPipe.WriteString("usb plug remove")
nickelPipe.Close()
}
func main() {
// If the file doesn't exist, create it or append to the
KM_Log_Path := ""
if _, err := os.Stat("/mnt/onboard/.add/kobomail/logs.txt"); err == nil {
KM_Log_Path = "/mnt/onboard/.add/kobomail/logs.txt"
} else if os.IsNotExist(err) {
KM_Log_Path = "logs.txt"
}
logFile, err := os.OpenFile(KM_Log_Path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
//let's output to both stoud and log file
mw := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(mw)
KM_Library_Path := ""
if _, err := os.Stat("/mnt/onboard/KoboMailLibrary/"); err == nil {
KM_Library_Path = "/mnt/onboard/KoboMailLibrary/"
} else if os.IsNotExist(err) {
KM_Library_Path = "Library/"
}
// Read Config file
KM_Config_Path := ""
if _, err := os.Stat("/mnt/onboard/.add/kobomail/kobomail_cfg.toml"); err == nil {
KM_Config_Path = "/mnt/onboard/.add/kobomail/kobomail_cfg.toml"
} else if os.IsNotExist(err) {
KM_Config_Path = "kobomail_cfg.toml"
}
var KM_Config KoboMailConfig
if _, err := toml.DecodeFile(KM_Config_Path, &KM_Config); err != nil {
chkErrFatal(err, "Couldn't read config. Aborting!", 5)
}
host := KM_Config.IMAP_Host
port := KM_Config.IMAP_Port
user := KM_Config.IMAP_User
pass := KM_Config.IMAP_Pwd
tlsn := ""
if port == "" {
port = "993"
}
connStr := fmt.Sprintf("%s:%s", host, port)
tlsc := &tls.Config{}
if tlsn != "" {
tlsc.ServerName = tlsn
}
c, err := client.DialTLS(connStr, tlsc)
if err != nil {
log.Fatal(err)
}
log.Println("Connected")
defer c.Logout()
if err := c.Login(user, pass); err != nil {
log.Fatal(err)
}
log.Println("Authenticated")
mbox, err := c.Select("INBOX", false)
if err != nil {
log.Fatal(err)
}
log.Println("Inbox selected: ", mbox.Name)
criteria := imap.NewSearchCriteria()
if KM_Config.Email_Unseen == "true" {
criteria.WithoutFlags = []string{"\\Seen"}
}
KM_Config_To_Kobo := strings.Replace(KM_Config.IMAP_User, "@", "+"+KM_Config.Email_Flag+"@", 1)
criteria.Header.Add("TO", KM_Config_To_Kobo)
uids, err := c.Search(criteria)
if err != nil {
log.Println(err)
}
seqset := new(imap.SeqSet)
seqset.AddNum(uids...)
log.Printf("Search complete, found %d messages", len(uids))
section := &imap.BodySectionName{}
items := []imap.FetchItem{imap.FetchEnvelope, imap.FetchFlags, imap.FetchInternalDate, section.FetchItem()}
messages := make(chan *imap.Message)
done := make(chan error, 1)
go func() {
done <- c.Fetch(seqset, items, messages)
log.Println("Fetch complete")
}()
for msg := range messages {
if msg != nil {
log.Printf("got message with address %p\n", msg)
r := msg.GetBody(section)
if r == nil {
log.Fatal("Server didn't returned message body")
}
// Create a new mail reader
mr, err := mail.CreateReader(r)
if err != nil {
log.Fatal(err)
}
// Print some info about the message
header := mr.Header
if date, err := header.Date(); err == nil {
log.Println("Date:", date)
}
// if from, err := header.AddressList("From"); err == nil {
// log.Println("From:", from)
// }
// if to, err := header.AddressList("To"); err == nil {
// log.Println("To:", to)
// }
if subject, err := header.Subject(); err == nil {
log.Println("Subject:", subject)
}
// Process each message's part
for {
p, err := mr.NextPart()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
switch h := p.Header.(type) {
case *mail.AttachmentHeader:
// This is an attachment
filename, _ := h.Filename()
log.Println("Got attachment:", filename)
contenttype, _, _ := h.ContentType()
log.Println("of type: ", contenttype)
b, _ := ioutil.ReadAll(p.Body)
// write the whole body at once
err = ioutil.WriteFile(KM_Library_Path+filename, b, 0644)
if err != nil {
panic(err)
}
}
}
} else {
log.Println("no messages matched criteria")
}
}
// if err := <-done; err != nil {
// log.Fatal(err)
// }
//now that we finished loading all messages we'll simulate the USB cable connect
//but only if there were any messages processed, no need to bug the user if there was nothing new
if len(uids) > 0 {
log.Println("Simulating PLugging USB cable and wait 10s for the user to click on the connect button")
nickelUSBplug()
time.Sleep(10 * time.Second)
log.Println("Simulating unplugging USB cable")
nickelUSBunplug()
//after this Nickel will do the job to import the new files loaded into the KoboMailLibrary folder
}
}