forked from emgenio/email-poller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail-poller.go
140 lines (118 loc) · 3.03 KB
/
email-poller.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
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/catuss-a/imap"
"github.com/streadway/amqp"
)
var (
err error
config PollerConfig
)
const (
Timeout = 30 * time.Second
defaultConfigFile = "./config.yaml"
)
func fatalOnError(err error, msg string) {
if err != nil {
fmt.Fprintln(os.Stderr, "%s: %s", msg, err)
panic(fmt.Sprintf("%s: %s", msg, err))
}
}
func pushIncomingMessagesToQueue(messagesChan chan []imapClient.GoImapMessage) {
conn, err := amqp.Dial(config.Amqp.Hostname)
fatalOnError(err, "Failed to connect to RabbitMQ")
defer conn.Close()
ch, err := conn.Channel()
fatalOnError(err, "Failed to open a channel")
defer ch.Close()
q, err := ch.QueueDeclare(
config.Amqp.MessageQueue, // queue
true, // durable
false, // delete when usused
false, // exclusive
false, // no-wait
nil, // arguments
)
fatalOnError(err, "Failed to declare a queue")
for newMessages := range messagesChan {
count := 0
for _, msg := range newMessages {
err = ch.Publish(
"", // exchange
q.Name, // routing key
false, // mandatory
false,
amqp.Publishing{
DeliveryMode: amqp.Persistent,
ContentType: "text/plain",
Body: msg.EncodeAsBytes(),
})
if err != nil {
fatalOnError(err, "Failed to publish a message")
} else {
count += 1
}
}
fmt.Println("Amount of Messages pushed to RabbitMQ:", count)
}
}
func monitorMailbox(client *imapClient.GoImapClient,
messagesChan chan []imapClient.GoImapMessage) {
for {
client.WaitForNotifications(Timeout)
ids := []uint32{}
ids = client.RetrieveMessageIds()
client.Client.Data = nil
messages := client.RetrieveMessagesFromIds(ids)
if len(messages) > 0 {
messagesChan <- messages
for _, msg := range messages {
msg.Dump()
}
client.ExpungeMessages(messages)
}
}
}
type PollerConfig struct {
Imap struct {
Hostname string
Port uint32
Login string
Password string
}
Amqp struct {
Hostname string
MessageQueue string
}
}
func init() {
configPath := flag.String("config", defaultConfigFile, "path to the configuration file.")
flag.Parse()
fmt.Println("Loading configuration file", *configPath)
loadConfig(*configPath, &config)
}
func main() {
// instanciate new GoImapClient
client := imapClient.NewClient(config.Imap.Hostname, config.Imap.Port, config.Imap.Login, config.Imap.Password)
// Connect to server (Dial)
err = client.Connect()
defer client.Logout(30 * time.Second)
// Authenticate
_, err = client.Login()
// Open a mailbox (synchronous command - no need for imap.Wait)
client.SelectMailBox("INBOX", false)
// Print box status
fmt.Print("\nMailbox status:\n", client.Client.Mailbox)
if !client.SupportIdleCap() {
fmt.Println("Error: Server does not support IDLE state")
return
}
messagesChan := make(chan []imapClient.GoImapMessage)
// Wait for Incoming ImapMessages and send them to RabbitMQ
go pushIncomingMessagesToQueue(messagesChan)
// Monitor mailbox and push incoming messages to Channel
monitorMailbox(client, messagesChan)
}