コード例 #1
0
ファイル: user.go プロジェクト: DanielKrawisz/bmagent
// NewUser creates a User object from the store.
func NewUser(username string, server ServerOps, keys *keymgr.Manager) (*User, error) {

	mboxes := server.Folders()

	if mboxes == nil {
		return nil, errors.New("Invalid user.")
	}

	u := &User{
		username: username,
		boxes:    make(map[string]*mailbox),
		server:   server,
		keys:     keys,
	}

	// The user is allowed to save in some mailboxes but not others.
	for _, mbox := range mboxes {
		var name = mbox.Name()
		var mb *mailbox
		var err error
		switch name {
		case DraftsFolderName:
			mb, err = newDrafts(mbox, keys.Names())
		default:
			mb, err = newMailbox(mbox, keys.Names())
		}
		if err != nil {
			return nil, err
		}
		u.boxes[name] = mb
	}

	return u, nil
}
コード例 #2
0
ファイル: imap.go プロジェクト: DanielKrawisz/bmagent
// InitializeUser initializes the store by creating the default mailboxes and
// inserting the welcome message.
func InitializeUser(u *store.UserData, keys *keymgr.Manager, GenKeys int16) error {

	// Create Inbox.
	mbox, err := u.NewFolder(InboxFolderName)
	if err != nil {
		return err
	}
	inbox, err := newMailbox(mbox, keys.Names())
	if err != nil {
		return err
	}

	_, err = u.NewFolder(OutboxFolderName)
	if err != nil {
		return err
	}
	_, err = u.NewFolder(SentFolderName)
	if err != nil {
		return err
	}
	_, err = u.NewFolder(LimboFolderName)
	if err != nil {
		return err
	}
	_, err = u.NewFolder(TrashFolderName)
	if err != nil {
		return err
	}
	_, err = u.NewFolder(CommandsFolderName)
	if err != nil {
		return err
	}
	_, err = u.NewFolder(DraftsFolderName)
	if err != nil {
		return err
	}

	// Determine how many new keys to produce. Default is 0, unless
	// the keymanager is empty, in which case it is 1.
	var genkeys uint16

	if GenKeys < 0 || keys.Size() == 0 {
		genkeys = 1
	} else {
		genkeys = uint16(GenKeys)
	}

	var i uint16
	for i = 0; i < genkeys; i++ {
		keys.NewHDIdentity(1, "")
	}

	// Get all keys from key manager.
	addresses := keys.Addresses()
	tags := keys.Names()

	// For each key, create a mailbox.
	var toAddr string
	keyList := ""

	for _, addr := range addresses {
		toAddr = addr
		var tag string
		if t, ok := tags[addr]; ok {
			tag = t
		}
		keyList = fmt.Sprint(keyList, fmt.Sprintf("\t%[email protected] %s\n", addr, tag))
	}

	welcome := fmt.Sprintf(welcomeMsg, keyList)

	// Add the introductory message.
	from := "*****@*****.**"
	subject := "Welcome to bmagent!"

	err = inbox.AddNew(&Bmail{
		From: from,
		To:   fmt.Sprintf("*****@*****.**", toAddr),
		Content: &format.Encoding2{
			Subject: subject,
			Body:    welcome,
		},
	}, types.FlagRecent)
	if err != nil {
		return err
	}

	return nil
}