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');
70 $status = db_result($res,$i,'status');
72 $listname = trim($listname);
74 $err .= "Empty name for a mailing list in 'mail_group_list' table\n";
77 if (!preg_match('/^[a-z0-9\-_\.]*$/', $listname) || $listname == '.' || $listname == '..') {
78 $err .= 'Invalid List Name: ' . $listname;
82 $is_commits_list = preg_match('/-commits$/', $listname);
84 // Hack to Disable auto-public of listname.
85 $is_commits_list = false;
87 // Here we assume that the privatize_list.py script is located in the same dir as this script
88 $script_dir = dirname(__FILE__);
89 $privatize_cmd = escapeshellcmd(forge_get_config('mailman_path').'/bin/config_list -i '.$script_dir.'/privatize_list.py '.$listname);
90 $publicize_cmd = escapeshellcmd(forge_get_config('mailman_path').'/bin/config_list -i '.$script_dir.'/publicize_list.py '.$listname);
92 if (! in_array($listname,$mailing_lists)) { // New list?
93 $err .= "Creating Mailing List: $listname\n";
94 //$lcreate_cmd = forge_get_config('mailman_path')."/bin/newlist -q $listname@".forge_get_config('lists_host')." $email $listpassword &> /dev/null";
95 $lcreate_cmd = forge_get_config('mailman_path')."/bin/newlist -q $listname $email $listpassword";
96 $err .= "Command to be executed is $lcreate_cmd\n";
97 passthru($lcreate_cmd, $failed);
99 $err .= 'Failed to create '.$listname.", skipping\n";
102 db_query_params('UPDATE mail_group_list set status=$1 WHERE status=$2 and group_list_id=$3',
103 array(MAIL__MAILING_LIST_IS_CREATED,
104 MAIL__MAILING_LIST_IS_REQUESTED,
107 if ($is_commits_list || $public) {
108 // Make the *-commits list public
109 $err .= "Making ".$listname." public: ".$publicize_cmd."\n";
110 passthru($publicize_cmd,$failed);
112 // Privatize the new list
113 $err .= "Privatizing ".$listname.": ".$privatize_cmd."\n";
114 passthru($privatize_cmd,$failed);
116 $fixurl_cmd = escapeshellcmd(forge_get_config('mailman_path')."/bin/withlist -l -r fix_url $listname -u ".forge_get_config('lists_host'));
117 passthru($fixurl_cmd,$failed);
119 db_query_params('UPDATE mail_group_list set status=$1 WHERE status=$2 and group_list_id=$3',
120 array(MAIL__MAILING_LIST_IS_CONFIGURED,
121 MAIL__MAILING_LIST_IS_CREATED,
125 $err .= 'Failed to configure '.$listname."\n";
129 $mailingListIds[] = $grouplistid;
130 } elseif ($status == MAIL__MAILING_LIST_IS_CREATED) {
131 if ($is_commits_list || $public) {
132 // Make the *-commits list public
133 $err .= "Making ".$listname." public: ".$publicize_cmd."\n";
134 passthru($publicize_cmd,$failed);
136 // Privatize the new list
137 $err .= "Privatizing ".$listname.": ".$privatize_cmd."\n";
138 passthru($privatize_cmd,$failed);
140 $fixurl_cmd = escapeshellcmd(forge_get_config('mailman_path')."/bin/withlist -l -r fix_url $listname -u ".forge_get_config('lists_host'));
141 passthru($fixurl_cmd,$failed);
143 db_query_params('UPDATE mail_group_list set status=$1 WHERE status=$2 and group_list_id=$3',
144 array(MAIL__MAILING_LIST_IS_CONFIGURED,
145 MAIL__MAILING_LIST_IS_CREATED,
149 $err .= 'Failed to configure '.$listname."\n";
152 } elseif ($status == MAIL__MAILING_LIST_IS_UPDATED) {
153 // For already created list, update only if status was changed on the forge to
154 // avoid anwanted reset of parameters.
156 // Get the mailman info on public/private to change
157 if ($is_commits_list || $public) {
158 $err .= "Making ".$listname." public: ".$publicize_cmd."\n";
159 passthru($publicize_cmd, $failed);
160 } elseif (!$public) {
161 // Privatize only if it is marked as private
162 $err .= "Privatizing ".$listname.": ".$privatize_cmd."\n";
163 passthru($privatize_cmd, $failed);
166 $err .= 'Failed to update '.$listname."\n";
168 db_query_params('UPDATE mail_group_list set status=$1 WHERE status=$2 and group_list_id=$3',
169 array(MAIL__MAILING_LIST_IS_CONFIGURED,
170 MAIL__MAILING_LIST_IS_UPDATED,
174 } elseif ($status == MAIL__MAILING_LIST_PW_RESET_REQUESTED) {
175 $change_pw_cmd = escapeshellcmd(forge_get_config ('mailman_path').'/bin/change_pw -l '.$listname);
176 $err .= "Resetting password of ".$listname."\n";
177 exec($change_pw_cmd, $returnnewpasswd, $failed);
179 $err .= 'Failed to reset password of '.$listname."\n";
181 $arrayReturnNewPasswd = explode(' ', $returnnewpasswd[0]);
182 $newpasswd = trim(end($arrayReturnNewPasswd));
183 db_query_params('UPDATE mail_group_list set (status, password) = ($1, $2) WHERE status=$3 and group_list_id=$4',
184 array(MAIL__MAILING_LIST_IS_CONFIGURED,
186 MAIL__MAILING_LIST_PW_RESET_REQUESTED,
192 // Privatize only if it is marked as private
193 $err .= "Privatizing ".$listname.": ".$privatize_cmd."\n";
194 passthru($privatize_cmd,$privatizeFailed);
198 if(file_exists(forge_get_config('mailman_path').'/mail/mailman')) {
201 $listname.': "|'.forge_get_config('mailman_path').'/mail/mailman post '.$listname.'"'."\n"
202 .$listname.'-admin: "|'.forge_get_config('mailman_path').'/mail/mailman admin '.$listname.'"'."\n"
203 .$listname.'-bounces: "|'.forge_get_config('mailman_path').'/mail/mailman bounces '.$listname.'"'."\n"
204 .$listname.'-confirm: "|'.forge_get_config('mailman_path').'/mail/mailman confirm '.$listname.'"'."\n"
205 .$listname.'-join: "|'.forge_get_config('mailman_path').'/mail/mailman join '.$listname.'"'."\n"
206 .$listname.'-leave: "|'.forge_get_config('mailman_path').'/mail/mailman leave '.$listname.'"'."\n"
207 .$listname.'-owner: "|'.forge_get_config('mailman_path').'/mail/mailman owner '.$listname.'"'."\n"
208 .$listname.'-request: "|'.forge_get_config('mailman_path').'/mail/mailman request '.$listname.'"'."\n"
209 .$listname.'-subscribe: "|'.forge_get_config('mailman_path').'/mail/mailman subscribe '.$listname.'"'."\n"
210 .$listname.'-unsubscribe: "|'.forge_get_config('mailman_path').'/mail/mailman unsubscribe '.$listname.'"'."\n\n"
215 $listname.': "|'.forge_get_config('mailman_path').'/mail/wrapper post '.$listname.'"'."\n"
216 .$listname.'-admin: "|'.forge_get_config('mailman_path').'/mail/wrapper mailowner '.$listname.'"'."\n"
217 .$listname.'-request: "|'.forge_get_config('mailman_path').'/mail/wrapper mailcmd '.$listname.'"'."\n"
218 .$listname.'-owner: '.$listname.'-admin'."\n\n";
221 fwrite($h1,$list_str);
226 //delete mailing lists
228 $res = db_query_params ('SELECT mailing_list_name FROM deleted_mailing_lists WHERE isdeleted = 0',
231 $rows = db_numrows($res);
233 for($k = 0; $k < $rows; $k++) {
234 $deleted_mail_list = db_result($res,$k,'mailing_list_name');
236 $deleted_mail_list = trim($deleted_mail_list);
237 if (!$deleted_mail_list) {
238 $err .= "Empty name for a mailing list in 'deleted_mailing_lists' table\n";
241 if (!preg_match('/^[a-z0-9\-_\.]*$/', $deleted_mail_list) || $deleted_mail_list == '.' || $deleted_mail_list == '..') {
242 $err .= 'Invalid List Name: ' . $deleted_mail_list;
246 exec(forge_get_config('mailman_path')."/bin/rmlist -a '$deleted_mail_list'", $output);
248 foreach ($output as $line) {
250 if (preg_match("/to finish removing/i", $line)) {
255 if (preg_match("/removing list info/i", $line)) {
261 $res1 = db_query_params ('UPDATE deleted_mailing_lists SET isdeleted = 1 WHERE mailing_list_name = $1',
262 array ($deleted_mail_list));
265 $err .= "Could not remove the list $deleted_mail_list \n";