Skip to content

Commit

Permalink
Merge pull request #1 from StevenMaude/refactor-and-tidy
Browse files Browse the repository at this point in the history
Refactor and tidy
  • Loading branch information
StevenMaude authored Nov 12, 2016
2 parents bffef80 + a11f2ab commit 09cbcbd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ too often in a short time. See TODO.

## TODO:

* Fix panics on no messages.
* Consider introducing slight pauses between requests to try and
improve reliability.
* Possibly consider selecting other than latest message (though outside
Expand Down
52 changes: 24 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@ type mail struct {
} `json:"data"`
}

// TODO: consider dropping most of this. Can get from the message itself.
type publicMsg struct {
ID string `json:"id"`
To string `json:"to"`
}

type mailboxDetails struct {
PublicMsgs []struct {
FromFull string `json:"fromfull"`
Subject string `json:"subject"`
ID string `json:"id"`
To string `json:"to"`
} `json:"public_msgs"`
PublicMsgs []publicMsg `json:"public_msgs"`
}

func getMailboxDetails(localPart string) (mailboxDetails, error) {
Expand All @@ -73,20 +72,12 @@ func getMailboxDetails(localPart string) (mailboxDetails, error) {

defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&mbxDetails)
if err != nil {
return mbxDetails, err
}
// No need for error check here as we return mbxDetails and err whether
// we have an error or not.
return mbxDetails, err
}

func getCookies(mbxDetails mailboxDetails) ([]*http.Cookie, error) {
// TODO: consider allow to retrieve more than one message.
// TODO: fix panic on no messages.
// TODO: just pass in the single message; avoid duplication?
// TODO: confirm that first item is the latest message (but I think
// it is from previous testing of the bash script).
latestMsg := mbxDetails.PublicMsgs[0]

func getCookies(latestMsg publicMsg) ([]*http.Cookie, error) {
// This request is for nothing but getting required cookies.
// Otherwise, the subsequent request fails.
inboxURL := "https://www.mailinator.com/inbox2.jsp?public_to=" + latestMsg.To
Expand All @@ -102,17 +93,13 @@ func getCookies(mbxDetails mailboxDetails) ([]*http.Cookie, error) {
return cookies, err
}

func getMail(mbxDetails mailboxDetails, cookies []*http.Cookie) error {
// TODO: consider allow to retrieve more than one message.
// TODO: fix panic on no messages.
// TODO: just pass in the single message; avoid duplication?
// TODO: confirm that first item is the latest message (but I think
// it is from previous testing of the bash script).
latestMsg := mbxDetails.PublicMsgs[0]

func getMail(latestMsg publicMsg, cookies []*http.Cookie) error {
msgURL := "https://www.mailinator.com/fetchmail?msgid=" + latestMsg.ID + "&zone=public"
fmt.Println("Retrieving URL:", msgURL)
req, err := http.NewRequest("GET", msgURL, nil)
if err != nil {
return err
}

for _, c := range cookies {
req.AddCookie(c)
Expand All @@ -139,6 +126,7 @@ func getMail(mbxDetails mailboxDetails, cookies []*http.Cookie) error {
}

func main() {
// TODO: consider allow to retrieve more than one message.
if len(os.Args) != 2 {
fmt.Println("Usage: mailin8 <local-part>")
os.Exit(1)
Expand All @@ -151,13 +139,21 @@ func main() {
os.Exit(1)
}

cookies, err := getCookies(mbxDetails)
numberMsgs := len(mbxDetails.PublicMsgs)
if numberMsgs == 0 {
fmt.Println("no messages in inbox")
os.Exit(0)
}

latestMsg := mbxDetails.PublicMsgs[numberMsgs-1]

cookies, err := getCookies(latestMsg)
if err != nil {
fmt.Println("failed to get cookies:", err)
os.Exit(1)
}

err = getMail(mbxDetails, cookies)
err = getMail(latestMsg, cookies)
if err != nil {
fmt.Println("failed to get mail:", err)
os.Exit(1)
Expand Down

0 comments on commit 09cbcbd

Please sign in to comment.