-
Notifications
You must be signed in to change notification settings - Fork 2
/
zbot.pl
executable file
·106 lines (88 loc) · 2.24 KB
/
zbot.pl
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
#!/usr/bin/perl
# This software is published under GPL. See http://www.gnu.org/licenses/gpl-2.0.html
# Author: [email protected] (Jabber: [email protected])
use strict;
use warnings;
use Encode;
use utf8;
use lib "modules";
use Bot;
#
# Var: $bot
#
# Object of Net::Jabber::Bot class
my $bot;
#
# Sub: backgroundChecks
#
# Is called in regular intervals defined at Configs "loop_sleep_time" in seconds (standard 15 seconds)
#
# The idle function of every plugin is called here (if defined and registered)
sub backgroundChecks {
my @functions = Plugins::getIdleFunc();
foreach my $func (@functions) {
&{$func}();
}
}
#
# Sub: newMessage
#
# Is called when Bot gets a message
# Should react on message, call plugins and answer
#
# Parameters:
# Are set by instance of bot
sub newMessage {
my @args = @_;
my $user = $args[3];
my $userMessage = $args[5];
my $type = $args[7];
my $reply = $args[9];
my $botname = Configs::getBotName();
my $logfile = Configs::getLogfile();
if(Configs::getLogging() == 1){
open(LOGFILE,">>$logfile");
print LOGFILE "(".localtime(time).") $user: $userMessage\n\n";
}
my $botMessage = "";
my @words=split(" ",$userMessage);
if($userMessage =~ /\b$botname\b/i){
unshift(@words,$botname);
} # elsif here to add more words to match per regexp
my $functionKey = shift(@words);
my $function = Plugins::getFunc(lc($functionKey));
if(! $function == 0) {
$botMessage = &{$function}($bot,$user,@words);
# if($type eq "groupchat") {
# my $roomname = $user;
# $roomname =~ s/(.*?)@.*/$1/;
# $bot->SendGroupMessage($reply, $botMessage);
# } else {
if(not($type eq "groupchat") or Configs::isInForum($reply)) {
$bot->SendJabberMessage($reply, $botMessage, $type,"");
if(Configs::getLogging() == 1){
print LOGFILE "(".localtime(time).") $botname: $botMessage\n\n";
close(LOGFILE);
}
}
}
}
#
# Sub: init
#
# Is called at startup. Loads modules and plugins. Initialises bot.
sub init {
# load all plugins from directory
use lib "plugins";
my @plugs = glob("plugins/*.pm");
foreach my $p (@plugs){
$p =~ s/plugins\/(.*)/$1/;
require $p;
}
use lib "modules";
use Configs;
use Plugins;
$bot = new Net::Jabber::Bot(Configs::getBotConfig(\&newMessage,\&backgroundChecks));
}
init();
$bot->Start();