forked from autocrypt/ui-autocrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocrypt.js
146 lines (121 loc) · 3.21 KB
/
autocrypt.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
// javascript implementation of essential Autocrypt UI
provider = localStorageProvider;
user = 'User';
// setup during initialization
ui = {};
// client state for all clients is stored here:
storage = {};
// messages for the current user
messages = [];
// autocrypt state for the current user
autocrypt = {};
replying_to = undefined;
setup_page = function() {
ui = userInterface();
ui.setup();
adduser('Alice', 'green');
adduser('Bob', 'darkorange');
switchuser(Object.keys(storage)[0]);
ui.pane('list');
ui.update_description();
};
autocrypt_switch = function(enabled) {
autocrypt['enabled'] = enabled;
if (enabled) {
if (autocrypt['key'] === undefined)
autocrypt['key'] = String(Math.random());
}
self_sync_autocrypt_state();
};
self_sync_autocrypt_state = function() {
if (autocrypt['enabled']) {
autocrypt['state'][user] = {
'date': new Date(),
'key': autocrypt['key'],
'prefer-encrypted': autocrypt['prefer-encrypted']
};
} else {
autocrypt['state'][user] = {
'date': new Date()
};
}
};
changeuser = function() {
names = Object.keys(storage);
index = -1;
for (var x in names) {
if (names[x] == user) {
index = x;
}
}
newindex = (Number(index) + 1) % (names.length);
switchuser(names[newindex]);
return false;
};
switchuser = function(name) {
user = name;
autocrypt = storage[name]['autocrypt']
messages = [];
provider.reload(name)
ui.switchuser(name)
};
adduser = function(username, color) {
lc = username.toLowerCase();
if (storage[lc] == undefined) {
storage[lc] = {
'name': username,
'color': color,
'autocrypt': {
'enabled': false,
'state': {}
}
};
}
};
autocryptheader = function() {
if (autocrypt['enabled'] == false)
return undefined;
return { 'key': autocrypt['key'],
'prefer-encrypted': autocrypt['prefer-encrypted']
};
};
indent = function(str) {
return str.split('\n').map(function(y) { return "> " + y; }).join('\n');
};
addmail = function(to, subj, body, encrypted) {
var msg = { 'from': storage[user]['name'],
'to': to,
'subject': subj,
'body': body,
'encrypted': encrypted,
'autocrypt': autocryptheader(),
'date': new Date()
};
provider.send(msg);
return true;
};
provider.receive = function(msg) {
acupdate(msg)
messages.push(msg);
};
getacforpeer = function(peer) {
var ac = autocrypt['state'][peer.toLowerCase()];
if (ac === undefined)
ac = { 'date': new Date("1970") };
return ac;
};
acupdate = function(msg) {
var peer = msg['from'];
var ac = getacforpeer(peer);
var newac = {
'date': msg['date']
};
if (msg['autocrypt'] === undefined) {
} else {
newac['prefer-encrypted'] = msg['autocrypt']['prefer-encrypted'];
newac['key'] = msg['autocrypt']['key'];
};
if (ac['date'].getTime() < newac['date'].getTime()) {
autocrypt['state'][peer.toLowerCase()] = newac;
}
};