當前位置: 首頁>>代碼示例>>Golang>>正文


Golang imap.Client類代碼示例

本文整理匯總了Golang中code/google/com/p/go-imap/go1/imap.Client的典型用法代碼示例。如果您正苦於以下問題:Golang Client類的具體用法?Golang Client怎麽用?Golang Client使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Client類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: getEmails

// getEmails will fetch the full bodies of all emails listed in the given command.
func getEmails(client *imap.Client, cmd *imap.Command) ([]map[string]interface{}, error) {
	var emails []map[string]interface{}
	seq := new(imap.SeqSet)
	for _, rsp := range cmd.Data {
		for _, uid := range rsp.SearchResults() {
			seq.AddNum(uid)
		}
	}
	if seq.Empty() {
		return emails, nil
	}
	fCmd, err := imap.Wait(client.UIDFetch(seq, "INTERNALDATE", "BODY[]", "UID", "RFC822.HEADER"))
	if err != nil {
		return emails, err
	}

	var email map[string]interface{}
	for _, msgData := range fCmd.Data {
		msgFields := msgData.MessageInfo().Attrs
		email, err = newEmailMessage(msgFields)
		if err != nil {
			return emails, err
		}
		emails = append(emails, email)

		// mark message as read
		fSeq := new(imap.SeqSet)
		fSeq.AddNum(imap.AsNumber(msgFields["UID"]))
		_, err = imap.Wait(client.UIDStore(fSeq, "+FLAGS", "\\SEEN"))
		if err != nil {
			return emails, err
		}
	}
	return emails, nil
}
開發者ID:hellcoderz,項目名稱:streamtools,代碼行數:36,代碼來源:fromEmail.go

示例2: FetchAllUIDs

func FetchAllUIDs(c *imap.Client) (uids []uint32, err error) {
	maxmessages := 150000
	uids = make([]uint32, maxmessages)

	set, errS := imap.NewSeqSet("1:*")
	if errS != nil {
		err = errS
		return
	}

	cmd, errF := c.UIDFetch(set, "RFC822.SIZE")
	if errF != nil {
		err = errF
		return
	}

	messagenum := uint32(0)
	for cmd.InProgress() {
		errC := c.Recv(-1)
		if errC != nil {
			continue
		}
		for _, rsp := range cmd.Data {
			uid := imap.AsNumber(rsp.MessageInfo().Attrs["UID"])
			uids[messagenum] = uid
		}
		cmd.Data = nil
		messagenum++
	}

	uids = uids[:messagenum]
	return
}
開發者ID:pruthvirajsinh,項目名稱:go-Simap,代碼行數:33,代碼來源:imap.go

示例3: FetchMessages

func FetchMessages(c *imap.Client, uidSet *imap.SeqSet) (fetched []MsgData, err error) {
	cmd, errF := c.UIDFetch(uidSet, "RFC822")
	if errF != nil {
		err = errF
		return
	}

	for cmd.InProgress() {
		errC := c.Recv(-1)
		if errC != nil {
			return
		}
		for _, rsp := range cmd.Data {

			uid := imap.AsNumber(rsp.MessageInfo().Attrs["UID"])
			mime := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822"])
			msg, errR := mail.ReadMessage(bytes.NewReader(mime))
			if errR != nil {
				continue
			}
			if msg != nil {
				msgdata := GetMessage(msg, uid)
				fetched = append(fetched, msgdata)
			}
		}
		cmd.Data = nil
	}

	return
}
開發者ID:pruthvirajsinh,項目名稱:go-Simap,代碼行數:30,代碼來源:imap.go

示例4: findUnreadEmails

// findUnreadEmails will run a find the UIDs of any unread emails in the
// mailbox.
func findUnreadEmails(conn *imap.Client) (*imap.Command, error) {
	// get headers and UID for UnSeen message in src inbox...
	cmd, err := imap.Wait(conn.UIDSearch("UNSEEN"))
	if err != nil {
		return &imap.Command{}, err
	}
	return cmd, nil
}
開發者ID:hellcoderz,項目名稱:streamtools,代碼行數:10,代碼來源:fromEmail.go

示例5: CheckAndAppendMessages

// checkAndStoreMessages will wait for WorkRequests to come acorss the pipe. When it receives a request, it will search
// the given destination inbox for the message. If it is not found, this method will attempt to pull the messages data
// from fetchRequests and then append it to the destination.
func CheckAndAppendMessages(dstConn *imap.Client, storeRequests chan WorkRequest, fetchRequests chan fetchRequest, wg *sync.WaitGroup) {
	defer wg.Done()

	// noop it every few to keep things alive
	timeout := time.NewTicker(NoopMinutes * time.Minute)
	done := false
	for {
		select {
		case request, ok := <-storeRequests:
			if !ok {
				done = true
				break
			}
			// search for in dst
			cmd, err := imap.Wait(dstConn.UIDSearch([]imap.Field{"HEADER", request.Header, request.Value}))
			if err != nil {
				log.Printf("Unable to search for message (%s): %s. skippin!", request.Value, err.Error())
				continue
			}

			results := cmd.Data[0].SearchResults()
			// if not found, PULL from SRC and STORE in DST
			if len(results) == 0 {
				// only fetch if we dont have data already
				if len(request.Msg.Body) == 0 {
					// build and send fetch request
					response := make(chan MessageData)
					fr := fetchRequest{MessageId: request.Value, UID: request.UID, Response: response}
					fetchRequests <- fr

					// grab response from fetchers
					request.Msg = <-response
				}
				if len(request.Msg.Body) == 0 {
					log.Printf("No data found for from fetch request (%s). giving up", request.Value)
					continue
				}

				err = AppendMessage(dstConn, request.Msg)
				if err != nil {
					log.Printf("Problems appending message to dst: %s. quitting.", err.Error())
					return
				}

			}

		case <-timeout.C:
			imap.Wait(dstConn.Noop())
		}

		if done {
			break
		}
	}

	log.Print("storer complete!")
	return
}
開發者ID:jprobinson,項目名稱:copycat-imap,代碼行數:61,代碼來源:store.go

示例6: fetchEmails

// FetchEmails will sit and wait for fetchRequests from the destination workers.
func fetchEmails(conn *imap.Client, requests chan fetchRequest, cache *Cache) {

	// noop every few to keep things alive
	timeout := time.NewTicker(NoopMinutes * time.Minute)
	done := false
	for {
		select {
		case request, ok := <-requests:
			if !ok {
				done = true
				break
			}
			found := true
			// check if the message body is in cache
			data, err := cache.Get(request.MessageId)
			if err != nil {
				found = false
				if err != ErrNotFound {
					log.Printf("problems pulling message data from cache: %s. Pulling message from src...", err.Error())
				}
				data = MessageData{}
			}

			if found {
				log.Print("cache success!")
				request.Response <- data
				continue
			}

			msgData, err := FetchMessage(conn, request.UID)
			if err != nil {
				if err == NotFound {
					log.Printf("No data found for UID: %d", request.UID)
				} else {
					log.Printf("Problems fetching message (%s) data: %s. Passing request and quitting.", request.MessageId, err.Error())
					requests <- request
					return
				}
			}
			request.Response <- msgData

			err = cache.Put(request.MessageId, msgData)
			if err != nil {
				log.Printf("Unable to add message (%s) to cache: %s", request.MessageId, err.Error())
			}

		case <-timeout.C:
			imap.Wait(conn.Noop())
		}

		if done {
			break
		}
	}

}
開發者ID:jprobinson,項目名稱:copycat-imap,代碼行數:57,代碼來源:store.go

示例7: ResetConnection

func ResetConnection(conn *imap.Client, readOnly bool) error {
	// dont check for error because its possible it's already closed.
	conn.Close(!readOnly)

	_, err := imap.Wait(conn.Select("INBOX", readOnly))
	if err != nil {
		return err
	}

	return nil
}
開發者ID:jprobinson,項目名稱:copycat-imap,代碼行數:11,代碼來源:copycat.go

示例8: GetAllMessages

func GetAllMessages(conn *imap.Client) (*imap.Command, error) {
	// get headers and UID for ALL message in src inbox...
	allMsgs, _ := imap.NewSeqSet("")
	allMsgs.Add("1:*")
	cmd, err := imap.Wait(conn.Fetch(allMsgs, "RFC822.HEADER", "UID"))
	if err != nil {
		return &imap.Command{}, err
	}

	return cmd, nil
}
開發者ID:jprobinson,項目名稱:copycat-imap,代碼行數:11,代碼來源:copycat.go

示例9: SearchUIDs

func SearchUIDs(c *imap.Client, query string) (uids []uint32, err error) {
	cmd, err := c.UIDSearch("X-GM-RAW", fmt.Sprint("\"", query, "\""))

	for cmd.InProgress() {
		c.Recv(-1)
		for _, rsp := range cmd.Data {
			uids = rsp.SearchResults()
		}
		cmd.Data = nil
	}
	return
}
開發者ID:sqs,項目名稱:go-synco,代碼行數:12,代碼來源:imap.go

示例10: FetchMessages

func FetchMessages(c *imap.Client, uidSet *imap.SeqSet) (err error) {
	cmd, err := c.UIDFetch(uidSet, "RFC822")

	for cmd.InProgress() {
		c.Recv(-1)
		for _, rsp := range cmd.Data {
			uid := imap.AsNumber(rsp.MessageInfo().Attrs["UID"])
			mime := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822"])
			if msg, _ := mail.ReadMessage(bytes.NewReader(mime)); msg != nil {
				PrintMessageAsJSON(msg, uid)
			}
		}
		cmd.Data = nil
	}

	return
}
開發者ID:sqs,項目名稱:go-synco,代碼行數:17,代碼來源:imap.go

示例11: checkMessagesExist

func checkMessagesExist(srcConn *imap.Client, checkRequests chan checkExistsRequest, wg *sync.WaitGroup) {
	defer wg.Done()
	// get memcache client
	cache := memcache.New(MemcacheServer)

	timeout := time.NewTicker(NoopMinutes * time.Minute)
	done := false
	for {
		select {
		case request, ok := <-checkRequests:
			if !ok {
				done = true
				break
			}
			// check if it exists in src
			// search for in src
			cmd, err := imap.Wait(srcConn.UIDSearch([]imap.Field{"HEADER", "Message-Id", request.MessageId}))
			if err != nil {
				log.Printf("Unable to search source: %s", err.Error())
				request.Response <- true
				continue
			}

			results := cmd.Data[0].SearchResults()
			// if not found, mark for deletion in DST
			found := (len(results) > 0)

			// response with found bool
			request.Response <- found

			// if it doesnt exist, attempt to remove it from memcached
			if !found {
				cache.Delete(request.MessageId)
			}
		case <-timeout.C:
			imap.Wait(srcConn.Noop())
		}

		if done {
			break
		}
	}

}
開發者ID:jprobinson,項目名稱:copycat-imap,代碼行數:44,代碼來源:purge.go

示例12: FetchMessage

func FetchMessage(conn *imap.Client, messageUID uint32) (msg MessageData, err error) {
	seq, _ := imap.NewSeqSet("")
	seq.AddNum(messageUID)
	var cmd *imap.Command
	cmd, err = imap.Wait(conn.UIDFetch(seq, "INTERNALDATE", "BODY[]", "UID", "RFC822.HEADER"))
	if err != nil {
		log.Printf("Unable to fetch message (%d): %s", messageUID, err.Error())
		return
	}

	if len(cmd.Data) == 0 {
		log.Printf("Unable to fetch message (%d) from src: NO DATA", messageUID)
		return msg, NotFound
	}

	msgFields := cmd.Data[0].MessageInfo().Attrs
	msg = MessageData{InternalDate: imap.AsDateTime(msgFields["INTERNALDATE"]), Body: imap.AsBytes(msgFields["BODY[]"])}
	return msg, nil
}
開發者ID:jprobinson,項目名稱:copycat-imap,代碼行數:19,代碼來源:copycat.go

示例13: checkAndPurgeMessages

func checkAndPurgeMessages(conn *imap.Client, requests chan WorkRequest, checkRequests chan checkExistsRequest, wg *sync.WaitGroup) {
	defer wg.Done()

	timeout := time.NewTicker(NoopMinutes * time.Minute)
	done := false
	for {
		select {
		case request, ok := <-requests:
			if !ok {
				done = true
				break
			}
			// check and wait for response
			response := make(chan bool)
			cr := checkExistsRequest{UID: request.UID, MessageId: request.Value, Response: response}
			checkRequests <- cr

			// if response is false (does not exist), flag as Deleted
			if exists := <-response; !exists {
				log.Printf("not found in src. marking for deletion: %s", request.Value)
				err := AddDeletedFlag(conn, request.UID)
				if err != nil {
					log.Printf("Problems removing message from dst: %s", err.Error())
				}
			}
		case <-timeout.C:
			imap.Wait(conn.Noop())
		}

		if done {
			break
		}
	}

	log.Printf("expunging...")
	// expunge at the end
	allMsgs, _ := imap.NewSeqSet("")
	allMsgs.Add("1:*")
	imap.Wait(conn.Expunge(allMsgs))
	log.Printf("expunge complete.")
}
開發者ID:jprobinson,項目名稱:copycat-imap,代碼行數:41,代碼來源:purge.go

示例14: GetUIDs

func GetUIDs(mbox string, client *imap.Client) ([]uint32, error) {

	uids := make([]uint32, 0)

	cmd, err := client.Select(mbox, true)
	if err != nil {
		return uids, err
	}

	//== Get UIDS of all messages
	cmd, err = imap.Wait(client.UIDSearch("", "ALL"))
	if err != nil {
		return uids, err
	}

	for idx := range cmd.Data {
		for _, uid := range cmd.Data[idx].SearchResults() {
			uids = append(uids, uid)
		}
	}
	return uids, nil

}
開發者ID:daffodil,項目名稱:go-mailkit,代碼行數:23,代碼來源:mailbox.go

示例15: getNextUID

// getNextUID will grab the next message UID from the inbox. Client.Mailbox.UIDNext is cached so we can't use it.
func getNextUID(conn *imap.Client) (uint32, error) {
	cmd, err := imap.Wait(conn.Status("INBOX", "UIDNEXT"))
	if err != nil {
		return 0, err
	}

	if len(cmd.Data) == 0 {
		return 0, errors.New("no data returned!")
	}

	var status *imap.MailboxStatus
	for _, resp := range cmd.Data {
		switch resp.Type {
		case imap.Data:
			status = resp.MailboxStatus()
			if status != nil {
				break
			}
		}
	}

	return status.UIDNext, nil
}
開發者ID:jprobinson,項目名稱:copycat-imap,代碼行數:24,代碼來源:idle.go


注:本文中的code/google/com/p/go-imap/go1/imap.Client類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。