-
Notifications
You must be signed in to change notification settings - Fork 4
/
command.go
64 lines (51 loc) · 1.62 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2016 Daniel Krawisz.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package user
import (
"strings"
"github.com/DanielKrawisz/bmagent/cmd"
"github.com/DanielKrawisz/bmagent/idmgr/keys"
"github.com/DanielKrawisz/bmutil"
"github.com/DanielKrawisz/bmutil/identity"
"github.com/jordwest/imap-server/types"
)
// PrivateIDToPublicID converts a PrivateID as returned from the key magager
// to a PublicID as expected by the command system.
func PrivateIDToPublicID(pi *keys.PrivateID) cmd.PublicID {
return cmd.PublicID{
ID: pi.Private.Public(),
Label: pi.Name,
}
}
// executeCommand executes an external command puts the result in
// our inbox as an email.
func (u *User) executeCommand(from, name, params string) error {
// Get the inbox.
commandFolder := u.boxes[CommandsFolderName]
bm := cmd.EmailCommand(u, from, "command@bm.agent", name, strings.Fields(params))
// Email ourselves the response.
err := commandFolder.AddNew(bm, types.FlagRecent)
if err != nil {
return err
}
return nil
}
// NewAddress creates n new keys for the user.
func (u *User) NewAddress(tag string, sendAck bool) cmd.PublicID {
var behavior uint32
if sendAck {
behavior = identity.BehaviorAck
}
// first generate the new keys.
return PrivateIDToPublicID(u.keys.NewUnnamed(bmutil.DefaultStream, behavior))
}
// ListAddresses lists all available addresses.
func (u *User) ListAddresses() []cmd.PublicID {
names := u.keys.Names()
pi := make([]cmd.PublicID, 0, len(names))
for addr := range u.keys.Names() {
pi = append(pi, PrivateIDToPublicID(u.keys.Get(addr)))
}
return pi
}