// 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 }
// importKeyfile is used to import a keys.dat file from PyBitmessage. It adds // private keys to the key manager. func importKeyfile(kmgr *keymgr.Manager, file string) error { b, err := ioutil.ReadFile(file) if err != nil { return err } keys := kmgr.ImportKeys(b) if keys == nil { return errors.New("Could not read file.") } for addr, name := range keys { fmt.Printf("Imported address %s %s\n", addr, name) } return nil }
func saveKeyfile(keys *keymgr.Manager, path string, pass []byte) { var serialized []byte var err error if pass == nil { serialized, err = keys.ExportPlaintext() } else { serialized, err = keys.ExportEncrypted(pass) } if err != nil { log.Criticalf("Failed to serialize key file: %v", err) return } err = ioutil.WriteFile(path, serialized, 0600) if err != nil { log.Criticalf("Failed to write key file: %v", err) } }
// 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 }