]> git.proxmox.com Git - pmg-gui.git/blob - js/Utils.js
add doQuarantineAction and sender_renderer
[pmg-gui.git] / js / Utils.js
1 /*global Proxmox */
2 Ext.ns('PMG');
3
4 console.log("Starting PMG Manager");
5
6
7 Ext.define('PMG.Utils', {
8 singleton: true,
9
10 // this singleton contains miscellaneous utilities
11
12 senderText: gettext('Sender'),
13 receiverText: gettext('Receiver'),
14 scoreText: gettext('Score'),
15
16 oclass_text: {
17 who: gettext('Who Objects'),
18 what: gettext('What Objects'),
19 when: gettext('When Objects'),
20 action: gettext('Action Objects'),
21 from: gettext('From'),
22 to: gettext('To')
23 },
24
25 oclass_icon: {
26 who: '<span class="fa fa-fw fa-user-circle"></span> ',
27 what: '<span class="fa fa-fw fa-cube"></span> ',
28 when: '<span class="fa fa-fw fa-clock-o"></span> ',
29 action: '<span class="fa fa-fw fa-flag"></span> ',
30 from: '<span class="fa fa-fw fa-user-circle"></span> ',
31 to: '<span class="fa fa-fw fa-user-circle"></span> '
32 },
33
34 mail_status_map: {
35 2: 'delivered',
36 4: 'deferred',
37 5: 'bounced',
38 N: 'rejected',
39 G: 'greylisted',
40 A: 'accepted',
41 B: 'blocked',
42 Q: 'quarantine'
43 },
44
45 format_oclass: function(oclass) {
46 var icon = PMG.Utils.oclass_icon[oclass] || '';
47 var text = PMG.Utils.oclass_text[oclass] || oclass;
48 return icon + text;
49 },
50
51 rule_direction_text: {
52 0: gettext('In'),
53 1: gettext('Out'),
54 2: gettext('In & Out')
55 },
56
57 rule_direction_icon: {
58 0: '<span class="fa fa-fw fa-long-arrow-left"></span> ',
59 1: '<span class="fa fa-fw fa-long-arrow-right"></span> ',
60 2: '<span class="fa fa-fw fa-exchange"></span> '
61 },
62
63 format_rule_direction: function(dir) {
64 var icon = PMG.Utils.rule_direction_icon[dir] || '';
65 var text = PMG.Utils.rule_direction_text[dir] || dir;
66 return icon + text;
67 },
68
69 format_otype: function(otype) {
70 var editor = PMG.Utils.object_editors[otype];
71 if (editor) {
72 return editor.subject;
73 }
74 return 'unknown';
75 },
76
77 format_ldap_protocol: function(p) {
78 if (p === undefined) { return 'LDAP'; }
79 if (p === 'ldap') { return 'LDAP'; }
80 if (p === 'ldaps') { return 'LDAPS'; }
81 return 'unknown';
82 },
83
84 object_editors: {
85 1000: {
86 xtype: 'proxmoxWindowEdit',
87 subdir: 'regex',
88 subject: gettext("Regular Expression"),
89 width: 400,
90 items: [
91 {
92 xtype: 'textfield',
93 name: 'regex',
94 labelWidth: 150,
95 fieldLabel: gettext("Regular Expression")
96 }
97 ]
98 },
99 1005: {
100 xtype: 'pmgLDAPGroupEditor',
101 subdir: 'ldap',
102 subject: gettext("LDAP Group")
103 },
104 1006: {
105 xtype: 'pmgLDAPUserEditor',
106 subdir: 'ldapuser',
107 subject: gettext("LDAP User")
108 },
109 1009: {
110 xtype: 'proxmoxWindowEdit',
111 subdir: 'receiver_regex',
112 subject: gettext("Regular Expression"),
113 receivertest: true,
114 width: 400,
115 items: [
116 {
117 xtype: 'textfield',
118 name: 'regex',
119 labelWidth: 150,
120 fieldLabel: gettext("Regular Expression")
121 }
122 ]
123 },
124 1001: {
125 xtype: 'proxmoxWindowEdit',
126 subdir: 'email',
127 subject: gettext("Email"),
128 width: 400,
129 items: [
130 {
131 xtype: 'textfield',
132 name: 'email',
133 fieldLabel: gettext("Email")
134 }
135 ]
136 },
137 1007: {
138 xtype: 'proxmoxWindowEdit',
139 subdir: 'receiver',
140 subject: gettext("Email"),
141 receivertest: true,
142 width: 400,
143 items: [
144 {
145 xtype: 'textfield',
146 name: 'email',
147 fieldLabel: gettext("Email")
148 }
149 ]
150 },
151 1002: {
152 xtype: 'proxmoxWindowEdit',
153 subdir: 'domain',
154 subject: gettext("Domain"),
155 width: 400,
156 items: [
157 {
158 xtype: 'textfield',
159 name: 'domain',
160 fieldLabel: gettext("Domain")
161 }
162 ]
163 },
164 1008: {
165 xtype: 'proxmoxWindowEdit',
166 subdir: 'receiver_domain',
167 subject: gettext("Domain"),
168 receivertest: true,
169 width: 400,
170 items: [
171 {
172 xtype: 'textfield',
173 name: 'domain',
174 fieldLabel: gettext("Domain")
175 }
176 ]
177 },
178 1003: {
179 xtype: 'proxmoxWindowEdit',
180 subdir: 'ip',
181 subject: gettext("IP Address"),
182 width: 400,
183 items: [
184 {
185 xtype: 'textfield',
186 name: 'ip',
187 fieldLabel: gettext("IP Address")
188 }
189 ]
190 },
191 1004: {
192 xtype: 'proxmoxWindowEdit',
193 subdir: 'network',
194 subject: gettext("IP Network"),
195 width: 400,
196 items: [
197 {
198 xtype: 'textfield',
199 name: 'cidr',
200 fieldLabel: gettext("IP Network")
201 }
202 ]
203 },
204 2000: {
205 xtype: 'proxmoxWindowEdit',
206 subdir: 'timeframe',
207 subject: gettext("TimeFrame"),
208 items: [
209 {
210 xtype: 'timefield',
211 name: 'start',
212 format: 'H:i',
213 fieldLabel: gettext("Start Time")
214 },
215 {
216 xtype: 'timefield',
217 name: 'end',
218 format: 'H:i',
219 fieldLabel: gettext("End Time")
220 }
221 ]
222 },
223 3000: {
224 xtype: 'proxmoxWindowEdit',
225 subdir: 'spamfilter',
226 subject: gettext('Spam Filter'),
227 items: [
228 {
229 xtype: 'proxmoxintegerfield',
230 name: 'spamlevel',
231 allowBlank: false,
232 minValue: 0,
233 fieldLabel: gettext('Level')
234 }
235 ]
236 },
237 3002: {
238 xtype: 'proxmoxWindowEdit',
239 subdir: 'matchfield',
240 subject: gettext('Match Field'),
241 width: 400,
242 items: [
243 {
244 xtype: 'textfield',
245 name: 'field',
246 labelWidth: 150,
247 allowBlank: false,
248 fieldLabel: gettext('Field')
249 },
250 {
251 xtype: 'textfield',
252 reference: 'value',
253 name: 'value',
254 labelWidth: 150,
255 allowBlank: false,
256 fieldLabel: gettext('Value')
257 },
258 {
259 labelWidth: 150,
260 fieldLabel: gettext('Test String'),
261 xtype: 'pmgRegexTester',
262 regexFieldReference: 'value'
263 }
264 ]
265 },
266 3003: {
267 xtype: 'proxmoxWindowEdit',
268 subdir: 'contenttype',
269 width: 400,
270 subject: gettext('Content Type Filter'),
271 items: [
272 {
273 xtype: 'combobox',
274 displayField: 'text',
275 labelWidth: 150,
276 valueField: 'mimetype',
277 name: 'contenttype',
278 editable: true,
279 queryMode: 'local',
280 store: {
281 autoLoad: true,
282 proxy: {
283 type: 'proxmox',
284 url: '/api2/json/config/mimetypes'
285 }
286 },
287 fieldLabel: gettext('Content Type'),
288 anyMatch: true,
289 matchFieldWidth: false,
290 listeners: {
291 change: function(cb, value) {
292 var me = this;
293 me.up().down('displayfield').setValue(value);
294 }
295 }
296 },
297 {
298 xtype: 'displayfield',
299 fieldLabel: gettext('Value'),
300 labelWidth: 150,
301 allowBlank: false,
302 reset: Ext.emptyFn
303 }
304 ]
305 },
306 3004: {
307 xtype: 'proxmoxWindowEdit',
308 subdir: 'filenamefilter',
309 width: 400,
310 subject: gettext('Match Filename'),
311 items: [
312 {
313 xtype: 'textfield',
314 name: 'filename',
315 reference: 'filename',
316 fieldLabel: gettext('Filename'),
317 labelWidth: 150,
318 allowBlank: false
319 },
320 {
321 labelWidth: 150,
322 fieldLabel: gettext('Test String'),
323 wholeMatch: true,
324 xtype: 'pmgRegexTester',
325 regexFieldReference: 'filename'
326 }
327 ]
328 },
329 3005: {
330 xtype: 'proxmoxWindowEdit',
331 subdir: 'archivefilter',
332 width: 400,
333 subject: gettext('Archive Filter'),
334 items: [
335 {
336 xtype: 'combobox',
337 displayField: 'text',
338 labelWidth: 150,
339 valueField: 'mimetype',
340 name: 'contenttype',
341 editable: true,
342 queryMode: 'local',
343 store: {
344 autoLoad: true,
345 proxy: {
346 type: 'proxmox',
347 url: '/api2/json/config/mimetypes'
348 }
349 },
350 fieldLabel: gettext('Content Type'),
351 anyMatch: true,
352 matchFieldWidth: false,
353 listeners: {
354 change: function(cb, value) {
355 var me = this;
356 me.up().down('displayfield').setValue(value);
357 }
358 }
359 },
360 {
361 xtype: 'displayfield',
362 fieldLabel: gettext('Value'),
363 labelWidth: 150,
364 allowBlank: false,
365 reset: Ext.emptyFn
366 }
367 ]
368 },
369 4005: {
370 xtype: 'proxmoxWindowEdit',
371 subdir: 'bcc',
372 subject: gettext('BCC'),
373 width: 400,
374 items: [
375 {
376 xtype: 'textfield',
377 name: 'name',
378 allowBlank: false,
379 fieldLabel: gettext('Name')
380 },
381 {
382 xtype: 'textareafield',
383 name: 'info',
384 fieldLabel: gettext("Description")
385 },
386 {
387 xtype: 'textfield',
388 name: 'target',
389 allowBlank: false,
390 fieldLabel: gettext("Target")
391 },
392 {
393 xtype: 'proxmoxcheckbox',
394 checked: true,
395 name: 'original',
396 fieldLabel: gettext("send orig. Mail")
397 }
398 ]
399
400 }
401 },
402
403 openVNCViewer: function(consoletype, nodename) {
404 var url = Ext.urlEncode({
405 console: consoletype, // upgrade or shell
406 novnc: 1,
407 node: nodename
408 });
409 var nw = window.open("?" + url, '_blank',
410 "innerWidth=745,innerheight=427");
411 nw.focus();
412 },
413
414 updateLoginData: function(data) {
415 Proxmox.CSRFPreventionToken = data.CSRFPreventionToken;
416 Proxmox.UserName = data.username;
417 Ext.util.Cookies.set('PMGAuthCookie', data.ticket, null, '/', null, true );
418 },
419
420 quarantineActionExtracted: false,
421
422 extractQuarantineAction: function() {
423
424 if (PMG.Utils.quarantineActionExtracted) { return; }
425
426 PMG.Utils.quarantineActionExtracted = true;
427
428 var qs = Ext.Object.fromQueryString(location.search);
429
430 var cselect = qs.cselect;
431 var action = qs.action;
432 var ticket = qs.ticket;
433
434 delete qs.cselect;
435 delete qs.action;
436 delete qs.ticket;
437
438 var newsearch = Ext.Object.toQueryString(qs);
439
440 var newurl = location.protocol + "//" + location.host + location.pathname;
441 if (newsearch) { newurl += '?' + newsearch; }
442 newurl += location.hash;
443
444 if (window.history) {
445 window.history.pushState({ path:newurl }, '', newurl);
446 }
447
448 if (action && cselect) {
449 return { action: action, cselect: cselect };
450 }
451 },
452
453 doQuarantineAction: function(action, id, callback) {
454 Proxmox.Utils.API2Request({
455 url: '/quarantine/content/',
456 params: {
457 action: action,
458 id: id
459 },
460 method: 'POST',
461 failure: function(response, opts) {
462 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
463 },
464 success: function(response, opts) {
465 Ext.Msg.show({
466 title: gettext('Info'),
467 message: "Action '" + action + ' ' +
468 id + "' successful",
469 buttons: Ext.Msg.OK,
470 icon: Ext.MessageBox.INFO
471 });
472
473 if (Ext.isFunction(callback)) {
474 callback();
475 }
476 }
477 });
478 },
479
480 sender_renderer: function(value, metaData, rec) {
481 var subject = Ext.htmlEncode(value);
482 var from = Ext.htmlEncode(rec.data.from);
483 var sender = Ext.htmlEncode(rec.data.sender);
484 if (sender) {
485 from = Ext.String.format(gettext("{0} on behalf of {1}"),
486 sender, from);
487 }
488 return '<small>' + from + '</small><br>' + subject;
489 },
490
491 constructor: function() {
492 var me = this;
493
494 // do whatever you want here
495 }
496 });