Exemple #1
1
func main() {

	log.Printf("Connect to %s\n", serverAddress)

	var client *pop3.Client
	var dialErr error
	if secure {
		client, dialErr = pop3.DialTLS(serverAddress)
	} else {
		client, dialErr = pop3.Dial(serverAddress)
	}
	checkErr(dialErr)

	authErr := client.Auth(username, password)
	checkErr(authErr)

	log.Println("Fetching mailbox stat")
	cmdErr := PrintMailBoxStat(client)
	checkErr(cmdErr)

	log.Println("Downloading mails")
	err := ScanMailbox(client)
	checkErr(err)

	log.Println("Finished to download all of mails")

}
Exemple #2
0
// Process mails at configured POP3 account
// and return a nagios state, including message reflecting what happened
func ProcessMails() (state nagios.Status, message string) {

	hp := net.JoinHostPort(Account.Host, Account.Port)
	info("Dialing %v ...", hp)
	client, err := pop3.DialTLS(hp)
	if err != nil {
		return nagios.UNKNOWN, err.Error()
	}

	// Send QUIT, so DELEted mails get expunged from the mailbox
	defer client.Quit()

	info("Authorizing as %v ...", Account.Email)
	if err = client.Auth(Account.Email, Account.Password); err != nil {
		return nagios.UNKNOWN, err.Error()
	}

	info("Retrieving messages ids ...")
	msgId, _, err := client.ListAll()
	if err != nil {
		return nagios.UNKNOWN, err.Error()
	}
	info("Got %v message ids", len(msgId))

	for i, id := range msgId {
		info("Retrieving message %v/%v id = %v", i+1, len(msgId), id)
		msg, err := client.Retr(id)
		if err != nil {
			return nagios.UNKNOWN, err.Error()
		}
		info("Retrieved %v bytes", len(msg))
		imp, err := ums.ExtractImportResult(strings.NewReader(msg))
		if err != nil {
			// Not for us
			if err == ums.ErrWrongSender {
				info("Message %v is not for UMS check", msgId)
				continue
			}
			return nagios.UNKNOWN, err.Error()
		}

		if delete_after {
			err := client.Dele(id)

			if err != nil {
				info("Error deleting processed mail %v, Error %v", id, err)
			} else {
				info("Deleted processed mail %v", id)
			}
		}

		if imp.Successful() {
			return nagios.OK, "UMS import successful"
		} else {
			err := &ums.ImportError{Import: imp}
			return nagios.WARNING, err.Error()
		}
	}

	// TODO(nightlyone): Use statefile, make it sticky or use nagios stalking
	return nagios.OK, "No mails for us"
}