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 // Here we assume that the privatize_list.py script is located in the same dir as this script
82 $script_dir = dirname(__FILE__);
83 $privatize_cmd = escapeshellcmd(forge_get_config('mailman_path').'/bin/config_list -i '.$script_dir.'/privatize_list.py '.$listname);
84 $publicize_cmd = escapeshellcmd(forge_get_config('mailman_path').'/bin/config_list -i '.$script_dir.'/publicize_list.py '.$listname);
86 if (! in_array($listname,$mailing_lists)) { // New list?
87 $err .= "Creating Mailing List: $listname\n";
88 //$lcreate_cmd = forge_get_config('mailman_path')."/bin/newlist -q $listname@".forge_get_config('lists_host')." $email $listpassword &> /dev/null";
89 $lcreate_cmd = forge_get_config('mailman_path')."/bin/newlist -q $listname $email $listpassword";
90 $err .= "Command to be executed is $lcreate_cmd\n";
91 passthru($lcreate_cmd, $failed);
93 $err .= 'Failed to create '.$listname.", skipping\n";
97 // Privatize the new list
98 $err .= "Privatizing ".$listname.": ".$privatize_cmd."\n";
99 passthru($privatize_cmd,$privatizeFailed);
101 $mailingListIds[] = $grouplistid;
104 // Privatize only if it is marked as private
105 $err .= "Privatizing ".$listname.": ".$privatize_cmd."\n";
106 passthru($privatize_cmd,$privatizeFailed);
110 if(file_exists(forge_get_config('mailman_path').'/mail/mailman')) {
113 $listname.': "|'.forge_get_config('mailman_path').'/mail/mailman post '.$listname.'"'."\n"
114 .$listname.'-admin: "|'.forge_get_config('mailman_path').'/mail/mailman admin '.$listname.'"'."\n"
115 .$listname.'-bounces: "|'.forge_get_config('mailman_path').'/mail/mailman bounces '.$listname.'"'."\n"
116 .$listname.'-confirm: "|'.forge_get_config('mailman_path').'/mail/mailman confirm '.$listname.'"'."\n"
117 .$listname.'-join: "|'.forge_get_config('mailman_path').'/mail/mailman join '.$listname.'"'."\n"
118 .$listname.'-leave: "|'.forge_get_config('mailman_path').'/mail/mailman leave '.$listname.'"'."\n"
119 .$listname.'-owner: "|'.forge_get_config('mailman_path').'/mail/mailman owner '.$listname.'"'."\n"
120 .$listname.'-request: "|'.forge_get_config('mailman_path').'/mail/mailman request '.$listname.'"'."\n"
121 .$listname.'-subscribe: "|'.forge_get_config('mailman_path').'/mail/mailman subscribe '.$listname.'"'."\n"
122 .$listname.'-unsubscribe: "|'.forge_get_config('mailman_path').'/mail/mailman unsubscribe '.$listname.'"'."\n\n"
127 $listname.': "|'.forge_get_config('mailman_path').'/mail/wrapper post '.$listname.'"'."\n"
128 .$listname.'-admin: "|'.forge_get_config('mailman_path').'/mail/wrapper mailowner '.$listname.'"'."\n"
129 .$listname.'-request: "|'.forge_get_config('mailman_path').'/mail/wrapper mailcmd '.$listname.'"'."\n"
130 .$listname.'-owner: '.$listname.'-admin'."\n\n";
133 fwrite($h1,$list_str);
137 //if(!empty($mailingListIds)) {
138 db_query_params ('UPDATE mail_group_list set status=$1 WHERE status=$2',
139 array (MAIL__MAILING_LIST_IS_CREATED,
140 MAIL__MAILING_LIST_IS_REQUESTED));
147 //delete mailing lists
149 $res = db_query_params ('SELECT mailing_list_name FROM deleted_mailing_lists WHERE isdeleted = 0',
152 $rows = db_numrows($res);
154 for($k = 0; $k < $rows; $k++) {
155 $deleted_mail_list = db_result($res,$k,'mailing_list_name');
157 $deleted_mail_list = trim($deleted_mail_list);
158 if (!$deleted_mail_list) {
159 $err .= "Empty name for a mailing list in 'deleted_mailing_lists' table\n";
162 if (!preg_match('/^[a-z0-9\-_\.]*$/', $deleted_mail_list) || $deleted_mail_list == '.' || $deleted_mail_list == '..') {
163 $err .= 'Invalid List Name: ' . $deleted_mail_list;
166 exec(forge_get_config('mailman_path')."/bin/rmlist -a $deleted_mail_list", $output);
168 foreach ($output as $line) {
170 if (preg_match("/to finish removing/i", $line)) {
175 if (preg_match("/removing list info/i", $line)) {
181 $res1 = db_query_params ('UPDATE deleted_mailing_lists SET isdeleted = 1 WHERE mailing_list_name = $1',
182 array ($deleted_mail_list));
185 $err .= "Could not remove the list $deleted_mail_list \n";