5 // This script will read in a list existing mailing lists, then add the new lists
6 // and, finally, create the lists in a /var/lib/gforge/dumps/mailman-aliases file
7 // The /var/lib/gforge/dumps/mailman-aliases file will then be read by the mailaliases.php file
10 require dirname(__FILE__).'/../../www/env.inc.php';
11 require_once $gfcommon.'include/pre.php';
12 require $gfcommon.'include/cron_utils.php';
16 if (is_dir(forge_get_config('mailman_path'))) {
17 } elseif (is_dir("/usr/lib/mailman")) {
18 $sys_path_to_mailman="/usr/lib/mailman";
20 echo "\nsys_path_to_mailman path is not set right for this script!!";
24 // Extract the mailing lists that already exist on the system and create
25 // a "list" of them for use later so we don't try to create ones that
28 $mailing_lists=array();
29 $mlists_cmd = escapeshellcmd(forge_get_config('mailman_path')."/bin/list_lists");
30 //$err .= "Command to be executed is $mlists_cmd\n";
31 $fp = popen ($mlists_cmd,"r");
33 $mlist = fgets($fp, 4096);
34 if (stristr($mlist,"matching mailing lists") !== FALSE) {
37 $mlist = trim($mlist);
39 list($listname, $listdesc) = explode(" ",$mlist);
40 $mailing_lists[] = strtolower($listname);
44 // $err .= 'Existing mailing lists : '.implode(', ', $mailing_lists)."\n";
48 $res = db_query_params ('SELECT users.user_name,email,mail_group_list.list_name,
49 mail_group_list.password,mail_group_list.status,
50 mail_group_list.group_list_id,mail_group_list.is_public
51 FROM mail_group_list,users
52 WHERE mail_group_list.list_admin=users.user_id',
56 $rows=db_numrows($res);
57 //$err .= "$rows rows returned from query\n";
59 $h1 = fopen(forge_get_config('data_path').'/dumps/mailman-aliases',"w");
61 $mailingListIds = array();
63 for ($i=0; $i<$rows; $i++) {
64 $listadmin = db_result($res,$i,'user_name');
65 $email = db_result($res,$i,'email');
66 $listname = strtolower(db_result($res,$i,'list_name'));
67 $listpassword = db_result($res,$i,'password');
68 $grouplistid = db_result($res,$i,'group_list_id');
69 $public = db_result($res,$i,'is_public');
71 $listname = trim($listname);
73 $err .= "Empty name for a mailing list in 'mail_group_list' table\n";
76 if (!preg_match('/^[a-z0-9\-_\.]*$/', $listname) || $listname == '.' || $listname == '..') {
77 $err .= 'Invalid List Name: ' . $listname;
81 $listname = trim($listname);
83 $err .= "Empty name for a mailing list in 'mail_group_list' table\n";
86 if (!preg_match('/^[a-z0-9\-_\.]*$/', $listname) || $listname == '.' || $listname == '..') {
87 $err .= 'Invalid List Name: ' . $listname;
91 // Here we assume that the privatize_list.py script is located in the same dir as this script
92 $script_dir = dirname(__FILE__);
93 $privatize_cmd = escapeshellcmd(forge_get_config('mailman_path').'/bin/config_list -i '.$script_dir.'/privatize_list.py '.$listname);
94 $publicize_cmd = escapeshellcmd(forge_get_config('mailman_path').'/bin/config_list -i '.$script_dir.'/publicize_list.py '.$listname);
96 if (! in_array($listname,$mailing_lists)) { // New list?
97 $err .= "Creating Mailing List: $listname\n";
98 //$lcreate_cmd = forge_get_config('mailman_path')."/bin/newlist -q $listname@".forge_get_config('lists_host')." $email $listpassword &> /dev/null";
99 $lcreate_cmd = forge_get_config('mailman_path')."/bin/newlist -q $listname $email $listpassword";
100 $err .= "Command to be executed is $lcreate_cmd\n";
101 passthru($lcreate_cmd, $failed);
103 $err .= 'Failed to create '.$listname.", skipping\n";
107 // Privatize the new list
108 $err .= "Privatizing ".$listname.": ".$privatize_cmd."\n";
109 passthru($privatize_cmd,$privatizeFailed);
111 $mailingListIds[] = $grouplistid;
112 } elseif ($status == MAIL__MAILING_LIST_PW_RESET_REQUESTED) {
113 $change_pw_cmd = escapeshellcmd(forge_get_config ('mailman_path').'/bin/change_pw -l '.$listname);
114 $err .= "Resetting password of ".$listname."\n";
115 passthru($change_pw_cmd,$failed);
117 $err .= 'Failed to reset password of '.$listname."\n";
121 // Privatize only if it is marked as private
122 $err .= "Privatizing ".$listname.": ".$privatize_cmd."\n";
123 passthru($privatize_cmd,$privatizeFailed);
127 if(file_exists(forge_get_config('mailman_path').'/mail/mailman')) {
130 $listname.': "|'.forge_get_config('mailman_path').'/mail/mailman post '.$listname.'"'."\n"
131 .$listname.'-admin: "|'.forge_get_config('mailman_path').'/mail/mailman admin '.$listname.'"'."\n"
132 .$listname.'-bounces: "|'.forge_get_config('mailman_path').'/mail/mailman bounces '.$listname.'"'."\n"
133 .$listname.'-confirm: "|'.forge_get_config('mailman_path').'/mail/mailman confirm '.$listname.'"'."\n"
134 .$listname.'-join: "|'.forge_get_config('mailman_path').'/mail/mailman join '.$listname.'"'."\n"
135 .$listname.'-leave: "|'.forge_get_config('mailman_path').'/mail/mailman leave '.$listname.'"'."\n"
136 .$listname.'-owner: "|'.forge_get_config('mailman_path').'/mail/mailman owner '.$listname.'"'."\n"
137 .$listname.'-request: "|'.forge_get_config('mailman_path').'/mail/mailman request '.$listname.'"'."\n"
138 .$listname.'-subscribe: "|'.forge_get_config('mailman_path').'/mail/mailman subscribe '.$listname.'"'."\n"
139 .$listname.'-unsubscribe: "|'.forge_get_config('mailman_path').'/mail/mailman unsubscribe '.$listname.'"'."\n\n"
144 $listname.': "|'.forge_get_config('mailman_path').'/mail/wrapper post '.$listname.'"'."\n"
145 .$listname.'-admin: "|'.forge_get_config('mailman_path').'/mail/wrapper mailowner '.$listname.'"'."\n"
146 .$listname.'-request: "|'.forge_get_config('mailman_path').'/mail/wrapper mailcmd '.$listname.'"'."\n"
147 .$listname.'-owner: '.$listname.'-admin'."\n\n";
150 fwrite($h1,$list_str);
154 //if(!empty($mailingListIds)) {
155 db_query_params ('UPDATE mail_group_list set status=$1 WHERE status=$2',
156 array (MAIL__MAILING_LIST_IS_CREATED,
157 MAIL__MAILING_LIST_IS_REQUESTED));
164 //delete mailing lists
166 $res = db_query_params ('SELECT mailing_list_name FROM deleted_mailing_lists WHERE isdeleted = 0',
169 $rows = db_numrows($res);
171 for($k = 0; $k < $rows; $k++) {
172 $deleted_mail_list = db_result($res,$k,'mailing_list_name');
174 $deleted_mail_list = trim($deleted_mail_list);
175 if (!$deleted_mail_list) {
176 $err .= "Empty name for a mailing list in 'deleted_mailing_lists' table\n";
179 if (!preg_match('/^[a-z0-9\-_\.]*$/', $deleted_mail_list) || $deleted_mail_list == '.' || $deleted_mail_list == '..') {
180 $err .= 'Invalid List Name: ' . $deleted_mail_list;
185 $deleted_mail_list = trim($deleted_mail_list);
186 if (!$deleted_mail_list) {
187 $err .= "Empty name for a mailing list in 'deleted_mailing_lists' table\n";
190 if (!preg_match('/^[a-z0-9\-_\.]*$/', $deleted_mail_list) || $deleted_mail_list == '.' || $deleted_mail_list == '..') {
191 $err .= 'Invalid List Name: ' . $deleted_mail_list;
194 exec(forge_get_config('mailman_path')."/bin/rmlist -a $deleted_mail_list", $output);
196 foreach ($output as $line) {
198 if (preg_match("/to finish removing/i", $line)) {
203 if (preg_match("/removing list info/i", $line)) {
209 $res1 = db_query_params ('UPDATE deleted_mailing_lists SET isdeleted = 1 WHERE mailing_list_name = $1',
210 array ($deleted_mail_list));
213 $err .= "Could not remove the list $deleted_mail_list \n";