Make --host argument optional

This commit is contained in:
Aleksander Machniak 2020-09-02 14:00:57 +02:00
parent f8236543b2
commit 40270f9d3f
3 changed files with 71 additions and 16 deletions

View file

@ -55,6 +55,11 @@ $action = $opts[0];
$rcmail = rcube::get_instance(rcube::INIT_WITH_DB | rcube::INIT_WITH_PLUGINS);
// Make --host argument optional where the default_host is a simple string
if (empty($opts['host'])) {
$opts['host'] = imap_host();
}
// connect to database
$db = $rcmail->get_dbh();
$db->db_connect('w');
@ -232,3 +237,23 @@ function authenticate(&$opts)
return $auth['valid'];
}
function imap_host()
{
global $rcmail;
$default_host = $rcmail->config->get('default_host');
if (is_array($default_host)) {
$key = key($default_host);
$imap_host = is_numeric($key) ? $default_host[$key] : $key;
}
else {
$imap_host = $default_host;
}
// strip protocol prefix
$uri = parse_url($imap_host);
if (!empty($uri['host'])) {
return $uri['host'];
}
}

View file

@ -31,7 +31,7 @@ require_once INSTALL_PATH . 'program/include/clisetup.php';
function print_usage()
{
print "Usage: randomcontacts.sh [OPTIONS] USERNAME FOLDER\n";
print "Create random contact that for then given user in the specified folder.\n";
print "Create random contact for a given user in a specified folder.\n";
print "-n, --num Number of contacts to be created, defaults to 50\n";
print "-h, --host IMAP host name\n";
print "-p, --password IMAP user password\n";
@ -53,11 +53,8 @@ $rcmail = rcube::get_instance(rcube::INIT_WITH_DB | rcube::INIT_WITH_PLUGINS);
$rcmail->plugins->load_plugins(array('libkolab'));
ini_set('display_errors', 1);
if (empty($opts['host'])) {
$opts['host'] = $rcmail->config->get('default_host');
if (is_array($opts['host'])) // not unique
$opts['host'] = null;
$opts['host'] = imap_host();
}
if (empty($opts['username']) || empty($opts['folder']) || empty($opts['host'])) {
@ -176,3 +173,24 @@ function random_string($len)
return rtrim($str);
}
function imap_host()
{
global $rcmail;
$default_host = $rcmail->config->get('default_host');
if (is_array($default_host)) {
$key = key($default_host);
$imap_host = is_numeric($key) ? $default_host[$key] : $key;
}
else {
$imap_host = $default_host;
}
// strip protocol prefix
$uri = parse_url($imap_host);
if (!empty($uri['host'])) {
return $uri['host'];
}
}

View file

@ -48,17 +48,7 @@ $imap_host = $opts['host'];
$rcmail = rcube::get_instance(rcube::INIT_WITH_DB | rcube::INIT_WITH_PLUGINS);
if (empty($imap_host)) {
$default_host = $rcmail->config->get('default_host');
if (is_array($default_host)) {
list($k,$v) = each($default_host);
$imap_host = is_numeric($k) ? $v : $k;
}
else {
$imap_host = $default_host;
}
// strip protocol prefix
$imap_host = preg_replace('!^[a-z]+://!', '', $imap_host);
$imap_host = imap_host();
}
if (empty($folder) || empty($imap_host)) {
@ -145,3 +135,25 @@ while ($result && ($sql_arr = $db->fetch_assoc($result))) {
print "----------------------------------------------------------------------------------\n";
echo "Done.\n";
function imap_host()
{
global $rcmail;
$default_host = $rcmail->config->get('default_host');
if (is_array($default_host)) {
$key = key($default_host);
$imap_host = is_numeric($key) ? $default_host[$key] : $key;
}
else {
$imap_host = $default_host;
}
// strip protocol prefix
$uri = parse_url($imap_host);
if (!empty($uri['host'])) {
return $uri['host'];
}
}