2 # Common functions for editing the jabber config files
4 # XXX - http://prdownloads.sourceforge.net/expat/expat-1.95.2-1.i686.rpm
5 # - XML::Parser XML::Generator
6 # XXX - admin <read> and <write> - what do they mean?
8 BEGIN { push(@INC, ".."); };
12 if ($config{'jabber_lib'}) {
13 $ENV{$gconfig{'ld_env'}} .= ':' if ($ENV{$gconfig{'ld_env'}});
14 $ENV{$gconfig{'ld_env'}} .= $config{'jabber_lib'};
17 eval "use XML::Parser; \$got_xml_parser++";
18 eval "use XML::Generator; \$got_xml_generator++";
21 # Parse the jabber XML config file
24 return $get_jabber_config_cache if (defined($get_jabber_config_cache));
25 local $xml = new XML::Parser('Style' => 'Tree');
26 eval { $get_jabber_config_cache = $xml->parsefile($config{'jabber_config'}); };
30 return $get_jabber_config_cache;
37 local $list = $_[1]->[1];
38 for($i=1; $i<@$list; $i+=2) {
39 if (lc($list->[$i]) eq lc($_[0])) {
40 push(@rv, [ $list->[$i], $list->[$i+1], $i ]);
43 return wantarray ? @rv : $rv[0];
46 # find_value(name, &config)
49 local @rv = map { &value_in($_) } &find($_[0], $_[1]);
50 return wantarray ? @rv : $rv[0];
54 # Returns the PID file used by jabber
57 local $conf = &get_jabber_config();
58 local $pidfile = &find_value("pidfile", $conf);
59 if ($pidfile =~ /^\//) {
63 return "$config{'jabber_dir'}/$pidfile";
66 return "$config{'jabber_dir'}/jabber.pid";
73 return undef if (!$_[0]);
74 local $zero = &find("0", $_[0]);
75 return $zero ? $zero->[1] : undef;
78 # generate_config(&tree, &gen)
79 # Returns an XML::Generator object created from a config tree structure
82 local $gen = $_[1] ? $_[1] : XML::Generator->new(escape => 'always');
83 local $list = $_[0]->[1];
85 for($i=1; $i<@$list-1; $i+=2) {
86 if ($list->[$i] eq '0') {
87 push(@mems, $list->[$i+1]);
90 push(@mems, &generate_config(
91 [ $list->[$i], $list->[$i+1] ], $gen));
94 local $name = $_[0]->[0];
95 return $gen->$name($list->[0], @mems);
98 # save_jabber_config(&config)
99 sub save_jabber_config
101 &open_tempfile(CONFIG, ">$config{'jabber_config'}");
102 local $xml = &generate_config($_[0]);
103 &print_tempfile(CONFIG, $xml);
104 &print_tempfile(CONFIG, "\n");
105 &close_tempfile(CONFIG);
108 # save_directive(&config, name|&old, &new)
109 # Replaces all directives of some name with new values
112 local @ov = ref($_[1]) ? @{$_[1]} : &find($_[1], $_[0]);
113 local @nv = @{$_[2]};
115 for($i=0; $i<@ov || $i<@nv; $i++) {
116 local $idx = $ov[$i]->[2] if ($ov[$i]);
117 if ($ov[$i] && $nv[$i]) {
118 # Updating an existing value
119 $_[0]->[1]->[$idx] = $nv[$i]->[0];
120 $_[0]->[1]->[$idx+1] = $nv[$i]->[1];
123 # Deleting an old value
124 splice(@{$_[0]->[1]}, $idx, 2);
125 map { $_->[2] -= 2 if ($_->[2] >= $idx) } @ov;
128 # Adding a new value after the last non-text one
130 for($j=1; $j<@{$_[0]->[1]}; $j+=2) {
131 $nt = $j if ($_[0]->[1]->[$j] ne '0');
133 splice(@{$_[0]->[1]}, $nt+2, 0, $nv[$i]->[0], $nv[$i]->[1]);
138 # find_by_tag(name, tag, value, &config)
141 local @m = &find($_[0], $_[3]);
142 @m = grep { lc($_->[1]->[0]->{lc($_[1])}) eq lc($_[2]) } @m;
143 return wantarray ? @m : $m[0];
146 # xml_string(name, &tree, ...)
147 # Converts a list of XML structures into text
151 for($i=0; $i<@_; $i+=2) {
152 local $xml = &generate_config([ $_[$i], $_[$i+1] ]);
160 # get_jabberd_version(&out)
161 sub get_jabberd_version
163 local $jabberd = $config{'jabber_daemon'} ? $config{'jabber_daemon'}
164 : "$config{'jabber_dir'}/bin/jabberd";
165 local $out = `$jabberd -v 2>&1`;
167 return $out =~ /\s(1\.4\S*)/ ? $1 : undef;
171 # Stops jabber, and returns undef on success or an error message on failure
174 if ($config{'stop_cmd'}) {
175 &system_logged("$config{'stop_cmd'} </dev/null >/dev/null 2>&1");
178 local $pid = &check_pid_file(&jabber_pid_file());
180 &kill_logged('TERM', $pid) || return $!;
183 return $text{'stop_epid'};
186 unlink(&jabber_pid_file());
191 # Starts jabber, and returns undef on success or an error message on failure
194 &system_logged("$config{'start_cmd'} </dev/null >/tmp/err 2>&1");
198 @register_fields = ( 'name', 'email' );
200 @karma_presets = ( { 'heartbeat' => 2, 'init' => 10,
201 'max' => 10, 'inc' => 1,
202 'dec' => 1, 'penalty' => -6,
204 { 'heartbeat' => 2, 'init' => 50,
205 'max' => 50, 'inc' => 4,
206 'dec' => 1, 'penalty' => -5,
208 { 'heartbeat' => 2, 'init' => 64,
209 'max' => 64, 'inc' => 6,
210 'dec' => 1, 'penalty' => -3,
214 @filter_conds = ( "ns", "unavailable", "from", "resource", "subject", "body",
215 "show", "type", "roster", "group" );
217 @filter_acts = ( "error", "offline", "forward", "reply", "continue",