Debian Codegraph @

contact_types.go

raw
1 package proton
2
3 import (
4 "encoding/base64"
5 "strconv"
6 "strings"
7
8 "github.com/ProtonMail/gluon/rfc822"
9 "github.com/ProtonMail/gopenpgp/v2/crypto"
10 "github.com/emersion/go-vcard"
11 )
12
13 type RecipientType int
14
15 const (
16 RecipientTypeInternal RecipientType = iota + 1
17 RecipientTypeExternal
18 )
19
20 type ContactSettings struct {
21 MIMEType *rfc822.MIMEType
22 Scheme *EncryptionScheme
23 Sign *bool
24 Encrypt *bool
25 Keys []*crypto.Key
26 }
27
28 type Contact struct {
29 ContactMetadata
30 ContactCards
31 }
32
33 func (c *Contact) GetSettings(kr *crypto.KeyRing, email string) (ContactSettings, error) {
34 signedCard, ok := c.Cards.Get(CardTypeSigned)
35 if !ok {
36 return ContactSettings{}, nil
37 }
38
39 group, err := signedCard.GetGroup(kr, vcard.FieldEmail, email)
40 if err != nil {
41 return ContactSettings{}, nil
42 }
43
44 var settings ContactSettings
45
46 scheme, err := group.Get(FieldPMScheme)
47 if err != nil {
48 return ContactSettings{}, err
49 }
50
51 if len(scheme) > 0 {
52 switch scheme[0] {
53 case "pgp-inline":
54 settings.Scheme = newPtr(PGPInlineScheme)
55
56 case "pgp-mime":
57 settings.Scheme = newPtr(PGPMIMEScheme)
58 }
59 }
60
61 mimeType, err := group.Get(FieldPMMIMEType)
62 if err != nil {
63 return ContactSettings{}, err
64 }
65
66 if len(mimeType) > 0 {
67 settings.MIMEType = newPtr(rfc822.MIMEType(mimeType[0]))
68 }
69
70 sign, err := group.Get(FieldPMSign)
71 if err != nil {
72 return ContactSettings{}, err
73 }
74
75 if len(sign) > 0 {
76 sign, err := strconv.ParseBool(sign[0])
77 if err != nil {
78 return ContactSettings{}, err
79 }
80
81 settings.Sign = newPtr(sign)
82 }
83
84 encrypt, err := group.Get(FieldPMEncrypt)
85 if err != nil {
86 return ContactSettings{}, err
87 }
88
89 if len(encrypt) > 0 {
90 encrypt, err := strconv.ParseBool(encrypt[0])
91 if err != nil {
92 return ContactSettings{}, err
93 }
94
95 settings.Encrypt = newPtr(encrypt)
96 }
97
98 keys, err := group.Get(vcard.FieldKey)
99 if err != nil {
100 return ContactSettings{}, err
101 }
102
103 if len(keys) > 0 {
104 for _, key := range keys {
105 dec, err := base64.StdEncoding.DecodeString(strings.SplitN(key, ",", 2)[1])
106 if err != nil {
107 return ContactSettings{}, err
108 }
109
110 pubKey, err := crypto.NewKey(dec)
111 if err != nil {
112 return ContactSettings{}, err
113 }
114
115 settings.Keys = append(settings.Keys, pubKey)
116 }
117 }
118
119 return settings, nil
120 }
121
122 type ContactMetadata struct {
123 ID string
124 Name string
125 UID string
126 Size int64
127 CreateTime int64
128 ModifyTime int64
129 ContactEmails []ContactEmail
130 LabelIDs []string
131 }
132
133 type ContactCards struct {
134 Cards Cards
135 }
136
137 type ContactEmail struct {
138 ID string
139 Name string
140 Email string
141 Type []string
142 ContactID string
143 LabelIDs []string
144 }
145
146 type CreateContactsReq struct {
147 Contacts []ContactCards
148 Overwrite int
149 Labels int
150 }
151
152 type CreateContactsRes struct {
153 Index int
154
155 Response struct {
156 APIError
157 Contact Contact
158 }
159 }
160
161 type UpdateContactReq struct {
162 Cards Cards
163 }
164
165 type DeleteContactsReq struct {
166 IDs []string
167 }
168
169 func newPtr[T any](v T) *T {
170 return &v
171 }