head	1.4;
access;
symbols
	TIGRIS_NOV_12_2000:1.3
	OLDHELM:1.3.0.4
	TIGRIS_NOV_11_2000:1.3
	TIGRIS_SEP_13_2000:1.3.0.2
	TIGRIS_BASELINE:1.3;
locks; strict;
comment	@# @;


1.4
date	2000.11.12.23.10.06;	author dlr;	state dead;
branches;
next	1.3;

1.3
date	2000.09.20.03.13.14;	author brian;	state Exp;
branches;
next	1.2;

1.2
date	2000.06.22.23.39.30;	author dlr;	state Exp;
branches;
next	1.1;

1.1
date	2000.06.01.01.03.28;	author ed;	state Exp;
branches;
next	;


desc
@@


1.4
log
@Does not exist--possibly only WRT this location--in newhelm.
@
text
@/* ================================================================
 * Copyright (c) 2000 Collab.Net.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by Collab.Net (http://www.Collab.Net/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of Collab.Net.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of Collab.Net.
 */


package org.tigris.helm.admin;

import org.webmacro.util.*;
import org.webmacro.servlet.*;
import org.webmacro.engine.*;
import org.webmacro.broker.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.sun.java.util.collections.ArrayList;
import com.sun.java.util.collections.Iterator;
import com.sun.java.util.collections.ListIterator;
import java.sql.*;
import gnu.regexp.RE;

import org.joist.security.PermissionSet;
import org.joist.security.RedirectException;
import org.joist.security.SecureServlet;
import org.joist.session.SessionManager;
import org.joist.database.DataManager;

import org.tigris.helm.database.TUserManager;
import org.tigris.helm.tool.TigrisUpdate;
import org.tigris.helm.tool.UpdateException;
import org.tigris.helm.tool.MailingList;

import java.util.StringTokenizer;

import java.net.URL;
import java.net.URLEncoder;

import java.io.BufferedReader;
import java.io.InputStreamReader;


/**
 * Present a form for editing one of the ezmlm "files"  -- this includes
 * the subscribers, the moderators, the digest list, and so on.
 * This should not be used by ordinary subscribers -- only project
 * admins/owners.
 * Parts taken from ProjMailEdit.java, parts taken from sourcexchange.
 */
public class ProjMailFileEdit extends SecureServlet {
    static private Log log = new Log("pmlEdt", "ProjMailFileEdit");
    static private boolean debug = true;

    static private RE emailRE = null;
    static private RE listnameRE = null;
    
    public void start() throws ServletException
    {
        super.start();
        try {
            emailRE = new RE("^[\\w\\-\\._=\\!#\\*\\+]+@@([\\w\\-]+\\.)+[a-zA-Z]{2,3}$");
            listnameRE= new RE("^[a-zA-Z0-9_]+$");
        }
        catch (gnu.regexp.REException e) {
            log.exception(e);
        }
    }

    // we should probably add a way to do validation.  but for now,
    // ezmlm will take care of that for us.
    public String parseList (String list) {
        StringBuffer buff = new StringBuffer();
        StringTokenizer tok = new StringTokenizer(list);
        while (tok.hasMoreTokens()) {
            buff.append(tok.nextToken()).append(" ");
        }
        return buff.toString();
    }

    
    public String doListCommand(String script, String params)
        throws HandlerException
    {
        try {
            // FIXME: if we move the mailhost to another machine, 
            // we need to adjust this accordingly.
            URL url = new URL("http://localhost/local-cgi/maillist.cgi?" +
                    "script=" + script + "&params=" +
                    URLEncoder.encode(params) + "&mailhost=localhost");
            if (debug) log.debug("list command: " + url.toString());
            BufferedReader r = new BufferedReader(
                    new InputStreamReader(url.openStream()));
            StringBuffer results = new StringBuffer(512);
            String line;
            while ((line = r.readLine()) != null)
                results.append(line).append("\n");
            r.close();
            if (debug) log.debug("list command done");
            return results.toString();
        }
        catch (Exception e) {
            throw new HandlerException("Unable to execute mail list cmd: " + e);
        }
    }
    
    protected void subscribe(String fileName, WebContext context) throws
           HandlerException
    {
        String members = context.getForm("subscribe");
        if (members == null || members.equals("")) {
            context.put("info", "no new subscriptions: no addresses provided");
            return;
        }
        StringBuffer cmd = new StringBuffer();
        cmd.append(fileName).append(" ");
        String list = parseList(members);
        cmd.append(list);
        if (debug) log.debug(cmd.toString());
        String results = doListCommand("list-subscribe",cmd.toString());
        if (!results.trim().equals(""))
           throw new HandlerException("subscribe failed: "+results);
        context.put("info", "new subscribers: "+list);
    }

    protected void unsubscribe(String fileName, WebContext context) throws
            HandlerException
    {
        String members[] = context.getFormList("unsubscribe");
        if (members == null || members[0] == null || members[0].equals(""))  {
            context.put("info","no unsubscriptions: no addresses selected");
            return;
        }
        StringBuffer cmd = new StringBuffer();
        cmd.append(fileName).append(" ");
        StringBuffer listBuff = new StringBuffer();
        for (int i = 0; i < members.length ; ++i) {
            if (members[i] != null) {
                listBuff.append(" ").append(members[i]);
            }
        }
        String list = listBuff.toString();
        if (debug) log.debug("list for unsub: "+list);
        cmd.append(list);
        if (debug) log.debug(cmd.toString());
        String results = doListCommand("list-unsubscribe", cmd.toString());
        if (!results.trim().equals(""))
            throw new HandlerException("unsubscribe failed: "+results);
        context.put("info", "unsubscribed: "+list);
    }

    protected String[] getMembers(String fileName, WebContext context)
            throws HandlerException
    {
        try {
            StringBuffer cmd = new StringBuffer();
            String filter = context.getForm("filter");
            context.put("filter", filter != null ? filter : "");
            if ("".equals(filter))
                filter = null;

            if (filter != null) {
                context.put("filter_info", "entries containing: " + filter);
            }

            // this stuff is for using regular expressions -- which
            // we're not doing right now.
            /* RE filterRE = null;
            if (filter != null) {
                try {
                    filterRE = new RE(filter);
                    context.put("filter_info", "addresses matching RE: '" +
                            filter + "'");
                }
                catch (gnu.regexp.REException e) {
                    log.exception(e);
                    filterRE = null;
                    context.put("filter_info", "unable to compile RE: '" +
                            filter + "' -- error: " + e.toString());
                }
            } */
            /* this goes below, in place of the member.indexOf(filter) line:
                if (filterRE == null || filterRE.isMatch(member)) 
                */

            cmd.append(fileName);
            if (debug) log.debug(cmd.toString());
            String results = doListCommand("list-list",cmd.toString());
            StringTokenizer tokenizer = new StringTokenizer(results);
            ArrayList membersList = new ArrayList();
            while (tokenizer.hasMoreTokens()) {
                String member = tokenizer.nextToken();
                membersList.add(member);
            }
            int count = 0;
            for (ListIterator i = membersList.listIterator() ; i.hasNext();) {
                String member = (String) i.next();
                if (filter == null || member.indexOf(filter) >= 0) {
                    ++count;
                }
            }

            String [] members = new String[count];
            int idx = 0;
            for (ListIterator i = membersList.listIterator(); i.hasNext();) {
                String member = (String) i.next();
                if (filter == null || member.indexOf(filter) >= 0) {
                    members[idx++] = member;
                }
            }
            return members;
        }
        catch (HandlerException e) {
            throw new HandlerException("Unable to list mail list " + fileName +
                    ": " + e.getMessage() + "/" + e);
        }
    }
            

    protected String processRequest(Connection connection, WebContext context)
        throws HandlerException, RedirectException
    {
        String templateName = super.processRequest(connection, context);
        // super sets $UserName, Cancel

        // Get the userID and permissions of the person who logged in.
        String loginUserID = (String)context.get("loginUserID");
        PermissionSet userPerms = (PermissionSet)context.get("UserPermissions");

        SessionManager sMgr = getSessionManager(context);
        DataManager dMgr = sMgr.getDataManager();
        TUserManager uMgr = (TUserManager)
            dMgr.getManager("org.tigris.helm.database.TUserManager");

        ArrayList missingFields = new ArrayList();
        ArrayList validationErrors = new ArrayList();
        boolean listExists = false;

        String project = context.getForm("project");
        if (project == null)
            redirectToError(context, "ProjMailFileEdit: No project specified");
        context.put("project", project);

        String list = context.getForm("list");
        if (list == null)
            redirectToError(context, "ProjMailFileEdit: No list specified");
        context.put("listname", list);

        String file = context.getForm("file");
        context.put("file", file != null ? file : "");

        MailingList mList;
        try {
              mList = new MailingList(connection, project, list);
        }
        catch (MailingList.DoesNotExist e) {
            redirectToError(context, "ProjMailFileEdit: List " + project +
                    '-' + list + " not present in database");
            throw new HandlerException(e.getMessage());
        }
        catch (SQLException e) {
            log.exception(e);
            String message = "ProjMailFileEdit: " + e.getMessage();
            if (debug) log.debug(message);
                throw new HandlerException(e.getMessage());
        }

        context.put("changed", Boolean.FALSE);

        // FIXME: this is hopelessly broken so far as sandboxes go ...
        // but it'll work for netbeans, for now.  getting to the
        // data isn't a simple matter right now (the accessor classes
        // are in Perl)...
        String fileName = "~tigrisq/" + project + "-" + list;
        if (file != null) {
            if (file.equals("Moderators"))
                fileName = fileName + "/mod";
            else if (file.equals("Digest Subscribers"))
                fileName = fileName + "/digest";
            // otherwise, use the normal subscribers list
        }
    
        boolean isProjectOwner = false;
        context.put("isProjectOwner", Boolean.FALSE);
    
        try {
            String userID = sMgr.getUserID();
            String loginID = uMgr.getLoginID(connection, userID);
            isProjectOwner = uMgr.isProjectOwner(connection,
                    loginID, project);
            context.put("isProjectOwner",
                    isProjectOwner ? Boolean.TRUE : Boolean.FALSE);
        }
        catch (SQLException e) {
            log.exception(e);
            String message = "ProjectUserEdit: " + e.getMessage();
            if (debug) log.debug(message);
            context.put("message", "ProjectUserEdit got an SQLException");
        }
        catch (TUserManager.NoSuchUserException e) {
              log.exception(e);
              String message = "ProjectUserEdit: " + e.getMessage();
              if (debug) log.debug(message);
              throw new HandlerException(e.getMessage());
        }
    
        // Determine which button was pushed
        String action = (String) context.get("action");

        context.put("info", "");
        // Allow edits?
        if (validationErrors.isEmpty() && !action.equals("") &&
                userPerms.accessAllowed("ProjMail - Change") &&
                isProjectOwner) {

            if (action.equals("Subscribe")) {
                if (debug) log.debug("Button=Subscribe");
                subscribe(fileName, context);
                context.put("changed", Boolean.TRUE);
                if (debug) log.debug("Button=Subscribe");

                // for reference :
//                catch (SQLException e) {
//                    log.exception(e);
//                    String message = "ProjMailFileEdit: " + e.getMessage();
//                    if (debug) log.debug(message);
//                    throw new HandlerException(e.getMessage());
//                }
            }
            else if (action.equals("Unsubscribe")) {
                if (debug) log.debug("Button=Unsubscribe");
                unsubscribe(fileName, context);
                context.put("changed", Boolean.TRUE);
                if (debug) log.debug("Button=Unsubscribe");
            }
            else if (action.equals("Filter")) {
                if (debug) log.debug("Button=Filter");
            }
            else {
                // assume standard view
            }
        }
        // // TODO: finish, why don't List's work with webmacro?
        // context.put("options", mList.getPrivArray());
        // if (debug) log.debug("step 4 Options="+ mList.getPrivArray());
    
        if (debug) log.debug("step 3");
        String members[] = getMembers(fileName, context);
        context.put("members", members);

        context.put("missingFields", missingFields);
        context.put("validationErrors", validationErrors);
    
        return templateName;
    }
} /* end class ProjMailFileEdit */
@


1.3
log
@
Obtained from:
Submitted by:
Reviewed by:

Moved from org.helm to org.tigris.helm
Moved from org/helm to org/tigris/helm

perl -pi.bak -e, how I love thee

note: this update was done POST-CVS-REPOSITORY surgury.  In order to
get this right, you may need to remove the directories

$CVSROOT/helm/java/org/helm
$CVSROOT/helm/data/htdoc/look/www/org/helm
$CVSROOT/helm/templates/org/helm

before doing the "CVS update" call, since those directories don't
exist anymore and CVS will try to create lockfiles for you in them,
giving you an error.
@
text
@@


1.2
log
@Switched from DataManager.getXxxManger() API to DataManager.getManager("xxx.xxx.xxx").  Please note that this has the possiblity of introducing minor bugs if I did not type all the fully qualified class names properly.
@
text
@d48 1
a48 1
package org.helm.admin;
d70 4
a73 4
import org.helm.database.TUserManager;
import org.helm.tool.TigrisUpdate;
import org.helm.tool.UpdateException;
import org.helm.tool.MailingList;
d273 1
a273 1
            dMgr.getManager("org.helm.database.TUserManager");
@


1.1
log
@add ProjMailFileEdit, used to edit ezmlm lists (subscribers, digest
subscribers, moderators, etc.)
PR:
Obtained from:
Submitted by:
Reviewed by:
@
text
@d272 2
a273 1
        TUserManager uMgr = (TUserManager) dMgr.getUserManager();
@

