Esempio n. 1
0
// TODO: more than just the last 50 of the inbox
// TODO: offline mode.  I think google throttles my requests
// after spending the day killing and restarting the server
// TODO: possibly split the imap client into a separate process
func MessagesHandler(w http.ResponseWriter, r *http.Request, ctx *Context) error {
	client, err := ctx.GetClient()
	if err != nil {
		return err
	}

	var mailbox *imap.Box
	boxName := r.URL.Query().Get("box")
	log.Println(r.URL)
	if boxName == "" {
		mailbox = client.Inbox()
	} else {
		mailbox = client.Box(boxName)
	}
	if mailbox == nil {
		w.WriteHeader(404)
		return nil
	}

	mailbox.Check()
	msgs := mailbox.Msgs()

	n := len(msgs)
	var headers []*Message
	if n > 0 {
		mn := max(1, (minusf(uint32(n), 50)))
		headers = make([]*Message, uint32(n)-mn)
		for i, msg := range msgs[mn:] {
			headers[i] = &Message{
				fmt.Sprintf("%v", msg.UID),
				msg.Hdr,
				(msg.Flags & imap.FlagSeen) == imap.FlagSeen,
				msg.Box.Name,
			}
		}
	} else {
		headers = make([]*Message, 0)
	}

	b, err := json.Marshal(headers)
	if err != nil {
		return err
	}
	w.WriteHeader(200)
	w.Write(b)
	return nil
}
Esempio n. 2
0
func MessageHandler(w http.ResponseWriter, r *http.Request, ctx *Context) error {
	client, err := ctx.GetClient()
	if err != nil {
		return err
	}

	uidstr := r.URL.Query().Get(":id")

	var mailbox *imap.Box
	boxName := r.URL.Query().Get("box")
	if boxName == "" {
		mailbox = client.Inbox()
	} else {
		mailbox = client.Box(boxName)
	}
	if mailbox == nil {
		w.WriteHeader(404)
		return nil
	}

	mailbox.Check()
	msgs := mailbox.Msgs()

	uid, err := strconv.ParseUint(uidstr, 10, 64)
	if err != nil {
		return err
	}
	exists := len(msgs)
	entryId := sort.Search(exists,
		func(i int) bool {
			return msgs[i].UID >= uid
		})
	if entryId < exists && msgs[entryId] != nil {
		parts := handleMail(&(msgs[entryId].Root))
		b, err := json.Marshal(parts)
		if err != nil {
			return err
		}
		w.WriteHeader(200)
		w.Write(b)
	} else {
		w.WriteHeader(404)
	}
	return nil
}