-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
295 lines (275 loc) · 8.96 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
const axios = require('axios');
const querystring = require('querystring');
const fs = require('fs');
/**
* @version 0.2
* @author dunklesToast / Tom Sacher
* @licence MIT
*/
class IServTool {
/**
* Create a IServTool instance
* @constructor
* @param {String} ServerHost - the host of the IServ Instance. Without protocol
* @param {String} username - username used for login
* @param {String} password - password used for login
* @param {boolean} [keepalive] - Not yet implemented
* @param {boolean} [log] - Enable debug logging
* @param {boolean} [reuseCookies] - save cookies and reuse them. only works for one user
*/
constructor(ServerHost, username, password, keepalive, log = false, reuseCookies) {
this._host = ServerHost;
this._username = username;
this._password = password;
this._log = function (msg) {
if (log) console.log(msg);
};
this._keepalive = keepalive;
this._cookieHeader = null;
this.reuseCookies = reuseCookies;
const cookies = this._getPresavedHeaders();
this._axios = axios.create({
baseURL: `https://${ServerHost}`,
maxRedirects: 0,
validateStatus: function (status) {
return status >= 200 && status < 303;
},
headers: {
'Cookie': cookies || ''
},
});
}
/**
* Login
* @returns {Promise<void>}
*/
async login() {
if (this.reuseCookies && await this.isCookieValid()) {
this._log('[LOGIN] Skipping Login since Cookies already valid!');
return;
}
this._log('[LOGIN] Logging in...');
const form = querystring.stringify({
'_username': this._username,
'_password': this._password
});
this._log('[LOGIN] Built q-string');
const data = await this._axios({
data: form,
method: 'POST',
url: '/iserv/login_check',
});
this._axios = await axios.create({
baseURL: `https://${this._host}`,
headers: {
'Cookie': (data.headers["set-cookie"]) || '',
},
maxRedirects: 0,
validateStatus: function (status) {
return status >= 200 && status < 303;
}
});
await this._saveCookies((data.headers["set-cookie"]));
this._log('[LOGIN] Logged In')
}
/**
* Get all notifications from the Server for the logged in account
* @param {String} since - Date where the Server should start fetching
* @returns {Promise<Object>}
*/
async getNotifications(since) {
if(!since) throw new Error('No since given')
this._log('[GETNOTI] Getting Notifications since ' + since);
// Time Format looks like this
// 2018-12-29T23:21:00+01:00
const resp = await this._axios({
url: '/iserv/user/api/notifications?since=' + encodeURIComponent(since)
});
this._log('[GETNOTI] Got notificications');
return resp.data
}
/**
* Get all Mailfolders / Inboxes for current user
* @returns {Promise<Object>}
*/
async getMailFolders() {
const resp = await this._axios({
url: 'iserv/mail/api/folder/list'
});
return resp.data;
}
/**
* Get all Mails in INBOX
* @returns {Promise<Object>}
*/
async getUnreadMails() {
const resp = await this._axios({
url: 'iserv/mail/api/unread/inbox'
});
return resp.data;
}
/**
* Get all Messages for specified Inbox
* @param {String} [path="INBOX"]
* @param {(int|string)} [length=50] - Amount of Mails returned
* @param {(int|string)} [start=0] - Offset (50 for starting at 50. Mail)
* @param {string} [column="date"] - Set column for sorting
* @param {string} [dir="desc"] - Sorting direction (desc/asc)
* @returns {Promise<Object>}
*/
async getMessagesForInbox(path = 'INBOX', length = 50, start = 0, column = 'date', dir = 'desc') {
const resp = await this._axios({
url: `iserv/mail/api/message/list`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
params: {
path: path,
length: length,
start: start,
'order[column]': column,
'order[dir]': dir
}
});
return resp.data
}
/**
* Get all upcoming Events
* @param {boolean} [includeSubscriptions=true] - Include Subscriptions
* @param {(int|String)} [limit=14] - how many events to be returnes
* @returns {Promise<Object>}
*/
async getUpcomingEvents(includeSubscriptions = false, limit = 14) {
const url = "iserv/calendar/api/upcoming";
const resp = await this._axios({
url: 'iserv/calendar/api/upcoming',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
params: {
includeSubscriptions: includeSubscriptions,
limit: limit
}
});
return resp.data;
}
/**
* Get a users Profile Picture. Returns false if no image was found
* @param {String} user - Username you want the image from
* @param {(int|String)} [w=""] - Image width, leave blank for full size
* @param {(int|String)} [h=""] - Image height, leave blank for full size
* @returns {Promise<Object>}
*/
async getUserProfilePic(user, w = '', h = '') {
try {
const resp = await this._axios({
url: `iserv/addressbook/public/image/${user}/photo/${w}/${h}`,
responseType: 'arraybuffer'
});
return resp.data;
} catch (e) {
return false;
}
}
/**
* Get a Message (Mail) by ID
* @param {(int|String)} id - Message ID
* @param {String} [path="INBOX"] - Message Path (Inbox name)
* @returns {Promise<Object>}
*/
async getMessageByID(id, path = "INBOX") {
this._log(`[GMBID] Getting Message #${id} from "${path}"`)
if (!path) throw new Error('No message ID given');
const resp = await this._axios({
url: '/iserv/mail/api/message',
params: {
path: path,
msg: id
}
});
return resp.data
}
/**
* Quick user lookup - for autocompletion
* @param {String} query - Query
* @returns {Promise<Object>}
*/
async userLookup(query) {
if (!query) throw new Error('No query given to user lookup');
const resp = await this._axios({
url: 'iserv/addressbook/lookup',
params: {
query: query
}
});
return resp.data
}
/**
* Get Folder Tree (Files)
* @param {String} [subfolder=""] - ID to create tree. Leave blank for root
* @returns {Promise<Object>}
*/
async getFolderTree(subfolder = '') {
const resp = await this._axios({
url: 'iserv/file.json/' + subfolder
});
return resp.data;
}
/**
* Get all EventSources aka Calendars
* @returns {Promise<Object>}
*/
async getEventSources() {
const resp = await this._axios({
url: 'iserv/calendar/api/eventsources'
});
return resp.data
}
/**
* Get Events from Source
* @param {String} source - Path to source
* @param {String} start - Start date for query
* @param {String} end - End date for query
* @returns {Promise<Object>}
*/
async getEventsFromSource(source, start, end){
const resp = await this._axios({
url: 'iserv/calendar/api/feed',
params: {
cal: source,
start: start,
end: end
}
});
return resp.data;
}
_getPresavedHeaders() {
this._log('[CKS] Trying to load saved cookies');
if (this.reuseCookies && fs.existsSync('./headers')) {
this._log('[CKS] Found them Cookies');
return JSON.parse(fs.readFileSync('./headers').toString())
} else {
this._log('[CKS] Havent found any pre-saved headers');
}
}
async _saveCookies(h) {
if (this.reuseCookies) fs.writeFileSync('./headers', JSON.stringify(h))
}
/**
* Check if the saved Cookies are still valid
* @returns {Promise<Object>}
*/
async isCookieValid() {
try {
await this._axios({
url: 'iserv/'
});
this._log('[isValid] Cookies are valid.');
return true;
} catch (e) {
this._log(`[isValid] Cookies NOT valid. Check returned ${e.response.status} (${e.response.statusText})`);
return false;
}
}
}
module.exports = IServTool;