Beispiel #1
0
//IMAP
//CheckImap() checks Imap inbox for any PGP requests ,numRequests returns number of Emails found with GPG requests.
func CheckImap() (err error) {

	defer func() {
		fmt.Println("Warning: IMapChecker Stopped: Check Config of email,pubkeys,stateFiles etc.")
		log.Println("IMapChecker Exited:", err)
	}()
	fmt.Println("Starting IMAP Checker ...")

	imapConf, err1 := GetMailerConf()

	if err1 != nil {
		err = err1
		return
	}

	imapHost := imapConf.imapHost
	port, errC := strconv.Atoi(imapConf.imapPort)
	if errC != nil {
		err = errC
		return
	}

	imapPort := uint16(port)
	userName := imapConf.userName
	password := imapConf.password
	imapFreq := imapConf.imapFreq
	procFolder := imapConf.procFolder
	pksAddr := imapConf.ownAddr

	server := &Simap.IMAPServer{imapHost, imapPort}
	imapAcct := &Simap.IMAPAccount{userName, password, server}

	ticker := time.NewTicker(time.Duration(imapFreq) * time.Second)
	fmt.Println("IMAP Checker Started.")
	for tickTime := range ticker.C {
		msgs, err := Simap.GetEMails(imapAcct, "ALL UNSEEN", "inbox", 5, true)
		var processed []uint32
		if err == nil {
			//fmt.Println("Time [uid] From -> To")
			fmt.Println("mailer.go:Checking IMAP at ", time.Now())
			for _, msg := range msgs {
				log.Println(tickTime, ": inbox [", msg.Imap_uid, "] ", msg.From, "|", msg.Subject)
				err = processEmails(pksAddr, msg)
				if err != nil { //Dont add to processed uids
					log.Println("processing id = ", msg.Imap_uid, ": ", err)
					continue
				}
				processed = append(processed, msg.Imap_uid)
			}
			//Mark Emails as Read
			err = Simap.MarkEmails(imapAcct, "inbox", "\\SEEN", processed, 20, true)
			if err != nil {
				fmt.Println("Main : Error while Marking ", err)
			}
			//Move Processed Emails to a mailbox named processed
			err = Simap.MoveEmails(imapAcct, "inbox", procFolder, processed, 20, true)
			if err != nil {
				log.Println("Eror while moving email ", processed, " : ", err)
			}

			log.Println("CheckIMAP: Processed ", len(processed), " emails out of ", len(msgs))
			fmt.Println(time.Now(), " CheckIMAP: Processed ", len(processed), " emails out of ", len(msgs))

		} else {
			log.Println(err)
			//FOrever Check for Email,hence comment out
			//ticker.Stop()
			//break
		}
	}
	return
}
Beispiel #2
0
func main() {
	flag.Usage = usage
	flag.Parse()

	args := flag.Args()
	if len(args) != 4 {
		usage()
	}
	portI, _ := strconv.Atoi(args[1])
	port := uint16(portI)
	server := &Simap.IMAPServer{args[0], port}
	acct := &Simap.IMAPAccount{args[2], args[3], server}

	mails, err := Simap.GetEMails(acct, *query, *mbox, *jobSize, *skipCerti)
	if err != nil {
		fmt.Println("Error while Getting mails ", err)
		return
	}
	var uids []uint32
	fmt.Println("Fetched Mails ", len(mails))
	fmt.Println("UID		|	From		|	To		|		Subject		|Body	| HTMLBODY	|GPGBody")
	for _, msg := range mails {
		//PRocess Emails here
		errP := processEmail(msg)
		if errP != nil {
			continue
		}
		//If successfull then append them to be moved to processed
		uids = append(uids, msg.Imap_uid)
	}
	if *imapFlag != "" {
		err = Simap.MarkEmails(acct, *mbox, *imapFlag, uids, *jobSize, *skipCerti)
		if err != nil {
			fmt.Println("Main : Error while Marking ", err)
		}
	}

	if *del == true {
		err = Simap.DeleteEmails(acct, *mbox, uids, *jobSize, *skipCerti)
		if err != nil {
			fmt.Println("Main : Error while Deleting ", err)
		}
		return

	}

	if *destBox != "" {
		if *move == true {
			err = Simap.MoveEmails(acct, *mbox, *destBox, uids, *jobSize, *skipCerti)
			if err != nil {
				fmt.Println("Eror while moving ", err)
			}
		} else {
			err = Simap.CopyEmails(acct, *mbox, *destBox, uids, *jobSize, *skipCerti)
			if err != nil {
				fmt.Println("Eror while Copying ", err)
			}
		}
	}

}