Skip to content

Commit

Permalink
#1 - Support to IMAP configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiao committed Dec 17, 2021
1 parent 14c99b4 commit c4436b8
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 2 deletions.
4 changes: 3 additions & 1 deletion config/cnfigLoader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ func TestGetLoading(t *testing.T) {
assert.Equal(t, confFile.PhaJenkins.Username, "example", "The params should be the same.")
assert.Equal(t, confFile.PhaJenkins.Token, "34567890", "The params should be the same.")
assert.Equal(t, confFile.PhaJenkins.Pipeline, "testingJenkins", "The params should be the same.")

assert.Equal(t, confFile.PhaImap.Username, "[email protected]", "The params should be the same.")
assert.Equal(t, confFile.PhaImap.Password, "my-dummy-password", "The params should be the same.")
assert.Equal(t, confFile.PhaImap.Address, "imap.gmail.com:993", "The params should be the same.")
}
13 changes: 12 additions & 1 deletion config/configLoader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,40 @@ package config
import (
"io/ioutil"
"log"

"gopkg.in/yaml.v2"
)

/**
* This is the module to load configuration
*/

// Configuration for Phabricator
type ConfPha struct {
Endpoint string `yaml:"endpoint"`
Token string `yaml:"token"`
Repo string `yaml:"repo"`
}

// Configuration for Jenkins
type ConfJenkins struct {
Endpoint string `yaml:"endpoint"`
Username string `yaml:"username"`
Token string `yaml:"token"`
Pipeline string `yaml:"pipeline"`
}

// Configurations for IMAP
type ConfImap struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
Address string `yaml:"address"`
}

// Configurations General
type ConfGoPath struct {
PhaConf ConfPha `yaml:"phabricator"`
PhaJenkins ConfJenkins `yaml:"jenkins"`
PhaImap ConfImap `yaml:"imap"`
}

// Load from File
Expand Down
5 changes: 5 additions & 0 deletions config/resources/pha_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ jenkins:
username: example
token: 34567890
pipeline: "testingJenkins" # Pipeline by default

imap:
address: "imap.gmail.com:993"
username: "[email protected]"
password: "my-dummy-password"
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.14
require (
9fans.net/go v0.0.2
github.com/bndr/gojenkins v1.0.1
github.com/emersion/go-imap v1.2.0 // indirect
github.com/gin-gonic/gin v1.6.3 // indirect
github.com/karlseguin/typed v1.1.7 // indirect
github.com/stretchr/testify v1.6.1
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ github.com/bndr/gojenkins v1.0.1/go.mod h1:J2FxlujWW87NJJrdysyctcDllRVYUONGGlHX1
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emersion/go-imap v1.2.0 h1:lyUQ3+EVM21/qbWE/4Ya5UG9r5+usDxlg4yfp3TgHFA=
github.com/emersion/go-imap v1.2.0/go.mod h1:Qlx1FSx2FTxjnjWpIlVNEuX+ylerZQNFE5NsmKFSejY=
github.com/emersion/go-message v0.15.0/go.mod h1:wQUEfE+38+7EW8p8aZ96ptg6bAb1iwdgej19uXASlE4=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1XQp98QTaHernxMYzRaOasRir9hUlFQ=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
Expand Down Expand Up @@ -57,6 +63,9 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20u
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
78 changes: 78 additions & 0 deletions mail/imap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"log"

"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
)

func CheckEmail(Username string, Password string, Address string) {
log.Println("Connecting to server...")

// Connect to server
c, err := client.DialTLS(Address, nil)
if err != nil {
log.Fatal(err)
}
log.Println("Connected")

// Don't forget to logout
defer c.Logout()

// Login
if err := c.Login(Username, Password); err != nil {
log.Fatal(err)
}
log.Println("Logged in")

// List mailboxes
mailboxes := make(chan *imap.MailboxInfo, 10)
done := make(chan error, 1)
go func() {
done <- c.List("", "*", mailboxes)
}()

log.Println("Mailboxes:")
for m := range mailboxes {
log.Println("* " + m.Name)
}

if err := <-done; err != nil {
log.Fatal(err)
}

// Select INBOX
mbox, err := c.Select("INBOX", false)
if err != nil {
log.Fatal(err)
}
log.Println("Flags for INBOX:", mbox.Flags)

// Get the last 4 messages
from := uint32(1)
to := mbox.Messages
if mbox.Messages > 3 {
// We're using unsigned integers here, only subtract if the result is > 0
from = mbox.Messages - 3
}
seqset := new(imap.SeqSet)
seqset.AddRange(from, to)

messages := make(chan *imap.Message, 10)
done = make(chan error, 1)
go func() {
done <- c.Fetch(seqset, []imap.FetchItem{imap.FetchEnvelope}, messages)
}()

log.Println("Last 4 messages:")
for msg := range messages {
log.Println("* " + msg.Envelope.Subject)
}

if err := <-done; err != nil {
log.Fatal(err)
}

log.Println("Done!")
}

0 comments on commit c4436b8

Please sign in to comment.