Exemple #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")
	}
}
Exemple #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))
}
Exemple #3
0
func (web *MailWeb) authenticate(session sessions.Session, postedUser auth.WatneyUser,
	r render.Render, req *http.Request) {
	// 1) Create a new IMAP mail server connection
	if imapCon, err := mail.NewMailCon(web.mconf); nil != err {
		fmt.Printf("Couldn't establish connection to imap mail server: %s\n", err.Error())
		r.HTML(200, "start", map[string]interface{}{
			"FailedLogin": true,
			"OrigError":   err.Error(),
		})
	} else {
		if _, err := imapCon.Authenticate(postedUser.Username, postedUser.Password); err == nil {
			var user auth.WatneyUser = auth.WatneyUser{
				Username: postedUser.Username,
				SMTPAuth: smtp.PlainAuth("", postedUser.Username, postedUser.Password,
					web.mconf.SMTPAddress),
				ImapCon: imapCon,
			}
			h := fnv.New32a()
			h.Write([]byte(postedUser.Username))
			user.Id = int64(h.Sum32())
			if err := sessionauth.AuthenticateSession(session, &user); err != nil {
				r.HTML(200, "start", map[string]interface{}{
					"FailedLogin": true,
					"OrigError":   err.Error(),
				})
			}
			r.Redirect("/main")
		} else {
			fmt.Println("FAILED!")
			imapCon.Close()
			r.HTML(200, "start", map[string]interface{}{
				"FailedLogin": true,
				"OrigError":   err.Error(),
			})
		}
	}
}