/**
 * File:	modules/UsersUI.ycp
 * Package:	Configuration of users and groups
 * Summary:	UI-related routines to be run from perl modules (Users.pm etc.)
 * Author:	Jiri Suchomel <jsuchome@suse.cz>
 *
 * $Id: UsersUI.ycp 20824 2005-01-21 13:31:10Z jsuchome $
 */
{
    module "UsersUI";
    textdomain "users";

    import "Label";

/**
 * Return the translated name for system user
 */
global define string SystemUserName (string name) ``{

// TODO Fo now (9.0), users are added manualy. For future, scripts which goes
// through all rpms would be better
map SystemUsers = $[

    /* User name for user: "root" */
    "root" : _("root"),
    /* User name for user: "bin" */
    "bin" : _("bin"),
    /* User name for user: "daemon" */
    "Daemon" : _("Daemon"),
    /* User name for user: "lp" */
    "Printing daemon" : _("Printing daemon"),
    /* User name for user: "mail" */
    "Mailer daemon" : _("Mailer daemon"),
    /* User name for user: "news" */
    "News system" : _("News system"),
    /* User name for user: "uucp" */
    "Unix-to-Unix CoPy system" : _("Unix-to-Unix CoPy system"),
    /* User name for user: "games" */
    "Games account" : _("Games account"),
    /* User name for user: "man" */
    "Manual pages viewer" : _("Manual pages viewer"),
    /* User name for user: "at" */
    "Batch jobs daemon" : _("Batch jobs daemon"),
    /* User name for user: "wwwrun" */
    "WWW daemon apache" : _("WWW daemon apache"),
    /* User name for user: "ftp" */
    "FTP account" : _("FTP account"),
    /* User name for user: "named" */
    "Nameserver daemon" : _("Name server daemon"),
    /* User name for user: "gdm" */
    "Gnome Display Manager daemon" : _("Gnome Display Manager daemon"),
    /* User name for user: "postfix" */
    "Postfix Daemon" : _("Postfix Daemon"),
    /* User name for user: "sshd" */
    "SSH daemon" : _("SSH daemon"),
    /* User name for user: "ntp" */
    "NTP daemon" : _("NTP daemon"),
    /* User name for user: "ldap" */
    "User for OpenLDAP" : _("User for OpenLDAP"),
    /* User name for user: "nobody" */
    "nobody" : _("nobody"),

    /* User name for user: "amanda" */
    "Amanda admin" : _("Amanda admin"),
    /* User name for user: "vscan" */
    "Vscan account" : _("Vscan account"),
    /* User name for user: "bigsister" */
    "Big Sister" : _("Big Sister"),
    /* User name for user: "wnn" */
    "Wnn System Account" : _("Wnn System Account"),
    /* User name for user: "cyrus" */
    "User for cyrus-imapd" : _("User for cyrus-imapd"),
    /* User name for user: "dpbox" */
    "DpBox account" : _("DpBox account"),
    /* User name for user: "gnats" */
    "Gnats Gnu Backtracking System" : _("Gnats Gnu Backtracking System"),
    /* User name for user: "gnump3d" */
    "GNUMP3 daemon" : _("GNUMP3 daemon"),
    /* User name for user: "hacluster" */
    "heartbeat processes" : _("heartbeat processes"),
    /* User name for user: "irc" */
    "IRC daemon" : _("IRC daemon"),
    /* User name for user: "mailman" */
    "GNU mailing list manager" : _("GNU mailing list manager"),
    /* User name for user: "mdom" */
    "Mailing list agent" : _("Mailing list agent"),
    /* User name for user: "mysql" */
    "MySQL database admin" : _("MySQL database admin"),
    /* User name for user: "oracle" */
    "Oracle user" : _("Oracle user"),
    /* User name for user: "postgres" */
    "PostgreSQL Server": _("PostgreSQL Server"),
    /* User name for user: "pop" */
    "POP admin" : _("POP admin"),
    /* User name for user: "sapdb" */
    "SAPDB account" : _("SAPDB account"),
    /* User name for user: "snort" */
    "Snort network monitor" : _("Snort network monitor"),
    /* User name for user: "squid" */
    "WWW-proxy squid" : _("WWW-proxy squid"),
    /* User name for user: "stunnel" */
    "Daemon user for stunnel (universal SSL tunnel)" : _("Daemon user for stunnel (universal SSL tunnel)"),
    /* User name for user: "zope" */
    "Zope" : _("Zope"),

    /* User name for user: "radiusd" */
    "Radius daemon" : _("Radius Daemon"),
    /* User name for user: "otrs" */
    "OTRS System User" : _("OTRS System User"),
    /* User name for user: "privoxy" */
    "Daemon user for privoxy" : _("Daemon User for privoxy"),
    /* User name for user: "vdr" */
    "Video Disk Recorder" : _("Video Disk Recorder"),

    /* User name for user: "icecream" */
    "Icecream Daemon" : _("Icecream Daemon"),

    ];
    return SystemUsers[name]:name;
}

/**
  * Ask user for configuration type (standard or NIS)
  * @param dir string directory with NIS settings
  * @return symbol `passwd or `nis or `abort
  */
define symbol getConfigurationType (string dir) ``{
    term contents = `VBox (
        // label
        `Label (_("You have installed an NIS master server.
It is configured to use a different database
of users and groups than the local system 
database in the /etc directory.
Select which one to configure.
")),
        `VSpacing (1),
        `RadioButtonGroup (`id (`configtype), `VBox (
        // radio button
        `RadioButton (`id (`passwd), `opt (`hstretch), _("&Local (/etc directory)"), true),
        `VSpacing (1),
        // radio button, %1 is path (eg. /etc)
        `RadioButton (`id (`nis), `opt (`hstretch), sformat(_("&NIS (%1 directory)"), dir), false)
        )),
        `VSpacing (1),
        `HBox (
        `HStretch (),
        `PushButton (`id (`ok), `opt(`default), Label::OKButton()),
        `HStretch (),
        `PushButton (`id (`abort), Label::AbortButton()),
        `HStretch ()
        )
    );
    UI::OpenDialog (contents);
    symbol ret = nil;
    while (ret == nil)
    {
        ret = (symbol) UI::UserInput ();
        if (ret != `cancel && ret != `ok)
            continue;
    }
    if (ret == `ok)
        ret = (symbol) UI::QueryWidget (`id (`configtype), `CurrentButton);
    UI::CloseDialog ();
    return ret;
}

/**
 * If we can ask and are a NIS server, ask which set of users
 * to administer and set UserWriteStack accordingly.
 * @param basedir the directory, where the data are stored
 * @return directory
 */
global define string ReadNISConfigurationType (string basedir)``{

    string ypdir = (string)SCR::Read(.sysconfig.ypserv.YPPWD_SRCDIR);
    while (substring (ypdir, size (ypdir) - 1) == "/")
    {
        ypdir = substring (ypdir, 0, size (ypdir) -1);
    }
    if (ypdir == "" || ypdir == nil)
        ypdir = basedir;
    if (ypdir != basedir)
    {
        symbol type = getConfigurationType (ypdir);
        if (type == `abort)
        {
            return nil;
        }
        if (type != `nis)
        {
            ypdir = basedir;
	}
    }
    return ypdir;
}

global define map ChooseTemplates (list<string> user_templates, list<string> group_templates)``{

    string user_template	= user_templates[0]:"";
    string group_template	= group_templates[0]:"";

    // label above radiobutton box
    term rbu_buttons = `VBox( `Left(`Label (_("User Templates"))));
    // label above radiobutton box
    term rbg_buttons = `VBox( `Left(`Label (_("Group Templates"))));
	foreach (string templ, user_templates, ``{
	    rbu_buttons = add (rbu_buttons,
		`Left(`RadioButton (`id(templ), templ, true)));
	});
	foreach (string templ, group_templates, ``{
	    rbg_buttons = add (rbg_buttons,
		`Left(`RadioButton (`id(templ), templ, true)));
	});
	term rb_users = `RadioButtonGroup (`id(`rbu), rbu_buttons);
	term rb_groups = `RadioButtonGroup (`id(`rbg), rbg_buttons);

	UI::OpenDialog (`opt(`decorated), `HBox (`HSpacing (1),
        `VBox(
            `HSpacing(50),
	    `VSpacing (0.5),
	    // label
            `Label (_("There are multiple templates defined as default. Select the one to read.")),
	    `VSpacing (0.5),
	    user_templates == [] ? `Empty(): rb_users,
	    `VSpacing (0.5),
	    group_templates == [] ? `Empty() : rb_groups,
            `HBox(
              `PushButton (`id(`ok),`opt(`default), Label::OKButton()),
              `PushButton (`id(`cancel), Label::CancelButton())
            )),
	`HSpacing (1))
	);
	any ret = UI::UserInput();
	if (ret == `ok)
	{
	    if (user_templates != [])
		user_template = (string) UI::QueryWidget (`id(`rbu), `CurrentButton);
	    if (group_templates != [])
		group_template = (string)UI::QueryWidget (`id(`rbg), `CurrentButton);
	}
	UI::CloseDialog();
    return $[
	"user"	: user_template,
	"group"	: user_template,
    ];
}

/**
 * Error messages for errors detected during reading via .passwd agent
 * @param error number returned by passwd-agent
 * @return error message
 */
global define string GetPasswdErrorMessage (integer errno, string more) ``{

    string last =
// error message 2/2 (= next sentence)
_("Correct them manually before running the YaST2 users module again.");

    map read_error = $[

    1: sformat(
// error message 1/2: %1 is file name, %2 is uid, %3 is next sentence (2/2)
_("There are multiple users with the same UID (\"%2\") in the file %1.
%3"), "/etc/passwd", more, last),

    2: sformat(
// error message 1/2: %1 is file name, %2 is username, %3 is next sentence (2/2)
_("There are multiple users with the same name (\"%2\") in the file %1.
%3"), "/etc/passwd", more, last),

    3: sformat(
// error message 1/2: %1 is file name, %2 is username, %3 is next sentence (2/2)
_("There are multiple users with the same name (\"%2\") in the file %1.
%3"), "/etc/shadow", more, last),

    5: sformat(
// error message 1/2: %1 is file name, %2 is gid, %3 is next sentence (2/2)
_("There are multiple groups with the same GID (\"%2\") in the file %1.
%3"), "/etc/group", more, last),

    6: sformat(
// error message 1/2: %1 is file name, %2 is groupname, %3 next sentence (2/2)
_("There are multiple groups with the same name (\"%2\") in the file %1.
%3"), "/etc/group", more, last),

    7: sformat(
// error message 1/2: %1 is file name, %2 is line
_("There is a strange line in the file %1:
%2
Perhaps the number of colons is wrong or some line entry is missing.
Correct the file manually before running the YaST2 users module again."),
"/etc/passwd", more),

    8: sformat(
// error message 1/2: %1 is file name, %2 is line
_("There is a strange line in the file %1:
%2
Perhaps the number of colons is wrong or some line entry is missing.
Correct the file manually before running the YaST2 users module again."),
"/etc/group", more),

    9: sformat(
// error message 1/2: %1 is file name
_("There is a strange line in the file %1.
Perhaps the number of colons is wrong or some line entry is missing.
Correct the file manually before running the YaST2 users module again."),
"/etc/shadow"),


];

    // default error message
    return read_error[errno]: _("Cannot read user or group data.");
}


/**
 * recode the string from "environment encoding" to UTF-8
 */
global define string RecodeUTF (string text) ``{

    return (string) UI::Recode (WFM::GetEnvironmentEncoding(), "UTF-8",text);
}

/**
 */
global define string HashPassword (string method, string pw) ``{

    if (method == "md5")
	return cryptmd5 (pw);
    if (method == "blowfish")
	return cryptblowfish (pw);
    return crypt (pw);
}


}//EOF
