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


Golang mime.WordDecoder類代碼示例

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


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

示例1: getSubject

func getSubject(msg *mail.Message) (string, error) {
	var dec mime.WordDecoder
	ret, err := dec.DecodeHeader(msg.Header.Get("Subject"))
	if err != nil {
		return "", err
	}
	return ret, nil
}
開發者ID:koffeinsource,項目名稱:kaffeeshare,代碼行數:8,代碼來源:subject.go

示例2: DecodeHeader

// DecodeHeader (per RFC 2047) using Golang's mime.WordDecoder
func DecodeHeader(input string) string {
	if !strings.Contains(input, "=?") {
		// Don't scan if there is nothing to do here
		return input
	}

	dec := new(mime.WordDecoder)
	dec.CharsetReader = NewCharsetReader
	header, err := dec.DecodeHeader(input)
	if err != nil {
		return input
	}
	return header
}
開發者ID:davrux,項目名稱:go.enmime,代碼行數:15,代碼來源:header.go

示例3: NonASCII

// NonASCII Decode non ASCII header string RFC 1342
func NonASCII(encoded string) string {

	regexRFC1342, _ := regexp.Compile(`=\?.*?\?=`)
	dec := new(mime.WordDecoder)
	dec.CharsetReader = charset.NewReader

	result := regexRFC1342.ReplaceAllStringFunc(encoded, func(encoded string) string {
		decoded, err := dec.Decode(encoded)
		if err != nil {
			log.Println("Error decode NonASCII", encoded, err)
			return encoded
		}
		return decoded
	})

	return result
}
開發者ID:rodrigocorsi2,項目名稱:mattermail,代碼行數:18,代碼來源:mattermail.go

示例4: parseAndDecodeHeader

func parseAndDecodeHeader(rawMimeHeader textproto.MIMEHeader, target string,
	mHeader PMIMEHeader) string {
	if targetArray, ok := rawMimeHeader[target]; !ok && len(targetArray) == 0 {
		return ""
	} else {
		var (
			encodedValue string = strings.TrimPrefix(targetArray[0], " ")
			decoded      string
			dec          *mime.WordDecoder = new(mime.WordDecoder)
			err          error
		)
		if decoded, err = dec.DecodeHeader(encodedValue); err != nil {
			fmt.Printf("[watney] WARNING: Couldn't decode string: \n\t%s\n\t%s\n", encodedValue,
				err.Error())
			return encodedValue
		}
		return decoded
	}
}
開發者ID:mdrobek,項目名稱:watney,代碼行數:19,代碼來源:mail_parse.go

示例5: ExampleWordDecoder_Decode

func ExampleWordDecoder_Decode() {
	dec := new(mime.WordDecoder)
	header, err := dec.Decode("=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=")
	if err != nil {
		panic(err)
	}
	fmt.Println(header)

	dec.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
		switch charset {
		case "x-case":
			// Fake character set for example.
			// Real use would integrate with packages such
			// as code.google.com/p/go-charset
			content, err := ioutil.ReadAll(input)
			if err != nil {
				return nil, err
			}
			return bytes.NewReader(bytes.ToUpper(content)), nil
		default:
			return nil, fmt.Errorf("unhandled charset %q", charset)
		}
	}
	header, err = dec.Decode("=?x-case?q?hello!?=")
	if err != nil {
		panic(err)
	}
	fmt.Println(header)
	// Output:
	// ¡Hola, señor!
	// HELLO!
}
開發者ID:RajibTheKing,項目名稱:gcc,代碼行數:32,代碼來源:example_test.go

示例6: WordDecoder

func WordDecoder() *mime.WordDecoder {
	decoder := new(mime.WordDecoder)
	decoder.CharsetReader = CharsetReader
	return decoder
}
開發者ID:dronfree,項目名稱:mml,代碼行數:5,代碼來源:mailbox.go

示例7: Parse

// Parse parses a MIME encoded message.
func Parse(r io.Reader) (
	header *Header,
	subject string,
	message string,
	attachments []*Attachment,
	err error,
) {
	var h Header
	// read message
	msg, err := mail.ReadMessage(r)
	if err != nil {
		return nil, "", "", nil, log.Error(err)
	}
	// parse 'From'
	h.From = msg.Header.Get("From")
	if h.From == "" {
		return nil, "", "", nil, log.Error("mime: 'From' not defined")
	}
	// parse 'To'
	h.To = msg.Header.Get("To")
	if h.To == "" {
		return nil, "", "", nil, log.Error("mime: 'To' not defined")
	}
	// parse 'Cc'
	addressList, err := msg.Header.AddressList("Cc")
	if err != nil && err != mail.ErrHeaderNotPresent {
		return nil, "", "", nil, log.Error(err)
	}
	if err != mail.ErrHeaderNotPresent {
		for _, address := range addressList {
			h.Cc = append(h.Cc, address.Address)
		}
	}
	// parse subject
	subj := msg.Header.Get("Subject")
	if subj == "" {
		return nil, "", "", nil, log.Error("mime: 'Subject' not defined")
	}
	dec := new(mime.WordDecoder)
	subject, err = dec.DecodeHeader(subj)
	if err != nil {
		return nil, "", "", nil, log.Error(err)
	}
	// parse 'Message-ID'
	h.MessageID = msg.Header.Get("Message-ID")
	if h.MessageID == "" {
		return nil, "", "", nil, log.Error("mime: 'Message-ID' not defined")
	}
	// parse 'In-Reply-To'
	h.InReplyTo = msg.Header.Get("In-Reply-To")
	// parse 'MIME-Version'
	if msg.Header.Get("MIME-Version") != "1.0" {
		return nil, "", "", nil, log.Error("mime: wrong 'MIME-Version' header")
	}
	// parse 'Content-Type'
	mediaType, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type"))
	if err != nil {
		return nil, "", "", nil, log.Error(err)
	} else if mediaType != "multipart/mixed" {
		return nil, "", "", nil, log.Error("mime: wrong 'Content-Type' header ")
	}
	// read first MIME part (message)
	mr := multipart.NewReader(msg.Body, params["boundary"])
	p, err := mr.NextPart()
	if err != nil {
		return nil, "", "", nil, log.Error(err)
	}
	// check 'Content-Type'
	if p.Header.Get("Content-Type") != "text/plain" {
		return nil, "", "", nil,
			log.Error("mime: expected 'text/plain' Content-Type")
	}
	// check 'Content-Transfer-Encoding'
	if p.Header.Get("Content-Transfer-Encoding") != "base64" {
		return nil, "", "", nil,
			log.Error("mime: expected 'base64' Content-Transfer-Encoding")
	}
	// read message
	enc, err := ioutil.ReadAll(p)
	if err != nil {
		return nil, "", "", nil, log.Error(err)
	}
	content, err := base64.Decode(string(enc))
	if err != nil {
		return nil, "", "", nil, log.Error(err)
	}
	message = string(content)
	// read optional additional MIME parts (attachments)
	for {
		p, err := mr.NextPart()
		if err == io.EOF {
			break
		}
		// parse header
		contentType := p.Header.Get("Content-Type")
		if contentType == "" {
			return nil, "", "", nil,
				log.Error("mime: Content-Type undefined for attachment")
		}
//.........這裏部分代碼省略.........
開發者ID:JonathanLogan,項目名稱:mute,代碼行數:101,代碼來源:mime.go


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