1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
package XXV::OUTPUT::NEWS::JABBER;
use strict;
use Tools;
# News Modules have only this methods
# init - for intervall or others
# send - send the informations
# push - push the news and parse it
# req - read the actual news print this out
# This module method must exist for XXV
# ------------------
sub module {
# ------------------
my $self = shift || return error('No object defined!');
my $args = {
Name => 'NEWS::JABBER',
Prereq => {
'Net::XMPP' => 'Jabber protocol for connect and send',
},
Description => gettext(qq|
This NEWS module generate a Jabber messages for your jabber client.
If come a Message from xxv with a lever >= as Preferences::level then
will this module send this Message to your jabber account
(Preferences::receiveUser).
The Problem xxv need a extra jabber account to allow to send messages in
the jabber network. This is very simple:
=over 4
=item 1 Start your jabber client, may the exodus (http://exodus.jabberstudio.org/)
=item 2 Create a new Profile with the name 'xxv'
=item 3 In the next window input following things:
- Jabber Id: newsxxv\@jabber.org (in Example!)
- Password: lalala (in Example!)
- save Password: yes
- new Account?: yes
=back
Thats all!
If you want, you can test the connection to send a testmessage with
the following url in the Webinterface:
http://vdr:8080/?cmd=request&data=jabber
or Telnet Interface:
XXV> request jabber
Then you must receive a message in your running jabber client.
|),
Preferences => {
active => {
description => gettext('Activate this service'),
default => 'n',
type => 'confirm',
required => gettext('This is required!'),
check => sub {
my $value = shift;
my $erg = $self->init
or return undef, gettext("Can't initialize news module!")
if($value eq 'y' and not exists $self->{JCON});
if($value eq 'y') {
my $emod = main::getModule('EVENTS');
if(!$emod or $emod->{active} ne 'y') {
return undef, sprintf(gettext("Module can't activated! This module depends module %s."),'EVENTS');
}
}
return $value;
},
},
level => {
description => gettext('Category of messages that should displayed'),
default => 1,
type => 'list',
choices => sub {
my $emod = main::getModule('EVENTS');
return undef unless($emod);
my $erg = $emod->get_level_as_array();
map { my $x = $_->[1]; $_->[1] = $_->[0]; $_->[0] = $x; } @$erg;
return @$erg;
},
required => gettext('This is required!'),
check => sub {
my $value = int(shift) || 0;
my $emod = main::getModule('EVENTS');
return undef unless($emod);
my $erg = $emod->get_level_as_array();
unless($value >= $erg->[0]->[0] and $value <= $erg->[-1]->[0]) {
return undef,
sprintf(gettext('Sorry, but value must be between %d and %d'),
$erg->[0]->[0],$erg->[-1]->[0]);
}
return $value;
},
},
receiveUser => {
description => gettext('User to be notified (as Jabber account to@jabber.server.org)'),
default => '',
type => 'string',
required => gettext('This is required!'),
},
user => {
description => gettext('Jabber account to send message (from@jabber.server.org)'),
default => '',
type => 'string',
required => gettext('This is required!'),
},
passwd => {
description => gettext('Password for Jabber account'),
default => '',
type => 'password',
required => gettext('This is required!'),
check => sub{
my $value = shift || return;
return $value unless(ref $value eq 'ARRAY');
# If no password given the
# take the old password as default
if($value->[0] and $value->[0] ne $value->[1]) {
return undef, gettext("The fields with the 1st and the 2nd password must match!");
} else {
return $value->[0];
}
},
},
},
};
return $args;
}
# ------------------
sub new {
# ------------------
my($class, %attr) = @_;
my $self = {};
bless($self, $class);
# paths
$self->{paths} = delete $attr{'-paths'};
# host
$self->{host} = delete $attr{'-host'};
# who am I
$self->{MOD} = $self->module;
# all configvalues to $self without parents (important for ConfigModule)
map {
$self->{$_} = $attr{'-config'}->{$self->{MOD}->{Name}}->{$_};
$self->{$_} = $self->{MOD}->{Preferences}->{$_}->{default} unless($self->{$_});
} keys %{$self->{MOD}->{Preferences}};
# Try to use the Requirments
map {
eval "use $_";
if($@) {
my $m = (split(/ /, $_))[0];
return panic("\nCouldn't load perl module: $m\nPlease install this module on your system:\nperl -MCPAN -e 'install $m'");
}
} keys %{$self->{MOD}->{Prereq}};
$self->{TYP} = 'text/plain';
# Initiat after load modules ...
main::after(sub{
# The Initprocess
my $erg = $self->init
or return error("Can't initialize news module!");
}, "NEWS::JABBER: Start initiate news module ...")
if($self->{active} eq 'y');
return $self;
}
# ------------------
sub init {
# ------------------
my $self = shift || return error('No object defined!');
1;
}
# ------------------
sub jconnect {
# ------------------
my $self = shift || return error('No object defined!');
my $jcon = Net::XMPP::Client->new(
debuglevel => 0,
) || return error("Can't create jabber client");
my ($user, $server) = split('\@', $self->{user});
debug sprintf("Connecting to jabber server: %s ...", $server);
my @res = $jcon->Connect(
hostname => $server,
);
return
unless($self->xmpp_check_result("Connect",\@res,$jcon));
debug sprintf("Authentificat with User:%s ...", $user);
@res = $jcon->AuthSend(
'hostname'=>$server,
'username'=>$user,
'password'=>$self->{passwd},
'resource'=>'xxv'
);
return $jcon
if($self->xmpp_check_result("Login",\@res,$jcon));
}
# ------------------
sub jdisconnect {
# ------------------
my $self = shift || return error('No object defined!');
my $cnx = shift || 0;
$cnx->Disconnect()
if(ref $cnx);
1;
}
# ------------------
sub send {
# ------------------
my $self = shift || return error('No object defined!');
my $vars = shift || return error('No data defined!');
return undef, lg('This function is deactivated!')
if($self->{active} ne 'y');
my $cnx = $self->jconnect()
|| return error ('No connected JabberClient!' );
$cnx->MessageSend(
'to' => $self->{receiveUser},
'subject'=> $vars->{Title},
'body' => ($vars->{Text} || $vars->{Url}),
);
$cnx = $self->jdisconnect($cnx);
1;
}
# ------------------
sub push {
# ------------------
my $self = shift || return error('No object defined!');
my $vars = shift || return error('No data defined!');
return $self->send($vars);
1;
}
# ------------------
sub req {
# ------------------
my $self = shift || return error('No object defined!');
return gettext('The module NEWS::JABBER is not active!')
if($self->{active} ne 'y');
my $vars = {
AddDate => time,
Title => 'This is a testmessage for NEWS::JABBER ...',
Text => "abcdefghijklmnopqrstuvwxyz\nABCDEFGHIJKLMNOPQRSTUVWXYZ\n0123456789\näüöÄÜÖ!@#$%^&*()_+=-':;<>?/\n",
Level => 100,
};
if($self->send($vars)) {
return sprintf('Message is send to %s at %s', $self->{receiveUser}, datum($vars->{AddDate}));
} else {
return sprintf("Sorry, couldn't send message to %s at %s", $self->{receiveUser}, datum($vars->{AddDate}));
}
}
# ------------------
sub xmpp_check_result {
# ------------------
my $self = shift || return error('No object defined!');
my ($txt,$res,$cnx)=@_;
return error("Error '$txt': result undefined")
unless($res);
# result can be true or 'ok'
if ((@$res == 1 && $$res[0]) || $$res[0] eq 'ok') {
return debug sprintf("%s: %s", $txt, $$res[0]);
# otherwise, there is some error
} else {
my $errmsg = $cnx->GetErrorCode() || '?';
$cnx->Disconnect();
return error sprintf("Error %s: %s [%s]", $txt, join (': ',@$res), $errmsg);
}
}
1;
|