-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
295 lines (290 loc) · 9.29 KB
/
index.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
let axios = require('axios');
const xmlBuilder = require('xmlbuilder2');
const crypto = require('crypto');
module.exports = (options) => ({
mail: {
authenticate: async () => {
let {data} = await axios.post(`${options.mail.apiUrl}/authenticate`, {
credentials: options.mail.credentials
})
return data && data.success;
},
getDomain: async (domain) => {
let {data} = await axios.post(`${options.mail.apiUrl}/get_domain`, {
credentials: options.mail.credentials,
domain
})
return data && data.success && data;
},
addDomain: async (domain) => {
let {data} = await axios.post(`${options.mail.apiUrl}/change_domain`, {
credentials: options.mail.credentials,
domain,
attributes: {},
create_only: true
})
if(!data || !data.success) console.log(data && data.success || 'Unknown error enabling email')
return data && data.success;
},
searchUsers: async (domain) => {
let {data} = await axios.post(`${options.mail.apiUrl}/search_users`, {
credentials: options.mail.credentials,
criteria:{domain}
})
return data && data.success && data;
},
/*
changeUser will create a new user or edit an existing one
type - Can be mailbox, forward, or filter
password - send as plain text
*/
changeUser: async (user, attributes) => {
let {data} = await axios.post(`${options.mail.apiUrl}/change_user`, {
credentials: options.mail.credentials,
user,
attributes
})
if(!data || !data.success)
console.error(data)
return data && data.success;
},
deleteUser: async (user) => {
let {data} = await axios.post(`${options.mail.apiUrl}/delete_user`, {
credentials: options.mail.credentials,
user
})
if(!data || !data.success)
console.error(data)
return data && data.success;
},
},
domains: {
getDomainsContacts: async (domains) => {
if(!domains) throw new Error('Domains parameter required')
if(typeof domains == 'string')
domains = [domains]
let xml = buildXmlRequest({
dt_assoc: {
item: [
{'@key': 'protocol', '#': 'XCP'},
{'@key': 'object', '#': 'DOMAIN'},
{'@key': 'action', '#': 'GET_DOMAINS_CONTACTS'},
{'@key': 'attributes', '#': {
dt_assoc: {
item: {'@key': 'domain_list', '#': {
dt_array: {
item: domains.map((domain, i) => ({'@key': i, '#': domain}))
}
}}
}
}}
]
}
});
let {data} = await axios.post(options.domains.apiUrl, xml, {
headers: {
'Content-Type': 'text/xml',
'X-Username': options.domains.username,
'X-Signature': getSignature(xml, options.domains.apiKey),
'Content-Length': xml.length
}
})
let contacts = parseXml(data);
return contacts.body.attributes
},
updateContacts: async (params) => {
if(!params)
throw new Error('Missing required paramaters for updateContacts()')
let xml = buildXmlRequest({
dt_assoc: {
item: [
{'@key': 'protocol', '#': 'XCP'},
{'@key': 'object', '#': 'DOMAIN'},
{'@key': 'action', '#': 'UPDATE_CONTACTS'},
{'@key': 'attributes', '#': {
dt_assoc: {
item: [
{'@key': 'domain', '#': params.domain},
Object.keys(params).filter((key) => ['affect_domains','report_email', 'domains'].includes(key)).map((key) => ({
'@key': key,
'#': params[key]
})),
{'@key': 'types', '#': {
dt_array: {
item: (params.types || Object.keys(params.contact_set)).map((type, i) => ({'@key': i, '#': type}))
}
}},
{'@key': 'contact_set', '#': {
dt_assoc: {
item: Object.keys(params.contact_set).map((type) => ({
'@key': type,
'#': {
dt_assoc: {
item: Object.keys(params.contact_set[type]).map((prop) => ({
'@key': prop,
'#': params.contact_set[type][prop]
}))
}
}
}))
}
}}
]
}
}}
]
}
})
let {data} = await axios.post(options.domains.apiUrl, xml, {
headers: {
'Content-Type': 'text/xml',
'X-Username': options.domains.username,
'X-Signature': getSignature(xml, options.domains.apiKey),
'Content-Length': xml.length
}
})
},
getPrice: async (params) => {
if(!params || !params.domain)
throw new Error('Missing required paramaters for getPrice()')
let xml = buildXmlRequest({
dt_assoc: {
item: [
{'@key': 'protocol', '#': 'XCP'},
{'@key': 'object', '#': 'DOMAIN'},
{'@key': 'action', '#': 'GET_PRICE'},
{'@key': 'attributes', '#': {
dt_assoc: {
item: [
Object.keys(params).map((key) => ({
'@key': key,
'#': params[key]
}))
]
}
}}
]
}
})
let {data} = await axios.post(options.domains.apiUrl, xml, {
headers: {
'Content-Type': 'text/xml',
'X-Username': options.domains.username,
'X-Signature': getSignature(xml, options.domains.apiKey),
'Content-Length': xml.length
}
})
let price = parseXml(data);
return price.body.attributes
}
},
events: {
poll: async (limit) => {
let xml = buildXmlRequest({
dt_assoc: {
item: [
{'@key': 'protocol', '#': 'XCP'},
{'@key': 'object', '#': 'EVENT'},
{'@key': 'action', '#': 'POLL'},
{'@key': 'attributes', '#': {
dt_assoc: {
item: {'@key': 'limit', '#': limit || 1}
}
}}
]
}
})
let {data} = await axios.post(options.domains.apiUrl, xml, {
headers: {
'Content-Type': 'text/xml',
'X-Username': options.domains.username,
'X-Signature': getSignature(xml, options.domains.apiKey),
'Content-Length': xml.length
}
})
let e = parseXml(data).body.attributes;
if(!e.events)
return [];
// If only one event returned. Otherwise it will look like: {total: 2, events: {0: ..., 1: ...}}
if(e.events.object)
return [e.events]
return Object.keys(e.events).map(k => e.events[k])
},
ack: async (eventId) => {
if(!eventId) throw new Error('eventId must be defined');
let xml = buildXmlRequest({
dt_assoc: {
item: [
{'@key': 'protocol', '#': 'XCP'},
{'@key': 'object', '#': 'EVENT'},
{'@key': 'action', '#': 'ACK'},
{'@key': 'attributes', '#': {
dt_assoc: {
item: {'@key': 'event_id', '#': eventId}
}
}}
]
}
})
let {data} = await axios.post(options.domains.apiUrl, xml, {
headers: {
'Content-Type': 'text/xml',
'X-Username': options.domains.username,
'X-Signature': getSignature(xml, options.domains.apiKey),
'Content-Length': xml.length
}
});
}
}
})
function getSignature(xml, key){
let signature = crypto.createHash('md5').update(xml + key).digest("hex")
return crypto.createHash('md5').update(signature + key).digest("hex")
}
function buildXmlRequest(data_block){
return xmlBuilder.create({
encoding: 'UTF-8',
standalone: true
}).ele({
OPS_envelope: {
header: { version: '0.9' },
body: { data_block }
}
}).dtd({ sysID: 'ops.dtd' }).end({ prettyPrint: true });
}
function parseXml(data){
if(typeof data == 'string')
data = xmlBuilder.create(data).end({ format: 'object' })
if(data instanceof Array){
let obj = {}
data.map((item) => parseXml(item)).forEach((item => obj = Object.assign(obj, item)))
return obj;
}
if(data.hasOwnProperty('@key') && data.hasOwnProperty('#')){
if(typeof data['#'] == 'string')
return {[data['@key']]: data['#']}
return {[data['@key']]: parseXml(data['#'])}
}
let out = {}
for(let key in data){
if(key == '@key') continue;
if(typeof data[key] == 'string')
out[key] = data[key]
else
out[key] = parseXml(data[key])
}
if(data.hasOwnProperty('@key'))
return {[data['@key']]: out}
return flattenObject(out)
}
function flattenObject(object){
if(!object || typeof object !== 'object') return object;
if(Object.keys(object).length == 0)
return null;
if(Object.keys(object).length == 1)
return flattenObject(object[Object.keys(object).pop()])
let flat = {}
for(let key in object)
flat[key] = flattenObject(object[key])
return flat
}