This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
clients.js
194 lines (174 loc) · 5.08 KB
/
clients.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Clients - keeping track of the client state for different users
*
* In the long run we will have multiple clients per user.
*
* For now this only keeps track of the autocrypt state on the client. We
* might want to expand this to mails on the client etc.
*
* */
if (!atc) var atc = {}
if (!atc.setup) atc.setup = {}
atc.setup.clients = function () {
var storage = {}
function get (user, device) {
id = user + '/' + device
if (storage[id] === undefined) {
storage[id] = {
id: id,
address: user,
enabled: false,
state: {}
}
}
var autocrypt = storage[id]
function prepareOutgoing(msg) {
var peer = getPeerAc(msg.to)
msg.autocrypt = makeHeader()
if (msg.encrypted) {
msg.encryptedTo = [
autocrypt.key,
peer.key
]
}
}
function decryptMessage(msg) {
if (!msg.encrypted) { return }
if (msg.encryptedTo.indexOf(autocrypt.key) == -1) {
msg.body = 'The message could not be decrypted.'
msg.unreadable = true
}
}
function importSecretKey () {
autocrypt.enabled = true
autocrypt.key = autocrypt.state[autocrypt.address].key
autocrypt.preferEncrypted = autocrypt.state[autocrypt.address].preferEncrypted
}
function makeHeader () {
if (autocrypt.enabled === false) { return undefined }
return { 'key': autocrypt.key,
'preferEncrypted': autocrypt.preferEncrypted
}
}
function getPeerAc (peer) {
var ac = autocrypt.state[peer.toLowerCase()]
ac = ac || { 'date': new Date('1970') }
return ac
}
function processIncoming (msg) {
var peer = msg.from
var ac = getPeerAc(peer)
var newac = {
'date': msg.date
}
if ((msg.from === msg.to) && msg.setupMessage) {
autocrypt.receivedSetupMail = true
}
if (msg.autocrypt === undefined) {
// TODO remove
} else {
newac.preferEncrypted = msg['autocrypt']['preferEncrypted']
newac.key = msg['autocrypt']['key']
}
if (ac.date.getTime() < newac.date.getTime()) {
autocrypt.state[peer.toLowerCase()] = newac
}
}
function hasSetupMail () {
return autocrypt.receivedSetupMail
}
function selfSyncAutocryptState () {
if (autocrypt.enabled) {
autocrypt.state[autocrypt.address] = {
date: new Date(),
key: autocrypt.key,
preferEncrypted: autocrypt.preferEncrypted
}
} else {
autocrypt.state[autocrypt.address] = {
'date': new Date()
}
}
}
function enable (enableNow) {
autocrypt.enabled = enableNow
if (enableNow) {
autocrypt.key = autocrypt.key || String(Math.random())
}
selfSyncAutocryptState()
}
function prefer (preferNow) {
if (preferNow === undefined) {
delete autocrypt.preferEncrypted
}
else {
autocrypt.preferEncrypted = prefer
}
selfSyncAutocryptState()
}
function isEnabled () {
return autocrypt.enabled
}
function preferEncrypted () {
return (autocrypt.preferEncrypted === true)
}
function preferUnencrypted () {
return (autocrypt.preferEncrypted === false)
}
function encryptOptionTo (recipient) {
var peer = getPeerAc(recipient)
function explanation () {
if (isEnabled()) {
if (peer.key) { return }
if (recipient === '') {
return 'please choose a recipient'
}
return 'If you want to encrypt to ' + recipient +
', ask ' + recipient +
' to enable Autocrypt and send you an e-mail'
}
if (peer.preferEncrypted) {
return 'enable Autocrypt to encrypt'
}
}
return {
visible: isEnabled() || peer.preferEncrypted,
enabled: (isEnabled() && peer.key) || (peer.key && peer.preferEncrypted),
preferred: isEnabled() && peer.key && peer.preferEncrypted,
explanation: explanation() || ''
}
}
// returns the explanation for the encrypt toggle during composition
function explain(enc) {
var to = enc.to
var disabled = enc.disabled
var peer = getPeerAc(to)
if (!isEnabled() && !enc.disabled) {
return('enable Autocrypt to encrypt')
}
if (enc.checked && peer.preferEncrypted === false) {
return(to + ' prefers to receive unencrypted mail. ' +
'It might be hard for them to read.')
}
if (!enc.checked && peer.preferEncrypted === true) {
return(to + ' prefers to receive encrypted mail!')
}
}
return {
processIncoming: processIncoming,
hasSetupMail: hasSetupMail,
prepareOutgoing: prepareOutgoing,
decryptMessage: decryptMessage,
importSecretKey: importSecretKey,
explain: explain,
enable: enable,
prefer: prefer,
isEnabled: isEnabled,
preferEncrypted: preferEncrypted,
preferUnencrypted: preferUnencrypted,
encryptOptionTo: encryptOptionTo
}
}
return {
get: get
}
}