<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># jabber-lib.pl
# Common functions for editing the jabber config files
#
# XXX - http://prdownloads.sourceforge.net/expat/expat-1.95.2-1.i686.rpm
#     - XML::Parser  XML::Generator
# XXX - admin &lt;read&gt; and &lt;write&gt; - what do they mean?

BEGIN { push(@INC, ".."); };
use WebminCore;
&amp;init_config();

if ($config{'jabber_lib'}) {
	$ENV{$gconfig{'ld_env'}} .= ':' if ($ENV{$gconfig{'ld_env'}});
	$ENV{$gconfig{'ld_env'}} .= $config{'jabber_lib'};
	}

eval "use XML::Parser; \$got_xml_parser++";
eval "use XML::Generator; \$got_xml_generator++";

# get_jabber_config()
# Parse the jabber XML config file
sub get_jabber_config
{
return $get_jabber_config_cache if (defined($get_jabber_config_cache));
local $xml = new XML::Parser('Style' =&gt; 'Tree');
eval { $get_jabber_config_cache = $xml-&gt;parsefile($config{'jabber_config'}); };
if ($@) {
	return $@;
	}
return $get_jabber_config_cache;
}

# find(name, &amp;config)
sub find
{
local (@rv, $i);
local $list = $_[1]-&gt;[1];
for($i=1; $i&lt;@$list; $i+=2) {
	if (lc($list-&gt;[$i]) eq lc($_[0])) {
		push(@rv, [ $list-&gt;[$i], $list-&gt;[$i+1], $i ]);
		}
	}
return wantarray ? @rv : $rv[0];
}

# find_value(name, &amp;config)
sub find_value
{
local @rv = map { &amp;value_in($_) } &amp;find($_[0], $_[1]);
return wantarray ? @rv : $rv[0];
}

# jabber_pid_file()
# Returns the PID file used by jabber
sub jabber_pid_file
{
local $conf = &amp;get_jabber_config();
local $pidfile = &amp;find_value("pidfile", $conf);
if ($pidfile =~ /^\//) {
	return $pidfile;
	}
elsif ($pidfile) {
	return "$config{'jabber_dir'}/$pidfile";
	}
else {
	return "$config{'jabber_dir'}/jabber.pid";
	}
}

# value_in(&amp;tag)
sub value_in
{
return undef if (!$_[0]);
local $zero = &amp;find("0", $_[0]);
return $zero ? $zero-&gt;[1] : undef;
}

# generate_config(&amp;tree, &amp;gen)
# Returns an XML::Generator object created from a config tree structure
sub generate_config
{
local $gen = $_[1] ? $_[1] : XML::Generator-&gt;new(escape =&gt; 'always');
local $list = $_[0]-&gt;[1];
local (@mems, $i);
for($i=1; $i&lt;@$list-1; $i+=2) {
	if ($list-&gt;[$i] eq '0') {
		push(@mems, $list-&gt;[$i+1]);
		}
	else {
		push(@mems, &amp;generate_config(
				[ $list-&gt;[$i], $list-&gt;[$i+1] ], $gen));
		}
	}
local $name = $_[0]-&gt;[0];
return $gen-&gt;$name($list-&gt;[0], @mems);
}

# save_jabber_config(&amp;config)
sub save_jabber_config
{
&amp;open_tempfile(CONFIG, "&gt;$config{'jabber_config'}");
local $xml = &amp;generate_config($_[0]);
&amp;print_tempfile(CONFIG, $xml);
&amp;print_tempfile(CONFIG, "\n");
&amp;close_tempfile(CONFIG);
}

# save_directive(&amp;config, name|&amp;old, &amp;new)
# Replaces all directives of some name with new values
sub save_directive
{
local @ov = ref($_[1]) ? @{$_[1]} : &amp;find($_[1], $_[0]);
local @nv = @{$_[2]};
local ($i, $j);
for($i=0; $i&lt;@ov || $i&lt;@nv; $i++) {
	local $idx = $ov[$i]-&gt;[2] if ($ov[$i]);
	if ($ov[$i] &amp;&amp; $nv[$i]) {
		# Updating an existing value
		$_[0]-&gt;[1]-&gt;[$idx] = $nv[$i]-&gt;[0];
		$_[0]-&gt;[1]-&gt;[$idx+1] = $nv[$i]-&gt;[1];
		}
	elsif ($ov[$i]) {
		# Deleting an old value
		splice(@{$_[0]-&gt;[1]}, $idx, 2);
		map { $_-&gt;[2] -= 2 if ($_-&gt;[2] &gt;= $idx) } @ov;
		}
	else {
		# Adding a new value after the last non-text one
		local $nt = -1;
		for($j=1; $j&lt;@{$_[0]-&gt;[1]}; $j+=2) {
			$nt = $j if ($_[0]-&gt;[1]-&gt;[$j] ne '0');
			}
		splice(@{$_[0]-&gt;[1]}, $nt+2, 0, $nv[$i]-&gt;[0], $nv[$i]-&gt;[1]);
		}
	}
}

# find_by_tag(name, tag, value, &amp;config)
sub find_by_tag
{
local @m = &amp;find($_[0], $_[3]);
@m = grep { lc($_-&gt;[1]-&gt;[0]-&gt;{lc($_[1])}) eq lc($_[2]) } @m;
return wantarray ? @m : $m[0];
}

# xml_string(name, &amp;tree, ...)
# Converts a list of XML structures into text
sub xml_string
{
local $rv = "";
for($i=0; $i&lt;@_; $i+=2) {
	local $xml = &amp;generate_config([ $_[$i], $_[$i+1] ]);
	if ($xml =~ /\S/) {
		$rv .= $xml."\n";
		}
	}
return $rv;
}

# get_jabberd_version(&amp;out)
sub get_jabberd_version
{
local $jabberd = $config{'jabber_daemon'} ? $config{'jabber_daemon'}
				    : "$config{'jabber_dir'}/bin/jabberd";
local $out = `$jabberd -v 2&gt;&amp;1`;
${$_[0]} = $out;
return $out =~ /\s(1\.4\S*)/ ? $1 : undef;
}

# stop_jabber()
# Stops jabber, and returns undef on success or an error message on failure
sub stop_jabber
{
if ($config{'stop_cmd'}) {
        &amp;system_logged("$config{'stop_cmd'} &lt;/dev/null &gt;/dev/null 2&gt;&amp;1");
        }
else {
	local $pid = &amp;check_pid_file(&amp;jabber_pid_file());
	if ($pid) {
		&amp;kill_logged('TERM', $pid) || return $!;
		}
	else {
		return $text{'stop_epid'};
		}
        }
unlink(&amp;jabber_pid_file());
return undef;
}

# start_jabber()
# Starts jabber, and returns undef on success or an error message on failure
sub start_jabber
{
&amp;system_logged("$config{'start_cmd'} &lt;/dev/null &gt;/tmp/err 2&gt;&amp;1");
return undef;
}

@register_fields = ( 'name', 'email' );

@karma_presets = ( { 'heartbeat' =&gt; 2,	'init' =&gt; 10,
		     'max' =&gt; 10,	'inc' =&gt; 1,
		     'dec' =&gt; 1,	'penalty' =&gt; -6,
		     'restore' =&gt; 10 },
		   { 'heartbeat' =&gt; 2,	'init' =&gt; 50,
		     'max' =&gt; 50,	'inc' =&gt; 4,
		     'dec' =&gt; 1,	'penalty' =&gt; -5,
		     'restore' =&gt; 50 },
		   { 'heartbeat' =&gt; 2,	'init' =&gt; 64,
		     'max' =&gt; 64,	'inc' =&gt; 6,
		     'dec' =&gt; 1,	'penalty' =&gt; -3,
		     'restore' =&gt; 64 }
		  );

@filter_conds = ( "ns", "unavailable", "from", "resource", "subject", "body",
		  "show", "type", "roster", "group" );

@filter_acts =  ( "error", "offline", "forward", "reply", "continue",
		  "settype" );

1;

</pre></body></html>