summaryrefslogtreecommitdiff
path: root/lib/KolabAdmin/Sieve/Script.php
blob: 98e609bf6d3748841b68511a6e046c37961480db (plain)
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
<?php
/*
 *  Copyright (c) 2004 Klarälvdalens Datakonsult AB
 *
 *    Written by Steffen Hansen <steffen@klaralvdalens-datakonsult.se>
 *
 *  This  program is free  software; you can redistribute  it and/or
 *  modify it  under the terms of the GNU  General Public License as
 *  published by the  Free Software Foundation; either version 2, or
 *  (at your option) any later version.
 *
 *  This program is  distributed in the hope that it will be useful,
 *  but WITHOUT  ANY WARRANTY; without even the  implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You can view the  GNU General Public License, online, at the GNU
 *  Project's homepage; see <http://www.gnu.org/licenses/gpl.html>.
 */

class KolabAdmin_Sieve_Script {
  // Funny multiline string escaping in Sieve
  /*static*/ function dotstuff( $str ) {
    return str_replace( "\n.", "\n..", $str );
  }
  
  /*static*/ function undotstuff( $str ) {
    return str_replace( "\n..", "\n.", $str );
  }
  
  /*static*/ function getDeliverFolder( $script ) {
    $inbox = false;      
	if( preg_match("/fileinto \"INBOX\/([^\"]*)\";/", $script, $regs ) ) {
	  $inbox = $regs[1];
	}
    return $inbox;
  }

  /*static*/ function getVacationAddresses( $script ) {
    $addresses = false;
    if( preg_match("/:addresses \\[([^\\]]*)\\]/s", $script, $regs ) ) {
      $tmp = split(',', $regs[1] );
      $addresses = array();
      foreach( $tmp as $a ) {
		if( ereg('^ *"(.*)" *$', $a, $regs ) ) $addresses[] = $regs[1];
		else $addresses[] = $a;
      }
    }
    return $addresses;
  }

  /*static*/ function getMailDomain( $script ) {
	$maildomain = false;
	if( preg_match( '/address :domain :contains "From" "(.*)"/i', $script, $regs ) ) {
	  $maildomain = $regs[1];
	}
	return $maildomain;
  }
  
  /*static*/ function getReactToSpam( $script ) {
	$spam = false;
	if( preg_match('/header :contains "X-Spam-Flag" "YES"/i', $script ) ) {
	  $spam = true;
	}
	return $spam;
  }

  /*static*/ function getVacationDays( $script ) {
    $days = false;
    if( preg_match("/:days ([0-9]+)/s", $script, $regs ) ) {
      $days = $regs[1];
    }
    return $days;
  }

  /*static*/ function getVacationText( $script ) {
    $text = false;
    if( preg_match("/text:(.*\r\n)\\.\r\n/s", $script, $regs ) ) {
      $text = $regs[1];
      $text = str_replace( '\n', "\r\n", $text );
      $text = KolabAdmin_Sieve_Script::undotstuff($text);
    }
    return $text;
  }

  /*static*/ function getForwardAddress( $script ) {
    $address = false;
    if( preg_match("/redirect \"([^\"]*)\"/s", $script, $regs ) ) {
      $address = $regs[1];
    }
    return $address;
  }

  /*static*/ function getKeepOnServer( $script ) {
    return ereg('"; keep;', $script, $regs ) > 0;
  }

  /*static*/ function getScriptInfo($script) {
	return array('maildomain'        => KolabAdmin_Sieve_Script::getMailDomain($script),
				 'vacationaddresses' => KolabAdmin_Sieve_Script::getVacationAddresses($script),
				 'days'              => KolabAdmin_Sieve_Script::getVacationDays($script),
				 'reacttospam'       => KolabAdmin_Sieve_Script::getReactToSpam($script),
				 'vacationtext'      => KolabAdmin_Sieve_Script::getVacationText($script),
				 'vacationenabled'   => KolabAdmin_Sieve_Script::isVacationEnabled($script),
				 'deliveryfolder'    => KolabAdmin_Sieve_Script::getDeliverFolder($script),
				 'deliveryenabled'   => KolabAdmin_Sieve_Script::isDeliveryEnabled($script));
  }

  /*static*/ function isDeliveryEnabled($script) {
	return ereg('## delivery enabled', $script, $regs )>0;
  }

  /*static*/ function isVacationEnabled($script) {
	return ereg('## vacation enabled', $script, $regs )>0;
  }
  
  /*static*/ function createScript($scriptinfo) {
	$tests = array();
	if( $scriptinfo['vacationenabled'] ) {
	  $tests[] = "## vacation enabled\r\ntrue";
	} else {
	  $tests[] = "## vacation disabled\r\nnot true";
	}

	if(!empty($scriptinfo['maildomain'])) {
	  $tests[] = "address :domain :contains \"From\" \"".$scriptinfo['maildomain']."\"";
	}
	if($scriptinfo['reacttospam']) {
	  $tests[] = "not header :contains \"X-Spam-Flag\" \"YES\"";
	}
	$script =
	  "require \"vacation\";\r\n\r\n".
	  "require \"fileinto\";\r\n\r\n".
	  "if allof (".join(",\r\n",$tests).") {\r\n".
	  "  vacation :addresses [ \"".join('", "', $scriptinfo['vacationaddresses'] )."\" ] :days ".
	  $scriptinfo['days']." text:\r\n".
	  KolabAdmin_Sieve_Script::dotstuff(trim($scriptinfo['vacationtext']))."\r\n.\r\n;\r\n}\r\n";
	if($scriptinfo['deliveryfolder']) {
	  if($scriptinfo['deliveryenabled']) {
		$script .= "if allof (true, ## delivery enabled\r\n";
	  } else {
		$script .= "if allof (not true, ## delivery disabled\r\n";
	  }
	  $script .= "header :contains [\"X-Kolab-Scheduling-Message\"] [\"FALSE\"]) {\r\nfileinto \"INBOX/".
		$scriptinfo['deliveryfolder']."\";\r\n}\r\n";
	}
	return $script;
  }
};