Skip to content

Commit 5a4d783

Browse files
author
Michele
committed
Implemented plugin update method
Some minor bugs fixed too
1 parent 585fce9 commit 5a4d783

File tree

1 file changed

+153
-113
lines changed

1 file changed

+153
-113
lines changed

server/plugins-available/mlmmj_plugin.inc.php

Lines changed: 153 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@
4343
*/
4444

4545
class mlmmj_plugin {
46-
const ML_ALIAS = 0;
47-
const ML_TRANSPORT = 1;
48-
const ML_VIRTUAL = 2;
46+
const ML_ALIAS = 0;
47+
const ML_TRANSPORT = 1;
48+
const ML_VIRTUAL = 2;
49+
const SECONDS_IN_DAY = 86400;
4950

5051
private $plugin_name = 'mlmmj_plugin';
5152
private $class_name = 'mlmmj_plugin';
@@ -73,123 +74,158 @@ function onLoad() {
7374
}
7475

7576
function insert($event_name, $data) {
76-
global $app;
77+
global $app, $conf;
7778

78-
/*[new] => Array
79-
(
80-
[mailinglist_id] => 8
81-
[sys_userid] => 1
82-
[sys_groupid] => 26
83-
[sys_perm_user] => riud
84-
[sys_perm_group] => ru
85-
[sys_perm_other] =>
86-
[server_id] => 0
87-
[domain] => 10100.to
88-
[listname] => merda
89-
[email] => michele@10100.to
90-
[password] => vbhXvWMK!1
91-
)*/
92-
93-
$mlConf = $this->getMlConfig();
94-
$listDomain = $data['new']['domain'];
95-
$listName = $data['new']['listname'];
96-
$listDir = $mlConf['spool_dir']."/$listDomain/$listName";
97-
$lang = 'en';
98-
$owner = $data['new']['email'];
99-
100-
// Creating ML directories structure
101-
mkdir("$listDir/incoming", 0755, true);
102-
mkdir("$listDir/queue/discarded", 0755, true);
103-
mkdir("$listDir/archive", 0755, true);
104-
mkdir("$listDir/text", 0755, true);
105-
mkdir("$listDir/subconf", 0755, true);
106-
mkdir("$listDir/unsubconf", 0755, true);
107-
mkdir("$listDir/bounce", 0755, true);
108-
mkdir("$listDir/control", 0755, true);
109-
mkdir("$listDir/moderation", 0755, true);
110-
mkdir("$listDir/subscribers.d", 0755, true);
111-
mkdir("$listDir/digesters.d", 0755, true);
112-
mkdir("$listDir/requeue", 0755, true);
113-
mkdir("$listDir/nomailsubs.d", 0755, true);
114-
115-
// Creating ML index file
116-
touch("$listDir/index");
117-
118-
// Saving ML base data
119-
file_put_contents("$listDir/control/owner", $owner);
120-
file_put_contents("$listDir/control/listaddress", "$listName@$listDomain");
121-
122-
// Copying language translations
123-
if(!is_dir("/usr/share/mlmmj/text.skel/$lang")) $lang = 'en';
124-
foreach (glob("/usr/share/mlmmj/text.skel/$lang/*") as $filename)
125-
copy($filename, "$listDir/text/".basename($filename));
126-
127-
// The mailinglist directory have to be owned by the user running the mailserver
128-
$this->chmodR($listDir);
129-
130-
// Creating alias entry
131-
$this->addMapEntry("$listName: \"|/usr/bin/mlmmj-recieve -L $listDir/\"", self::ML_ALIAS);
132-
133-
// Creating transport entry
134-
$this->addMapEntry("$listDomain--$listName@localhost.mlmmj mlmmj:$listDomain/$listName", self::ML_TRANSPORT);
135-
136-
// Creating virtual entry
137-
$this->addMapEntry("$listName@$listDomain $listDomain--$listName@localhost.mlmmj", self::ML_VIRTUAL);
138-
139-
$mlmmjmaintd='/usr/bin/mlmmj-maintd';
140-
// CRONENTRY="0 */2 * * * \"$MLMMJMAINTD -F -L $SPOOLDIR/$FQDN/$LISTNAME/\""
141-
142-
// /usr/sbin/postfix reload
143-
$app->db->query("UPDATE mail_mailinglist SET password = '' WHERE mailinglist_id = ".$app->db->quote($data["new"]['mailinglist_id']));
79+
$mlManager = $app->getconf->get_server_config($conf['server_id'], 'mail')['mailinglist'];
80+
81+
if($mlManager == 'mlmmj') {
82+
$mlConf = $this->getMlConfig();
83+
$rec = $data['new'];
84+
$listDomain = $rec['domain'];
85+
$listName = $rec['listname'];
86+
$listDir = $mlConf['spool_dir']."/$listDomain/$listName";
87+
$lang = 'en';
88+
$owner = $rec['email'];
89+
90+
// Creating ML directories structure
91+
mkdir("$listDir/incoming", 0755, true);
92+
mkdir("$listDir/queue/discarded", 0755, true);
93+
mkdir("$listDir/archive", 0755, true);
94+
mkdir("$listDir/text", 0755, true);
95+
mkdir("$listDir/subconf", 0755, true);
96+
mkdir("$listDir/unsubconf", 0755, true);
97+
mkdir("$listDir/bounce", 0755, true);
98+
mkdir("$listDir/control", 0755, true);
99+
mkdir("$listDir/moderation", 0755, true);
100+
mkdir("$listDir/subscribers.d", 0755, true);
101+
mkdir("$listDir/digesters.d", 0755, true);
102+
mkdir("$listDir/requeue", 0755, true);
103+
mkdir("$listDir/nomailsubs.d", 0755, true);
104+
105+
// Creating ML index file
106+
touch("$listDir/index");
107+
108+
// Creating default control files
109+
// WARNING: Edit this section if default DB values will be modified!
110+
touch("$listDir/control/nodigestsub");
111+
touch("$listDir/control/noarchive");
112+
113+
// Saving ML base data
114+
file_put_contents("$listDir/control/owner", $owner);
115+
file_put_contents("$listDir/control/listaddress", "$listName@$listDomain");
116+
117+
// Copying language translations
118+
if(!is_dir("/usr/share/mlmmj/text.skel/$lang")) $lang = 'en';
119+
foreach (glob("/usr/share/mlmmj/text.skel/$lang/*") as $filename)
120+
copy($filename, "$listDir/text/".basename($filename));
121+
122+
// The mailinglist directory have to be owned by the user running the mailserver
123+
$this->changeOwnership($listDir);
124+
125+
// Creating alias entry
126+
$this->addMapEntry("$listName: \"|/usr/bin/mlmmj-recieve -L $listDir/\"", self::ML_ALIAS);
127+
128+
// Creating transport entry
129+
$this->addMapEntry("$listDomain--$listName@localhost.mlmmj mlmmj:$listDomain/$listName", self::ML_TRANSPORT);
130+
131+
// Creating virtual entry
132+
$this->addMapEntry("$listName@$listDomain $listDomain--$listName@localhost.mlmmj", self::ML_VIRTUAL);
133+
134+
$mlmmjmaintd='/usr/bin/mlmmj-maintd';
135+
// CRONENTRY="0 */2 * * * \"$MLMMJMAINTD -F -L $SPOOLDIR/$FQDN/$LISTNAME/\""
136+
137+
// /usr/sbin/postfix reload
138+
$app->db->query("UPDATE mail_mailinglist SET password = '' WHERE mailinglist_id = ".$app->db->quote($rec['mailinglist_id']));
139+
}
144140
}
145141

146142
// The purpose of this plugin is to rewrite the main.cf file
147143
function update($event_name, $data) {
148144
global $app, $conf;
149145

150-
// $this->update_config();
151-
//
152-
// if($data["new"]["password"] != $data["old"]["password"] && $data["new"]["password"] != '') {
153-
// exec("nohup /usr/lib/mailman/bin/change_pw -l ".escapeshellcmd($data["new"]["listname"])." -p ".escapeshellcmd($data["new"]["password"])." >/dev/null 2>&1 &");
154-
// exec('nohup '.$conf['init_scripts'] . '/' . 'mailman reload >/dev/null 2>&1 &');
155-
// $app->db->query("UPDATE mail_mailinglist SET password = '' WHERE mailinglist_id = ".$app->db->quote($data["new"]['mailinglist_id']));
156-
// }
157-
//
158-
// if(is_file('/var/lib/mailman/data/virtual-mailman')) exec('postmap /var/lib/mailman/data/virtual-mailman');
159-
// if(is_file('/var/lib/mailman/data/transport-mailman')) exec('postmap /var/lib/mailman/data/transport-mailman');
160-
}
161-
162-
function delete($event_name, $data) {
163-
global $app;
164-
165-
$mlConf = $this->getMlConfig();
166-
$listDomain = $data['old']['domain'];
167-
$listName = $data['old']['listname'];
168-
$listDir = $mlConf['spool_dir']."/$listDomain/$listName";
169-
$lang = 'en';
170-
$owner = $data['old']['email'];
171-
172-
// Removing alias entry
173-
$this->delMapEntry("$listName: \"|/usr/bin/mlmmj-recieve -L $listDir/\"", self::ML_ALIAS);
174-
175-
// Removing transport entry
176-
$this->delMapEntry("$listDomain--$listName@localhost.mlmmj mlmmj:$listDomain/$listName", self::ML_TRANSPORT);
146+
$mlManager = $app->getconf->get_server_config($conf['server_id'], 'mail')['mailinglist'];
147+
148+
if($mlManager == 'mlmmj') {
149+
$rec = $data['new'];
150+
$mlConf = $this->getMlConfig();
151+
152+
$controlDir = "{$mlConf['spool_dir']}/{$rec['domain']}/{$rec['listname']}/control";
153+
154+
155+
// Does'nt matter if list is open or close, members can ALWAYS unsubscribe
156+
if($rec['list_type'] == 'open') {
157+
switch($rec['subscribe_policy']) {
158+
case 'disabled':
159+
touch("$controlDir/closedlistsub");
160+
@unlink("$controlDir/submod");
161+
@unlink("$controlDir/nosubconfirm");
162+
break;
163+
case 'both':
164+
touch("$controlDir/submod");
165+
case 'approval':
166+
touch("$controlDir/nosubconfirm");
167+
@unlink("$controlDir/closedlistsub");
168+
break;
169+
case 'none':
170+
touch("$controlDir/nosubconfirm");
171+
@unlink("$controlDir/closedlistsub");
172+
break;
173+
case 'confirm':
174+
@unlink("$controlDir/nosubconfirm");
175+
@unlink("$controlDir/closedlistsub");
176+
@unlink("$controlDir/submod");
177+
break;
178+
}
179+
180+
switch($rec['posting_policy']) {
181+
case 'closed':
182+
touch("$controlDir/subonlypost");
183+
break;
184+
case 'moderated':
185+
touch("$controlDir/modnonsubposts");
186+
break;
187+
case 'free':
188+
@unlink("$controlDir/modnonsubposts");
189+
@unlink("$controlDir/subonlypost");
190+
break;
191+
}
192+
} elseif($rec['list_type'] == 'closed') {
193+
touch("$controlDir/closedlistsub");
194+
touch("$controlDir/subonlypost");
195+
@unlink("$controlDir/modnonsubposts");
196+
@unlink("$controlDir/submod");
197+
@unlink("$controlDir/nosubconfirm");
198+
}
177199

178-
// Removing virtual entry
179-
$this->delMapEntry("$listName@$listDomain $listDomain--$listName@localhost.mlmmj", self::ML_VIRTUAL);
200+
if($rec['digestsub'] == 'y') {
201+
@unlink("$controlDir/nodigestsub");
202+
if($rec['digesttext'] == 'y') @unlink("$controlDir/nodigesttext");
203+
else touch("$controlDir/nodigesttext");
204+
file_put_contents("$controlDir/digestinterval", $rec['digestinterval']*self::SECONDS_IN_DAY);
205+
file_put_contents("$controlDir/digestmaxmails", $rec['digestmaxmails']);
206+
} else {
207+
touch("$controlDir/nodigestsub");
208+
@unlink("$controlDir/nodigesttext");
209+
@unlink("$controlDir/digestinterval");
210+
@unlink("$controlDir/digestmaxmails");
211+
}
180212

213+
$this->changeOwnership("$controlDir/*");
214+
}
215+
}
181216

182-
$this->rmdirR($listDir);
217+
function delete($event_name, $data) {
218+
global $app, $conf;
183219

184-
// Remove parent folder if is empty (means there aren't other ML for the domain)
185-
@rmdir($mlConf['spool_dir']."/$listDomain");
220+
$mlManager = $app->getconf->get_server_config($conf['server_id'], 'mail')['mailinglist'];
186221

187-
$mlmmjmaintd='/usr/bin/mlmmj-maintd';
188-
// CRONENTRY="0 */2 * * * \"$MLMMJMAINTD -F -L $SPOOLDIR/$FQDN/$LISTNAME/\""
222+
if($mlManager == 'mlmmj') {
223+
$a=0;
224+
}
189225
}
190226

191227
private function getMlConfig() {
192-
$mlConfig = parse_ini_file($this->mlmmj_config_dir.'mlmmj.conf');
228+
$mlConfig = @parse_ini_file($this->mlmmj_config_dir.'mlmmj.conf');
193229

194230
// Force PHP7 to use # to mark comments
195231
if(PHP_MAJOR_VERSION >= 7)
@@ -198,16 +234,20 @@ private function getMlConfig() {
198234
return $mlConfig;
199235
}
200236

201-
private function chmodR($dir) {
202-
if($objs = glob($dir."/*")) {
203-
foreach($objs as $obj) {
204-
chown($obj, 'mlmmj');
205-
chgrp($obj, 'mlmmj');
206-
if(is_dir($obj)) $this->chmodR($obj);
237+
private function changeOwnership($path, $recursive=true) {
238+
if(basename($path) == '*') $path = dirname($path);
239+
240+
if(is_dir($path)) {
241+
if($objs = glob($path."/*")) {
242+
foreach($objs as $obj) {
243+
chown($obj, 'mlmmj');
244+
chgrp($obj, 'mlmmj');
245+
if(is_dir($obj) && $recursive) $this->changeOwnership($obj);
246+
}
207247
}
208248
}
209249

210-
return chown($dir, 'mlmmj') && chgrp($dir, 'mlmmj');
250+
return chown($path, 'mlmmj') && chgrp($path, 'mlmmj');
211251
}
212252

213253
private function rmdirR($path) {
@@ -284,4 +324,4 @@ private function checkSys() {
284324
}
285325
} // end class
286326

287-
?>
327+
?>

0 commit comments

Comments
 (0)