func (m DummyMailbox) NewMessage() Message {
	return DummyMessage{
		sequenceNumber: 0,
		uid:            0,
		header:         make(textproto.MIMEHeader),
		internalDate:   time.Now(),
		flags:          types.Flags(0),
		mailstore:      m.mailstore,
		mailboxID:      m.ID,
		body:           "",
	}
}
示例#2
0
// Add a new message to a mailbox
func cmdAppend(args commandArgs, c *Conn) {
	if !c.assertAuthenticated(args.ID()) {
		return
	}

	mailboxName := args.Arg(appendArgMailbox)
	mailbox, err := c.User.MailboxByName(mailboxName)
	if err != nil {
		c.writeResponse(args.ID(), "NO could not get mailbox")
		return
	}

	length, err := strconv.ParseUint(args.Arg(appendArgLength), 10, 64)
	if err != nil || length == 0 {
		c.writeResponse(args.ID(), "BAD invalid length for message literal")
		return
	}

	flagString := args.Arg(appendArgFlags)
	flags := types.Flags(0)
	if flagString != "" {
		flags = types.FlagsFromString(flagString)
	}

	// Tell client to send the mail message
	c.writeResponse("+", "go ahead, feed me your message")

	// Read in the whole message
	messageData, err := c.ReadFixedLength(int(length))
	if err != nil {
		return
	}

	msg := mailbox.NewMessage()
	rawMsg, err := types.MessageFromBytes(messageData)
	if err != nil {
		c.writeResponse(args.ID(), "NO "+err.Error())
		return
	}
	msg = msg.SetHeaders(rawMsg.Headers)
	msg = msg.SetBody(rawMsg.Body)
	msg = msg.OverwriteFlags(flags)

	msg, err = msg.Save()
	if err != nil {
		c.writeResponse(args.ID(), "NO "+err.Error())
		return
	}

	c.writeResponse(args.ID(), "OK APPEND completed")
}
示例#3
0
// DecodeBitmessage takes the protobuf encoding of a Bitmessage and converts it
// back to a Bitmessage.
func DecodeBitmessage(data []byte) (*Bmail, error) {
	msg := &serialize.Message{}
	err := proto.Unmarshal(data, msg)
	if err != nil {
		return nil, err
	}

	var q format.Encoding
	switch msg.Encoding.Format {
	case 1:
		r := &format.Encoding1{}

		if msg.Encoding.Body == nil {
			return nil, errors.New("Body required in encoding format 1")
		}
		r.Body = string(msg.Encoding.Body)

		q = r
	case 2:
		r := &format.Encoding2{}

		if msg.Encoding.Subject == nil {
			r.Subject = ""
		} else {
			r.Subject = string(msg.Encoding.Subject)
		}

		if msg.Encoding.Body == nil {
			return nil, errors.New("Body required in encoding format 2")
		}
		r.Body = string(msg.Encoding.Body)

		q = r
	default:
		return nil, errors.New("Unsupported encoding")
	}

	l := &Bmail{}

	if msg.Expiration != "" {
		expr, _ := time.Parse(dateFormat, msg.Expiration)
		l.Expiration = expr
	}

	l.From = msg.From
	l.To = msg.To
	l.OfChannel = msg.OfChannel
	l.Ack = msg.Ack
	l.Content = q
	if msg.Object != nil {
		l.object, err = obj.ReadObject(msg.Object)
		if err != nil {
			return nil, err
		}
	}

	if msg.ImapData != nil {
		timeReceived, err := time.Parse(dateFormat, msg.ImapData.TimeReceived)
		if err != nil {
			return nil, err
		}

		l.ImapData = &ImapData{
			Flags:        types.Flags(msg.ImapData.Flags),
			TimeReceived: timeReceived,
		}
	}

	if msg.State != nil {
		lastSend, err := time.Parse(dateFormat, msg.State.LastSend)
		if err != nil {
			return nil, err
		}

		l.state = &MessageState{
			PubkeyRequestOutstanding: msg.State.PubkeyRequested,
			SendTries:                msg.State.SendTries,
			LastSend:                 lastSend,
			AckExpected:              msg.State.AckExpected,
			AckReceived:              msg.State.AckReceived,
			Received:                 msg.State.Received,
		}
	}

	return l, nil
}