Beispiel #1
0
/**
 * Handler to load all mails for a given folder.
 */
func (web *MailWeb) mails(r render.Render, user sessionauth.User, req *http.Request) {
	var (
		mails      []mail.Mail      = []mail.Mail{}
		watneyUser *auth.WatneyUser = user.(*auth.WatneyUser)
	)
	if nil != watneyUser && watneyUser.IsAuthenticated() {
		switch req.FormValue("mailInformation") {
		case mail.FULL:
			mails, _ = watneyUser.ImapCon.LoadAllMailsFromFolder(req.FormValue("mailbox"))
		case mail.OVERVIEW:
			fallthrough
		default:
			mails, _ = watneyUser.ImapCon.LoadAllMailOverviewsFromFolder(req.FormValue("mailbox"))
		}
		// Reverse the retrieved mail array
		sort.Sort(mail.MailSlice(mails))
		r.JSON(200, mails)
	} else {
		web.notifyAuthTimeout(r, "Retrieve mail overview")
	}
}
Beispiel #2
0
/**
 * Handler to check, whether new mails have arrived in the meantime.
 */
func (web *MailWeb) poll(r render.Render, user sessionauth.User, session sessions.Session,
	req *http.Request) {
	var watneyUser *auth.WatneyUser = user.(*auth.WatneyUser)
	// 1) Check for authentication status, and return if no user is given or not authenticated
	if nil == watneyUser || !watneyUser.IsAuthenticated() {
		web.notifyAuthTimeout(r, "Poll for new mails")
		return
	}
	// 2) Check, whether new mails have arrived since the last poll
	// recentMails[0] -> nbr of new mails | recentMails[1] - last sequence number in mail list
	if newMailSeqNbrs, err := watneyUser.ImapCon.CheckNewMails(); err != nil {
		// 2a) Check for new mails failed for some reason
		web.notifyError(r, 500, fmt.Sprintf("Error while checking for new mails"), err.Error())
	} else if len(newMailSeqNbrs) > 0 {
		// 2b) If new mails have arrived, load them from the mail server
		if mails, err := watneyUser.ImapCon.LoadNMailsFromFolderWithSeqNbrs("/", newMailSeqNbrs); err == nil {
			// If the number of loaded mails is not equal to the number of new mail UIDs, send error
			if len(newMailSeqNbrs) != len(mails) {
				web.notifyError(r, 500,
					fmt.Sprintf("New mails are available, but they couldn't be retrieved"),
					fmt.Sprintf("Expected %d mail(s), but could only load %d mail(s)",
						len(newMailSeqNbrs), len(mails)))
				return
			}
			// Reverse the retrieved mail array
			sort.Sort(mail.MailSlice(mails))
			// Return the mails as json
			r.JSON(200, mails)
			return
		}
		// TODO: If the mail loading failed, the information about the recent mails
		//		 will be lost at that point => counteract
		web.notifyError(r, 500,
			fmt.Sprintf("New mails are available, but they couldn't be retrieved"), err.Error())
		return
	}
	// 3) No new mails have arrived
	r.JSON(200, make([]mail.Mail, 0))
}