source: trunk/expressoMail1_2/js/filter.js @ 246

Revision 246, 18.4 KB checked in by niltonneto, 16 years ago (diff)

Ver o ticket #161 do Trac.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1function charCounter(form)
2{
3        if (form.value.length >= 300) {
4                alert(get_lang("You have exceeded the number of allowed characters"));
5                return false;
6        }
7
8}
9
10        function cfilter(){
11
12                this.mode_in   = "";
13                this.rulest              = new Array;
14                this.out_officeR = "";
15                this.out_officeF = false;
16                this.email_deny  = new Array;
17                this.ac_form     = "";
18                this.ind                 = "";
19                this.email_deny  = new Array;
20                this.email_ld    = "";
21                this.values = new Array("",
22                                        ",checkBox1",
23                                        ",select_size=1",
24                                        ",checkBox1,select_size=1",
25                                        ",select_rules=1",
26                                        ",checkBox1,select_rules=1",                           
27                                        ",select_size=1,select_rules=1",                               
28                                        ",checkBox1,select_size=1,select_rules=1",
29                                        ",checkBox2",                           
30                                        ",checkBox1,checkBox2",                         
31                                        ",checkBox2,select_size=1",
32                                        ",ckeckBox1,checkBox2,select_size=1",
33                                        ",checkBox2,select_rules=1",                                                                                                                                                           
34                                        ",checkBox1,checkBox2,select_rules=1",                         
35                                        ",checkBox2,select_size=1,select_rules=1",                             
36                                        ",checkBox1,checkBox2,select_size=1,select_rules=1");
37               
38        }
39       
40        cfilter.prototype.load_rules = function(){
41                var _this = this;
42                if(_this.rulest.length == 0){
43                        var handler_sieve = function(data){
44                                if(data.rule.length > 0){
45                                        for(var i=0 ; i < data.rule.length; i++){
46                                                _this.rulest[_this.rulest.length] = data.rule[i];
47                                        }
48                                }
49                                _this.out_officeR = data.vacation[0];
50                                _this.out_officeR = _this.out_officeR ? trim(_this.out_officeR.toString().replace("\n","")) : "";
51                                if(data.mode.length > 0){_this.mode_in = data.mode[0];}
52                        }
53                        if(Element('form_status') != null)
54                                Element('form_status').innerHTML = "<span style='background:#cc4444;'>&nbsp;&nbsp;<font color='WHITE'>Aguarde...</font>&nbsp;</span>";
55                        cExecute("$this.ScriptS.init_a",handler_sieve);
56                        _this.get_email();
57            }
58        }
59       
60        cfilter.prototype.form_m = function(){
61                Element('form_body').innerHTML = "";
62                Element('form_buttons').style.display = '';
63                filters.mount_list();
64                this.ac_form = "";             
65        }
66       
67        cfilter.prototype.form_out = function(){
68                Element('form_body').innerHTML = "";
69                Element('form_body').innerHTML = this.forms_();
70                Element('div_vacation').style.display = "";
71                this.ac_form = "old_out";               
72                this.r_rules_out();             
73        }
74       
75        cfilter.prototype.form_r = function(pos){
76                Element('form_body').innerHTML = "";
77                Element('form_body').innerHTML = this.forms_();
78                Element('div_rule').style.display = "";
79                this.ac_form = "old_rule";
80                this.ind = pos;
81                this.r_rules_form(pos);
82        }
83       
84        cfilter.prototype.get_email = function(){
85                var _this = this;
86                var handler_get_email = function(data){
87                        _this.email_ld = data ? data : "";
88                }
89                cExecute("$this.user.get_email",handler_get_email);
90        }
91       
92        cfilter.prototype.sel_boxes = function(){
93                var nm_folders = tree_folders.getNodesList(cyrus_delimiter);
94                if(document.getElementById("select_mailboxes") != null){
95                        var sel_nm = document.getElementById("select_mailboxes");
96                        if(sel_nm.length > 0 ){
97                                for(var i=0; i < sel_nm.options.length; i++){
98                                        sel_nm.options[i] = null;
99                                        i--;
100                                }
101                        }
102                        for(var i=0; i < nm_folders.length; i++){
103                                if(nm_folders[i].id != "root"){
104                                        var opt = new Option(nm_folders[i].caption,nm_folders[i].id,false,true);
105                                        sel_nm[sel_nm.length] = opt;
106                                }
107                        }
108                        sel_nm[0].selected = true;
109                }
110        }
111
112        cfilter.prototype.box_select = function(param){
113                var aux = this.BoxSelection[param].split(",");
114                var ele1 = document.getElementById(aux[0]);
115                var ele2 = document.getElementById(aux[1]);
116                var noption = "";
117                if(param == 0 || param == 1){
118                        if(ele1.selectedIndex != -1){
119                                noption = new Option(ele1.value,ele1.value,false,false);
120                                ele2.options[ele2.length] = noption;
121                                ele1.options[ele1.selectedIndex] = null;
122                                ele1.selectedIndex = 0;
123                        }
124                }
125        }
126
127        cfilter.prototype.r_rules_form = function(ind){
128               
129                // hide buttons
130                Element('form_buttons').style.display = 'none';
131
132                this.sel_boxes();
133                var aux = new Array;
134                var _this = this;
135                if(this.rulest.length == 0){
136                        return false;
137                }
138                aux = _this.rulest[ind].split("&&");
139                document.getElementById("field1").value = aux[3];
140                document.getElementById("field2").value = aux[4];
141                document.getElementById("field3").value = aux[5];
142                document.getElementById("field4").value = aux[11];                             
143               
144                switch(aux[6]){
145                        case "folder":
146                                document.getElementById("radio1").checked  = true;
147                                var name_mb = aux[7];
148                                var sel_mb = document.getElementById("select_mailboxes");
149                                for(var i=0; i < sel_mb.options.length; i++){if((sel_mb.options[i].value) ==  name_mb){sel_mb.options[i].selected = true;}}
150                                break;
151                        case "address":
152                                document.getElementById("radio2").checked   = true;
153                                document.getElementById("field5").value         = aux[7];
154                                break;
155                        case "reject":
156                                 document.getElementById("radio3").checked  = true;
157                                 var text0 = aux[7].split("\\n");                                       
158                                 for(var i=0; i < text0.length; i++){document.getElementById("field6").value += text0[i] + "\n";}
159                                 break;
160                        case "discard":
161                                document.getElementById("radio4").checked       = true;
162                                break;
163                }
164                var mark_values = this.values[aux[8]].split(",");
165                for(var i=0; i < mark_values.length; i++){
166                        if( mark_values[i] == "checkBox1" || mark_values[i] == "checkBox2"){
167                                document.getElementById(mark_values[i]).checked = true;
168                        }
169                        if( mark_values[i] == "select_size=1" || mark_values[i] == "select_rules=1"){
170                                var mark_val = mark_values[i].split("=");
171                                document.getElementById(mark_val[0]).options[mark_val[1]].selected = true;
172                        }
173                }
174        }
175
176        cfilter.prototype.r_rules_out = function(){
177       
178                var _this = this;
179                if(_this.out_officeR.length == 0){
180                        return false;
181                }
182               
183                // hide buttons
184                Element('form_buttons').style.display = 'none';
185       
186                var aux = _this.out_officeR.split("&&");
187                var days   = aux[1];
188                var emails = aux[2];
189                var mens   = aux[3];           
190                var p_emails = new Array;
191                var d_emails = new Array;
192                               
193                var p_aux = emails.split(", ");
194                for(var i=0; i < p_aux.length; i++){
195                        p_emails[i] = p_aux[i].substr(0,(p_aux[i].length - 1));
196                        p_emails[i] = p_emails[i].substr(1,(p_aux[i].length));
197                }
198                for(var i=0; i < _this.email_ld.length; i++){
199                        d_emails[i] = _this.email_ld[i];
200                }
201
202                diff = function(vet, comp){
203                        var sel1 = new Array;
204                        for(var i=0; i < vet.length; i++){
205                                for(var j=0; j < comp.length; j++){
206                                        if(vet[i] == comp[j]){
207                                                comp.splice(j,1);
208                                                j--;
209                                        }
210                                }
211                        }
212                };     
213                diff(p_emails,d_emails);
214                var text    = mens.split("\\n");
215                for(var i=0; i < text.length; i++){document.getElementById("field8").value += text[i] + " ";}
216                for(var i=0; i < _this.email_ld.length; i++){
217                        d_emails[i] = _this.email_ld[i];
218                }
219
220        }
221
222        cfilter.prototype.forms_ = function(){
223                        var form   = "";
224                        form       = "<div id='div_rule' style='display:none'><table id='table_rule' border='0' cellpading='0' cellspacing='0' width='100%'>"+
225                                                 "<tr><td colspan='2'><input type='checkBox' id='checkBox1' name='checkb'>"+get_lang('Also check message against next rule') + "</td></tr>"+
226                                                 "<tr><td colspan='2'><input type='checkBox' id='checkBox2' name='checkb'>"+get_lang('Keep a copy of the message at your Inbox')+ "</td><tr>"+
227                                                 "<tr><td colspan='2'><hr size='1' width='100%'></td></tr><tr>"+
228                                                 "<td rowspan='4' width='20%'>"+get_lang('Coincident')+"<br><select id='select_rules' name='select_rules'>"+
229                                                 "<option value='0'>"+get_lang("with all")+"</option><option value='1'>"+get_lang("with some")+"</option>"+
230                                                 "</select></td><td>"+get_lang('The field \"%1\" of the message it contains',get_lang('From'))+".: <input type='text' id='field1' name='field1' size='35' maxlength='200'></td>"+
231                                                 "</tr><tr><td>"+get_lang('The field \"%1\" of the message it contains', get_lang('To'))+".: <input type='text' id='field2' name='field2' size='35' maxlength='200'></td>"+
232                                                 "</tr><tr><td>"+get_lang('The field \"%1\" of the message it contains', get_lang('Subject'))+".: <input type='text' id='field3' name='field3' size='35' maxlength='200'></td>"+
233                                                 "</tr><tr><td>"+get_lang('The size of the message is')+".: &nbsp<select id='select_size' name='select_size'>"+
234                                                 "<option value='0'>"+get_lang("Less than")+"</option><option value='1'>"+get_lang("Greater than")+"</option>"+                                         
235                                                 "</select>&nbsp;<input type='text' id='field4' name='field4' size='8' maxlength='8'> Kb</td>"+
236                                                 "</tr><tr><td colspan='2'><hr size='1' width='100%'></td></tr>"+
237                                                 "</table><table id='table_rule1' border='0' cellpading='0' cellspacing='0' width='100%'>"+
238                                                 "<tr><td width='50%'><input type='radio' id='radio1' name='radio' value='folder'>"+get_lang('Store at')+".:</td>"+
239                                                 "<td width='50%'><select id='select_mailboxes' name='select_mailboxes'></select></td>"+
240                                                 "</tr><tr><td width='50%'><input type='radio' id='radio2' name='radio' value='address'>"+get_lang('Forward to the address')+".:</td>"+
241                                                 "<td width='50%'><input type='text' id='field5' name='field5' size='35' maxlength='70'></td>"+
242                                                 "</tr><tr><td width='50%'><input type='radio' id='radio3' name='radio' value='reject'>"+get_lang('Send a rejection message')+".:</td>"+
243                                                 "<td width='50%'><textarea id='field6' onkeypress='return charCounter(this);' name='field6' rows='3' cols='25'></textarea></td>"+
244                                                 "</tr><tr><td colspan='2'><input type='radio' id='radio4' name='radio' value='discard'>"+get_lang('Erase the message')+"</td>"+
245                                                 "</tr></table></div><div id='div_vacation' style='display:none'>"+
246                                                 "<table id='table_vacation' border='0' cellpading='0' cellspacing='0' width='100%'>"+
247                                                 "<tr><td colspan='3'><br><b>"+get_lang('out office')+"</b></td></tr>"+
248                                                 //"<tr><td colspan='3'><br>"+get_lang('Subject')+".: <input type='text' id='field7' name='field7' size='35' maxlength='200'/></td></tr>"+
249                                                 "<tr><td colspan='3'><br>"+get_lang('With the following message')+".:</td>"+
250                                                 "</tr><tr><td colspan='3'><textarea id='field8' onkeypress='return charCounter(this);' rows='8' cols='50'></textarea></td></tr></table></div>"+
251                                                 "<span align='right'><input type='button' value="+get_lang("Back")+" onclick='filter.form_m()'></span>"+
252                                                 "<span align='right'><input type='button' value="+get_lang("Save")+" onclick='filter.saved_rules()'></span>";                                           
253                return form;
254        }
255
256        cfilter.prototype.enabled_disabled = function(param){
257
258                // Rules
259                if(Element("rule_0") != null){
260                        for(var i=0; i < this.rulest.length; i++){
261                                if(Element("rule_"+i).checked){
262                                        var aux_rul = this.rulest[i].split("&&");
263                                        if(aux_rul[2] != param){
264                                                aux_rul[2] = param;                                     
265                                                var rl = "";
266                                                for(var j=0; j < aux_rul.length; j++){
267                                                        rl += aux_rul[j] + "&&";
268                                                }
269                                                rl = rl.substr(0,(rl.length - 2));
270                                                this.rulest[i] = rl;                                   
271                                        }
272                                }
273                        }
274                }
275                // Out Office
276                if(Element("out_0") != null){
277                        if(Element("out_0").checked){
278                                var aux_out = this.out_officeR.split("&&");
279                                if(param == "ENABLED")
280                                        aux_out[4] = "on ";
281                                else
282                                        aux_out[4] = "off";
283                                var out = "";
284                                for(var i=0; i < aux_out.length; i++){
285                                        out += aux_out[i] + "&&";
286                                }                               
287                                out = out.substr(0,(out.length - 2));
288                                this.out_officeR = out;
289                        }
290                }
291                this.reload_rules();
292        }
293       
294        cfilter.prototype.new_rule = function(email){
295
296                if(this.email_deny.length > 0){
297                        for(var i=0 ; i < this.email_deny.length; i++){
298                                if(this.email_deny[i] == email){
299                                        alert(get_lang("Sender blocked!"));
300                                        return false;
301                                }
302                        }
303                }
304
305                // Verifica Email
306                var emailReg = /^[a-z][a-z-_0-9\.]+@[a-z-_=>0-9\.]+\.[a-z]{2,3}$/i
307        if(!emailReg.test(email)){
308                        alert(get_lang("Inform a valid e-mail!"));
309                        return false;
310                }
311
312                this.load_rules();
313                if(confirm(get_lang("Do you want to block this e-mail?"))){
314                        new_r = "&&ENABLED&&" + email + "&&&&&&discard&&&&0&&&&&&0";
315//                      setTimeout("filter.e_newrule('" + new_r + "')",2000);           
316                        if (filter.e_newrule(new_r)){
317                                this.email_deny.push(email);
318                                write_msg(get_lang("This sender was blocked"));                                                 
319                        }else
320                                write_msg(get_lang("You have reached the maximum number of rules"));
321                }
322        }
323       
324        cfilter.prototype.e_newrule = function(nw_rule){
325
326                var new_rl = "";
327                if (this.rulest.length >= rules_limit)
328                        return false;
329                       
330                if(this.rulest.length == 0){
331                        new_rl = "#rule&&1" + nw_rule;
332                }else{
333                        var aux = this.rulest[this.rulest.length -1 ].split("&&");
334                        new_rl = "#rule&&" + (parseInt(aux[1]) + parseInt(2)) + nw_rule;
335                }
336                this.rulest.push(new_rl);
337                this.saved_all();
338                return true;
339        }
340       
341        cfilter.prototype.delete_r = function(){
342       
343                // rule
344                var _this = this;
345                if(Element("rule_0") != null){
346                        for(var i=0; i < _this.rulest.length; i++){
347                                if(Element("rule_"+i).checked){_this.rulest[i] = "delete";}
348                        }
349                        for(var i=0; i < _this.rulest.length; i++){
350                                if(_this.rulest[i] == "delete"){_this.rulest.splice(i,1);i--;}
351                        }
352                }
353       
354                if(_this.rulest.length > 0){
355                        var cont = parseInt(1);
356                        for(var i=0; i < _this.rulest.length ; i++){
357                                var n_rulest = _this.rulest[i].split("&&");
358                                n_rulest[1] = cont;
359                                cont = cont + parseInt(2);
360                                var aux = "";
361                                for(var j=0; j < n_rulest.length; j++){aux += n_rulest[j] + "&&";}
362                                aux = aux.substr(0,(aux.length - 2));
363                                _this.rulest[i] = aux;
364                        }
365                }
366                // out office
367                if(Element("out_0") != null){
368                        if(Element("out_0").checked){
369                                _this.out_officeR ='';
370                                _this.out_officeF = false;
371                                //Save outoffice in prefs:
372                                if(!prefe)
373                                        connector.loadScript("preferences");
374                                prefe.save("outoffice", _this.out_officeF);
375                        }
376                }
377                _this.reload_rules();
378        }
379       
380        cfilter.prototype.reload_rules = function(){
381                this.saved_all();
382                Element('form_body').innerHTML = "";
383                this.load_rules();
384               
385                if(this.out_officeF){
386                        write_msg(get_lang("Attention, you are in out of office mode."), true);
387                }else {
388                        clean_msg();
389                        this.out_officeF = false;
390                }
391                filters.mount_list();
392        }
393
394        cfilter.prototype.saved_rules = function(){
395
396                var mount_rule = "";
397                var form = this.ac_form.split("_");
398                var n_rule = "";
399
400                if(form[1] == "rule"){
401                        mount_rule = "#rule&&";
402                        if(form[0] == "new"){
403                                n_rule = "1&&";
404                                if(this.rulest.length > 0){
405                                        aux = this.rulest[this.rulest.length - parseInt(1)].split("&&");
406                                        n_rule = (parseInt(aux[1]) + parseInt(2)) + "&&";
407                                }
408                                mount_rule += n_rule + "ENABLED&&";
409                        }else{
410                                n_rule = this.rulest[this.ind].split("&&");
411                                mount_rule += n_rule[1] + "&&";
412                                mount_rule += n_rule[2] + "&&";
413                        }
414
415                        if(LTrim(Element("field1").value) == "" && LTrim(Element("field2").value) == "" && LTrim(Element("field3").value) == "" && Element("field4").value == "")
416                        {
417                                alert(get_lang("Define some criterion to the fields From, To and Subject with more than 3 characters!"));
418                                return false;
419                        }
420
421                        if((LTrim(Element("field1").value).length <= 3) && (LTrim(Element("field2").value).length <= 3) && (LTrim(Element("field3").value).length <= 3 && Element("field4").value == "")){
422                                alert(get_lang("Define some criterion to the fields From, To and Subject with more than 3 characters!"));
423                                return false;
424                        }
425                       
426                        for(var i=1; i < 4; i++){mount_rule += LTrim(Element("field"+i).value) + "&&";}
427                        var v_checked = false;
428                        if(Element("radio1").checked){
429                                mount_rule += "folder&&";
430                                var sel_nameBox = Element("select_mailboxes");
431                                for(var i=0; i < sel_nameBox.options.length; i++){if(sel_nameBox.options[i].selected == true){mount_rule += sel_nameBox.options[i].value + "&&";}}
432                                v_checked = true;
433                        }
434                        if(Element("radio2").checked){
435                                mount_rule += "address&&";
436                                if(Element("field5").value == ""){
437                                        alert(get_lang("Inform a forwarding e-mail!"));
438                                        return false;
439                                }else{
440                                        var emailReg = /^[a-z][a-z-_0-9\.]+@[a-z-_=>0-9\.]+\.[a-z]{2,3}$/i
441                                if(emailReg.test(Element("field5").value)){
442                                                mount_rule += Element("field5").value + "&&";
443                                        }else{
444                                                alert(get_lang("Inform a valid e-mail!"));
445                                                return false;
446                                        }
447                                }
448                                v_checked = true;
449                        }
450                        if(Element("radio3").checked){
451                                mount_rule += "reject&&";
452                                if(Element("field6").value == ""){
453                                        alert(get_lang("Inform a text for rejection!"));
454                                        return false;
455                                }else{
456                                        mount_rule += Element("field6").value + "&&";
457                                }
458                                v_checked = true;
459                        }                               
460                        if(Element("radio4").checked){
461                                mount_rule += "discard&&&&";
462                                v_checked = true;
463                        }
464                        if(!v_checked){
465                                alert(get_lang("No option marked!"));
466                                return false;
467                        }
468                        var opts = "";
469                        if(Element("checkBox1").checked == true){opts += ",checkBox1";}
470                        if(Element("checkBox2").checked == true){opts += ",checkBox2";}
471                        if(Element("select_size").options[1].selected == true){opts += ",select_size=1";}
472                        if(Element("select_rules").options[1].selected == true){opts += ",select_rules=1";}
473                        for(var i=0; i < this.values.length; i++){if(this.values[i] == opts){mount_rule += i + "&&";}}
474                        mount_rule += "&&&&";
475                        if(LTrim(Element("field4").value) != ""){
476                                mount_rule += LTrim(Element("field4").value);
477                        }else{
478                                mount_rule += 0;
479                        }
480                        if(form[0] == "new")
481                                this.rulest[this.rulest.length] = mount_rule;
482                        else
483                                this.rulest[this.ind] = mount_rule;
484                }else{
485                        mount_rule = "";
486                        var fld_emails = this.email_ld; // Get first email of list!
487                        var fld_men        = Element("field8");
488                        mount_rule = "#vacation&&";
489                        mount_rule += "1&&";
490                        mount_rule += "\"" + fld_emails + "\", ";                       
491                        mount_rule = mount_rule.substr(0,(mount_rule.length - 2));
492                        mount_rule += "&&";
493                        mount_rule += fld_men.value + "&&on";
494                        if(LTrim(fld_men.value) == ""){
495                                alert(get_lang("Inform a message!"));
496                                return false;
497                        }
498                        this.out_officeR = mount_rule;
499                }
500                Element('form_buttons').style.display = '';
501                this.reload_rules();
502        }
503
504        cfilter.prototype.close_frm= function(){
505                filters.filter_Sh['window_ffilter_ccform'].close();
506        }
507       
508        cfilter.prototype.saved_all = function(){
509                var aux_rul = "";
510                var _this = this;
511                if(_this.rulest.length > 0){
512                        for(var i=0; i < _this.rulest.length; i++){
513                                var aux = _this.rulest[i].split("&&");                         
514                                aux_rul += "_begin_##";
515                                for(var j=0 ; j < aux.length; j++){
516                                        if( (j +1) > aux.length){aux_rul += url_encode(aux[j]);}
517                                        else{aux_rul += url_encode(aux[j]) + "##";}
518                                }       
519                                aux_rul += "_end_\n";
520                        }
521                }
522                if(_this.out_officeR.length > 0){
523                                var aux = _this.out_officeR.split("&&");                               
524                                aux_rul += "_begin_##";
525                                for(var j=0 ; j < aux.length; j++){
526                                        if( (j +1) > aux.length){aux_rul += url_encode(aux[j]);}
527                                        else{aux_rul += url_encode(aux[j]) + "##";}
528                                }       
529                                aux_rul += "_end_\n";
530                                _this.out_officeF = (aux[4].replace("\n","") == "off") ? false : true;
531                                //Save outoffice in prefs:
532                                if(!prefe)
533                                        connector.loadScript("preferences");
534                                prefe.save("outoffice", _this.out_officeF);
535                }
536                var h_filter = function(data){
537                        if(data != "Ok"){alert("Erro : \n" + data);}
538                }
539                var args   = "$this.ScriptS.rec_rules";
540                var params = "arfilter="+aux_rul;
541                cExecute(args,h_filter,params);
542        }
543
544// build object
545   var filter;
546   filter = new cfilter();
Note: See TracBrowser for help on using the repository browser.