Net-Jabber-Bot-2.1.7/0000755000000000000000000000000013754314164012662 5ustar rootrootNet-Jabber-Bot-2.1.7/t/0000755000000000000000000000000013754314163013124 5ustar rootrootNet-Jabber-Bot-2.1.7/t/00-load.t0000644000000000000000000000047413610514657014453 0ustar rootroot#!perl -T # Trap for these modules being avail or we can't do our tests... use Test::More tests => 1; BEGIN { use_ok( 'Net::Jabber::Bot' ); } eval { require Net::Jabber::Bot }; BAIL_OUT("Net::Jabber::Bot not installed", 2) if $@; diag( "Testing Net::Jabber::Bot $Net::Jabber::Bot::VERSION, Perl $], $^X" ); Net-Jabber-Bot-2.1.7/t/99-pod.t0000644000000000000000000000035613610527254014334 0ustar rootroot#!perl -T use Test::More; plan skip_all => "\$ENV{RELEASE_TESTING} required for these tests" if(!$ENV{RELEASE_TESTING}); eval "use Test::Pod 1.14"; plan skip_all => "Test::Pod 1.14 required for testing POD" if $@; all_pod_files_ok(); Net-Jabber-Bot-2.1.7/t/05-helper_functions.t0000644000000000000000000002142513610514657017107 0ustar rootroot#!perl use strict; use warnings; use Test::More tests => 127; use Net::Jabber::Bot; #InitLog4Perl(); # stuff for mock client object use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; # Test object # Setup my $bot_alias = 'make_test_bot'; my $client_alias = 'bot_test_client'; my $server = 'talk.google.com'; my $personal_address = "test_user\@$server/$bot_alias"; my $loop_sleep_time = 5; my $server_info_timeout = 5; my %forums_and_responses; my $forum1 = 'test_forum1'; my $forum2 = 'test_forum2'; $forums_and_responses{$forum1} = ["jbot:", ""]; $forums_and_responses{$forum2} = ["notjbot:"]; my $ignore_server_messages = 1; my $ignore_self_messages = 1; my $out_messages_per_second = 5; my $max_message_size = 800; my $long_message_test_messages = 6; my $flood_messages_to_send = 40; my $max_messages_per_hour = ($flood_messages_to_send*2 + 2 + $long_message_test_messages ); # Globals we'll keep track of variables we use each test. our ($messages_seen, $initial_message_count, $start_time); $messages_seen = 0; $initial_message_count = 0; $start_time = time; ok(1, "Creating Net::Jabber::Bot object with Mock client library asserted in place of Net::Jabber::Client"); my $bot = Net::Jabber::Bot->new( server => $server , conference_server => "conference.$server" , port => 5222 , username => 'test_username' , password => 'test_pass' , alias => $bot_alias , message_function => \&new_bot_message # Called if new messages arrive. , background_function => \&background_checks # What the bot does outside jabber. , loop_sleep_time => $loop_sleep_time # Minimum time before doing background function. , process_timeout => $server_info_timeout # Time to wait for new jabber messages before doing background stuff , forums_and_responses => \%forums_and_responses , ignore_server_messages => $ignore_server_messages , ignore_self_messages => $ignore_self_messages , out_messages_per_second => $out_messages_per_second , max_message_size => $max_message_size , max_messages_per_hour => $max_messages_per_hour ); is($bot->message_delay, 0.2, "Message delay is set right to .20 seconds"); is($bot->max_messages_per_hour, $max_messages_per_hour, "Max messages per hour ($max_messages_per_hour) didn't get messed with by safeties"); isa_ok($bot, "Net::Jabber::Bot"); ok(1, "Sleeping 12 seconds to make sure we get past initializtion"); ok((sleep 12) > 10, "Making sure the bot get's past login initialization (sleep 12)"); process_bot_messages(); # Clean off the queue before we start? # continue editing here. Need to next enhance mock object to know jabber bot callbacks. # Not sure how we're going to chase chicken/egg issue. start_new_test("Testing Group Message bursting is not possible"); { for my $counter (1..$flood_messages_to_send) { my $result = $bot->SendGroupMessage($forum1, "Testing message speed $counter"); diag("got return value $result") if(defined $result); ok(!defined $result, "Sent group message $counter"); } my $running_time = time - $start_time; my $expected_run_time = $flood_messages_to_send / $out_messages_per_second; cmp_ok($running_time, '>=', int($expected_run_time), "group Message burst: \$running_time ($running_time) >= \$expected_run_time ($expected_run_time)"); process_bot_messages(); verify_messages_sent($flood_messages_to_send); verify_messages_seen(0, "Didn't see the messages I sent to the group"); } start_new_test("Testing PERSONAL_ADDRESS Message bursting is not possible"); { for my $counter (1..$flood_messages_to_send) { my $result = $bot->SendPersonalMessage($personal_address, "Testing personal_address message speed $counter"); diag("got return value $result") if(defined $result); ok(!defined $result, "Sent personal message $counter"); } my $running_time = time - $start_time; my $expected_run_time = $flood_messages_to_send / $out_messages_per_second; cmp_ok($running_time, '>=', int($expected_run_time), "group Message burst: \$running_time ($running_time) >= \$expected_run_time ($expected_run_time)"); process_bot_messages(); verify_messages_sent($flood_messages_to_send); verify_messages_seen(0, "Didn't see the messages I sent to myself..."); } TODO: { # Need a way to test for historical - up top or in diff code? ;# cmp_ok($messages_seen, '==', 0, "Didn't see any historical messages..."); } cmp_ok($bot->respond_to_self_messages( ), '==', 1, "no pass to respond_to_self_messages is 1"); cmp_ok($bot->respond_to_self_messages(0), '==', 0, "Ignore Self Messages"); cmp_ok($bot->respond_to_self_messages(2), '==', 1, "Respond to Self Messages"); start_new_test("Test a successful message"); ok(!defined $bot->SendPersonalMessage($personal_address, "Testing message to myself"), "Testing message to myself"); process_bot_messages(); verify_messages_sent(1); verify_messages_seen(1, "Got it!"); # Setup a really long message and make sure it's longer than 1 message. my $repeating_string = 'Now is the time for all good men to come to the aide of their country '; my $message_repeats = int( # Make it a whole number ($max_message_size # Maximum size of 1 message * $long_message_test_messages # How many messages we want to produce - $max_message_size / 2) # Shorten it a little. / length $repeating_string # Length of our string we're going to repeat ); my $long_message = $repeating_string x $message_repeats; my $long_message_length = length $long_message; cmp_ok(length($long_message), '>=' , $max_message_size , "Length of message is greater than 1 message chunk ($long_message_length bytes)"); ok(1, "Testing messages that will be split:"); { start_new_test("Send to self"); cmp_ok($bot->respond_to_self_messages( ), '==', 1, "Make sure I'm responding to self messages."); # Group Test. ok(1, "Sending long message of " . length($long_message) . " bytes to forum"); my $result = $bot->SendGroupMessage($forum1, $long_message); diag("got return value $result\nWhile trying to send: $long_message") if(defined $result); ok(!defined $result, "Sent long message."); process_bot_messages(); cmp_ok($messages_seen, '>=',$long_message_test_messages, "Saw $long_message_test_messages messages so we know it was chunked into messages smaller than $max_message_size"); start_new_test("Set long subject in forum (illegal)"); my $subject_change_result = $bot->SetForumSubject($forum1, $long_message); is($subject_change_result, "Subject is too long!", 'Verify long subject changes are rejected.'); verify_messages_sent(0); verify_messages_seen(0, "Bot should not have sent anything to the server."); } DEBUG("Finished with first burst"); start_new_test("Test a successful message with a panic"); ok(!defined $bot->SendPersonalMessage($personal_address, "Testing message to myself"), "Testing message to myself"); process_bot_messages(); verify_messages_sent(1); verify_messages_seen(2, "With Panic"); start_new_test("Test message limits"); my $failure_message = $bot->SendPersonalMessage($personal_address, "Testing message to myself that should fail"); ok(defined $failure_message, "Testing hourly message limits (failure to send)"); process_bot_messages(); verify_messages_seen(0, "Should be not have been sent to server"); verify_messages_seen(0, "Rejected by bot"); exit; sub new_bot_message { $messages_seen += 1; } sub background_checks {} sub verify_messages_sent { my $expected_messages = shift; my $messages_sent = $bot->get_messages_this_hour() - $initial_message_count; cmp_ok($messages_sent, '==', $expected_messages, "Verify that $expected_messages were sent"); } sub verify_messages_seen { my $expected_messages = shift; my $comment = shift; if(!defined $comment) { $comment = ""; } else { $comment = "($comment)"; } cmp_ok($messages_seen, '==', $expected_messages, "Verify that $expected_messages were seen $comment"); } sub start_new_test { my $comment = shift; $comment = "no description" if(!defined $comment); ok(1, "****** New test: $comment ******"); $initial_message_count = $bot->get_messages_this_hour(); $messages_seen = 0; $start_time = time; } sub process_bot_messages { DEBUG("Processing bot messages from test file ($0)"); ok(defined $bot->Process(5), "Processed new messages and didn't lose connection."); } sub InitLog4Perl { use Log::Log4perl qw(:easy); my $config_file .= <<'CONFIG_DATA'; # Regular Screen Appender log4perl.appender.Screen = Log::Log4perl::Appender::Screen log4perl.appender.Screen.stderr = 0 log4perl.appender.Screen.layout = PatternLayout log4perl.appender.Screen.layout.ConversionPattern = %d %p (%L): %m%n log4perl.category = ALL, Screen CONFIG_DATA Log::Log4perl->init(\$config_file); $| = 1; #unbuffer stdout! } Net-Jabber-Bot-2.1.7/t/99-pod-coverage.t0000644000000000000000000000102413610527241016112 0ustar rootroot#!perl use Test::More; plan skip_all => "\$ENV{RELEASE_TESTING} required for these tests" if(!$ENV{RELEASE_TESTING}); eval "use Test::Pod::Coverage 1.04"; plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@; plan tests => 1; my $private_subs = { private => [qr/^(BUILD|_callback_maker|_init_jabber|_process_jabber_message|_request_version|_send_individual_message)$/] }; pod_coverage_ok('Net::Jabber::Bot', $private_subs, "Test Net::Jabber::Bot for docs. Private functions not listed in docs"); Net-Jabber-Bot-2.1.7/t/lib/0000755000000000000000000000000013754314163013672 5ustar rootrootNet-Jabber-Bot-2.1.7/t/lib/MockJabberClient.pm0000644000000000000000000000611413610514657017371 0ustar rootrootpackage Net::Jabber::Client; use strict; use warnings; use Net::Jabber; use Log::Log4perl qw(:easy); # NOTE: Need to inherit from Jabber bot object so we don't have to re-do message code, etc. sub new { my $proto = shift; my $self = { }; bless($self, $proto); $self->init(@_); $self->{SESSION}->{id} = int(rand(9999)); # Gen a random session ID. my @empty_array; $self->{message_queue} = \@empty_array; $self->{is_connected} = 1; $self->{presence_callback} = undef; $self->{iq_callback} = undef; $self->{message_callback} = undef; $self->{server} = undef; $self->{username} = undef; $self->{password} = undef; $self->{resource} = undef; return $self; } # Read from array of messages and pass them to the message functions. sub Process { my $self = shift; my $timeout = shift or 0; return if(!$self->{is_connected}); # Return undef if we're not connected. foreach my $message (@{$self->{message_queue}}) { $timeout = 0; # zero out sleep timer; next if(!defined $self->{message_callback}); $self->{message_callback}->($self->{SESSION}->{id}, $message); } @{$self->{message_queue}} = (); sleep $timeout; return 1; # undef means we lost connection. } sub PresenceSend {;} sub SetCallBacks { my $self = shift; my %callbacks = @_; $self->{presence_callback} = $callbacks{'presence'}; $self->{iq_callback} = $callbacks{'iq'}; $self->{message_callback} = $callbacks{'message'}; } sub Connect { my $self = shift; $self->{server} = shift; return 1; # Always confirm we're connected. } sub AuthSend { my $self = shift; my %arg_hash = @_; $self->{'username'} = $arg_hash{'username'}; $self->{'password'} = $arg_hash{'password'}; $self->{'resource'} = $arg_hash{'resource'}; return ("ok", "connected"); # Always confirm auth succeeds. } sub MessageSend { #Loop the messages into the in queue so we can see the server send em back. Needs peer review my $self = shift; my %arg_hash = @_; my $message = new Net::Jabber::Message(); my $sent_to = $arg_hash{'to'}; my ($forum, $server) = split(/\@/, $sent_to, 2); $server =~ s{\/.*$}{}; # Remove the /resource if it came from an individual, not a groupchat my $from = "$forum\@$server/$self->{resource}"; my $to = "$self->{username}\@$self->{server}/$self->{resource}"; DEBUG("$sent_to --- $from --- $to"); $message->SetFrom($from); $message->SetTo($to); $message->SetType($arg_hash{'type'}); $message->SetSubject($arg_hash{'subject'}); $message->SetBody($arg_hash{'body'}); # ERROR($message->GetXML()); exit; push @{$self->{message_queue}}, $message; } sub MUCJoin {; } sub Disconnect { my $self = shift; $self->{is_connected} = 1; } sub Send {;} # Used for IQ. need to see if we need to put something here. sub Subscription {;} # Used to process JabberPresenceMessages we don't really use this data at the moment. sub RosterGet {;} sub PresenceDB {;} sub PresenceDBParse{;} 1; Net-Jabber-Bot-2.1.7/t/03-test_connectivity.t0000644000000000000000000000357213610524641017310 0ustar rootroot#!perl -T BEGIN { use Test::More; # Author tests - requires Config::Std plan skip_all => "\$ENV{AUTHOR_TESTING} required for these tests" if(!$ENV{AUTHOR_TESTING}); eval "use Config::Std"; plan skip_all => "Optional Module Config::Std Required for these tests" if($@); } use Net::Jabber::Bot; use Log::Log4perl qw(:easy); use Test::NoWarnings; # This breaks the skips in CPAN. # Otherwise it's 7 tests plan tests => 7; # Load config file. use Config::Std; # Uses read_config to pull info from a config files. enhanced INI format. my $config_file = 't/test_config.cfg'; my %config_file_hash; ok((read_config($config_file => %config_file_hash)), "Load config file") or die("Can't test without config file $config_file"); my $alias = 'make_test_bot'; my $loop_sleep_time = 5; my $server_info_timeout = 5; my %forums_and_responses; $forums_and_responses{$config_file_hash{'main'}{'test_forum1'}} = ["jbot:", ""]; $forums_and_responses{$config_file_hash{'main'}{'test_forum2'}} = ["notjbot:"]; my $bot = Net::Jabber::Bot->new( server => $config_file_hash{'main'}{'server'} , conference_server => $config_file_hash{'main'}{'conference'} , port => $config_file_hash{'main'}{'port'} , username => $config_file_hash{'main'}{'username'} , password => $config_file_hash{'main'}{'password'} , alias => $alias , forums_and_responses => \%forums_and_responses ); isa_ok($bot, "Net::Jabber::Bot"); ok(defined $bot->Process(), "Bot connected to server"); sleep 5; ok($bot->Disconnect() > 0, "Bot successfully disconnects"); # Disconnects is($bot->Disconnect(), undef, "Bot fails to disconnect cause it already is"); # If already disconnected, we get a negative number eval{Net::Jabber::Bot->Disconnect()}; like($@, qr/^\QCan't use string ("Net::Jabber::Bot") as a HASH ref while "strict refs" in use\E/, "Error when trying to disconnect not as an object"); Net-Jabber-Bot-2.1.7/t/test_config.sample0000644000000000000000000000027613610523400016623 0ustar rootroot[main] server: talk.google.com port: 5222 conference: conference.talk.google.com username: user_here password: password_here test_forum1: perl_njb1 test_forum2: perl_njb2 Net-Jabber-Bot-2.1.7/t/06-test_safeties.t0000644000000000000000000002162413610514657016404 0ustar rootroot#!perl use strict; use warnings; use Test::More tests => 129; use Net::Jabber::Bot; # stuff for mock client object use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; # Test object #InitLog4Perl(); # Setup my $bot_alias = 'make_test_bot'; my $client_alias = 'bot_test_client'; my $server = 'talk.google.com'; my $personal_address = "test_user\@$server/$bot_alias"; my $loop_sleep_time = 5; my $server_info_timeout = 5; my %forums_and_responses; my $forum1 = 'test_forum1'; my $forum2 = 'test_forum2'; $forums_and_responses{$forum1} = ["jbot:", ""]; $forums_and_responses{$forum2} = ["notjbot:"]; my $ignore_server_messages = 1; my $ignore_self_messages = 1; my $out_messages_per_second = 5; my $max_message_size = 800; my $long_message_test_messages = 6; my $flood_messages_to_send = 40; my $max_messages_per_hour = ($flood_messages_to_send*2 + 2 + $long_message_test_messages ); # Globals we'll keep track of variables we use each test. our ($messages_seen, $initial_message_count, $start_time); $messages_seen = 0; $initial_message_count = 0; $start_time = time; ok(1, "Creating Net::Jabber::Bot object with Mock client library asserted in place of Net::Jabber::Client"); my $bot = Net::Jabber::Bot->new({ server => $server , conference_server => "conference.$server" , port => 5222 , username => 'test_username' , password => 'test_pass' , alias => $bot_alias , message_function => \&new_bot_message # Called if new messages arrive. , background_function => \&background_checks # What the bot does outside jabber. , loop_sleep_time => $loop_sleep_time # Minimum time before doing background function. , process_timeout => $server_info_timeout # Time to wait for new jabber messages before doing background stuff , forums_and_responses => \%forums_and_responses , ignore_server_messages => $ignore_server_messages , ignore_self_messages => $ignore_self_messages , out_messages_per_second => $out_messages_per_second , max_message_size => $max_message_size , max_messages_per_hour => $max_messages_per_hour }); isa_ok($bot, "Net::Jabber::Bot"); is($bot->message_delay, 0.2, "Message delay is set right to .20 seconds"); is($bot->max_messages_per_hour, $max_messages_per_hour, "Max messages per hour ($max_messages_per_hour) didn't get messed with by safeties"); is($bot->get_safety_mode, 1, "Validate safety mode is on") or die("Safety mode is not turning on. Tests will not be valid"); is($bot->forum_join_grace, 10, "Forum Grace is 10 seconds as expected"); ok(1, "Sleeping 12 seconds to make sure we get past initializtion"); ok((sleep 12) > 10, "Making sure the bot get's past initialization (sleep 12)"); process_bot_messages(); start_new_test("Testing Group Message bursting is not possible"); { for my $counter (1..$flood_messages_to_send) { my $result = $bot->SendGroupMessage($forum1, "Testing message speed $counter"); diag("got return value $result") if(defined $result); ok(!defined $result, "Sent group message $counter"); } my $running_time = time - $start_time; my $expected_run_time = $flood_messages_to_send / $out_messages_per_second; cmp_ok($running_time, '>=', int($expected_run_time), "group Message burst: \$running_time ($running_time) >= \$expected_run_time ($expected_run_time)"); process_bot_messages(); verify_messages_sent($flood_messages_to_send); verify_messages_seen(0, "Didn't see the messages I sent to the group"); } start_new_test("Test PERSONAL_ADDRESS Message bursting is not possible"); { for my $counter (1..$flood_messages_to_send) { my $result = $bot->SendPersonalMessage($personal_address, "Testing personal_address message speed $counter"); diag("got return value $result") if(defined $result); ok(!defined $result, "Sent personal message $counter"); } my $running_time = time - $start_time; my $expected_run_time = $flood_messages_to_send / $out_messages_per_second; cmp_ok($running_time, '>=', int($expected_run_time), "group Message burst: \$running_time ($running_time) >= \$expected_run_time ($expected_run_time)"); process_bot_messages(); verify_messages_sent($flood_messages_to_send); verify_messages_seen(0, "Didn't see the messages I sent to myself..."); } TODO: { # Need a way to test for historical - up top or in diff code? ;# cmp_ok($messages_seen, '==', 0, "Didn't see any historical messages..."); } cmp_ok($bot->respond_to_self_messages( ), '==', 1, "no pass to respond_to_self_messages is 1"); cmp_ok($bot->respond_to_self_messages(0), '==', 0, "Ignore Self Messages"); cmp_ok($bot->respond_to_self_messages(2), '==', 1, "Respond to Self Messages"); cmp_ok($bot->ignore_self_messages, '==', 0, "Moose variable is set right for ignore_self_messages"); start_new_test("Test a successful message"); ok(!defined $bot->SendPersonalMessage($personal_address, "Testing message to myself"), "Testing message to myself"); process_bot_messages(); verify_messages_sent(1); verify_messages_seen(1, "Got it!"); # Setup a really long message and make sure it's longer than 1 message. my $repeating_string = 'Now is the time for all good men to come to the aide of their country '; my $message_repeats = int( # Make it a whole number ($max_message_size # Maximum size of 1 message * $long_message_test_messages # How many messages we want to produce - $max_message_size / 2) # Shorten it a little. / length $repeating_string # Length of our string we're going to repeat ); my $long_message = $repeating_string x $message_repeats; my $long_message_length = length $long_message; cmp_ok(length($long_message), '>=' , $max_message_size , "Length of message is greater than 1 message chunk ($long_message_length bytes)"); # Test messages that will be split: { cmp_ok($bot->respond_to_self_messages, '==', 1, "Make sure I'm responding to self messages."); start_new_test("Split Testing for forum messages"); # Group Test. ok(1, "Sending long message of " . length($long_message) . " bytes to forum"); my $result = $bot->SendGroupMessage($forum1, $long_message); diag("got return value $result\nWhile trying to send: $long_message") if(defined $result); ok(!defined $result, "Sent long message."); process_bot_messages(); cmp_ok($messages_seen, '>=',$long_message_test_messages, "Saw $long_message_test_messages messages so we know it was chunked into messages smaller than $max_message_size"); start_new_test("Test subject too long error"); my $subject_change_result = $bot->SetForumSubject($forum1, $long_message); is($subject_change_result, "Subject is too long!", 'Verify long subject changes are rejected.'); verify_messages_sent(0); verify_messages_seen(0, "Bot should not have sent anything to the server."); } start_new_test("Test a successful message with a panic"); ok(!defined $bot->SendPersonalMessage($personal_address, "Testing message to myself"), "Testing message to myself"); process_bot_messages(); verify_messages_sent(1); verify_messages_seen(2, "With Panic"); start_new_test("Test message limits"); my $failure_message = $bot->SendPersonalMessage($personal_address, "Testing message to myself that should fail"); ok(defined $failure_message, "Testing hourly message limits (failure to send)"); process_bot_messages(); verify_messages_seen(0, "Should be not have been sent to server"); verify_messages_seen(0, "Rejected by bot"); exit; sub new_bot_message { $messages_seen += 1; } sub background_checks {} sub verify_messages_sent { my $expected_messages = shift; my $messages_sent = $bot->get_messages_this_hour() - $initial_message_count; cmp_ok($messages_sent, '==', $expected_messages, "Verify that $expected_messages were sent"); } sub verify_messages_seen { my $expected_messages = shift; my $comment = shift; if(!defined $comment) { $comment = ""; } else { $comment = "($comment)"; } cmp_ok($messages_seen, '==', $expected_messages, "Verify that $expected_messages were seen $comment"); } sub start_new_test { my $comment = shift; $comment = "no description" if(!defined $comment); ok(1, "****** New test: $comment ******"); $initial_message_count = $bot->get_messages_this_hour(); $messages_seen = 0; $start_time = time; } sub process_bot_messages { sleep 2; # Pause a little to make sure message make it to the server and back. ok(defined $bot->Process(5), "Processed new messages and didn't lose connection."); } sub InitLog4Perl { use Log::Log4perl qw(:easy); my $config_file .= <<'CONFIG_DATA'; # Regular Screen Appender log4perl.appender.Screen = Log::Log4perl::Appender::Screen log4perl.appender.Screen.stderr = 0 log4perl.appender.Screen.layout = PatternLayout log4perl.appender.Screen.layout.ConversionPattern = %d %p (%L): %m%n log4perl.category = ALL, Screen CONFIG_DATA Log::Log4perl->init(\$config_file); $| = 1; #unbuffer stdout! } Net-Jabber-Bot-2.1.7/MANIFEST.SKIP0000644000000000000000000000010113610531620014535 0ustar rootroot^.github/ ^.git/.* ^MYMETA.* ^MANIFEST.bak ^.gitignore ^Makefile$Net-Jabber-Bot-2.1.7/examples/0000755000000000000000000000000013754314163014477 5ustar rootrootNet-Jabber-Bot-2.1.7/examples/gtalk_RSSbot.pl0000755000000000000000000000464313610514657017405 0ustar rootrootuse Net::Jabber::Bot; use XML::Smart; use utf8; use strict; # Simple RSS bot (yjesus@security-projects.com) # It works fine with Feedburner my $url = 'http://feeds.boingboing.net/boingboing/iBag' ; my $username = 'your.gtalk.user'; my $password = 'yourpassword'; my ($last_title, $last_link) = checa(); my $bot = Net::Jabber::Bot->new({ server => 'talk.google.com' , gtalk => 1 , conference_server => 'talk.google.com' , port => 5222 , username => $username , password => $password , alias => $username , message_function => \&new_bot_message , background_function => \&background_checks , loop_sleep_time => 15 , process_timeout => 5 , ignore_server_messages => 0 , ignore_self_messages => 0 , out_messages_per_second => 40 , max_message_size => 1000 , max_messages_per_hour => 100 })|| die "ooops\n" ; my @users = $bot->GetRoster() ; $bot->Start(); sub new_bot_message { my %bot_message_hash = @_; my $user = $bot_message_hash{reply_to} ; my $message = lc($bot_message_hash{body}); if ($message =~ m/\bhelp\b/) { $bot->SendPersonalMessage($user, "Hi Im a RSS-BOT for Gtalk !!"); } } sub background_checks { my ($title, $link) = checa(); return if ($last_title eq $title); foreach my $tosend (@users) { my $status = $bot->GetStatus($tosend); if ($status != "unavailable") { $bot->SendPersonalMessage($tosend, "$title"); $bot->SendPersonalMessage($tosend, "$link"); } } $last_title=$title; # Now make the new title recieved the most recent title. } sub checa { my $XML ; eval { $XML = XML::Smart->new($url) }; if ($@) { return undef } $XML = $XML->cut_root ; my $title =$XML->{channel}{item}[0]{title}[0] ; my $link =$XML->{channel}{item}[0]{link}[0] ; utf8::encode($title); utf8::encode($link); return($title, $link) } Net-Jabber-Bot-2.1.7/examples/bot_example.pl0000755000000000000000000002526613610514657017352 0ustar rootroot#!perl use strict; use warnings; use Getopt::Euclid; # Uses POD at bottom of file to auto-parse ARGV. use Log::Log4perl qw(:easy); # Also gives additional debug info back from bot. use Net::Jabber::Bot; # Init log4perl based on command line options my %log4perl_init; $log4perl_init{'cron'} = 1 if(defined $ARGV{'-cron'}); $log4perl_init{'nostdout'} = 1 if(defined $ARGV{'-nostdout'}); $log4perl_init{'log_file'} = $ARGV{'-logfile'} if(defined $ARGV{'-logfile'}); $log4perl_init{'email'} = $ARGV{'-email'} if(defined $ARGV{'-email'}); $log4perl_init{'debug_level'} = $ARGV{'-debuglevel'}; InitLog4Perl(%log4perl_init); my $bot_forum1 = "test_forum"; # Forum the bot will monitor my $bot_forum2 = "other_test_forum"; # Second forum just to show it's possible. my @forums = ($bot_forum1, $bot_forum2); my $alias = "perl_bot"; # Who I will show up as in the forum my %alerts_sent_hash; my %next_alert_time_hash; my %next_alert_increment; my %forums_and_responses; foreach my $forum (@forums) { my $responses = "bot:|hey you|"; # Note the pipe at the end indicates it will act on all messages my @response_array = split(/\|/, $responses); push @response_array, "" if($responses =~ m/\|\s*$/); $forums_and_responses{$forum} = \@response_array; } my $bot = Net::Jabber::Bot->new( server => $ARGV{'-server'} , conference_server => $ARGV{'-conference_server'} , port => $ARGV{'-port'} , username => $ARGV{'-user'} , password => $ARGV{'-pass'} , alias => $alias , message_function => \&new_bot_message # Called if new messages arrive. , background_function => \&background_checks # What the bot does outside jabber. , loop_sleep_time => 20 # Minimum time before doing background function. , process_timeout => 5 # Time to wait for new jabber messages before timing out , forums_and_responses => \%forums_and_responses , ignore_server_messages => 1 # Usually you don't care about admin messages from the server , ignore_self_messages => 1 # Usually you don't want to see your own messages , out_messages_per_second => 5 # Maximum messages allowed per second (server flood throttling) , max_message_size => 1000 # Maximum byte size of the message before we chop it into pieces , max_messages_per_hour => 1000 # Keep the bot from going out of control with noisy messages ); foreach my $forum (@forums) { $bot->SendGroupMessage($forum, "$alias logged into forum $forum"); } $bot->Start(); #Endless loop where everything happens after initialization. DEBUG("Something's gone horribly wrong. Jabber bot exiting..."); exit; # This sub is called every 20 seconds (configurable) by the bot so it can do background activity sub background_checks { my $bot= shift; my $counter = shift; check_a_file(); monitor_a_web_page(); } sub new_bot_message { my %bot_message_hash = @_; # Who to speak to if you need to. $bot_message_hash{'sender'} = $bot_message_hash{'from_full'}; $bot_message_hash{'sender'} =~ s{^.+\/([^\/]+)$}{$1}; my($command, @options) = split(' ', $bot_message_hash{body}); $command = lc($command); my %command_actions; $command_actions{'subject'} = \&bot_change_subject; $command_actions{'nslookup'} = \&bot_nslookup; $command_actions{'say'} = \&bot_say; $command_actions{'help'} = \&bot_help; $command_actions{'unknown_command_passed'} = \&bot_unknown_command; if(defined $command_actions{$command}) { $command_actions{$command}->(\%bot_message_hash, @options); } else { $command_actions{'unknown_command_passed'}->(\%bot_message_hash, @options); } } sub bot_change_subject { my %bot_message_hash = %{shift @_}; my $new_subject = join " ", @_; my $bot_object = $bot_message_hash{bot_object}; my $reply_to = $bot_message_hash{reply_to}; if($bot_message_hash{type} ne 'groupchat') { $bot_object->SendJabberMessage($reply_to , "Sorry, I can't change subject outside a forum!" , $bot_message_hash{type}); WARN("Denied subject change from $reply_to ($new_subject)"); return; } $bot_object->SendGroupMessage($reply_to, "Setting Forum subject to: $new_subject"); $bot_object->SetForumSubject($reply_to, $new_subject); return; } sub bot_nslookup { my %bot_message_hash = %{shift @_}; my $host = CleanInput(shift @_); my $bot_object = $bot_message_hash{bot_object}; my $reply_to = $bot_message_hash{reply_to}; if(!defined $host) { $bot_object->SendJabberMessage($reply_to , "Stop screwing around $bot_message_hash{sender}!" , $bot_message_hash{type}); return; } my $output = `/usr/sbin/nslookup $host 2>&1`; $bot_object->SendJabberMessage($reply_to, $output, $bot_message_hash{type}); } sub bot_say { my %bot_message_hash = %{shift @_}; my $to_say = join " ", @_; my $bot_object = $bot_message_hash{bot_object}; $bot_object->SendJabberMessage($bot_message_hash{reply_to} , $to_say , $bot_message_hash{type}); } sub bot_help { my %bot_message_hash = %{shift @_}; my @options = @_; my $bot_object = $bot_message_hash{bot_object}; my $reply_to = $bot_message_hash{reply_to}; my $message_type = $bot_message_hash{type}; $bot_object->SendJabberMessage($reply_to , "I know how to do the following: nslookup , say, " . "subject " , $message_type); } sub bot_unknown_command { my %bot_message_hash = %{shift @_}; my @options = @_; my $bot_object = $bot_message_hash{bot_object}; my $reply_to = $bot_message_hash{reply_to}; my $message_type = $bot_message_hash{type}; # Don't get confused about vague addresses empty messages return if(length $bot_message_hash{bot_address_from} <= 2); $bot_object->SendJabberMessage($reply_to , "Sorry $bot_message_hash{sender}, I don't know what you're asking me." , $message_type); } sub CleanInput { my $string = shift; my $revised_string = $string; $revised_string =~ s{[\>\<\&\n\r;]}{}g; # Strip things that would allow enhanced commands. $revised_string =~ s/[^ -~]//g; #Strip out anything that's not a printable character return if($string ne $revised_string); # Error! return $string; } sub InitLog4Perl { my(%tag_hash) = @_; my $config_file = ''; my $debug_level = 'DEBUG'; $debug_level = $tag_hash{debug_level} if(defined $tag_hash{debug_level}); my $log_to_line = "log4perl.category = $debug_level"; my $layout = '%d %p (%L): %m%n'; $layout = $tag_hash{layout} if(defined $tag_hash{layout}); if(!-t STDOUT && !defined $tag_hash{cron}) { confess("You have run this program from cron but not acknowledged to log4perl that this is the case. I don't know where to send output!"); } # Unless explicitly stated, we will send to STDOUT. if(!defined $tag_hash{nostdout}) { $config_file .= <<"CONFIG_DATA"; # Regular Screen Appender log4perl.appender.Screen = Log::Log4perl::Appender::Screen log4perl.appender.Screen.stderr = 0 log4perl.appender.Screen.layout = PatternLayout log4perl.appender.Screen.layout.ConversionPattern = $layout CONFIG_DATA $log_to_line .= ", Screen"; } if(defined $tag_hash{'log_file'}) { $config_file .= <<"CONFIG_DATA"; log4perl.appender.Log = Log::Log4perl::Appender::File log4perl.appender.Log.filename = $tag_hash{log_file} log4perl.appender.Log.mode = append log4perl.appender.Log.layout = PatternLayout log4perl.appender.Log.layout.ConversionPattern = $layout CONFIG_DATA $log_to_line .= ", Log"; } if(defined $tag_hash{'email_to'}) { $config_file .= <<"CONFIG_DATA"; log4perl.appender.Mailer = Log::Dispatch::Email::MailSendmail log4perl.appender.Mailer.to = $tag_hash{email_to} log4perl.appender.Mailer.subject = Log4Perl: $0 error message. log4perl.appender.Mailer.layout = PatternLayout log4perl.appender.Mailer.layout.ConversionPattern = $layout log4perl.appender.Mailer.buffered = 0 CONFIG_DATA $log_to_line .= ", Mailer"; } $config_file .= "$log_to_line\n"; # print "***\n$config_file***\n"; Log::Log4perl->init(\$config_file); $| = 1; #unbuffer stdout! } __END__ =head1 USAGE Euclid auto-generates this. Run program with --help for usage. =head1 VERSION VERSION 1.0 =head1 NAME $0 - Example bot to show how to use the module. =head1 REQUIRED ARGUMENTS =over =item -server =for Euclid host.type: string =item -conference_server =for Euclid host.type: string =item -user =for Euclid username.type: string =item -pass =for Euclid password.type: string =back =head1 OPTIONS =over =item -port port number (defaults to 5222) =for Euclid port_num.type: int > 0 port_num.default: 5222 =item -log[file] Where to log to file =for Euclid file.type: writeable file.type.error: Cannot write to file . Please check permissions! =item -nostdout Turn off STDOUT =item -cron Indicate this program is running from cron. =item -debug[level] Set debug level (DEBUG INFO WARN ERROR FATAL ALL OFF) Defaults to INFO =for Euclid: level.type: string, level =~ /DEBUG|INFO|WARN|ERROR|FATAL|ALL|OFF$/ level.default: "INFO" =item -exiton Exit if this level of message is detected (DEBUG INFO WARN ERROR FATAL ALL OFF) Defaults to OFF =for Euclid: level.type: string, level =~ /DEBUG|INFO|WARN|ERROR|FATAL|ALL|OFF$/ level.default: "OFF" =item --version =item --usage =item --help =item --man Print the usual program information =back Bot code to show how to use the bot =head1 AUTHOR Todd Rinaldo, Robert Boone, Wade Johnson (perl-net-jabber-bot@googlegroups.com) =head1 BUGS Send Bug Reports to perl-net-jabber-bot@googlegroups.com or submit them yourself at: http://code.google.com/p/perl-net-jabber-bot/issues/list/entry Net-Jabber-Bot-2.1.7/Makefile.PL0000644000000000000000000000372013610527531014631 0ustar rootrootuse strict; use warnings; use ExtUtils::MakeMaker; WriteMakefile( NAME => 'Net::Jabber::Bot', AUTHOR => 'Todd E Rinaldo ', VERSION_FROM => 'lib/Net/Jabber/Bot.pm', ABSTRACT_FROM => 'lib/Net/Jabber/Bot.pm', PL_FILES => {}, ($ExtUtils::MakeMaker::VERSION >= 6.3002 ? ('LICENSE' => 'perl', ) : ()), PREREQ_PM => { 'Moose' => 0.82, # Object Base 'MooseX::Types' => 0.12, # New variable types 'Mozilla::CA' => 0, # SSL Cert support. 'Time::HiRes' => 0, # Partial second sleeping 'Net::Jabber' => 2.0, # The whole thing is based on Net::Jabber. 'Log::Log4perl' => 0, # We use log4perl. not sure how bad this'll screw people over... should consider removal later or make it optional 'version' => 0, # It comes with most dists, but might as well be explicit 'Test::More' => 0, # For testing 'Test::NoWarnings' => 0, # For testing 'Sys::Hostname' => 0, # For unique resource name per connection 'FindBin' => 0, # Find mock module 'lib' => 0, # add lib for Mock jabber module }, dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, clean => { FILES => 'Net-Jabber-Bot-*' }, META_MERGE => { build_requires => { 'Test::More' => 0, # For testing 'Test::NoWarnings' => 0, # For testing 'FindBin' => 0, # Find mock module 'lib' => 0, # add lib for Mock jabber module }, resources => { license => 'http://dev.perl.org/licenses/', homepage => 'http://wiki.github.com/toddr/perl-net-jabber-bot', bugtracker => 'https://github.com/toddr/perl-net-jabber-bot/issues', repository => 'http://github.com/toddr/perl-net-jabber-bot/tree/master', # MailingList => 'http://groups.google.com/group/perl-net-jabber-bot', }, }, ); Net-Jabber-Bot-2.1.7/Changes0000644000000000000000000001110513754313461014152 0ustar rootrootRevision history for Net-Jabber-Bot 2.1.7 2020-11-05 Todd Rinaldo - #15 Fix _send_individual_message to not strip new lines. 2.1.6 - Fix examples for new moose code - Spelling errors in documentation - Display server error message when we think there was a disconnect event. - Allow the user to specify the path to the CA cert bundle via the 'ssl_ca_path' parameter. - Move jabber object creation to lazy moose - Add ignore file to repo - Clean up POD so new() documentation lays out correctly - Remove DD from code. Moose has a helper sub already for this anyways - Explicitly set priority of users - Adam Malone - Allow the user to specify the path to the CA cert bundle via the 'ssl_ca_path' parameter - Jan Schaumann - Don't bail if the IQ message doesn't contain a query - Jan Schaumann - Allow user to disable server certificate validity check - eleksir - Use Mozilla::CA for default path for ssl_ca_path - eleksir - Fix undefined warnings due to insufficient Moose Laziness - Misc distro file cleanup. - Automated testing with github actions - Remove author tests from user installs. - Point support to github now. 2.1.5 - resource now unique per instance of bot based on alias_hostname_pid - new dependency from core modules - Sys::Hostname - __PACKAGE__->meta->make_immutable; for performance. - Removed gtalk option. Use tls => 1, server_host => 'gmail.com' instead. - using 'componentname' in connect rather than after connection like we were hacking it in. - All non-printable characters stripped and replaced with '.' via [:printable:] regex - Added documentation on minimal connect parameters now we have quite a few optionals. 2.1.4 - _process_jabber_message was failing to parse multiline strings - fixed - Move to github - http://github.com/toddr/perl-net-jabber-bot - Tickets/Groups will stay on Google for now. - Discussed using Backend of POE::Component::Jabber which would be a more stable/supported solution but requires perl 5.10 - MooseX::Types now. - no Moose and no MooseX::Types at end of object for droppings 2.1.3 - Tests were failing if people didn't have Config::Std installed which is only used for Author tests 2.1.2 - Added warning message for legacy users initializing with message_callback or background_activity. 2.1.1 - Add proper meta data into makefile.pl - Cleanup debug messages. Used to be able to do them inline but moose subs don't call inside a string any more. 2.1.0 - MOOSE!!! 2.0.9 - New subroutines (AddUser, RmUser, GetStatus, GetRoster) to track ??? - IsConnected reports connect status now. - ReConnect now works as expected. Calls background each re-connect attempt. 2.0.8 - Bot now resonds to iq requests for version info. Also added gtalk example into the manifest (forgot for 2.0.7) 2.0.7 - Fix to get gtalk working, kindly provided by Yago Jesus. It's doing something really funky with setting the hostname to gmail.com. - Need to later review why we're doing this. maybe we're ignorning connect messages from the server? - Also added gtalk bot example courtesy of Yago - New subs: GetRoster, ChangeStatus 2.0.6 - Test::Pod::Coverage not configured to skip tests if not avail. Corrected this. 2.0.5 - Missed a test file mentioning IO::Prompt (t/03) 2.0.4 - Removed some email addresses present. - Tidy up manifest - Referring to google project in POD now. - Inserted gtalk fixes so the module will work with them. - Funky eval issue with gtalk client commented out. We'll have to look at that later, but for now we don't need it at all 2.0.3 - Creation of Mock Client to allow automation of testing without a server. - Also added Example script so someone can see how to use the module. 2.0.2 - Added Log::Log4Perl as dependancy. This should be in everyone's CPAN so it shouldn't be a big deal that people need to install it even though it's not necessary for people to use the module... 2.0.1 - Oops! Guess I need to make this module dependant on Net::Jabber if it's ever going to make test. 2.0.0 - Move to 3 digit version (see pause.perl.org FAQ about starting with 2 digit version and going to 3) - internal callback maker created to reduce code. 1.2.1 - Call back functions how call self funcion via anonymous subs. - Minor bug fixes and cleanup. 1.2.0 - Re-enabled config test, plus fixed some docs. Versioning changes from here out to be 3 digit. - Bot will respond to different addressings per forum (all messages, jbot:, etc.) 1.1 - Initial CPAN release - Basic tests built. Still more needed. Some of the limits are hard - coded. Arguably, these should be more in a child module, not the base class? - 1.0 Initial pre-CPAN release -- Does basic stuff but no tests yet. not CPAN ready Net-Jabber-Bot-2.1.7/README.md0000644000000000000000000003222013610656122014132 0ustar rootroot[![](https://github.com/toddr/perl-net-jabber-bot/workflows/linux/badge.svg)](https://github.com/toddr/perl-net-jabber-bot/actions) [![](https://github.com/toddr/perl-net-jabber-bot/workflows/macos/badge.svg)](https://github.com/toddr/perl-net-jabber-bot/actions) [![](https://github.com/toddr/perl-net-jabber-bot/workflows/windows/badge.svg)](https://github.com/toddr/perl-net-jabber-bot/actions) # NAME Net::Jabber::Bot - Automated Bot creation with safeties # VERSION Version 2.1.6 # SYNOPSIS Program design: This is a Moose based Class. The idea behind the module is that someone creating a bot should not really have to know a whole lot about how the Jabber protocol works in order to use it. It also allows us to abstract away all the things that can get a bot maker into trouble. Essentially the object helps protect the coders from their own mistakes. All someone should have to know and define in the program away from the object is: - 1. Config - Where to connect, how often to do things, timers, etc - 2. A subroutine to be called by the bot object when a new message comes in. - 3. A subroutine to be called by the bot object every so often that lets the user do background activities (check logs, monitor web pages, etc.), The object at present has the following enforced safeties as long as you do not override safety mode: - 1. Limits messages per second, configurable at start up, (Max is 5 per second) by requiring a sleep timer in the message sending subroutine each time one is sent. - 2. Endless loops of responding to self prevented by now allowing the bot message processing subroutine to know about messages from self - 3. Forum join grace period to prevent bot from reacting to historical messages - 4. Configurable aliases the bot will respond to per forum - 5. Limits maximum message size, preventing messages that are too large from being sent (largest configurable message size limit is 1000). - 6. Automatic chunking of messages to split up large messages in message sending subroutine - 7. Limit on messages per hour. (max configurable limit of 125) Messages are visible via log4perl, but not ever be sent once the message limit is reached for that hour. # FUNCTIONS - **new** Minimal: my $bot = Net::Jabber::Bot->new( server => 'host.domain.com', # Name of server when sending messages internally. conference_server => 'conference.host.domain.com', port => 522, username => 'username', password => 'pasword', safety_mode => 1, message_function => \&new_bot_message, background_function => \&background_checks, forums_and_responses => \%forum_list ); All options: my $bot = Net::Jabber::Bot->new( server => 'host.domain.com', # Name of server when sending messages internally. conference_server => 'conference.host.domain.com', server_host => 'talk.domain.com', # used to specify what jabber server to connect to on connect? tls => 0, # set to 1 for google ssl_ca_path => '', # path to your CA cert bundle ssl_verify => 0, # for testing and for self-signed certificates connection_type => 'tcpip', port => 522, username => 'username', password => 'pasword', alias => 'cpan_bot', message_function => \&new_bot_message, background_function => \&background_checks, loop_sleep_time => 15, process_timeout => 5, forums_and_responses => \%forum_list, ignore_server_messages => 1, ignore_self_messages => 1, out_messages_per_second => 4, max_message_size => 1000, max_messages_per_hour => 100 ); Set up the object and connect to the server. Hash values are passed to new as a hash. The following initialization variables can be passed. Only marked variables are required (TODO) - **safety\_mode** safety_mode = (1,0) Determines if the bot safety features are turned on and enforced. This mode is on by default. Many of the safety features are here to assure you do not crash your favorite jabber server with floods, etc. DO NOT turn it off unless you're sure you know what you're doing (not just Sledge Hammer ceratin) - **server** Jabber server name - **server\_host** Defaults to the same value set for 'server' above. This is where the bot initially connects. For google for instance, you should set this to 'gmail.com' - **conference\_server** conferencee server (usually conference.$server\_name) - **port** Defaults to 5222 - **tls** Boolean value. defaults to 0. for google, it is know that this value must be 1 to work. - **ssl\_ca\_path** The path to your CA cert bundle. This is passed on to XML::Stream eventually. - **ssl\_verify** Enable or disable server certificate validity check when connecting to server. This is passed on to XML::Stream eventually. - **connection\_type** defaults to 'tcpip' also takes 'http' - **username** The user you authenticate with to access the server. Not full name, just the stuff to the left of the @... - **password** password to get into the server - **alias** This will be your nickname in rooms, as well as the login resource (which can't have duplicates). I couldn't come up with any reason these should not be the same so hardcoded them to be the same. - **forums\_and\_responses** A hash ref which lists the forum names to join as the keys and the values are an array reference to a list of strings they are supposed to be responsive to. The array is order sensitive and an empty string means it is going to respond to all messages in this forum. Make sure you list this last. The found 'response string' is assumed to be at the beginning of the message. The message\_funtion function will be called with the modified string. alias = jbot:, attention: example1: message: 'jbot: help' passed to callback: 'help' - **message\_function** The subroutine the bot will call when a new message is recieved by the bot. Only called if the bot's logic decides it's something you need to know about. - **background\_function** The subroutine the bot will call when every so often (loop\_sleep\_time) to allow you to do background activities outside jabber stuff (check logs, web pages, etc.) - **loop\_sleep\_time** Frequency background function is called. - **process\_timeout** Time Process() will wait if no new activity is received from the server - **ignore\_server\_messages** Boolean value as to whether we should ignore messages sent to us from the jabber server (addresses can be a little cryptic and hard to process) - **ignore\_self\_messages** Boolean value as to whether we should ignore messages sent by us. BE CAREFUL if you turn this on!!! Turning this on risks potentially endless loops. If you're going to do this, please be sure safety is turned on at least initially. - **out\_messages\_per\_second** Limits the number of messages per second. Number must be <gt> 0 default: 5 safety: 5 - **max\_message\_size** Specify maximimum size a message can be before it's split and sent in pieces. default: 1,000,000 safety: 1,000 - **max\_messages\_per\_hour** Limits the number of messages per hour before we refuse to send them default: 125 safety: 166 - **JoinForum** Joins a jabber forum and sleeps safety time. Also prevents the object from responding to messages for a grace period in efforts to get it to not respond to historical messages. This has failed sometimes. NOTE: No error detection for join failure is present at the moment. (TODO) - **Process** Mostly calls it's client connection's "Process" call. Also assures a timeout is enforced if not fed to the subroutine You really should not have to call this very often. You should mostly be calling Start() and just let the Bot kernel handle all this. - **Start** Primary subroutine save new called by the program. Does an endless loop of: - 1. Process - 2. If Process failed, Reconnect to server over larger and larger timeout - 3. run background process fed from new, telling it who I am and how many loops we have been through. - 4. Enforce a sleep to prevent server floods. - **ReconnectToServer** You should not ever need to use this. the Start() kernel usually figures this out and calls it. Internal process: 1. Disconnects 3. Re-initializes - **Disconnect** Disconnects from server if client object is defined. Assures the client object is deleted. - **IsConnected** Reports connect state (true/false) based on the status of client\_start\_time. - **\_process\_jabber\_message** - DO NOT CALL Handles incoming messages. - **get\_responses** $bot->get_ident($forum_name); Returns the array of messages we are monitoring for in supplied forum or replies with undef. - **\_jabber\_in\_iq\_message** - DO NOT CALL Called when the client receives new messages during Process of this type. - **\_jabber\_presence\_message** - DO NOT CALL Called when the client receives new presence messages during Process. Mostly we are just pushing the data down into the client DB for later processing. - **respond\_to\_self\_messages** $bot->respond_to_self_messages($value = 1); Tells the bot to start reacting to it\\'s own messages if non-zero is passed. Default is 1. - **get\_messages\_this\_hour** $bot->get_messages_this_hour(); replys with number of messages sent so far this hour. - **get\_safety\_mode** Validates that we are in safety mode. Returns a bool as long as we are an object, otherwise returns undef - **SendGroupMessage** $bot->SendGroupMessage($name, $message); Tells the bot to send a message to the recipient room name - **SendPersonalMessage** $bot->SendPersonalMessage($recipient, $message); How to send an individual message to someone. $recipient must read as user@server/Resource or it will not send. - **SendJabberMessage** $bot->SendJabberMessage($recipient, $message, $message_type, $subject); The master subroutine to send a message. Called either by the user, SendPersonalMessage, or SendGroupMessage. Sometimes there is call to call it directly when you do not feel like figuring you messaged you. Assures message size does not exceed a limit and chops it into pieces if need be. NOTE: non-printable characters (unicode included) will be stripped before sending to the server via: s/\[^\[:print:\]\]+/./xmsg - **SetForumSubject** $bot->SetForumSubject($recipient, $subject); Sets the subject of a forum - **ChangeStatus** $bot->ChangeStatus($presence_mode, $status_string); Sets the Bot's presence status. $presence mode could be something like: (Chat, Available, Away, Ext. Away, Do Not Disturb). $status\_string is an optional comment to go with your presence mode. It is not required. - **GetRoster** $bot->GetRoster(); Returns a list of the people logged into the server. I suspect we really want to know who is in a paticular forum right? In which case we need another sub for this. - **GetStatus** Need documentation from Yago on this sub. - **AddUser** Need documentation from Yago on this sub. - **RmUser** Need documentation from Yago on this sub. # AUTHOR Todd Rinaldo `` # BUGS Please report any bugs or feature requests to `perl-net-jabber-bot@googlegroups.com`, or through the web interface at [http://code.google.com/p/perl-net-jabber-bot/issues/entry](http://code.google.com/p/perl-net-jabber-bot/issues/entry). I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. # SUPPORT You can find documentation for this module with the perldoc command. perldoc Net::Jabber::Bot You can also look for information at: - AnnoCPAN: Annotated CPAN documentation [http://annocpan.org/dist/Net-Jabber-Bot](http://annocpan.org/dist/Net-Jabber-Bot) - CPAN Ratings [http://cpanratings.perl.org/d/Net-Jabber-Bot](http://cpanratings.perl.org/d/Net-Jabber-Bot) - Metacpan [https://metacpan.org/pod/Net::Jabber::Bot](https://metacpan.org/pod/Net::Jabber::Bot) - Github [https://github.com/toddr/perl-net-jabber-bot](https://github.com/toddr/perl-net-jabber-bot) # ACKNOWLEDGEMENTS # COPYRIGHT & LICENSE Copyright 2007 Todd E Rinaldo, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Net-Jabber-Bot-2.1.7/MANIFEST0000644000000000000000000000070413754314164014014 0ustar rootrootChanges examples/bot_example.pl examples/gtalk_RSSbot.pl lib/Net/Jabber/Bot.pm Makefile.PL MANIFEST MANIFEST.SKIP README.md t/00-load.t t/03-test_connectivity.t t/05-helper_functions.t t/06-test_safeties.t t/99-pod-coverage.t t/99-pod.t t/lib/MockJabberClient.pm t/test_config.sample META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) Net-Jabber-Bot-2.1.7/lib/0000755000000000000000000000000013754314163013427 5ustar rootrootNet-Jabber-Bot-2.1.7/lib/Net/0000755000000000000000000000000013754314163014155 5ustar rootrootNet-Jabber-Bot-2.1.7/lib/Net/Jabber/0000755000000000000000000000000013754314163015342 5ustar rootrootNet-Jabber-Bot-2.1.7/lib/Net/Jabber/Bot.pm0000644000000000000000000011657513754313641016443 0ustar rootrootpackage Net::Jabber::Bot; use Moose; use MooseX::Types -declare => [qw( JabberClientObject PosInt PosNum HundredInt )]; # import builtin types use MooseX::Types::Moose qw/Int HashRef Str Maybe ArrayRef Bool CodeRef Object Num/; use version; use Net::Jabber; use Time::HiRes; use Sys::Hostname; use Log::Log4perl qw(:easy); use Mozilla::CA; coerce Bool, from Str, via { ( $_ =~ m/(^on$)|(^true$)/i ) + 0 }; # True if it's on or true. Otherwise false. subtype JabberClientObject, as Object, where { $_->isa('Net::Jabber::Client') }; subtype PosInt, as Int, where { $_ > 0 }; subtype PosNum, as Num, where { $_ > 0 }; subtype HundredInt, as Num, where { $_ > 100 }; has jabber_client => ( isa => Maybe [JabberClientObject], is => 'rw', default => sub { Net::Jabber::Client->new } ); #my %connection_hash : ATTR; # Keep track of connection options fed to client. has 'client_session_id' => ( isa => Str, is => 'rw' ); has 'connect_time' => ( isa => PosInt, is => 'rw', default => 9_999_999_999 ); has 'forum_join_grace' => ( isa => PosNum, is => 'rw', default => 10 ); has 'server_host' => ( isa => Str, is => 'rw', lazy => 1, default => sub { shift->server } ); has 'server' => ( isa => Str, is => 'rw' ); has 'port' => ( isa => PosInt, is => 'rw', default => 5222 ); has 'tls' => ( isa => Bool, is => 'rw', default => '0' ); has 'ssl_ca_path' => ( isa => Str, is => 'rw', default => Mozilla::CA::SSL_ca_file() ); has 'ssl_verify' => ( isa => Bool, is => 'rw', default => '1' ); has 'connection_type' => ( isa => Str, is => 'rw', default => 'tcpip' ); has 'conference_server' => ( isa => Str, is => 'rw' ); has 'username' => ( isa => Str, is => 'rw' ); has 'password' => ( isa => Str, is => 'rw' ); has 'alias' => ( isa => Str, lazy => 1, is => 'rw', default => 'net_jabber_bot' ); # Resource defaults to alias_hostname_pid has 'resource' => ( isa => Str, lazy => 1, is => 'rw', default => sub { shift->alias . "_" . hostname . "_" . $$ } ); has 'message_function' => ( isa => Maybe [CodeRef], is => 'rw', default => sub { undef } ); has 'background_function' => ( isa => Maybe [CodeRef], is => 'rw', default => sub { undef } ); has 'loop_sleep_time' => ( isa => PosNum, is => 'rw', default => 5 ); has 'process_timeout' => ( isa => PosNum, is => 'rw', default => 5 ); has 'from_full' => ( isa => Str, lazy => 1, is => 'rw', default => sub { my $self = shift; $self->username || '' . '@' . $self->server || '' . '/' . $self->alias || ''; } ); has 'safety_mode' => ( isa => Bool, is => 'rw', default => 1, coerce => 1 ); has 'ignore_server_messages' => ( isa => Bool, is => 'rw', default => 1, coerce => 1 ); has 'ignore_self_messages' => ( isa => Bool, is => 'rw', default => 1, coerce => 1 ); has 'forums_and_responses' => ( isa => HashRef [ ArrayRef [Str] ], is => 'rw' ); # List of forums we're in and the strings we monitor for. has 'forum_join_time' => ( isa => HashRef [Int], is => 'rw', default => sub { {} } ); # List of when we joined each forum has 'out_messages_per_second' => ( isa => PosNum, is => 'rw', default => sub { 5 } ); has 'message_delay' => ( isa => PosNum, is => 'rw', default => sub { 1 / 5 } ); has 'max_message_size' => ( isa => HundredInt, is => 'rw', default => 1000000 ); has 'max_messages_per_hour' => ( isa => PosInt, is => 'rw', default => 1000000 ); # Initialize this hour's message count. has 'messages_sent_today' => ( isa => 'HashRef', is => 'ro', default => sub { { (localtime)[7] => { (localtime)[2] => 0 } } } ); #my %message_function : ATTR; # What is called if we are fed a new message once we are logged in. #my %bot_background_function : ATTR; # What is called if we are fed a new message once we are logged in. #my %forum_join_time : ATTR; # Tells us if we've parsed historical messages yet. #my %client_start_time :ATTR; # Track when we came online. Also used to determine if we're online. #my %process_timeout : ATTR; # Time to take in process loop if no messages found #my %loop_sleep_time : ATTR; # Time to sleep each time we go through a Start() loop. #my %ignore_messages : ATTR; # Messages to ignore if we recieve them. #my %forums_and_responses: ATTR; # List of forums we have joined and who we respond to in each forum #my %message_delay: ATTR; # Allows us to limit Messages per second #my %max_message_size: ATTR; # Maximum allowed message size before we chunk them. #my %forum_join_grace: ATTR; # Time before we start responding to forum messages. #my %messages_sent_today: ATTR; # Tracks messages sent in 2 dimentional hash by day/hour #my %max_messages_per_hour: ATTR; # Limits the number of messages per hour. #my %safety_mode: ATTR; # Tracks if we are in safety mode. =head1 NAME Net::Jabber::Bot - Automated Bot creation with safeties =head1 VERSION Version 2.1.7 =cut our $VERSION = '2.1.7'; =head1 SYNOPSIS Program design: This is a Moose based Class. The idea behind the module is that someone creating a bot should not really have to know a whole lot about how the Jabber protocol works in order to use it. It also allows us to abstract away all the things that can get a bot maker into trouble. Essentially the object helps protect the coders from their own mistakes. All someone should have to know and define in the program away from the object is: =over =item 1. Config - Where to connect, how often to do things, timers, etc =item 2. A subroutine to be called by the bot object when a new message comes in. =item 3. A subroutine to be called by the bot object every so often that lets the user do background activities (check logs, monitor web pages, etc.), =back The object at present has the following enforced safeties as long as you do not override safety mode: =over =item 1. Limits messages per second, configurable at start up, (Max is 5 per second) by requiring a sleep timer in the message sending subroutine each time one is sent. =item 2. Endless loops of responding to self prevented by now allowing the bot message processing subroutine to know about messages from self =item 3. Forum join grace period to prevent bot from reacting to historical messages =item 4. Configurable aliases the bot will respond to per forum =item 5. Limits maximum message size, preventing messages that are too large from being sent (largest configurable message size limit is 1000). =item 6. Automatic chunking of messages to split up large messages in message sending subroutine =item 7. Limit on messages per hour. (max configurable limit of 125) Messages are visible via log4perl, but not ever be sent once the message limit is reached for that hour. =back =head1 FUNCTIONS =over 4 =item B Minimal: my $bot = Net::Jabber::Bot->new( server => 'host.domain.com', # Name of server when sending messages internally. conference_server => 'conference.host.domain.com', port => 522, username => 'username', password => 'pasword', safety_mode => 1, message_function => \&new_bot_message, background_function => \&background_checks, forums_and_responses => \%forum_list ); All options: my $bot = Net::Jabber::Bot->new( server => 'host.domain.com', # Name of server when sending messages internally. conference_server => 'conference.host.domain.com', server_host => 'talk.domain.com', # used to specify what jabber server to connect to on connect? tls => 0, # set to 1 for google ssl_ca_path => '', # path to your CA cert bundle ssl_verify => 0, # for testing and for self-signed certificates connection_type => 'tcpip', port => 522, username => 'username', password => 'pasword', alias => 'cpan_bot', message_function => \&new_bot_message, background_function => \&background_checks, loop_sleep_time => 15, process_timeout => 5, forums_and_responses => \%forum_list, ignore_server_messages => 1, ignore_self_messages => 1, out_messages_per_second => 4, max_message_size => 1000, max_messages_per_hour => 100 ); Set up the object and connect to the server. Hash values are passed to new as a hash. The following initialization variables can be passed. Only marked variables are required (TODO) =over 5 =item B safety_mode = (1,0) Determines if the bot safety features are turned on and enforced. This mode is on by default. Many of the safety features are here to assure you do not crash your favorite jabber server with floods, etc. DO NOT turn it off unless you're sure you know what you're doing (not just Sledge Hammer ceratin) =item B Jabber server name =item B Defaults to the same value set for 'server' above. This is where the bot initially connects. For google for instance, you should set this to 'gmail.com' =item B conferencee server (usually conference.$server_name) =item B Defaults to 5222 =item B Boolean value. defaults to 0. for google, it is know that this value must be 1 to work. =item B The path to your CA cert bundle. This is passed on to XML::Stream eventually. =item B Enable or disable server certificate validity check when connecting to server. This is passed on to XML::Stream eventually. =item B defaults to 'tcpip' also takes 'http' =item B The user you authenticate with to access the server. Not full name, just the stuff to the left of the @... =item B password to get into the server =item B This will be your nickname in rooms, as well as the login resource (which can't have duplicates). I couldn't come up with any reason these should not be the same so hardcoded them to be the same. =item B A hash ref which lists the forum names to join as the keys and the values are an array reference to a list of strings they are supposed to be responsive to. The array is order sensitive and an empty string means it is going to respond to all messages in this forum. Make sure you list this last. The found 'response string' is assumed to be at the beginning of the message. The message_funtion function will be called with the modified string. alias = jbot:, attention: example1: message: 'jbot: help' passed to callback: 'help' =item B The subroutine the bot will call when a new message is recieved by the bot. Only called if the bot's logic decides it's something you need to know about. =item B The subroutine the bot will call when every so often (loop_sleep_time) to allow you to do background activities outside jabber stuff (check logs, web pages, etc.) =item B Frequency background function is called. =item B Time Process() will wait if no new activity is received from the server =item B Boolean value as to whether we should ignore messages sent to us from the jabber server (addresses can be a little cryptic and hard to process) =item B Boolean value as to whether we should ignore messages sent by us. BE CAREFUL if you turn this on!!! Turning this on risks potentially endless loops. If you're going to do this, please be sure safety is turned on at least initially. =item B Limits the number of messages per second. Number must be 0 default: 5 safety: 5 =item B Specify maximimum size a message can be before it's split and sent in pieces. default: 1,000,000 safety: 1,000 =item B Limits the number of messages per hour before we refuse to send them default: 125 safety: 166 =back =cut # Handle initialization of objects of this class... sub BUILD { my ( $self, $params ) = @_; # Deal with legacy bug if ( $params->{background_activity} || $params->{message_callback} ) { my $warn_message = "\n\n" . "*" x 70 . "\n" . "WARNING!!! You're using old parameters for your bot initialization\n" . "'message_callback' should be changed to 'message_function'\n" . "'background_activity' should be changed to 'background_function'\n" . "I'm correcting this, but you should fix your code\n" . "*" x 70 . "\n" . "\n\n"; warn($warn_message); WARN($warn_message); $self->background_function( $params->{background_activity} ) if ( !$self->background_function && $params->{background_activity} ); $self->message_function( $params->{message_callback} ) if ( !$self->message_function && $params->{message_callback} ); sleep 30; } # Message delay is inverse of out_messages_per_second $self->message_delay( 1 / $self->out_messages_per_second ); # Enforce all our safety restrictions here. if ( $self->safety_mode ) { # more than 5 messages per second risks server flooding. $self->message_delay( 1 / 5 ) if ( $self->message_delay < 1 / 5 ); # Messages should be small to not overwhelm rooms/people/server $self->max_message_size(1000) if ( $self->max_message_size > 1000 ); # More than 4,000 messages a day is a little excessive. $self->max_messages_per_hour(125) if ( $self->max_messages_per_hour > 166 ); # Should not be responding to self messages to prevent loops. $self->ignore_self_messages(1); } #Initialize the connection. $self->_init_jabber; } # Return a code reference that will pass self in addition to arguements passed to callback code ref. sub _callback_maker { my $self = shift; my $Function = shift; # return sub {return $code_ref->($self, @_);}; return sub { return $Function->( $self, @_ ); }; } # Creates client object and manages connection. Called on new but also called by re-connect sub _init_jabber { my $self = shift; # Autocreate the jabber object (see has jabber_client) my $connection = $self->jabber_client; DEBUG("Set the call backs."); $connection->PresenceDB(); # Init presence DB. $connection->RosterDB(); # Init Roster DB. $connection->SetCallBacks( 'message' => $self->_callback_maker( \&_process_jabber_message ), 'presence' => $self->_callback_maker( \&_jabber_presence_message ) , 'iq' => $self->_callback_maker( \&_jabber_in_iq_message ) ); DEBUG( "Connect. hostname => " . $self->server . ", port => " . $self->port ); my %client_connect_hash = ( hostname => $self->server, port => $self->port, tls => $self->tls, ssl_ca_path => $self->ssl_ca_path, ssl_verify => $self->ssl_verify, connectiontype => $self->connection_type, componentname => $self->server_host, ); my $status = $connection->Connect(%client_connect_hash); if ( !defined $status ) { ERROR("ERROR: Jabber server is down or connection was not allowed: $!"); die("Jabber server is down or connection was not allowed: $!"); } DEBUG( "Logging in... as user " . $self->username . " / " . $self->resource ); DEBUG( "PW: " . $self->password ); # Moved into connect hash via 'componentname' # my $sid = $connection->{SESSION}->{id}; # $connection->{STREAM}->{SIDS}->{$sid}->{hostname} = $self->server_host; my @auth_result = $connection->AuthSend( username => $self->username, password => $self->password, resource => $self->resource, ); if ( !defined $auth_result[0] || $auth_result[0] ne "ok" ) { ERROR( "Authorization failed: for " . $self->username . " / " . $self->resource ); foreach my $result (@auth_result) { ERROR("$result"); } die( "Failed to re-connect: " . join( "\n", @auth_result ) ); } $connection->RosterRequest(); $self->client_session_id( $connection->{SESSION}->{id} ); DEBUG("Sending presence to tell world that we are logged in"); $connection->PresenceSend(); $self->Process(5); DEBUG("Getting Roster to tell server to send presence info"); $connection->RosterGet(); $self->Process(5); foreach my $forum ( keys %{ $self->forums_and_responses } ) { $self->JoinForum($forum); } INFO( "Connected to server '" . $self->server . "' successfully" ); $self->connect_time(time); # Track when we came online. return 1; } =item B Joins a jabber forum and sleeps safety time. Also prevents the object from responding to messages for a grace period in efforts to get it to not respond to historical messages. This has failed sometimes. NOTE: No error detection for join failure is present at the moment. (TODO) =cut sub JoinForum { my $self = shift; my $forum_name = shift; DEBUG( "Joining $forum_name on " . $self->conference_server . " as " . $self->alias ); $self->jabber_client->MUCJoin( room => $forum_name, server => $self->conference_server, nick => $self->alias, ); $self->forum_join_time->{$forum_name} = time; DEBUG( "Sleeping " . $self->message_delay . " seconds" ); Time::HiRes::sleep $self->message_delay; } =item B Mostly calls it's client connection's "Process" call. Also assures a timeout is enforced if not fed to the subroutine You really should not have to call this very often. You should mostly be calling Start() and just let the Bot kernel handle all this. =cut sub Process { # Call connection process. my $self = shift; my $timeout_seconds = shift; #If not passed explicitly $timeout_seconds = $self->process_timeout if ( !defined $timeout_seconds ); my $process_return = $self->jabber_client->Process($timeout_seconds); return $process_return; } =item B Primary subroutine save new called by the program. Does an endless loop of: =over =item 1. Process =item 2. If Process failed, Reconnect to server over larger and larger timeout =item 3. run background process fed from new, telling it who I am and how many loops we have been through. =item 4. Enforce a sleep to prevent server floods. =back =cut sub Start { my $self = shift; my $time_between_background_routines = $self->loop_sleep_time; my $process_timeout = $self->process_timeout; my $background_subroutine = $self->background_function; my $message_delay = $self->message_delay; my $last_background = time - $time_between_background_routines - 1; # Call background process every so often... my $counter = 0; # Keep track of how many times we've looped. Not sure if we'll use this long term. while (1) { # Loop for ever! # Process and re-connect if you have to. my $reconnect_timeout = 1; eval { $self->Process($process_timeout) }; if ($@) { #Assume the connection is down... ERROR("Server error: $@"); my $message = "Disconnected from " . $self->server . ":" . $self->port . " as " . $self->username; ERROR("$message Reconnecting..."); sleep 5; # TODO: Make re-connect time flexible somehow $self->ReconnectToServer(); } # Call background function if ( defined $background_subroutine && $last_background + $time_between_background_routines < time ) { &$background_subroutine( $self, ++$counter ); $last_background = time; } Time::HiRes::sleep $message_delay; } } =item B You should not ever need to use this. the Start() kernel usually figures this out and calls it. Internal process: 1. Disconnects 3. Re-initializes =cut sub ReconnectToServer { my $self = shift; my $background_subroutine = $self->background_function; $self->Disconnect(); my $sleep_time = 5; while ( !$self->IsConnected() ) { # jabber_client variable defines if we're connected. INFO("Sleeping $sleep_time before attempting re-connect"); sleep $sleep_time; $sleep_time *= 2 if ( $sleep_time < 300 ); $self->InitJabber(); INFO("Running background routine."); &$background_subroutine( $self, 0 ); # call background proc so we can check for errors while down. } } =item B Disconnects from server if client object is defined. Assures the client object is deleted. =cut sub Disconnect { my $self = shift; $self->connect_time( '9' x 10 ); # Way in the future INFO("Disconnecting from server"); return if ( !defined $self->jabber_client ); # do not proceed, no object. $self->jabber_client->Disconnect(); my $old_client = $self->jabber_client; $self->jabber_client(undef); DEBUG("Disconnected."); return 1; } =item B Reports connect state (true/false) based on the status of client_start_time. =cut sub IsConnected { my $self = shift; DEBUG( "REF = " . ref( $self->jabber_client ) ); return $self->connect_time; } # TODO: ***NEED VERY GOOD DOCUMENTATION HERE***** =item B<_process_jabber_message> - DO NOT CALL Handles incoming messages. =cut sub _process_jabber_message { my $self = shift; DEBUG("_process_jabber_message called"); my $session_id = shift; my $message = shift; my $type = $message->GetType(); my $fromJID = $message->GetFrom("jid"); my $from_full = $message->GetFrom(); my $from = $fromJID->GetUserID(); my $resource = $fromJID->GetResource(); my $subject = $message->GetSubject(); my $body = $message->GetBody(); my $reply_to = $from_full; $reply_to =~ s/\/.*$// if ( $type eq 'groupchat' ); # TODO: # Don't know exactly why but when a message comes from gtalk-web-interface, it works well, but if the message comes from Gtalk client, bot dies # my $message_date_text; eval { $message_date_text = $message->GetTimeStamp(); } ; # Eval is a really bad idea. we need to understand why this is failing. # my $message_date_text = $message->GetTimeStamp(); # Since we're not using the data, we'll turn this off since it crashes gtalk clients aparently? # my $message_date = UnixDate($message_date_text, "%s") - 1*60*60; # Convert to EST from CST; # Ignore any messages within 'forum_join_grace' seconds of start or join of that forum my $grace_period = $self->forum_join_grace; my $time_now = time; if ( $self->connect_time > $time_now - $grace_period || ( defined $self->forum_join_time->{$from} && $self->forum_join_time->{$from} > $time_now - $grace_period ) ) { my $cond1 = $self->connect_time . " > $time_now - $grace_period"; my $cond2 = $self->forum_join_time->{$from} || 'undef' . " > $time_now - $grace_period"; DEBUG("Ignoring messages cause I'm in startup for forum $from\n$cond1\n$cond2"); return; # Ignore messages the first few seconds. } # Ignore Group messages with no resource on them. (Server Messages?) if ( $self->ignore_server_messages ) { if ( $from_full !~ m/^([^\@]+)\@([^\/]+)\/(.+)$/ ) { DEBUG("Server message? ($from_full) - $message"); return if ( $from_full !~ m/^([^\@]+)\@([^\/]+)\// ); ERROR("Couldn't recognize from_full ($from_full). Ignoring message: $body"); return; } } # Are these my own messages? if ( $self->ignore_self_messages ) { # TODO: || $self->safety_mode (this breaks tests in 06?) if ( defined $resource && $resource eq $self->resource ) { # Ignore my own messages. DEBUG("Ignoring message from self...\n"); return; } } # Determine if this message was addressed to me. (groupchat only) my $bot_address_from; my @aliases_to_respond_to = $self->get_responses($from); if ( $#aliases_to_respond_to >= 0 and $type eq 'groupchat' ) { my $request; foreach my $address_type (@aliases_to_respond_to) { my $qm_address_type = quotemeta($address_type); next if ( $body !~ m/^\s*$qm_address_type\s*(\S.*)$/ms ); $request = $1; $bot_address_from = $address_type; last; # do not need to loop any more. } DEBUG("Message not relevant to bot"); return if ( !defined $request ); $body = $request; } # Call the message callback if it's defined. if ( defined $self->message_function ) { $self->message_function->( bot_object => $self, from_full => $from_full, body => $body, type => $type, reply_to => $reply_to, bot_address_from => $bot_address_from, message => $message ); return; } else { WARN("No handler for messages!"); INFO("New Message: $type from $from ($resource). sub=$subject -- $body"); } } =item B $bot->get_ident($forum_name); Returns the array of messages we are monitoring for in supplied forum or replies with undef. =cut sub get_responses { my $self = shift; my $forum = shift; if ( !defined $forum ) { WARN("No forum supplied for get_responses()"); return; } my @aliases_to_respond_to; if ( defined $self->forums_and_responses->{$forum} ) { @aliases_to_respond_to = @{ $self->forums_and_responses->{$forum} }; } return @aliases_to_respond_to; } =item B<_jabber_in_iq_message> - DO NOT CALL Called when the client receives new messages during Process of this type. =cut sub _jabber_in_iq_message { my $self = shift; my $session_id = shift; my $iq = shift; DEBUG( "IQ Message:" . $iq->GetXML() ); my $from = $iq->GetFrom(); # my $type = $iq->GetType();DEBUG("Type=$type"); my $query = $iq->GetQuery(); #DEBUG("query=" . Dumper($query)); if ( !$query ) { DEBUG("iq->GetQuery() returned undef."); return; } my $xmlns = $query->GetXMLNS(); DEBUG("xmlns=$xmlns"); my $iqReply; # Respond to version requests with information about myself. if ( $xmlns eq "jabber:iq:version" ) { # convert 5.010000 to 5.10.0 my $perl_version = $]; $perl_version =~ s/(\d{3})(?=\d)/$1./g; $perl_version =~ s/\.0+(\d)/.$1/; $self->jabber_client->VersionSend( to => $from, name => __PACKAGE__, ver => $VERSION, os => "Perl v$perl_version" ); } else { # Unknown request. Just ignore it. return; } if ($iqReply) { DEBUG( "Reply: ", $iqReply->GetXML() ); $self->jabber_client->Send($iqReply); } # INFO("IQ from $from ($type). XMLNS: $xmlns"); } =item B<_jabber_presence_message> - DO NOT CALL Called when the client receives new presence messages during Process. Mostly we are just pushing the data down into the client DB for later processing. =cut sub _jabber_presence_message { my $self = shift; my $session_id = shift; my $presence = shift; my $type = $presence->GetType(); if ( $type eq 'subscribe' ) { # Always allow people to subscribe to us. Why wouldn't we? my $from = $presence->GetFrom(); $self->jabber_client->Subscription( type => "subscribe", to => $from ); $self->jabber_client->Subscription( type => "subscribed", to => $from ); INFO("Processed subscription request from $from"); return; } elsif ( $type eq 'unsubscribe' ) { # Always allow people to subscribe to us. Why wouldn't we? my $from = $presence->GetFrom(); $self->jabber_client->Subscription( type => "unsubscribed", to => $from ); INFO("Processed unsubscribe request from $from"); return; } # Without explicitly setting a priority, XMPP::Protocol will store all JIDs with an empty # priority under the same key rather than in an array. $presence->SetPriority(0) unless $presence->GetPriority(); $self->jabber_client->PresenceDBParse($presence); # Since we are always an object just throw it into the db. my $from = $presence->GetFrom(); $from = "." if ( !defined $from ); my $status = $presence->GetStatus(); $status = "." if ( !defined $status ); DEBUG("Presence From $from t=$type s=$status"); DEBUG( "Presence XML: " . $presence->GetXML() ); } =item B $bot->respond_to_self_messages($value = 1); Tells the bot to start reacting to it\'s own messages if non-zero is passed. Default is 1. =cut sub respond_to_self_messages { my $self = shift; my $setting = shift; $setting = 1 if ( !defined $setting ); $self->ignore_self_messages( !$setting ); return !!$setting; } =item B $bot->get_messages_this_hour(); replys with number of messages sent so far this hour. =cut sub get_messages_this_hour { my $self = shift; my $yday = (localtime)[7]; my $hour = (localtime)[2]; my $messages_this_hour = $self->messages_sent_today->{$yday}->{$hour}; return $messages_this_hour || 0; # Assure it's not undef to avoid math warnings } =item B Validates that we are in safety mode. Returns a bool as long as we are an object, otherwise returns undef =cut sub get_safety_mode { my $self = shift; # Must be in safety mode and all thresholds met. my $mode = $self->safety_mode && $self->message_delay >= 1 / 5 && $self->max_message_size <= 1000 && $self->max_messages_per_hour <= 166 && $self->ignore_self_messages; return $mode || 0; } =item B $bot->SendGroupMessage($name, $message); Tells the bot to send a message to the recipient room name =cut sub SendGroupMessage { my $self = shift; my $recipient = shift; my $message = shift; $recipient .= '@' . $self->conference_server if ( $recipient !~ m{\@} ); return $self->SendJabberMessage( $recipient, $message, 'groupchat' ); } =item B $bot->SendPersonalMessage($recipient, $message); How to send an individual message to someone. $recipient must read as user@server/Resource or it will not send. =cut sub SendPersonalMessage { my $self = shift; my $recipient = shift; my $message = shift; return $self->SendJabberMessage( $recipient, $message, 'chat' ); } =item B $bot->SendJabberMessage($recipient, $message, $message_type, $subject); The master subroutine to send a message. Called either by the user, SendPersonalMessage, or SendGroupMessage. Sometimes there is call to call it directly when you do not feel like figuring you messaged you. Assures message size does not exceed a limit and chops it into pieces if need be. NOTE: non-printable characters (unicode included) will be stripped before sending to the server via: s/[^[:print:]]+/./xmsg =cut sub SendJabberMessage { my $self = shift; my $recipient = shift; my $message = shift; my $message_type = shift; my $subject = shift; my $max_size = $self->max_message_size; # Split the message into no more than max_message_size so that we do not piss off jabber. # Split on new line. Space if you have to or just chop at max size. my @message_chunks = ( $message =~ /.{1,$max_size}$|.{1,$max_size}\n|.{1,$max_size}\s|.{1,$max_size}/gs ); DEBUG("Max message = $max_size. Splitting...") if ( $#message_chunks > 0 ); my $return_value; foreach my $message_chunk (@message_chunks) { my $msg_return = $self->_send_individual_message( $recipient, $message_chunk, $message_type, $subject ); if ( defined $msg_return ) { $return_value .= $msg_return; } } return $return_value; } # $self->_send_individual_message($recipient, $message_chunk, $message_type, $subject); # Private subroutine only called directly by SetForumSubject and SendJabberMessage. # There are a bunch of fancy things this does, but the important things are: # 1. sleep a minimum of .2 seconds every message # 2. Make sure we have not sent too many messages this hour and block sends if they are attempted over a certain limit (max limit is 125) # 3. Strip out special characters that will get us booted from the server. sub _send_individual_message { my $self = shift; my $recipient = shift; my $message_chunk = shift; my $message_type = shift; my $subject = shift; if ( !defined $message_type ) { ERROR("Undefined \$message_type"); return "No message type!\n"; } if ( !defined $recipient ) { ERROR('$recipient not defined!'); return "No recipient!\n"; } my $yday = (localtime)[7]; my $hour = (localtime)[2]; my $messages_this_hour = $self->messages_sent_today->{$yday}->{$hour} += 1; if ( $messages_this_hour > $self->max_messages_per_hour ) { $subject = "" if ( !defined $subject ); # Keep warning messages quiet. $message_chunk = "" if ( !defined $message_chunk ); # Keep warning messages quiet. ERROR( "Can't Send message because we've already tried to send $messages_this_hour of $self->max_messages_per_hour messages this hour.\n" . "To: $recipient\n" . "Subject: $subject\n" . "Type: $message_type\n" . "Message sent:\n" . "$message_chunk" ); # Send 1 panic message out to jabber if this is our last message before quieting down. return "Too many messages ($messages_this_hour)\n"; } if ( !$self->IsConnected ) { $subject = "" if ( !defined $subject ); # Keep warning messages quiet. $message_chunk = "" if ( !defined $message_chunk ); # Keep warning messages quiet. ERROR( "Can't Jabber server is down. Tried to send: \n" . "To: $recipient\n" . "Subject: $subject\n" . "Type: $message_type\n" . "Message sent:\n" . "$message_chunk" ); # Send 1 panic message out to jabber if this is our last message before quieting down. return "Server is down.\n"; } # Strip out anything that's not a printable character except new line, we want to be able to send multiline message, aren't we? # Now with unicode support? $message_chunk =~ s/[^\r\n[:print:]]+/./xmsg; my $message_length = length($message_chunk); DEBUG("Sending message $yday-$hour-$messages_this_hour $message_length bytes to $recipient"); $self->jabber_client->MessageSend( to => $recipient, body => $message_chunk , type => $message_type # , from => $connection_hash{$obj_ID}{'from_full'} , subject => $subject ); DEBUG( "Sleeping " . $self->message_delay . " after sending message." ); Time::HiRes::sleep $self->message_delay; #Throttle messages. if ( $messages_this_hour == $self->max_messages_per_hour ) { $self->jabber_client->MessageSend( to => $recipient, body => "Cannot send more messages this hour. " . "$messages_this_hour of " . $self->max_messages_per_hour . " already sent." , type => $message_type ); } return; # Means we succeeded! } =item B $bot->SetForumSubject($recipient, $subject); Sets the subject of a forum =cut sub SetForumSubject { my $self = shift; my $recipient = shift; my $subject = shift; if ( length $subject > $self->max_message_size ) { my $subject_len = length($subject); ERROR("Someone tried to send a subject message $subject_len bytes long!"); my $subject = substr( $subject, 0, $self->max_message_size ); DEBUG("Truncated subject: $subject"); return "Subject is too long!"; } $self->_send_individual_message( $recipient, "Setting subject to $subject", 'groupchat', $subject ); return; } =item B $bot->ChangeStatus($presence_mode, $status_string); Sets the Bot's presence status. $presence mode could be something like: (Chat, Available, Away, Ext. Away, Do Not Disturb). $status_string is an optional comment to go with your presence mode. It is not required. =cut sub ChangeStatus { my $self = shift; my $presence_mode = shift; my $status_string = shift; # (optional) $self->jabber_client->PresenceSend( show => $presence_mode, status => $status_string ); return 1; } =item B $bot->GetRoster(); Returns a list of the people logged into the server. I suspect we really want to know who is in a paticular forum right? In which case we need another sub for this. =cut sub GetRoster { my $self = shift; my @rosterlist; foreach my $jid ( $self->jabber_client->RosterDBJIDs() ) { my $username = $jid->GetJID(); push( @rosterlist, $username ); } return @rosterlist; } =item B Need documentation from Yago on this sub. =cut sub GetStatus { my $self = shift; my ($jid) = shift; my $Pres = $self->jabber_client->PresenceDBQuery($jid); if ( !( defined($Pres) ) ) { return "unavailable"; } my $show = $Pres->GetShow(); if ($show) { return $show; } return "available"; } =item B Need documentation from Yago on this sub. =cut sub AddUser { my $self = shift; my $user = shift; $self->jabber_client->Subscription( type => "subscribe", to => $user ); $self->jabber_client->Subscription( type => "subscribed", to => $user ); } =item B Need documentation from Yago on this sub. =cut sub RmUser { my $self = shift; my $user = shift; $self->jabber_client->Subscription( type => "unsubscribe", to => $user ); $self->jabber_client->Subscription( type => "unsubscribed", to => $user ); } =back =head1 AUTHOR Todd Rinaldo C<< >> =head1 BUGS Please report any bugs or feature requests to C, or through the web interface at L. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. =head1 SUPPORT You can find documentation for this module with the perldoc command. perldoc Net::Jabber::Bot You can also look for information at: =over 4 =item * AnnoCPAN: Annotated CPAN documentation L =item * CPAN Ratings L =item * Metacpan L =item * Github L =back =head1 ACKNOWLEDGEMENTS =head1 COPYRIGHT & LICENSE Copyright 2007 Todd E Rinaldo, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut __PACKAGE__->meta->make_immutable; no Moose; no MooseX::Types; 1; # End of Net::Jabber::Bot Net-Jabber-Bot-2.1.7/META.yml0000644000000000000000000000207613754314164014140 0ustar rootroot--- abstract: 'Automated Bot creation with safeties' author: - 'Todd E Rinaldo ' build_requires: ExtUtils::MakeMaker: '0' FindBin: '0' Test::More: '0' Test::NoWarnings: '0' lib: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 7.44, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Net-Jabber-Bot no_index: directory: - t - inc requires: FindBin: '0' Log::Log4perl: '0' Moose: '0.82' MooseX::Types: '0.12' Mozilla::CA: '0' Net::Jabber: '2' Sys::Hostname: '0' Test::More: '0' Test::NoWarnings: '0' Time::HiRes: '0' lib: '0' version: '0' resources: bugtracker: https://github.com/toddr/perl-net-jabber-bot/issues homepage: http://wiki.github.com/toddr/perl-net-jabber-bot license: http://dev.perl.org/licenses/ repository: http://github.com/toddr/perl-net-jabber-bot/tree/master version: v2.1.7 x_serialization_backend: 'CPAN::Meta::YAML version 0.018' Net-Jabber-Bot-2.1.7/META.json0000644000000000000000000000340613754314164014306 0ustar rootroot{ "abstract" : "Automated Bot creation with safeties", "author" : [ "Todd E Rinaldo " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 7.44, CPAN::Meta::Converter version 2.150010", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Net-Jabber-Bot", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0", "FindBin" : "0", "Test::More" : "0", "Test::NoWarnings" : "0", "lib" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "FindBin" : "0", "Log::Log4perl" : "0", "Moose" : "0.82", "MooseX::Types" : "0.12", "Mozilla::CA" : "0", "Net::Jabber" : "2", "Sys::Hostname" : "0", "Test::More" : "0", "Test::NoWarnings" : "0", "Time::HiRes" : "0", "lib" : "0", "version" : "0" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/toddr/perl-net-jabber-bot/issues" }, "homepage" : "http://wiki.github.com/toddr/perl-net-jabber-bot", "license" : [ "http://dev.perl.org/licenses/" ], "repository" : { "url" : "http://github.com/toddr/perl-net-jabber-bot/tree/master" } }, "version" : "v2.1.7", "x_serialization_backend" : "JSON::PP version 4.04" }