This repository was archived by the owner on Apr 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (150 loc) · 4.2 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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"github.com/emersion/go-message/mail"
_ "github.com/emersion/go-message/charset"
)
type Credentials struct {
User string
Pass string
Serv string
Mail string
}
func main() {
creds := Credentials{}
user := flag.String("user", "", "Username. Often formatted as \"username@example.com\"")
pass := flag.String("pass", "", "Password.")
serv := flag.String("serv", "", "Server. The IMAP server to connect to. Include the port. Example: \"imap.example.com:993\"")
mail := flag.String("mail", "", "Mailbox. The Mailbox to scan. Example: \"INBOX\"")
flag.Parse()
creds.User = *user
creds.Pass = *pass
creds.Serv = *serv
creds.Mail = *mail
file, err := os.Open("credentials.json")
if err != nil {
fmt.Println("No credentials file found.")
} else {
defer file.Close()
decode := json.NewDecoder(file)
err = decode.Decode(&creds)
if err != nil {
fmt.Println(err)
}
}
if creds.User == "" || creds.Pass == "" || creds.Serv == "" || creds.Mail == "" {
fmt.Println("Missing arguments! Please include:")
fmt.Println("Your username: (\"-user username@example.com\")")
fmt.Println("Your password: (\"-pass yourpassword\")")
fmt.Println("Your email server: (\"-serv imap.example.com:993\")")
fmt.Println("Your mailbox: (\"-mail INBOX\")")
fmt.Println("If you can, place theese in the credentials.json file instead. (see headblockhead.com/files/examples/mailtidy/credentials.json)")
fmt.Println("For other arguments that are not required, use \"-help\"")
return
}
err = process(creds)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
func process(creds Credentials) (err error) {
fmt.Println("Connecting to server...")
c, err := client.DialTLS(creds.Serv, nil)
if err != nil {
return fmt.Errorf("error connecting: %w", err)
}
fmt.Println("Connected")
// Don't forget to logout.
defer c.Logout()
// Login.
if err := c.Login(creds.User, creds.Pass); err != nil {
return fmt.Errorf("error logging in: %w", err)
}
fmt.Println("Logged in")
// Select the mailbox.
mbox, err := c.Select(creds.Mail, false)
if err != nil {
return fmt.Errorf("could not select the mailbox: %w", err)
}
from := uint32(1)
to := mbox.Messages
if mbox.Messages == 0 {
return
}
if to > 100 {
to = 100
}
handlers := []Handler{
SecurityAlertHandler{},
FailedMessageSendHandler{},
CalendarHandler{},
ExpiredEventHandler{},
}
act := NewDefaultActions()
// Get all email in the inbox.
seqset := new(imap.SeqSet)
seqset.AddRange(from, to)
var section imap.BodySectionName
items := []imap.FetchItem{section.FetchItem()}
messages := make(chan *imap.Message, 10)
var channelError error
go func() {
channelError = c.Fetch(seqset, items, messages)
}()
// Loop through all messages.
for msg := range messages {
if msg == nil {
return fmt.Errorf("server didn't return a message")
}
r := msg.GetBody(§ion)
if r == nil {
return fmt.Errorf("server didn't return message body")
}
// Create a new mail reader.
mr, err := mail.CreateReader(r)
if err != nil {
fmt.Printf("failed to read body: %v", err)
continue
}
email, err := NewMessage(msg.SeqNum, mr)
if err != nil {
fmt.Printf("failed to read email: %v", err)
continue
}
// Print the email. NewLine before it.
fmt.Printf("\n" + email.String())
// Process it.
for _, h := range handlers {
err := h.Handle(email, act)
if err != nil {
fmt.Printf("failed to process email: %v", err)
}
}
}
if channelError != nil {
return fmt.Errorf("failed to fetch: %w", channelError)
}
// Delete all the deletion-flagged messages.
if len(act.MessagesToDelete.Set) > 0 {
item := imap.FormatFlagsOp(imap.AddFlags, true)
flags := []interface{}{imap.DeletedFlag}
if err := c.Store(act.MessagesToDelete, item, flags, nil); err != nil {
return fmt.Errorf("failed to mark the message for deletion: %w", err)
}
if err := c.Expunge(nil); err != nil {
return fmt.Errorf("failed to apply deletions: %w", err)
}
}
// Open deferred links.
fmt.Println("Opening all listed links... This may lag your computer.")
for _, url := range act.LinksToOpen {
act.OpenBrowser(url)
}
return nil
}