Net-Jabber-Bot-3.01/000755 000765 000024 00000000000 15164032431 015613 5ustar00todd.rinaldostaff000000 000000 Net-Jabber-Bot-3.01/Changes000644 000765 000024 00000024647 15164032134 017123 0ustar00todd.rinaldostaff000000 000000 Revision history for Net-Jabber-Bot 3.01 2026-04-03 Todd Rinaldo [Fixes] - #69 Fix from_full attribute to use resource instead of alias, which caused incorrect sender identification (PR #64) - Fix self-message detection in MUC group chats — bot could process its own messages as if from another user (PR #59) - Fix Start() to reconnect on silent disconnection when Process returns undef (PR #63) - Fix SetForumSubject to propagate send errors instead of silently discarding them (PR #68) - Fix message chunking off-by-one that could exceed max_message_size by one character (PR #71) - Replace // operator with ternary for Perl 5.8 compatibility (PR #70) [Improvements] - Bump MIN_PERL_VERSION from 5.008 to 5.010 in Makefile.PL (PR #66) [Maintenance] - Parallelize Windows and macOS CI jobs with Linux matrix (PR #73) - New test: comprehensive coverage for 6 undertested public API methods (PR #60) - New test: comprehensive coverage for message_function callback contract (PR #61) - New test: comprehensive coverage for presence and IQ protocol handlers (PR #62) - New test: comprehensive coverage for message sending pipeline (PR #65) - New test: coverage for SetForumSubject method (PR #67) - Update MANIFEST and README.md 2.1.9 2026-03-22 Todd Rinaldo [Fixes] - Fix uninitialized value warning in SendJabberMessage when concatenating error strings from chunked messages via .= on undef variable; also fix POD method name typo (get_ident -> get_responses) and broken E escape sequence (PR #58) - Guard all public API methods (ChangeStatus, GetRoster, GetStatus, Process, JoinForum, AddUser, RmUser) against crashes when called while disconnected — previously crashed with "Can't call method on an undefined value" (PR #55) - Fix misleading "Message not relevant to bot" debug log that fired for all messages instead of only genuinely irrelevant ones (PR #57) [Improvements] - Add Stop() method for graceful shutdown of Start() event loop; Start() now returns the loop iteration count and can be re-called after Stop() (PR #56) [Maintenance] - Remove dead code: unused $iqReply and unreachable block in _jabber_in_iq_message, unused $reconnect_timeout in Start() (PR #57) - Remove legacy Class::Std commented-out attribute declarations, write real documentation for GetStatus, AddUser, and RmUser methods (PR #58) - New test: t/07-test_disconnect_public_api.t for public API calls while disconnected (PR #55) - New test: t/08-test_start_stop.t for Stop() and Start() lifecycle (PR #56) 2.1.8 2026-03-20 Todd Rinaldo [Fixes] - #12 Fix broken reconnection (called nonexistent InitJabber), fix IsConnected always returning truthy, handle undef jabber_client after disconnect, and plug memory leak in messages_sent_today (PR #18) - #14 Fix GTalk connection failures and from_full operator precedence bug that returned only the username instead of user@server/alias (PR #16) - #9 Update POD docs to match newline-preserving regex behavior and add multiline message tests (PR #19) - #28 Remove legacy developer names from bot_example.pl AUTHOR section (PR #29) - #30 Migrate from Moose to Moo, replace MooseX::Types with Type::Tiny/Types::Standard, remove Config::Std dependency (PR #47) - #37 Remove disproportionate sleep 30 from BUILD legacy parameter handling (PR #43) - #38 Fix MockJabberClient::Disconnect setting is_connected=1 instead of 0 (PR #44) - #39 Add missing RosterDB/RosterRequest/RosterDBJIDs stubs to mock client (PR #45) - #40 Fix uninitialized value warnings from .= on fresh variables in tests (PR #42) - #48 Suppress 'Subroutine redefined' warnings in MockJabberClient (PR #50) - #32 Eliminate unnecessary sleeps from mock test suite, cutting runtime from ~120s to ~60s (PR #51) - Fix four bugs in Bot.pm: broken string interpolation of method call, typo in error message, variable shadowing in SetForumSubject, and crash on reconnect from undefined background_function (PR #20) - Mask plaintext password in DEBUG log output and fix operator precedence bug (PR #26) - ReconnectToServer now survives _init_jabber failures instead of dying on transient server outages; partial jabber_client state is cleaned up and the retry loop continues with exponential backoff. Also fixes $] version formatting in IQ responses. (PR #53) - Don't count unsent messages toward the hourly rate limit; _send_individual_message was incrementing the counter before checking IsConnected, which could exhaust the limit during disconnection and block legitimate messages after reconnection. (PR #54) [Improvements] - #13 Add optional $from parameter to SendPersonalMessage, SendGroupMessage, and SendJabberMessage for relay bots (PR #17) - #36 Make auto-subscribe to presence requests configurable via new auto_subscribe attribute (PR #46) [Maintenance] - #23 Consolidate three CI workflows into single testsuite.yml, update actions/checkout v1→v4, use perldocker/perl-tester images, add dynamic perl-actions/perl-versions matrix (PR #24) - #33 Extend CI test matrix back to Perl 5.10 (PR #52) - Modernize Makefile.PL: separate TEST_REQUIRES, add CONFIGURE_REQUIRES, MIN_PERL_VERSION, upgrade META_MERGE to meta-spec v2 (PR #21) - Modernize POD: fix typos, update URLs to cpan-authors org, fix command injection in bot_example.pl, fix string comparison bug in gtalk_RSSbot.pl (PR #22) - Add cpanfile for modern CI dependency management (PR #24) - Add CLAUDE.md with project conventions (PR #25) - Move README.md to .github - New test: t/07-test_reconnect_failure.t for reconnection resilience - New test: t/07-test_disconnect_message_count.t for rate-limit accounting during disconnection 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-3.01/MANIFEST000644 000765 000024 00000001622 15164032431 016745 0ustar00todd.rinaldostaff000000 000000 Changes 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/07-forum_join_grace.t t/07-gtalk_connect.t t/07-multiline_messages.t t/07-test_disconnect_message_count.t t/07-test_disconnect_public_api.t t/07-test_from_parameter.t t/07-test_message_callback.t t/07-test_message_chunking.t t/07-test_message_sending.t t/07-test_muc_self_detection.t t/07-test_presence_and_iq.t t/07-test_public_api_connected.t t/07-test_reconnect_and_leaks.t t/07-test_reconnect_failure.t t/07-test_set_forum_subject.t t/08-test_start_stop.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-3.01/t/000755 000765 000024 00000000000 15164032430 016055 5ustar00todd.rinaldostaff000000 000000 Net-Jabber-Bot-3.01/README.md000644 000765 000024 00000034740 15164032142 017101 0ustar00todd.rinaldostaff000000 000000 [![testsuite](https://github.com/cpan-authors/perl-net-jabber-bot/actions/workflows/testsuite.yml/badge.svg)](https://github.com/cpan-authors/perl-net-jabber-bot/actions/workflows/testsuite.yml) # NAME Net::Jabber::Bot - Automated Bot creation with safeties # VERSION Version 3.01 # SYNOPSIS Program design: This is a Moo 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 certain) - **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** Conference server (usually conference.$server\_name) - **port** Defaults to 5222 - **gtalk** Boolean value. defaults to 0. Set to 1 for Google Talk connections. This automatically enables TLS and sets server\_host to 'gmail.com' (unless you provide your own server\_host). - **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 chat rooms. The XMPP resource (used for login and presence) defaults to `alias_hostname_pid` to ensure uniqueness across multiple bot instances. - **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 received 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. - **auto\_subscribe** Boolean value controlling whether the bot automatically accepts presence subscription requests from any JID. Defaults to 1 (enabled) for backward compatibility. Set to 0 to ignore subscription requests, which prevents unknown users from tracking the bot's online status. - **out\_messages\_per\_second** Limits the number of messages per second. Number must be > 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. Runs a 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. The loop runs until Stop() is called. Returns the loop iteration count. - **Stop** $bot->Stop(); Signals the Start() loop to exit after the current iteration completes. Typically called from within the background\_function or message\_function callback. - **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_responses($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(); Returns the 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); $bot->SendGroupMessage($name, $message, $from); Tells the bot to send a message to the recipient room name. $from is an optional JID to set as the sender of the message. Note that most XMPP servers will not allow spoofing the from field and may reject the message or disconnect the client. - **SendPersonalMessage** $bot->SendPersonalMessage($recipient, $message); $bot->SendPersonalMessage($recipient, $message, $from); How to send an individual message to someone. $recipient must read as user@server/Resource or it will not send. $from is an optional JID to set as the sender of the message. Note that most XMPP servers will not allow spoofing the from field and may reject the message or disconnect the client. - **SendJabberMessage** $bot->SendJabberMessage($recipient, $message, $message_type, $subject, $from); 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 replaced with a dot before sending to the server. Newlines (LF and CR) are preserved so that multiline messages work correctly. s/[^\r\n[: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 particular forum right? In which case we need another sub for this. - **GetStatus** my $status = $bot->GetStatus($jid); Returns the presence status of the given JID. Possible return values are "unavailable" (if not connected or JID not found in the presence database), "available" (if present with no specific show value), or the XMPP show value (e.g. "away", "xa", "dnd", "chat"). - **AddUser** $bot->AddUser($jid); Sends a subscription request to the given JID and auto-approves their reciprocal subscription. This adds the user to the bot's roster and allows mutual presence visibility. - **RmUser** $bot->RmUser($jid); Sends an unsubscribe request for the given JID and revokes their subscription to the bot's presence. This effectively removes the user from the bot's roster. # AUTHOR Todd Rinaldo `` # BUGS Please report any bugs or feature requests through the GitHub issue tracker at [https://github.com/cpan-authors/perl-net-jabber-bot/issues](https://github.com/cpan-authors/perl-net-jabber-bot/issues). # SUPPORT You can find documentation for this module with the perldoc command. perldoc Net::Jabber::Bot You can also look for information at: - Metacpan [https://metacpan.org/pod/Net::Jabber::Bot](https://metacpan.org/pod/Net::Jabber::Bot) - GitHub [https://github.com/cpan-authors/perl-net-jabber-bot](https://github.com/cpan-authors/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-3.01/MANIFEST.SKIP000644 000765 000024 00000000151 15157323573 017522 0ustar00todd.rinaldostaff000000 000000 ^.github/ ^.git/.* ^MYMETA.* ^MANIFEST.bak ^.gitignore ^Makefile$ ^\.perltidyrc$ ^CLAUDE\.md$ ^cpanfile$ Net-Jabber-Bot-3.01/examples/000755 000765 000024 00000000000 15164032430 017430 5ustar00todd.rinaldostaff000000 000000 Net-Jabber-Bot-3.01/META.yml000644 000765 000024 00000002032 15164032431 017061 0ustar00todd.rinaldostaff000000 000000 --- 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: '6.64' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 7.76, 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: Log::Log4perl: '0' Moo: '2' Mozilla::CA: '0' Net::Jabber: '2' Sys::Hostname: '0' Time::HiRes: '0' Type::Tiny: '1' Types::Standard: '0' perl: '5.010' version: '0' resources: bugtracker: https://github.com/cpan-authors/perl-net-jabber-bot/issues homepage: https://metacpan.org/pod/Net::Jabber::Bot license: https://dev.perl.org/licenses/ repository: https://github.com/cpan-authors/perl-net-jabber-bot.git version: '3.01' x_serialization_backend: 'CPAN::Meta::YAML version 0.020' Net-Jabber-Bot-3.01/lib/000755 000765 000024 00000000000 15164032430 016360 5ustar00todd.rinaldostaff000000 000000 Net-Jabber-Bot-3.01/Makefile.PL000644 000765 000024 00000004324 15161556566 017611 0ustar00todd.rinaldostaff000000 000000 use strict; use warnings; use ExtUtils::MakeMaker; my %WriteMakefileArgs = ( NAME => 'Net::Jabber::Bot', AUTHOR => 'Todd E Rinaldo ', VERSION_FROM => 'lib/Net/Jabber/Bot.pm', ABSTRACT_FROM => 'lib/Net/Jabber/Bot.pm', LICENSE => 'perl', MIN_PERL_VERSION => '5.010', PL_FILES => {}, CONFIGURE_REQUIRES => { 'ExtUtils::MakeMaker' => '6.64', }, PREREQ_PM => { 'Log::Log4perl' => 0, 'Moo' => 2, 'Type::Tiny' => 1, 'Types::Standard' => 0, 'Mozilla::CA' => 0, 'Net::Jabber' => 2.0, 'Sys::Hostname' => 0, 'Time::HiRes' => 0, 'version' => 0, }, TEST_REQUIRES => { 'FindBin' => 0, 'Test::More' => 0, 'Test::NoWarnings' => 0, 'lib' => 0, }, dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, clean => { FILES => 'Net-Jabber-Bot-*' }, META_MERGE => { 'meta-spec' => { version => 2 }, resources => { license => ['https://dev.perl.org/licenses/'], homepage => 'https://metacpan.org/pod/Net::Jabber::Bot', bugtracker => { web => 'https://github.com/cpan-authors/perl-net-jabber-bot/issues', }, repository => { type => 'git', url => 'https://github.com/cpan-authors/perl-net-jabber-bot.git', web => 'https://github.com/cpan-authors/perl-net-jabber-bot', }, }, }, ); # Fallback for older EUMM without TEST_REQUIRES unless (eval { ExtUtils::MakeMaker->VERSION('6.64') }) { $WriteMakefileArgs{PREREQ_PM} = { %{ $WriteMakefileArgs{PREREQ_PM} }, %{ delete $WriteMakefileArgs{TEST_REQUIRES} }, }; } # Fallback for older EUMM without CONFIGURE_REQUIRES unless (eval { ExtUtils::MakeMaker->VERSION('6.52') }) { delete $WriteMakefileArgs{CONFIGURE_REQUIRES}; } # Fallback for older EUMM without MIN_PERL_VERSION unless (eval { ExtUtils::MakeMaker->VERSION('6.48') }) { delete $WriteMakefileArgs{MIN_PERL_VERSION}; } WriteMakefile(%WriteMakefileArgs); Net-Jabber-Bot-3.01/META.json000644 000765 000024 00000003541 15164032431 017237 0ustar00todd.rinaldostaff000000 000000 { "abstract" : "Automated Bot creation with safeties", "author" : [ "Todd E Rinaldo " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 7.76, 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" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "6.64" } }, "runtime" : { "requires" : { "Log::Log4perl" : "0", "Moo" : "2", "Mozilla::CA" : "0", "Net::Jabber" : "2", "Sys::Hostname" : "0", "Time::HiRes" : "0", "Type::Tiny" : "1", "Types::Standard" : "0", "perl" : "5.010", "version" : "0" } }, "test" : { "requires" : { "FindBin" : "0", "Test::More" : "0", "Test::NoWarnings" : "0", "lib" : "0" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/cpan-authors/perl-net-jabber-bot/issues" }, "homepage" : "https://metacpan.org/pod/Net::Jabber::Bot", "license" : [ "https://dev.perl.org/licenses/" ], "repository" : { "type" : "git", "url" : "https://github.com/cpan-authors/perl-net-jabber-bot.git", "web" : "https://github.com/cpan-authors/perl-net-jabber-bot" } }, "version" : "3.01", "x_serialization_backend" : "JSON::PP version 4.16" } Net-Jabber-Bot-3.01/lib/Net/000755 000765 000024 00000000000 15164032430 017106 5ustar00todd.rinaldostaff000000 000000 Net-Jabber-Bot-3.01/lib/Net/Jabber/000755 000765 000024 00000000000 15164032430 020273 5ustar00todd.rinaldostaff000000 000000 Net-Jabber-Bot-3.01/lib/Net/Jabber/Bot.pm000644 000765 000024 00000126155 15164032137 021373 0ustar00todd.rinaldostaff000000 000000 package Net::Jabber::Bot; use 5.010; use Moo; use Types::Standard qw(Int HashRef Str Maybe ArrayRef Bool CodeRef InstanceOf Num); use Type::Tiny; use version; use Net::Jabber; use Time::HiRes; use Sys::Hostname; use Log::Log4perl qw(:easy); use Mozilla::CA; my $PosInt = Type::Tiny->new( name => 'PosInt', parent => Int, constraint => sub { $_ > 0 } ); my $PosNum = Type::Tiny->new( name => 'PosNum', parent => Num, constraint => sub { $_ > 0 } ); my $NonNegNum = Type::Tiny->new( name => 'NonNegNum', parent => Num, constraint => sub { $_ >= 0 } ); my $HundredInt = Type::Tiny->new( name => 'HundredInt', parent => Num, constraint => sub { $_ > 100 } ); my $CoercedBool = Bool->plus_coercions( Str, sub { ( $_ =~ m/(^on$)|(^true$)/i ) + 0 } ); has jabber_client => ( isa => Maybe [ InstanceOf ['Net::Jabber::Client'] ], is => 'rw', default => sub { Net::Jabber::Client->new } ); 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 => $NonNegNum, 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 'gtalk' => ( isa => Bool, is => 'rw', default => '0' ); 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->resource || ''); } ); has 'safety_mode' => ( isa => $CoercedBool, is => 'rw', default => 1, coerce => 1 ); has 'ignore_server_messages' => ( isa => $CoercedBool, is => 'rw', default => 1, coerce => 1 ); has 'ignore_self_messages' => ( isa => $CoercedBool, is => 'rw', default => 1, coerce => 1 ); has 'auto_subscribe' => ( isa => $CoercedBool, 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 } } } ); has '_running' => ( isa => Bool, is => 'rw', default => 0 ); =for markdown [![testsuite](https://github.com/cpan-authors/perl-net-jabber-bot/actions/workflows/testsuite.yml/badge.svg)](https://github.com/cpan-authors/perl-net-jabber-bot/actions/workflows/testsuite.yml) =head1 NAME Net::Jabber::Bot - Automated Bot creation with safeties =head1 VERSION Version 3.01 =cut our $VERSION = '3.01'; =head1 SYNOPSIS Program design: This is a Moo 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 certain) =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 Conference server (usually conference.$server_name) =item B Defaults to 5222 =item B Boolean value. defaults to 0. Set to 1 for Google Talk connections. This automatically enables TLS and sets server_host to 'gmail.com' (unless you provide your own server_host). =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 chat rooms. The XMPP resource (used for login and presence) defaults to C to ensure uniqueness across multiple bot instances. =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 received 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 Boolean value controlling whether the bot automatically accepts presence subscription requests from any JID. Defaults to 1 (enabled) for backward compatibility. Set to 0 to ignore subscription requests, which prevents unknown users from tracking the bot's online status. =item B Limits the number of messages per second. Number must be E 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 removed — the warning above is sufficient to alert developers } # GTalk convenience: auto-configure TLS and server_host for Google Talk if ( $self->gtalk ) { $self->tls(1); $self->server_host('gmail.com') if ( !$params->{server_host} ); } # 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; # Create a new client if we don't have one (e.g., after disconnect/reconnect) $self->jabber_client( Net::Jabber::Client->new ) if !defined $self->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: ********" ); # 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; if ( !$self->IsConnected ) { WARN("Cannot join forum '$forum_name': not connected"); return; } 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; return if !$self->IsConnected; #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. Runs a 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 The loop runs until Stop() is called. Returns the loop iteration count. =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. $self->_running(1); while ( $self->_running ) { # Process and re-connect if you have to. my $process_result; eval { $process_result = $self->Process($process_timeout) }; if ($@ || !defined $process_result) { #Assume the connection is down... my $error = $@ || "Process returned undef (connection lost)"; ERROR("Server error: $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; } return $counter; } =item B $bot->Stop(); Signals the Start() loop to exit after the current iteration completes. Typically called from within the background_function or message_function callback. =cut sub Stop { my $self = shift; INFO("Stop requested, will exit Start() loop after current iteration"); $self->_running(0); return 1; } =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 ); eval { $self->_init_jabber() }; if ($@) { WARN("Reconnection attempt failed: $@"); # Clean up partial client state so IsConnected() stays false # and next _init_jabber() creates a fresh client $self->jabber_client(undef); next; } if ( defined $background_subroutine ) { 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; return defined $self->jabber_client; } # 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 ) { # In MUC (groupchat), the from JID resource is the room nickname (alias), # not the XMPP resource. Check both to handle direct and group messages. if ( defined $resource && ( $resource eq $self->resource || ( $type eq 'groupchat' && $resource eq $self->alias ) ) ) { 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. } if ( !defined $request ) { DEBUG("Message not relevant to bot"); return; } $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_responses($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"); # 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/g; $self->jabber_client->VersionSend( to => $from, name => __PACKAGE__, ver => $VERSION, os => "Perl v$perl_version" ); } } =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' ) { my $from = $presence->GetFrom(); if ( $self->auto_subscribe ) { $self->jabber_client->Subscription( type => "subscribe", to => $from ); $self->jabber_client->Subscription( type => "subscribed", to => $from ); INFO("Processed subscription request from $from"); } else { INFO("Ignored subscription request from $from (auto_subscribe disabled)"); } return; } elsif ( $type eq 'unsubscribe' ) { 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(); Returns the 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); $bot->SendGroupMessage($name, $message, $from); Tells the bot to send a message to the recipient room name. $from is an optional JID to set as the sender of the message. Note that most XMPP servers will not allow spoofing the from field and may reject the message or disconnect the client. =cut sub SendGroupMessage { my $self = shift; my $recipient = shift; my $message = shift; my $from = shift; $recipient .= '@' . $self->conference_server if ( $recipient !~ m{\@} ); return $self->SendJabberMessage( $recipient, $message, 'groupchat', undef, $from ); } =item B $bot->SendPersonalMessage($recipient, $message); $bot->SendPersonalMessage($recipient, $message, $from); How to send an individual message to someone. $recipient must read as user@server/Resource or it will not send. $from is an optional JID to set as the sender of the message. Note that most XMPP servers will not allow spoofing the from field and may reject the message or disconnect the client. =cut sub SendPersonalMessage { my $self = shift; my $recipient = shift; my $message = shift; my $from = shift; return $self->SendJabberMessage( $recipient, $message, 'chat', undef, $from ); } =item B $bot->SendJabberMessage($recipient, $message, $message_type, $subject, $from); 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 replaced with a dot before sending to the server. Newlines (LF and CR) are preserved so that multiline messages work correctly. s/[^\r\n[:print:]]+/./xmsg =cut sub SendJabberMessage { my $self = shift; my $recipient = shift; my $message = shift; my $message_type = shift; my $subject = shift; my $from = shift; my $max_size = $self->max_message_size; # Split the message into chunks of at most max_message_size characters. # Prefer breaking at newlines, then spaces, then hard chop. my @message_chunks; my $remaining = $message; while ( length $remaining ) { if ( length $remaining <= $max_size ) { push @message_chunks, $remaining; last; } my $chunk = substr( $remaining, 0, $max_size ); my $break_pos; # Prefer breaking at the last newline within the chunk my $nl_pos = rindex( $chunk, "\n" ); if ( $nl_pos >= 0 ) { $break_pos = $nl_pos + 1; # include the newline } else { # Fall back to the last space my $sp_pos = rindex( $chunk, " " ); if ( $sp_pos >= 0 ) { $break_pos = $sp_pos + 1; # include the space } else { $break_pos = $max_size; # hard chop } } push @message_chunks, substr( $remaining, 0, $break_pos ); $remaining = substr( $remaining, $break_pos ); } 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, $from ); if ( defined $msg_return ) { $return_value = ( defined $return_value ? $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; my $from = 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"; } # Check connection first — don't count messages that can't actually be sent. # Otherwise, messages attempted during disconnection inflate the hourly # counter and can exhaust the limit before real messages are sent. if ( !$self->IsConnected ) { $subject = "" if ( !defined $subject ); # Keep warning messages quiet. $message_chunk = "" if ( !defined $message_chunk ); # Keep warning messages quiet. ERROR( "Can't send: Jabber server is down. Tried to send: \n" . "To: $recipient\n" . "Subject: $subject\n" . "Type: $message_type\n" . "Message sent:\n" . "$message_chunk" ); return "Server is down.\n"; } my $yday = (localtime)[7]; my $hour = (localtime)[2]; # Clean up entries from previous days to prevent unbounded memory growth for my $old_day ( keys %{ $self->messages_sent_today } ) { delete $self->messages_sent_today->{$old_day} if $old_day != $yday; } 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. my $max_per_hour = $self->max_messages_per_hour; ERROR( "Can't send message because we've already tried to send $messages_this_hour of $max_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"; } # 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"); my %message_args = ( to => $recipient, body => $message_chunk, type => $message_type, subject => $subject, ); $message_args{from} = $from if defined $from; $self->jabber_client->MessageSend(%message_args); 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!"); $subject = substr( $subject, 0, $self->max_message_size ); DEBUG("Truncated subject: $subject"); return "Subject is too long!"; } return $self->_send_individual_message( $recipient, "Setting subject to $subject", 'groupchat', $subject ); } =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) if ( !$self->IsConnected ) { WARN("Cannot change status: not connected"); return 0; } $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 particular forum right? In which case we need another sub for this. =cut sub GetRoster { my $self = shift; if ( !$self->IsConnected ) { WARN("Cannot get roster: not connected"); return (); } my @rosterlist; foreach my $jid ( $self->jabber_client->RosterDBJIDs() ) { my $username = $jid->GetJID(); push( @rosterlist, $username ); } return @rosterlist; } =item B my $status = $bot->GetStatus($jid); Returns the presence status of the given JID. Possible return values are "unavailable" (if not connected or JID not found in the presence database), "available" (if present with no specific show value), or the XMPP show value (e.g. "away", "xa", "dnd", "chat"). =cut sub GetStatus { my $self = shift; my ($jid) = shift; return "unavailable" if !$self->IsConnected; my $Pres = $self->jabber_client->PresenceDBQuery($jid); if ( !( defined($Pres) ) ) { return "unavailable"; } my $show = $Pres->GetShow(); if ($show) { return $show; } return "available"; } =item B $bot->AddUser($jid); Sends a subscription request to the given JID and auto-approves their reciprocal subscription. This adds the user to the bot's roster and allows mutual presence visibility. =cut sub AddUser { my $self = shift; my $user = shift; if ( !$self->IsConnected ) { WARN("Cannot add user '$user': not connected"); return; } $self->jabber_client->Subscription( type => "subscribe", to => $user ); $self->jabber_client->Subscription( type => "subscribed", to => $user ); } =item B $bot->RmUser($jid); Sends an unsubscribe request for the given JID and revokes their subscription to the bot's presence. This effectively removes the user from the bot's roster. =cut sub RmUser { my $self = shift; my $user = shift; if ( !$self->IsConnected ) { WARN("Cannot remove user '$user': not connected"); return; } $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 through the GitHub issue tracker at L. =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 * 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 1; # End of Net::Jabber::Bot Net-Jabber-Bot-3.01/examples/gtalk_RSSbot.pl000755 000765 000024 00000004662 15156543215 022347 0ustar00todd.rinaldostaff000000 000000 use strict; use warnings; use utf8; use Net::Jabber::Bot; use XML::Smart; # 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 ne "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-3.01/examples/bot_example.pl000755 000765 000024 00000025277 15156563277 022327 0ustar00todd.rinaldostaff000000 000000 #!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; if (open(my $fh, '-|', '/usr/sbin/nslookup', $host)) { local $/; $output = <$fh>; close $fh; } else { $output = "nslookup failed: $!"; } $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 =head1 BUGS Report bugs at L Net-Jabber-Bot-3.01/t/05-helper_functions.t000644 000765 000024 00000021225 15156711514 022045 0ustar00todd.rinaldostaff000000 000000 #!perl use strict; use warnings; use Test::More tests => 125; 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 , forum_join_grace => 0 ); 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"); 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-3.01/t/07-test_muc_self_detection.t000644 000765 000024 00000007353 15164031674 023401 0ustar00todd.rinaldostaff000000 000000 #!perl # Test that self-message detection works correctly in MUC (groupchat). # # In XMPP MUC, the from JID resource is the room nickname (alias), # not the XMPP resource. This test verifies that the bot correctly # ignores its own echoed messages in group chat even though the # resource in the from JID differs from the XMPP login resource. use strict; use warnings; use Test::More tests => 7; use Net::Jabber::Bot; use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; BEGIN { *CORE::GLOBAL::sleep = sub { }; } my $bot_alias = 'testbot'; my $server = 'jabber.example.com'; my $conf = "conference.$server"; my $forum = 'dev_room'; my $messages_seen = 0; my $bot = Net::Jabber::Bot->new({ server => $server, conference_server => $conf, port => 5222, username => 'botuser', password => 'secret', alias => $bot_alias, message_function => sub { $messages_seen++ }, background_function => sub { }, forums_and_responses => { $forum => [''] }, # respond to all messages ignore_self_messages => 1, safety_mode => 0, forum_join_grace => 0, }); isa_ok($bot, 'Net::Jabber::Bot'); # Verify alias and resource are different (this is the crux of the bug) isnt($bot->alias, $bot->resource, "alias ('${\$bot->alias}') differs from resource ('${\$bot->resource}')"); # Send a group message — the mock will echo it back with the MUC nickname # (alias) as the from JID resource, matching real XMPP MUC behavior. $messages_seen = 0; $bot->SendGroupMessage($forum, "Hello room!"); $bot->Process(1); is($messages_seen, 0, "Bot ignores its own echoed group message (MUC alias-based detection)"); # Now disable self-message ignoring and verify messages come through $bot->respond_to_self_messages(1); $messages_seen = 0; $bot->SendGroupMessage($forum, "Hello again!"); $bot->Process(1); is($messages_seen, 1, "Bot sees its own group message when respond_to_self_messages is on"); # Re-enable self-message ignoring $bot->respond_to_self_messages(0); # Test direct (chat) messages still use resource-based detection. # Send a personal message — the mock echoes with the XMPP resource. $messages_seen = 0; $bot->SendPersonalMessage("peer\@$server/$bot_alias", "Hello peer!"); $bot->Process(1); is($messages_seen, 0, "Bot ignores its own echoed personal message (resource-based detection)"); # Verify a message from someone ELSE with a different resource is NOT ignored my $other_msg = Net::Jabber::Message->new(); $other_msg->SetFrom("$forum\@$conf/other_user"); $other_msg->SetTo("botuser\@$server/${\$bot->resource}"); $other_msg->SetType('groupchat'); $other_msg->SetBody("Hey bot!"); # Inject the message directly into the mock client's queue push @{$bot->jabber_client->{message_queue}}, $other_msg; $messages_seen = 0; $bot->Process(1); is($messages_seen, 1, "Bot processes messages from other users in group chat"); # Verify a message from someone with an alias that happens to match # the bot's alias from a DIFFERENT room context doesn't get suppressed # (edge case: only suppress in rooms the bot actually joined) my $impersonator_msg = Net::Jabber::Message->new(); $impersonator_msg->SetFrom("other_room\@$conf/$bot_alias"); $impersonator_msg->SetTo("botuser\@$server/${\$bot->resource}"); $impersonator_msg->SetType('groupchat'); $impersonator_msg->SetBody("I'm pretending to be the bot!"); push @{$bot->jabber_client->{message_queue}}, $impersonator_msg; $messages_seen = 0; $bot->Process(1); # This WILL be suppressed because the alias matches — that's acceptable # security behavior (better to over-suppress than risk loops) is($messages_seen, 0, "Bot suppresses groupchat from matching alias (safe default)"); Net-Jabber-Bot-3.01/t/07-multiline_messages.t000644 000765 000024 00000005304 15156711514 022371 0ustar00todd.rinaldostaff000000 000000 #!perl use strict; use warnings; use Test::More tests => 8; use Net::Jabber::Bot; use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; my $bot_alias = 'make_test_bot'; my $server = 'talk.google.com'; my $personal_address = "test_user\@$server/$bot_alias"; my %forums_and_responses; my $forum1 = 'test_forum1'; $forums_and_responses{$forum1} = ["jbot:", ""]; 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 => sub {}, background_function => sub {}, loop_sleep_time => 5, process_timeout => 5, forums_and_responses => \%forums_and_responses, ignore_server_messages => 1, ignore_self_messages => 1, out_messages_per_second => 5, max_message_size => 800, max_messages_per_hour => 100, forum_join_grace => 0, }); isa_ok($bot, "Net::Jabber::Bot"); # Test 1: Newlines are preserved in sent messages { my $multiline_msg = "Line one\nLine two\nLine three"; my $result = $bot->SendPersonalMessage($personal_address, $multiline_msg); ok(!defined $result, "Sent multiline message successfully"); # Check that the mock client received the message with newlines intact my $client = $bot->jabber_client; my @queue = @{$client->{message_queue}}; is(scalar @queue, 1, "One message in queue"); my $body = $queue[0]->GetBody(); like($body, qr/Line one\nLine two\nLine three/, "Newlines preserved in message body"); # Clear the queue @{$client->{message_queue}} = (); } # Test 2: Carriage return + newline preserved { my $crlf_msg = "Line one\r\nLine two"; my $result = $bot->SendPersonalMessage($personal_address, $crlf_msg); ok(!defined $result, "Sent CRLF message successfully"); my $client = $bot->jabber_client; my @queue = @{$client->{message_queue}}; my $body = $queue[0]->GetBody(); like($body, qr/Line one\r\nLine two/, "CRLF preserved in message body"); @{$client->{message_queue}} = (); } # Test 3: Non-printable characters (other than newlines) are still stripped { my $msg_with_control = "Hello\x00\x01\x02World"; my $result = $bot->SendPersonalMessage($personal_address, $msg_with_control); ok(!defined $result, "Sent message with control chars successfully"); my $client = $bot->jabber_client; my @queue = @{$client->{message_queue}}; my $body = $queue[0]->GetBody(); is($body, "Hello.World", "Control characters replaced with dot, printable text preserved"); @{$client->{message_queue}} = (); } Net-Jabber-Bot-3.01/t/07-test_message_callback.t000644 000765 000024 00000027515 15161476324 023012 0ustar00todd.rinaldostaff000000 000000 #!perl use strict; use warnings; use Test::More; use Net::Jabber::Bot; use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; # No-op sleep to avoid real delays BEGIN { *CORE::GLOBAL::sleep = sub { }; } my $server = 'jabber.example.com'; my $conference_server = "conference.$server"; my $bot_alias = 'testbot'; my $bot_username = 'botuser'; # Capture callback parameters my @callback_calls; sub message_handler { push @callback_calls, {@_}; } sub background_noop { } my %forums_and_responses = ( room1 => [ "bot:", "hey bot " ], room2 => [ "admin:" ], room3 => [ "" ], # empty string = respond to all messages ); my $bot = Net::Jabber::Bot->new( server => $server, conference_server => $conference_server, port => 5222, username => $bot_username, password => 'secret', alias => $bot_alias, message_function => \&message_handler, background_function => \&background_noop, loop_sleep_time => 5, process_timeout => 5, forums_and_responses => \%forums_and_responses, ignore_server_messages => 1, ignore_self_messages => 0, # allow self messages so echoed messages get through safety_mode => 0, # disable safety to avoid overriding ignore_self_messages max_messages_per_hour => 10000, forum_join_grace => 0, ); isa_ok( $bot, 'Net::Jabber::Bot' ); # Helper: inject a message directly into the mock client's queue sub inject_message { my (%args) = @_; my $msg = Net::Jabber::Message->new(); $msg->SetFrom( $args{from} ); $msg->SetTo( $args{to} // "$bot_username\@$server/$bot_alias" ); $msg->SetType( $args{type} // 'chat' ); $msg->SetBody( $args{body} // '' ); $msg->SetSubject( $args{subject} ) if defined $args{subject}; push @{ $bot->jabber_client->{message_queue} }, $msg; } # Helper: process and return captured callback calls, then reset sub process_and_collect { @callback_calls = (); $bot->Process(1); return @callback_calls; } # ========================================================================= # Test 1: Personal (chat) message delivers correct parameters # ========================================================================= { inject_message( from => "alice\@$server/desktop", type => 'chat', body => 'Hello bot!', ); my @calls = process_and_collect(); is( scalar @calls, 1, 'chat message: callback called once' ); my $c = $calls[0]; isa_ok( $c->{bot_object}, 'Net::Jabber::Bot', 'chat message: bot_object' ); is( $c->{from_full}, "alice\@$server/desktop", 'chat message: from_full' ); is( $c->{body}, 'Hello bot!', 'chat message: body' ); is( $c->{type}, 'chat', 'chat message: type' ); is( $c->{reply_to}, "alice\@$server/desktop", 'chat message: reply_to equals from_full (no resource stripping for chat)' ); ok( !defined $c->{bot_address_from}, 'chat message: bot_address_from is undef for non-groupchat' ); isa_ok( $c->{message}, 'Net::Jabber::Message', 'chat message: raw message object' ); } # ========================================================================= # Test 2: Groupchat message with alias prefix strips it from body # ========================================================================= { inject_message( from => "room1\@$conference_server/alice", type => 'groupchat', body => 'bot: what time is it?', ); my @calls = process_and_collect(); is( scalar @calls, 1, 'groupchat alias: callback called once' ); my $c = $calls[0]; is( $c->{type}, 'groupchat', 'groupchat alias: type is groupchat' ); is( $c->{body}, 'what time is it?', 'groupchat alias: body has alias prefix stripped' ); is( $c->{bot_address_from}, 'bot:', 'groupchat alias: bot_address_from is the matched alias' ); is( $c->{reply_to}, "room1\@$conference_server", 'groupchat alias: reply_to has resource stripped' ); is( $c->{from_full}, "room1\@$conference_server/alice", 'groupchat alias: from_full preserved with resource' ); } # ========================================================================= # Test 3: Second alias also works # ========================================================================= { inject_message( from => "room1\@$conference_server/bob", type => 'groupchat', body => 'hey bot do something', ); my @calls = process_and_collect(); is( scalar @calls, 1, 'second alias: callback called once' ); my $c = $calls[0]; is( $c->{body}, 'do something', 'second alias: body has "hey bot " prefix stripped' ); is( $c->{bot_address_from}, 'hey bot ', 'second alias: bot_address_from is the matched alias' ); } # ========================================================================= # Test 4: Empty-string alias in room3 catches all messages # ========================================================================= { inject_message( from => "room3\@$conference_server/charlie", type => 'groupchat', body => 'random chatter', ); my @calls = process_and_collect(); is( scalar @calls, 1, 'empty alias: callback called for any message' ); my $c = $calls[0]; is( $c->{body}, 'random chatter', 'empty alias: body is the full message text' ); is( $c->{bot_address_from}, '', 'empty alias: bot_address_from is empty string' ); } # ========================================================================= # Test 5: Groupchat message NOT matching any alias is ignored # ========================================================================= { inject_message( from => "room2\@$conference_server/dave", type => 'groupchat', body => 'just talking to myself', ); my @calls = process_and_collect(); is( scalar @calls, 0, 'no alias match: callback not called for irrelevant groupchat' ); } # ========================================================================= # Test 6: Groupchat message matching alias in room2 # ========================================================================= { inject_message( from => "room2\@$conference_server/dave", type => 'groupchat', body => 'admin: restart service', ); my @calls = process_and_collect(); is( scalar @calls, 1, 'room2 alias match: callback called' ); my $c = $calls[0]; is( $c->{body}, 'restart service', 'room2: body stripped' ); is( $c->{bot_address_from}, 'admin:', 'room2: correct alias' ); } # ========================================================================= # Test 7: Server messages are ignored when ignore_server_messages is on # ========================================================================= { # Message from bare server (no user@server/resource format) inject_message( from => $server, type => 'chat', body => 'Server announcement', ); my @calls = process_and_collect(); is( scalar @calls, 0, 'server message: ignored when ignore_server_messages=1' ); } # ========================================================================= # Test 8: Message with user@server but no resource is also filtered # ========================================================================= { inject_message( from => "system\@$server", type => 'chat', body => 'System notice', ); my @calls = process_and_collect(); is( scalar @calls, 0, 'no-resource message: ignored when ignore_server_messages=1' ); } # ========================================================================= # Test 9: Self-message detection when ignore_self_messages is on # ========================================================================= { $bot->ignore_self_messages(1); inject_message( from => "room1\@$conference_server/" . $bot->resource, type => 'groupchat', body => 'bot: I said this myself', ); my @calls = process_and_collect(); is( scalar @calls, 0, 'self message: ignored when ignore_self_messages=1' ); $bot->ignore_self_messages(0); # restore } # ========================================================================= # Test 10: Multiple messages in one Process() call # ========================================================================= { inject_message( from => "alice\@$server/laptop", type => 'chat', body => 'First message', ); inject_message( from => "bob\@$server/phone", type => 'chat', body => 'Second message', ); my @calls = process_and_collect(); is( scalar @calls, 2, 'multiple messages: both delivered' ); is( $calls[0]->{body}, 'First message', 'multiple: first body correct' ); is( $calls[1]->{body}, 'Second message', 'multiple: second body correct' ); } # ========================================================================= # Test 11: Groupchat message from unknown forum (no aliases defined) # ========================================================================= { # Send from a forum not in forums_and_responses — get_responses returns empty list inject_message( from => "unknownroom\@$conference_server/eve", type => 'groupchat', body => 'bot: hello?', ); my @calls = process_and_collect(); # No aliases to respond to for this forum → message passed through without alias stripping is( scalar @calls, 1, 'unknown forum: message still delivered (no aliases to check)' ); is( $calls[0]->{body}, 'bot: hello?', 'unknown forum: body is unmodified (no alias stripping)' ); ok( !defined $calls[0]->{bot_address_from}, 'unknown forum: bot_address_from is undef' ); } # ========================================================================= # Test 12: Alias matching is order-sensitive (first match wins) # ========================================================================= { # room1 has ["bot:", ""] — "bot:" should match before the catch-all "" inject_message( from => "room1\@$conference_server/frank", type => 'groupchat', body => 'bot: help me', ); my @calls = process_and_collect(); is( scalar @calls, 1, 'alias order: callback called' ); is( $calls[0]->{bot_address_from}, 'bot:', 'alias order: first alias matched, not catch-all' ); is( $calls[0]->{body}, 'help me', 'alias order: body stripped of first-matched alias' ); } # ========================================================================= # Test 13: Whitespace before alias is tolerated # ========================================================================= { inject_message( from => "room1\@$conference_server/grace", type => 'groupchat', body => ' bot: spaced out', ); my @calls = process_and_collect(); is( scalar @calls, 1, 'leading whitespace: callback called' ); is( $calls[0]->{body}, 'spaced out', 'leading whitespace: alias matched despite leading spaces' ); is( $calls[0]->{bot_address_from}, 'bot:', 'leading whitespace: correct alias matched' ); } # ========================================================================= # Test 14: Message with no handler defined logs warning but doesn't crash # ========================================================================= { my $saved_handler = $bot->message_function; $bot->message_function(undef); inject_message( from => "alice\@$server/desktop", type => 'chat', body => 'No handler here', ); # Should not die my @calls = process_and_collect(); is( scalar @calls, 0, 'no handler: callback not called (no handler)' ); $bot->message_function($saved_handler); # restore } done_testing(); Net-Jabber-Bot-3.01/t/07-test_disconnect_message_count.t000644 000765 000024 00000004145 15157322447 024612 0ustar00todd.rinaldostaff000000 000000 #!perl use strict; use warnings; BEGIN { *CORE::GLOBAL::sleep = sub { }; } use Test::More tests => 10; use Net::Jabber::Bot; use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; my $bot_alias = 'make_test_bot'; my $server = 'talk.google.com'; my $personal_address = "test_user\@$server/$bot_alias"; my %forums_and_responses; $forums_and_responses{'test_forum1'} = [ "jbot:", "" ]; 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 => sub { }, background_function => sub { }, loop_sleep_time => 5, process_timeout => 5, forums_and_responses => \%forums_and_responses, ignore_server_messages => 1, ignore_self_messages => 1, out_messages_per_second => 5, max_message_size => 1000, max_messages_per_hour => 10, forum_join_grace => 0, ); isa_ok( $bot, "Net::Jabber::Bot" ); # Send one message while connected — should succeed and count my $count_before = $bot->get_messages_this_hour(); my $result = $bot->SendPersonalMessage( $personal_address, "message while connected" ); ok( !defined $result, "Message sent successfully while connected" ); is( $bot->get_messages_this_hour(), $count_before + 1, "Counter incremented for sent message" ); # Now disconnect $bot->Disconnect(); ok( !$bot->IsConnected(), "Bot is disconnected" ); # Send several messages while disconnected — they should all fail my $count_after_disconnect = $bot->get_messages_this_hour(); for my $i ( 1 .. 5 ) { my $fail_result = $bot->SendPersonalMessage( $personal_address, "message while disconnected $i" ); ok( defined $fail_result, "Message $i correctly rejected while disconnected" ); } # The hourly counter should NOT have increased during disconnection is( $bot->get_messages_this_hour(), $count_after_disconnect, "Hourly message counter unchanged by messages attempted while disconnected" ); Net-Jabber-Bot-3.01/t/03-test_connectivity.t000644 000765 000024 00000004225 15156711514 022252 0ustar00todd.rinaldostaff000000 000000 #!perl -T BEGIN { use Test::More; plan skip_all => "\$ENV{AUTHOR_TESTING} required for these tests" if(!$ENV{AUTHOR_TESTING}); plan skip_all => "t/test_config.cfg required for connectivity tests" if(! -f 't/test_config.cfg'); } 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 (simple INI parser, replaces Config::Std). my $config_file = 't/test_config.cfg'; my %config_file_hash; if (open my $fh, '<', $config_file) { my $section = ''; while (my $line = <$fh>) { chomp $line; $line =~ s/^\s+//; $line =~ s/\s+$//; next if $line eq '' || $line =~ /^[#;]/; if ($line =~ /^\[(.+)\]$/) { $section = $1; next; } if ($line =~ /^([^:=]+?)\s*[:=]\s*(.*)$/) { $config_file_hash{$section}{$1} = $2; } } close $fh; } ok(scalar keys %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-3.01/t/07-forum_join_grace.t000644 000765 000024 00000005621 15156711514 022012 0ustar00todd.rinaldostaff000000 000000 #!perl use strict; use warnings; use Test::More tests => 5; use Net::Jabber::Bot; use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; my $bot_alias = 'grace_test_bot'; my $server = 'talk.google.com'; my %forums_and_responses; my $forum1 = 'test_forum1'; $forums_and_responses{$forum1} = ["jbot:", ""]; my $messages_seen = 0; # Use a short but nonzero grace period so the test runs fast 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 => sub { $messages_seen++ }, background_function => sub {}, loop_sleep_time => 5, process_timeout => 5, forums_and_responses => \%forums_and_responses, ignore_server_messages => 1, ignore_self_messages => 0, out_messages_per_second => 5, max_message_size => 800, max_messages_per_hour => 100, forum_join_grace => 2, }); isa_ok($bot, "Net::Jabber::Bot"); is($bot->forum_join_grace, 2, "Forum join grace set to 2 seconds"); # Enable responding to self messages (safety_mode forces ignore_self_messages on, # and the mock echoes messages back with the same resource as the bot) $bot->respond_to_self_messages(1); # Send a message immediately — should be ignored (within grace period) my $personal_address = "test_user\@$server/$bot_alias"; $bot->SendPersonalMessage($personal_address, "Hello during grace period"); $bot->Process(1); is($messages_seen, 0, "Message during grace period is ignored"); # Wait past the grace period sleep 3; # Send another message — should be processed now $messages_seen = 0; $bot->SendPersonalMessage($personal_address, "Hello after grace period"); $bot->Process(1); is($messages_seen, 1, "Message after grace period is processed"); # Test that forum_join_grace => 0 means no grace period at all my $bot2 = Net::Jabber::Bot->new({ server => $server, conference_server => "conference.$server", port => 5222, username => 'test_username2', password => 'test_pass', alias => $bot_alias, message_function => sub { $messages_seen++ }, background_function => sub {}, loop_sleep_time => 5, process_timeout => 5, forums_and_responses => \%forums_and_responses, ignore_server_messages => 1, ignore_self_messages => 0, out_messages_per_second => 5, max_message_size => 800, max_messages_per_hour => 100, forum_join_grace => 0, }); $bot2->respond_to_self_messages(1); $messages_seen = 0; $bot2->SendPersonalMessage($personal_address, "Immediate message with zero grace"); $bot2->Process(1); is($messages_seen, 1, "Message processed immediately when forum_join_grace is 0"); Net-Jabber-Bot-3.01/t/07-test_message_sending.t000644 000765 000024 00000025163 15163571666 022711 0ustar00todd.rinaldostaff000000 000000 #!perl use strict; use warnings; BEGIN { *CORE::GLOBAL::sleep = sub { }; } use Test::More; use Net::Jabber::Bot; use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; my $bot_alias = 'test_bot'; my $server = 'jabber.example.com'; my %forums_and_responses; $forums_and_responses{'test_room'} = [ "bot:", "" ]; my $bot = Net::Jabber::Bot->new( server => $server, conference_server => "conference.$server", port => 5222, username => 'testuser', password => 'testpass', alias => $bot_alias, message_function => sub { }, background_function => sub { }, loop_sleep_time => 5, process_timeout => 5, forums_and_responses => \%forums_and_responses, out_messages_per_second => 5, max_message_size => 150, max_messages_per_hour => 100, forum_join_grace => 0, ); isa_ok( $bot, "Net::Jabber::Bot" ); # Helper: drain the sent_messages_log and return the entries sub drain_sent { my $client = shift || $bot->jabber_client; my @msgs = @{ $client->{sent_messages_log} }; @{ $client->{sent_messages_log} } = (); @{ $client->{message_queue} } = (); return @msgs; } # ─── SendGroupMessage ───────────────────────────────────── subtest 'SendGroupMessage appends conference_server when no @ present' => sub { drain_sent(); $bot->SendGroupMessage( "myroom", "hello" ); my @msgs = drain_sent(); ok( scalar @msgs >= 1, "At least one message sent" ); is( $msgs[0]->{to}, "myroom\@conference.$server", "Recipient has conference server appended" ); is( $msgs[0]->{type}, 'groupchat', "Message type is groupchat" ); is( $msgs[0]->{body}, 'hello', "Body preserved" ); }; subtest 'SendGroupMessage preserves full JID when @ present' => sub { drain_sent(); $bot->SendGroupMessage( "myroom\@custom.conference.example.com", "hello" ); my @msgs = drain_sent(); is( $msgs[0]->{to}, 'myroom@custom.conference.example.com', "Full JID preserved, conference_server not appended" ); }; # ─── SendPersonalMessage ────────────────────────────────── subtest 'SendPersonalMessage sends as chat type' => sub { drain_sent(); $bot->SendPersonalMessage( "friend\@$server/resource", "hey" ); my @msgs = drain_sent(); is( scalar @msgs, 1, "One message sent" ); is( $msgs[0]->{type}, 'chat', "Message type is chat" ); is( $msgs[0]->{body}, 'hey', "Body preserved" ); is( $msgs[0]->{to}, "friend\@$server/resource", "Recipient preserved" ); }; # ─── Message chunking ───────────────────────────────────── subtest 'Short message not chunked' => sub { drain_sent(); my $msg = "short message"; $bot->SendPersonalMessage( "friend\@$server/res", $msg ); my @msgs = drain_sent(); is( scalar @msgs, 1, "Single chunk for short message" ); is( $msgs[0]->{body}, $msg, "Body unchanged" ); }; subtest 'Long message chunked to max_message_size' => sub { drain_sent(); my $max = $bot->max_message_size; my $msg = "a" x ( $max * 3 ); $bot->SendPersonalMessage( "friend\@$server/res", $msg ); my @msgs = drain_sent(); ok( scalar @msgs >= 3, "Message split into 3+ chunks (got " . scalar(@msgs) . ")" ); for my $i ( 0 .. $#msgs ) { my $len = length( $msgs[$i]->{body} ); # Each chunk should be at most max_message_size (+1 for possible delimiter) ok( $len <= $max + 1, "Chunk $i is $len bytes (<= " . ( $max + 1 ) . ")" ); ok( $len > 0, "Chunk $i is non-empty" ); } # Reassembled message should equal original my $reassembled = join( '', map { $_->{body} } @msgs ); is( $reassembled, $msg, "Chunks reassemble to original message" ); }; subtest 'Chunking prefers newline boundaries' => sub { drain_sent(); my $max = $bot->max_message_size; # Build a message where a newline sits before max_size # so the chunker prefers to split there my $first_part = "a" x ( $max - 10 ); my $second_part = "b" x ( $max - 10 ); my $msg = $first_part . "\n" . $second_part; $bot->SendPersonalMessage( "friend\@$server/res", $msg ); my @msgs = drain_sent(); is( scalar @msgs, 2, "Split into 2 chunks at newline" ); like( $msgs[0]->{body}, qr/^a+\n$/, "First chunk ends with newline" ); like( $msgs[1]->{body}, qr/^b+$/, "Second chunk is the remainder" ); }; subtest 'Chunking prefers whitespace boundaries' => sub { drain_sent(); my $max = $bot->max_message_size; my $first_part = "a" x ( $max - 10 ); my $second_part = "b" x ( $max - 10 ); my $msg = $first_part . " " . $second_part; $bot->SendPersonalMessage( "friend\@$server/res", $msg ); my @msgs = drain_sent(); is( scalar @msgs, 2, "Split into 2 chunks at whitespace" ); like( $msgs[0]->{body}, qr/^a+ $/, "First chunk ends with space" ); like( $msgs[1]->{body}, qr/^b+$/, "Second chunk is the remainder" ); }; subtest 'Chunking hard-cuts when no natural boundaries' => sub { drain_sent(); my $max = $bot->max_message_size; my $msg = "x" x ( $max * 2 + 1 ); # No newlines or spaces $bot->SendPersonalMessage( "friend\@$server/res", $msg ); my @msgs = drain_sent(); is( scalar @msgs, 3, "Split into 3 chunks" ); is( length( $msgs[0]->{body} ), $max, "First chunk is exactly max_size" ); is( length( $msgs[1]->{body} ), $max, "Second chunk is exactly max_size" ); is( length( $msgs[2]->{body} ), 1, "Third chunk is remainder" ); }; # ─── Non-printable character stripping ──────────────────── subtest 'Non-printable characters stripped from messages' => sub { drain_sent(); my $msg = "hello\x00world\x07!"; $bot->SendPersonalMessage( "friend\@$server/res", $msg ); my @msgs = drain_sent(); unlike( $msgs[0]->{body}, qr/\x00/, "Null byte stripped" ); unlike( $msgs[0]->{body}, qr/\x07/, "Bell character stripped" ); like( $msgs[0]->{body}, qr/hello.*world/, "Printable text preserved" ); }; subtest 'Newlines preserved in messages' => sub { drain_sent(); my $msg = "line1\nline2\r\nline3"; $bot->SendPersonalMessage( "friend\@$server/res", $msg ); my @msgs = drain_sent(); like( $msgs[0]->{body}, qr/\n/, "LF preserved" ); like( $msgs[0]->{body}, qr/\r/, "CR preserved" ); }; # ─── Rate limiting ──────────────────────────────────────── subtest 'Rate limit enforced at max_messages_per_hour' => sub { my $rate_bot = Net::Jabber::Bot->new( server => $server, conference_server => "conference.$server", port => 5222, username => 'testuser', password => 'testpass', alias => 'rate_bot', message_function => sub { }, loop_sleep_time => 5, process_timeout => 5, forums_and_responses => {}, out_messages_per_second => 5, max_message_size => 1000, max_messages_per_hour => 3, forum_join_grace => 0, ); # Send up to the limit for my $i ( 1 .. 3 ) { my $result = $rate_bot->SendPersonalMessage( "user\@$server/res", "msg $i" ); ok( !defined $result, "Message $i sent successfully (within limit)" ); } # The 4th message should be rate-limited my $result = $rate_bot->SendPersonalMessage( "user\@$server/res", "msg 4" ); like( $result, qr/Too many messages/, "4th message rejected by rate limit" ); }; subtest 'Panic message sent when hitting rate limit exactly' => sub { my $panic_bot = Net::Jabber::Bot->new( server => $server, conference_server => "conference.$server", port => 5222, username => 'testuser', password => 'testpass', alias => 'panic_bot', message_function => sub { }, loop_sleep_time => 5, process_timeout => 5, forums_and_responses => {}, out_messages_per_second => 5, max_message_size => 1000, max_messages_per_hour => 2, forum_join_grace => 0, ); my $client = $panic_bot->jabber_client; # Send first message (below limit) $panic_bot->SendPersonalMessage( "user\@$server/res", "msg 1" ); drain_sent($client); # Send the message that hits the limit exactly $panic_bot->SendPersonalMessage( "user\@$server/res", "msg 2" ); my @msgs = drain_sent($client); # Should have the actual message AND the panic notification is( scalar @msgs, 2, "Two messages: the actual send + panic notification" ); like( $msgs[1]->{body}, qr/Cannot send more messages this hour/, "Panic message warns about rate limit" ); }; # ─── SetForumSubject ────────────────────────────────────── subtest 'SetForumSubject sends subject with groupchat message' => sub { drain_sent(); my $result = $bot->SetForumSubject( "myroom\@conference.$server", "New Topic" ); ok( !defined $result, "Returns undef on success" ); my @msgs = drain_sent(); is( scalar @msgs, 1, "One message sent" ); is( $msgs[0]->{type}, 'groupchat', "Type is groupchat" ); is( $msgs[0]->{subject}, 'New Topic', "Subject set correctly" ); like( $msgs[0]->{body}, qr/Setting subject to New Topic/, "Body announces subject change" ); }; # ─── Edge cases ─────────────────────────────────────────── subtest 'SendJabberMessage with undefined message_type returns error' => sub { my $result = $bot->SendJabberMessage( "user\@$server", "test", undef ); like( $result, qr/No message type/, "Returns error for undefined type" ); }; subtest 'SendJabberMessage with undefined recipient returns error' => sub { my $result = $bot->SendJabberMessage( undef, "test", "chat" ); like( $result, qr/No recipient/, "Returns error for undefined recipient" ); }; subtest 'Sending while disconnected returns error' => sub { $bot->Disconnect(); my $result = $bot->SendPersonalMessage( "user\@$server/res", "test" ); like( $result, qr/Server is down/, "Returns server down error" ); }; done_testing(); Net-Jabber-Bot-3.01/t/99-pod-coverage.t000644 000765 000024 00000001050 15156711514 021060 0ustar00todd.rinaldostaff000000 000000 #!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|messages_sent_today)$/] }; pod_coverage_ok('Net::Jabber::Bot', $private_subs, "Test Net::Jabber::Bot for docs. Private functions not listed in docs"); Net-Jabber-Bot-3.01/t/99-pod.t000644 000765 000024 00000000356 15156543215 017300 0ustar00todd.rinaldostaff000000 000000 #!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-3.01/t/07-test_reconnect_failure.t000644 000765 000024 00000006207 15157322447 023235 0ustar00todd.rinaldostaff000000 000000 #!perl use strict; use warnings; # Override sleep to avoid delays in tests BEGIN { *CORE::GLOBAL::sleep = sub { }; } use Test::More tests => 10; use Net::Jabber::Bot; # stuff for mock client object use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; # Test object my $bot_alias = 'reconnect_fail_bot'; my $server = 'talk.google.com'; my %forums_and_responses; $forums_and_responses{'test_forum1'} = [ "jbot:", "" ]; 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 => sub { }, background_function => sub { }, loop_sleep_time => 5, process_timeout => 5, forums_and_responses => \%forums_and_responses, out_messages_per_second => 5, max_message_size => 800, max_messages_per_hour => 100, forum_join_grace => 0, ); isa_ok( $bot, "Net::Jabber::Bot" ); ok( $bot->IsConnected(), "Bot connected after init" ); # Test 1: _init_jabber dies when Connect returns undef $bot->Disconnect(); ok( !$bot->IsConnected(), "Bot disconnected" ); $Net::Jabber::Client::connect_fail_remaining = 1; eval { $bot->_init_jabber() }; like( $@, qr/Jabber server is down/, "_init_jabber dies on connection failure" ); # The partial jabber_client object is left behind — this is the state # that ReconnectToServer must clean up ok( defined $bot->jabber_client, "Partial client exists after failed _init_jabber" ); # Clean up for next test $bot->jabber_client(undef); # Test 2: ReconnectToServer survives transient failures and reconnects $Net::Jabber::Client::connect_fail_remaining = 2; $bot->ReconnectToServer(); ok( $bot->IsConnected(), "ReconnectToServer reconnects after 2 transient failures" ); is( $Net::Jabber::Client::connect_fail_remaining, 0, "All failures consumed" ); # Test 3: Background function runs after successful reconnect my $bg_called = 0; my $bot2 = Net::Jabber::Bot->new( server => $server, conference_server => "conference.$server", port => 5222, username => 'test_username2', password => 'test_pass', alias => 'bg_test_bot', message_function => sub { }, background_function => sub { $bg_called++ }, loop_sleep_time => 5, process_timeout => 5, forums_and_responses => \%forums_and_responses, out_messages_per_second => 5, max_message_size => 800, max_messages_per_hour => 100, forum_join_grace => 0, ); $bg_called = 0; $Net::Jabber::Client::connect_fail_remaining = 1; $bot2->Disconnect(); $bot2->ReconnectToServer(); ok( $bot2->IsConnected(), "Bot2 reconnects after 1 failure" ); ok( $bg_called > 0, "Background function called after successful reconnect" ); # Test 4: Multiple failures with increasing backoff (all caught) $Net::Jabber::Client::connect_fail_remaining = 5; $bot->Disconnect(); $bot->ReconnectToServer(); ok( $bot->IsConnected(), "ReconnectToServer survives 5 consecutive failures" ); Net-Jabber-Bot-3.01/t/07-test_presence_and_iq.t000644 000765 000024 00000031774 15163571666 022702 0ustar00todd.rinaldostaff000000 000000 #!perl use strict; use warnings; use Test::More; use Net::Jabber::Bot; use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; # No-op sleep to avoid real delays BEGIN { *CORE::GLOBAL::sleep = sub { }; } my $server = 'jabber.example.com'; my $conference_server = "conference.$server"; my $bot_alias = 'testbot'; my $bot_username = 'botuser'; sub background_noop { } my %forums_and_responses = ( room1 => ["bot:"], ); my $bot = Net::Jabber::Bot->new( server => $server, conference_server => $conference_server, port => 5222, username => $bot_username, password => 'secret', alias => $bot_alias, background_function => \&background_noop, loop_sleep_time => 5, process_timeout => 5, forums_and_responses => \%forums_and_responses, safety_mode => 0, max_messages_per_hour => 10000, forum_join_grace => 0, auto_subscribe => 1, ); isa_ok( $bot, 'Net::Jabber::Bot' ); # Grab the mock client and callbacks my $client = $bot->jabber_client; my $session_id = $client->{SESSION}->{id}; my $presence_cb = $client->{presence_callback}; my $iq_cb = $client->{iq_callback}; ok( defined $presence_cb, "presence callback is registered" ); ok( defined $iq_cb, "iq callback is registered" ); # ─── Presence: subscribe with auto_subscribe enabled ────────── subtest 'auto_subscribe accepts subscription requests' => sub { @{$client->{subscription_log}} = (); # reset my $presence = Net::Jabber::Presence->new(); $presence->SetFrom('friend@example.com'); $presence->SetType('subscribe'); $presence_cb->($session_id, $presence); my @subs = @{$client->{subscription_log}}; is( scalar @subs, 2, "two subscription calls made" ); is( $subs[0]->{type}, 'subscribe', "first call: subscribe (request mutual)" ); is( $subs[0]->{to}, 'friend@example.com', "subscribe to correct JID" ); is( $subs[1]->{type}, 'subscribed', "second call: subscribed (approve)" ); is( $subs[1]->{to}, 'friend@example.com', "subscribed to correct JID" ); }; # ─── Presence: subscribe with auto_subscribe disabled ───────── subtest 'auto_subscribe disabled ignores subscription requests' => sub { @{$client->{subscription_log}} = (); # reset $bot->auto_subscribe(0); my $presence = Net::Jabber::Presence->new(); $presence->SetFrom('stranger@example.com'); $presence->SetType('subscribe'); $presence_cb->($session_id, $presence); my @subs = @{$client->{subscription_log}}; is( scalar @subs, 0, "no subscription calls when auto_subscribe is off" ); $bot->auto_subscribe(1); # restore }; # ─── Presence: unsubscribe ──────────────────────────────────── subtest 'unsubscribe sends unsubscribed response' => sub { @{$client->{subscription_log}} = (); # reset my $presence = Net::Jabber::Presence->new(); $presence->SetFrom('leaving@example.com'); $presence->SetType('unsubscribe'); $presence_cb->($session_id, $presence); my @subs = @{$client->{subscription_log}}; is( scalar @subs, 1, "one subscription call for unsubscribe" ); is( $subs[0]->{type}, 'unsubscribed', "sends unsubscribed response" ); is( $subs[0]->{to}, 'leaving@example.com', "unsubscribed correct JID" ); }; # ─── Presence: normal available (no type) ───────────────────── subtest 'normal presence is stored in PresenceDB' => sub { my $presence = Net::Jabber::Presence->new(); $presence->SetFrom('buddy@example.com/home'); # No SetType — this is a normal "available" presence $presence_cb->($session_id, $presence); # Verify it was stored via PresenceDBParse my $stored = $client->{presence_db}{'buddy@example.com/home'}; ok( defined $stored, "presence stored in DB" ); is( $stored->GetFrom(), 'buddy@example.com/home', "stored presence has correct from" ); }; # ─── Presence: priority is set to 0 when missing ────────────── subtest 'missing priority defaults to 0' => sub { my $presence = Net::Jabber::Presence->new(); $presence->SetFrom('noprio@example.com/laptop'); # No SetPriority — should be set to 0 by the handler $presence_cb->($session_id, $presence); my $stored = $client->{presence_db}{'noprio@example.com/laptop'}; ok( defined $stored, "presence stored" ); is( $stored->GetPriority(), 0, "priority set to 0 when missing" ); }; # ─── Presence: explicit priority is preserved ───────────────── subtest 'explicit priority is preserved' => sub { my $presence = Net::Jabber::Presence->new(); $presence->SetFrom('prio@example.com/work'); $presence->SetPriority(10); $presence_cb->($session_id, $presence); my $stored = $client->{presence_db}{'prio@example.com/work'}; ok( defined $stored, "presence stored" ); is( $stored->GetPriority(), 10, "explicit priority 10 preserved" ); }; # ─── Presence: with show and status ─────────────────────────── subtest 'presence with show/status values' => sub { my $presence = Net::Jabber::Presence->new(); $presence->SetFrom('away@example.com/mobile'); $presence->SetShow('away'); $presence->SetStatus('On vacation'); $presence->SetPriority(5); $presence_cb->($session_id, $presence); my $stored = $client->{presence_db}{'away@example.com/mobile'}; ok( defined $stored, "presence stored" ); is( $stored->GetShow(), 'away', "show value preserved" ); is( $stored->GetStatus(), 'On vacation', "status string preserved" ); }; # ─── Presence: subscribe does NOT store in PresenceDB ───────── subtest 'subscribe does not store in PresenceDB' => sub { delete $client->{presence_db}{'sub_only@example.com'}; my $presence = Net::Jabber::Presence->new(); $presence->SetFrom('sub_only@example.com'); $presence->SetType('subscribe'); $presence_cb->($session_id, $presence); ok( !exists $client->{presence_db}{'sub_only@example.com'}, "subscribe presence is not stored in DB" ); }; # ─── Presence: unsubscribe does NOT store in PresenceDB ─────── subtest 'unsubscribe does not store in PresenceDB' => sub { delete $client->{presence_db}{'unsub_only@example.com'}; my $presence = Net::Jabber::Presence->new(); $presence->SetFrom('unsub_only@example.com'); $presence->SetType('unsubscribe'); $presence_cb->($session_id, $presence); ok( !exists $client->{presence_db}{'unsub_only@example.com'}, "unsubscribe presence is not stored in DB" ); }; # ─── Presence: undefined from handled gracefully ────────────── subtest 'presence with undefined from does not crash' => sub { my $presence = Net::Jabber::Presence->new(); # No SetFrom — from will be undef/empty $presence->SetPriority(1); # Should not die eval { $presence_cb->($session_id, $presence) }; ok( !$@, "presence with no from does not crash" ); }; # ═══════════════════════════════════════════════════════════════ # IQ Handler Tests # ═══════════════════════════════════════════════════════════════ # ─── IQ: version request ────────────────────────────────────── subtest 'IQ version request sends version response' => sub { delete $client->{last_version_send}; my $iq = Net::Jabber::IQ->new(); $iq->SetFrom('requester@example.com/client'); $iq->SetType('get'); # Add a query with jabber:iq:version namespace my $query = $iq->NewQuery('jabber:iq:version'); $iq_cb->($session_id, $iq); ok( defined $client->{last_version_send}, "VersionSend was called" ); is( $client->{last_version_send}->{to}, 'requester@example.com/client', "version sent to correct JID" ); is( $client->{last_version_send}->{name}, 'Net::Jabber::Bot', "version name is package name" ); is( $client->{last_version_send}->{ver}, $Net::Jabber::Bot::VERSION, "version matches module VERSION" ); like( $client->{last_version_send}->{os}, qr/^Perl v/, "OS string starts with 'Perl v'" ); }; # ─── IQ: non-version query is ignored ───────────────────────── subtest 'IQ with non-version xmlns does not send version' => sub { delete $client->{last_version_send}; my $iq = Net::Jabber::IQ->new(); $iq->SetFrom('other@example.com'); $iq->SetType('get'); # Use a different namespace my $query = $iq->NewQuery('jabber:iq:roster'); $iq_cb->($session_id, $iq); ok( !defined $client->{last_version_send}, "VersionSend NOT called for non-version query" ); }; # ─── IQ: no query element ───────────────────────────────────── subtest 'IQ without query element does not crash' => sub { delete $client->{last_version_send}; my $iq = Net::Jabber::IQ->new(); $iq->SetFrom('bare@example.com'); $iq->SetType('result'); # No query added eval { $iq_cb->($session_id, $iq) }; ok( !$@, "IQ with no query does not crash" ); ok( !defined $client->{last_version_send}, "VersionSend NOT called when no query" ); }; # ─── IQ: version response contains valid Perl version ───────── subtest 'IQ version response has well-formatted Perl version' => sub { delete $client->{last_version_send}; my $iq = Net::Jabber::IQ->new(); $iq->SetFrom('checker@example.com'); $iq->SetType('get'); $iq->NewQuery('jabber:iq:version'); $iq_cb->($session_id, $iq); my $os = $client->{last_version_send}->{os}; # Perl version should be formatted like "Perl v5.X.Y" (not raw like "Perl v5.042000") like( $os, qr/^Perl v\d+\.\d+/, "OS contains formatted Perl version" ); unlike( $os, qr/\d{6}/, "Perl version does not contain raw 6-digit minor version" ); }; # ═══════════════════════════════════════════════════════════════ # Integration: Presence feeds into GetStatus # ═══════════════════════════════════════════════════════════════ subtest 'presence updates are visible via GetStatus' => sub { # Send an "available" presence my $presence = Net::Jabber::Presence->new(); $presence->SetFrom('statustest@example.com/desktop'); $presence->SetPriority(1); $presence_cb->($session_id, $presence); my $status = $bot->GetStatus('statustest@example.com/desktop'); is( $status, 'available', "GetStatus returns available for presence with no show" ); # Now send an "away" presence my $away = Net::Jabber::Presence->new(); $away->SetFrom('statustest@example.com/desktop'); $away->SetShow('away'); $away->SetPriority(1); $presence_cb->($session_id, $away); $status = $bot->GetStatus('statustest@example.com/desktop'); is( $status, 'away', "GetStatus reflects updated show value" ); }; # ═══════════════════════════════════════════════════════════════ # Integration: auto_subscribe toggle at runtime # ═══════════════════════════════════════════════════════════════ subtest 'auto_subscribe can be toggled at runtime' => sub { # Start with auto_subscribe on $bot->auto_subscribe(1); @{$client->{subscription_log}} = (); my $p1 = Net::Jabber::Presence->new(); $p1->SetFrom('toggle1@example.com'); $p1->SetType('subscribe'); $presence_cb->($session_id, $p1); is( scalar @{$client->{subscription_log}}, 2, "auto_subscribe on: 2 calls" ); # Turn it off $bot->auto_subscribe(0); @{$client->{subscription_log}} = (); my $p2 = Net::Jabber::Presence->new(); $p2->SetFrom('toggle2@example.com'); $p2->SetType('subscribe'); $presence_cb->($session_id, $p2); is( scalar @{$client->{subscription_log}}, 0, "auto_subscribe off: 0 calls" ); # Turn it back on $bot->auto_subscribe(1); @{$client->{subscription_log}} = (); my $p3 = Net::Jabber::Presence->new(); $p3->SetFrom('toggle3@example.com'); $p3->SetType('subscribe'); $presence_cb->($session_id, $p3); is( scalar @{$client->{subscription_log}}, 2, "auto_subscribe back on: 2 calls" ); }; done_testing(); Net-Jabber-Bot-3.01/t/test_config.sample000644 000765 000024 00000000276 15156543215 021602 0ustar00todd.rinaldostaff000000 000000 [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-3.01/t/00-load.t000644 000765 000024 00000000474 15156543215 017414 0ustar00todd.rinaldostaff000000 000000 #!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-3.01/t/07-test_public_api_connected.t000644 000765 000024 00000030723 15164031674 023674 0ustar00todd.rinaldostaff000000 000000 #!perl use strict; use warnings; BEGIN { *CORE::GLOBAL::sleep = sub { }; } use Test::More; use Net::Jabber::Bot; use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; my $bot_alias = 'make_test_bot'; my $server = 'talk.google.com'; my %forums_and_responses; $forums_and_responses{'test_forum1'} = [ "jbot:", "" ]; 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 => sub { }, background_function => sub { }, loop_sleep_time => 5, process_timeout => 5, forums_and_responses => \%forums_and_responses, ignore_server_messages => 1, ignore_self_messages => 1, out_messages_per_second => 5, max_message_size => 1000, max_messages_per_hour => 100, forum_join_grace => 0, ); isa_ok( $bot, "Net::Jabber::Bot" ); ok( $bot->IsConnected(), "Bot starts connected" ); # ─── AddUser ─────────────────────────────────────────────── subtest 'AddUser when connected' => sub { my $client = $bot->jabber_client; @{$client->{subscription_log}} = (); # Reset log $bot->AddUser("friend\@$server"); my @subs = @{$client->{subscription_log}}; is( scalar @subs, 2, "AddUser sends 2 subscription stanzas" ); is( $subs[0]{type}, 'subscribe', "First stanza is subscribe request" ); is( $subs[0]{to}, "friend\@$server", "Subscribe targets correct JID" ); is( $subs[1]{type}, 'subscribed', "Second stanza is subscribed approval" ); is( $subs[1]{to}, "friend\@$server", "Subscribed targets correct JID" ); }; subtest 'AddUser when disconnected' => sub { $bot->Disconnect(); ok( !$bot->IsConnected(), "Bot is disconnected" ); # Should not crash my $result = eval { $bot->AddUser("someone\@$server") }; ok( !$@, "AddUser does not crash when disconnected" ) or diag("AddUser died: $@"); }; # Reconnect for remaining tests $bot->ReconnectToServer(); ok( $bot->IsConnected(), "Bot reconnected after AddUser disconnect test" ); # ─── RmUser ──────────────────────────────────────────────── subtest 'RmUser when connected' => sub { my $client = $bot->jabber_client; @{$client->{subscription_log}} = (); # Reset log $bot->RmUser("enemy\@$server"); my @subs = @{$client->{subscription_log}}; is( scalar @subs, 2, "RmUser sends 2 subscription stanzas" ); is( $subs[0]{type}, 'unsubscribe', "First stanza is unsubscribe" ); is( $subs[0]{to}, "enemy\@$server", "Unsubscribe targets correct JID" ); is( $subs[1]{type}, 'unsubscribed', "Second stanza is unsubscribed" ); is( $subs[1]{to}, "enemy\@$server", "Unsubscribed targets correct JID" ); }; subtest 'RmUser when disconnected' => sub { $bot->Disconnect(); my $result = eval { $bot->RmUser("someone\@$server") }; ok( !$@, "RmUser does not crash when disconnected" ) or diag("RmUser died: $@"); }; $bot->ReconnectToServer(); ok( $bot->IsConnected(), "Bot reconnected after RmUser disconnect test" ); # ─── JoinForum (direct call) ────────────────────────────── subtest 'JoinForum direct call' => sub { my $client = $bot->jabber_client; @{$client->{muc_join_log}} = (); # Reset $bot->JoinForum('new_room'); my @joins = @{$client->{muc_join_log}}; is( scalar @joins, 1, "JoinForum sends one MUCJoin" ); is( $joins[0]{room}, 'new_room', "MUCJoin room is correct" ); is( $joins[0]{server}, "conference.$server", "MUCJoin server is correct" ); is( $joins[0]{nick}, $bot_alias, "MUCJoin nick is bot alias" ); ok( exists $bot->forum_join_time->{'new_room'}, "forum_join_time recorded" ); ok( $bot->forum_join_time->{'new_room'} > 0, "forum_join_time is positive timestamp" ); }; subtest 'JoinForum when disconnected' => sub { $bot->Disconnect(); my $result = eval { $bot->JoinForum('some_room') }; ok( !$@, "JoinForum does not crash when disconnected" ) or diag("JoinForum died: $@"); }; $bot->ReconnectToServer(); # ─── ChangeStatus (connected) ───────────────────────────── subtest 'ChangeStatus when connected' => sub { my $client = $bot->jabber_client; @{$client->{presence_send_log}} = (); my $result = $bot->ChangeStatus("away", "brb"); is( $result, 1, "ChangeStatus returns 1 on success" ); my @sends = @{$client->{presence_send_log}}; # The init also does PresenceSend, so filter by our call # Actually the log was reset, so we only see our call ok( scalar @sends >= 1, "PresenceSend was called" ); is( $sends[-1]{show}, 'away', "Presence show is correct" ); is( $sends[-1]{status}, 'brb', "Presence status is correct" ); }; subtest 'ChangeStatus with no status string' => sub { my $client = $bot->jabber_client; @{$client->{presence_send_log}} = (); my $result = $bot->ChangeStatus("chat"); is( $result, 1, "ChangeStatus returns 1 with mode only" ); my @sends = @{$client->{presence_send_log}}; is( $sends[-1]{show}, 'chat', "Presence show is 'chat'" ); }; # ─── GetRoster (with data) ──────────────────────────────── { # Create mock JID objects for the roster package MockJID; sub new { my ($class, $jid) = @_; return bless { jid => $jid }, $class; } sub GetJID { return $_[0]->{jid} } } subtest 'GetRoster with users' => sub { my $client = $bot->jabber_client; # Populate roster my @jids = ( MockJID->new("alice\@$server"), MockJID->new("bob\@$server"), MockJID->new("carol\@$server"), ); $client->{roster_jids} = \@jids; my @roster = $bot->GetRoster(); is( scalar @roster, 3, "GetRoster returns 3 users" ); is( $roster[0], "alice\@$server", "First roster entry correct" ); is( $roster[1], "bob\@$server", "Second roster entry correct" ); is( $roster[2], "carol\@$server", "Third roster entry correct" ); }; subtest 'GetRoster with empty roster' => sub { my $client = $bot->jabber_client; $client->{roster_jids} = []; my @roster = $bot->GetRoster(); is( scalar @roster, 0, "GetRoster returns empty list for empty roster" ); }; # ─── GetStatus (with presence data) ─────────────────────── subtest 'GetStatus for user with show value' => sub { my $client = $bot->jabber_client; # Create a presence entry in the mock DB my $presence = Net::Jabber::Presence->new(); $presence->SetFrom("alice\@$server"); $presence->SetShow("away"); $client->{presence_db}{"alice\@$server"} = $presence; my $status = $bot->GetStatus("alice\@$server"); is( $status, 'away', "GetStatus returns show value for present user" ); }; subtest 'GetStatus for available user (no show)' => sub { my $client = $bot->jabber_client; my $presence = Net::Jabber::Presence->new(); $presence->SetFrom("bob\@$server"); # No SetShow — user is just "available" $client->{presence_db}{"bob\@$server"} = $presence; my $status = $bot->GetStatus("bob\@$server"); is( $status, 'available', "GetStatus returns 'available' when no show value" ); }; subtest 'GetStatus for unknown user' => sub { my $status = $bot->GetStatus("unknown\@$server"); is( $status, 'unavailable', "GetStatus returns 'unavailable' for unknown JID" ); }; subtest 'GetStatus various show values' => sub { my $client = $bot->jabber_client; for my $show_val (qw(chat xa dnd)) { my $presence = Net::Jabber::Presence->new(); $presence->SetFrom("user_$show_val\@$server"); $presence->SetShow($show_val); $client->{presence_db}{"user_$show_val\@$server"} = $presence; my $status = $bot->GetStatus("user_$show_val\@$server"); is( $status, $show_val, "GetStatus returns '$show_val' correctly" ); } }; # ─── SetForumSubject (happy path) ───────────────────────── subtest 'SetForumSubject successful' => sub { my $client = $bot->jabber_client; @{$client->{message_queue}} = (); # Clear queue my $result = $bot->SetForumSubject("test_forum1\@conference.$server", "New Topic"); ok( !defined $result, "SetForumSubject returns undef on success" ); }; subtest 'SetForumSubject too long' => sub { my $long_subject = 'x' x 1500; my $result = $bot->SetForumSubject("test_forum1\@conference.$server", $long_subject); is( $result, "Subject is too long!", "SetForumSubject rejects oversized subject" ); }; subtest 'SetForumSubject at boundary' => sub { my $exact_subject = 'y' x 1000; my $result = $bot->SetForumSubject("test_forum1\@conference.$server", $exact_subject); ok( !defined $result, "SetForumSubject accepts subject at exactly max_message_size" ); }; # ─── get_responses ───────────────────────────────────────── subtest 'get_responses for known forum' => sub { my @resp = $bot->get_responses('test_forum1'); is( scalar @resp, 2, "test_forum1 has 2 response patterns" ); is( $resp[0], 'jbot:', "First pattern is 'jbot:'" ); is( $resp[1], '', "Second pattern is empty (respond to all)" ); }; subtest 'get_responses for unknown forum' => sub { my @resp = $bot->get_responses('nonexistent_forum'); is( scalar @resp, 0, "Unknown forum returns empty response list" ); }; subtest 'get_responses with undef' => sub { my @resp = $bot->get_responses(undef); is( scalar @resp, 0, "undef forum returns empty response list" ); }; # ─── respond_to_self_messages ────────────────────────────── subtest 'respond_to_self_messages toggling' => sub { # Safety mode is on, so ignore_self_messages was forced to 1 ok( $bot->ignore_self_messages, "ignore_self_messages starts on (safety mode)" ); $bot->respond_to_self_messages(1); ok( !$bot->ignore_self_messages, "respond_to_self_messages(1) disables ignore" ); $bot->respond_to_self_messages(0); ok( $bot->ignore_self_messages, "respond_to_self_messages(0) enables ignore" ); # Default argument $bot->respond_to_self_messages(); ok( !$bot->ignore_self_messages, "respond_to_self_messages() defaults to 1" ); # Restore $bot->ignore_self_messages(1); }; # ─── get_messages_this_hour ──────────────────────────────── subtest 'get_messages_this_hour tracking' => sub { # The bot has been sending messages in SetForumSubject tests, # so the count should be > 0 my $count = $bot->get_messages_this_hour(); ok( $count >= 0, "get_messages_this_hour returns a non-negative number" ); # Send a known number and verify increment my $before = $bot->get_messages_this_hour(); $bot->SendPersonalMessage("test_user\@$server/resource", "counting test"); my $after = $bot->get_messages_this_hour(); is( $after, $before + 1, "Message count increments by 1 after sending" ); }; # ─── get_safety_mode ─────────────────────────────────────── subtest 'get_safety_mode reports correctly' => sub { ok( $bot->get_safety_mode(), "get_safety_mode returns true when safety is on" ); }; # ─── from_full attribute ────────────────────────────────── subtest 'from_full uses resource not alias' => sub { # resource defaults to alias_hostname_pid, which differs from alias my $from = $bot->from_full; my $expected = $bot->username . '@' . $bot->server . '/' . $bot->resource; is( $from, $expected, "from_full matches username\@server/resource" ); # Verify it uses resource (which includes hostname/pid) not bare alias like( $from, qr/\Q$bot_alias\E_/, "from_full resource starts with alias but includes more" ); unlike( $from, qr/\/$bot_alias$/, "from_full does not end with bare alias" ); }; done_testing(); Net-Jabber-Bot-3.01/t/06-test_safeties.t000644 000765 000024 00000021305 15156711514 021340 0ustar00todd.rinaldostaff000000 000000 #!perl use strict; use warnings; use Test::More tests => 127; 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 , forum_join_grace => 0 }); 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, 0, "Forum Grace is 0 seconds as configured"); 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, "Moo 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 { 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-3.01/t/lib/000755 000765 000024 00000000000 15164032430 016623 5ustar00todd.rinaldostaff000000 000000 Net-Jabber-Bot-3.01/t/08-test_start_stop.t000644 000765 000024 00000025542 15163571666 021762 0ustar00todd.rinaldostaff000000 000000 #!perl use strict; use warnings; # Override sleep to avoid delays in tests BEGIN { *CORE::GLOBAL::sleep = sub { }; } use Test::More tests => 22; use Net::Jabber::Bot; # stuff for mock client object use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; # Test object my $server = 'talk.google.com'; my %forums_and_responses; $forums_and_responses{'test_forum1'} = [ "jbot:", "" ]; # Test 1: Stop() method exists and returns true { my $bot = Net::Jabber::Bot->new( server => $server, conference_server => "conference.$server", port => 5222, username => 'test_username', password => 'test_pass', alias => 'stop_test_bot', message_function => sub { }, background_function => sub { }, loop_sleep_time => 5, process_timeout => 5, forums_and_responses => \%forums_and_responses, out_messages_per_second => 5, max_message_size => 800, max_messages_per_hour => 100, forum_join_grace => 0, ); isa_ok( $bot, "Net::Jabber::Bot" ); ok( $bot->IsConnected(), "Bot connected after init" ); ok( $bot->Stop(), "Stop() returns true" ); } # Test 2: Start() exits when Stop() is called from background_function { my $bg_count = 0; my $bot = Net::Jabber::Bot->new( server => $server, conference_server => "conference.$server", port => 5222, username => 'test_username', password => 'test_pass', alias => 'start_stop_bot', message_function => sub { }, background_function => sub { my ( $bot_obj, $counter ) = @_; $bg_count = $counter; $bot_obj->Stop() if $counter >= 3; }, loop_sleep_time => 0.01, process_timeout => 0.01, forums_and_responses => \%forums_and_responses, out_messages_per_second => 5, max_message_size => 800, max_messages_per_hour => 100, forum_join_grace => 0, ); my $iterations = $bot->Start(); ok( !$bot->_running, "Bot is no longer running after Start() returns" ); is( $bg_count, 3, "Background function was called 3 times" ); is( $iterations, 3, "Start() returns the iteration count" ); } # Test 3: Calling Stop() before Start() does not prevent Start() from running { my $bg_count = 0; my $bot = Net::Jabber::Bot->new( server => $server, conference_server => "conference.$server", port => 5222, username => 'test_username', password => 'test_pass', alias => 'prestop_bot', message_function => sub { }, background_function => sub { my ( $bot_obj, $counter ) = @_; $bg_count = $counter; $bot_obj->Stop(); # Stop on first background call }, loop_sleep_time => 0.01, process_timeout => 0.01, forums_and_responses => \%forums_and_responses, out_messages_per_second => 5, max_message_size => 800, max_messages_per_hour => 100, forum_join_grace => 0, ); $bot->Stop(); # Stop before Start — should have no lasting effect my $iterations = $bot->Start(); # Start() resets _running to 1, so a prior Stop() does not prevent the loop. ok( $bg_count > 0, "Start() still runs despite prior Stop() call" ); } # Test 4: Start() can be called again after Stop() { my $total_bg_calls = 0; my $run_number = 0; my $bot = Net::Jabber::Bot->new( server => $server, conference_server => "conference.$server", port => 5222, username => 'test_username', password => 'test_pass', alias => 'restart_bot', message_function => sub { }, background_function => sub { my ( $bot_obj, $counter ) = @_; $total_bg_calls++; $bot_obj->Stop() if $counter >= 2; }, loop_sleep_time => 0.01, process_timeout => 0.01, forums_and_responses => \%forums_and_responses, out_messages_per_second => 5, max_message_size => 800, max_messages_per_hour => 100, forum_join_grace => 0, ); my $iters1 = $bot->Start(); is( $iters1, 2, "First Start() ran 2 iterations" ); my $iters2 = $bot->Start(); is( $iters2, 2, "Second Start() ran 2 iterations" ); is( $total_bg_calls, 4, "Background function called 4 times total across both runs" ); } # Test 5: Stop() from message_function also works { my $msg_received = 0; my $bot = Net::Jabber::Bot->new( server => $server, conference_server => "conference.$server", port => 5222, username => 'test_username', password => 'test_pass', alias => 'msg_stop_bot', message_function => sub { my %args = @_; $msg_received++; $args{bot_object}->Stop(); }, background_function => sub { }, loop_sleep_time => 0.01, process_timeout => 0.01, forums_and_responses => \%forums_and_responses, out_messages_per_second => 5, max_message_size => 800, max_messages_per_hour => 100, forum_join_grace => 0, ignore_self_messages => 0, safety_mode => 0, ); # Inject a message that will trigger the callback $bot->SendPersonalMessage( 'test_user@' . $server . '/res', "trigger stop" ); my $iterations = $bot->Start(); ok( $msg_received > 0, "Message function was called" ); ok( !$bot->_running, "Bot stopped from message_function" ); } # Test 6: Start() returns cleanly after normal operation { my $bot = Net::Jabber::Bot->new( server => $server, conference_server => "conference.$server", port => 5222, username => 'test_username', password => 'test_pass', alias => 'error_stop_bot', message_function => sub { }, background_function => sub { my ( $bot_obj, $counter ) = @_; $bot_obj->Stop(); }, loop_sleep_time => 0.01, process_timeout => 0.01, forums_and_responses => \%forums_and_responses, out_messages_per_second => 5, max_message_size => 800, max_messages_per_hour => 100, forum_join_grace => 0, ); ok( $bot->IsConnected(), "Bot connected before Start" ); my $iterations = $bot->Start(); ok( defined $iterations, "Start() returned cleanly after normal operation" ); } # Test 7: Start() reconnects when Process() dies (exception) { my $bg_count = 0; my $bot = Net::Jabber::Bot->new( server => $server, conference_server => "conference.$server", port => 5222, username => 'test_username', password => 'test_pass', alias => 'die_reconnect_bot', message_function => sub { }, background_function => sub { my ( $bot_obj, $counter ) = @_; $bg_count = $counter; $bot_obj->Stop() if $counter >= 2; }, loop_sleep_time => 0.01, process_timeout => 0.01, forums_and_responses => \%forums_and_responses, out_messages_per_second => 5, max_message_size => 800, max_messages_per_hour => 100, forum_join_grace => 0, ); # Make Process() die on the first call — Start() should catch it and reconnect $Net::Jabber::Client::process_die_remaining = 1; my $iterations = $bot->Start(); ok( $bot->IsConnected(), "Bot reconnected after Process() exception" ); is( $bg_count, 2, "Background function ran after reconnection from exception" ); is( $Net::Jabber::Client::process_die_remaining, 0, "Process die counter exhausted" ); } # Test 8: Start() reconnects when Process() returns undef (silent disconnect) { my $bg_count = 0; my $bot = Net::Jabber::Bot->new( server => $server, conference_server => "conference.$server", port => 5222, username => 'test_username', password => 'test_pass', alias => 'undef_reconnect_bot', message_function => sub { }, background_function => sub { my ( $bot_obj, $counter ) = @_; $bg_count = $counter; $bot_obj->Stop() if $counter >= 2; }, loop_sleep_time => 0.01, process_timeout => 0.01, forums_and_responses => \%forums_and_responses, out_messages_per_second => 5, max_message_size => 800, max_messages_per_hour => 100, forum_join_grace => 0, ); # Make Process() return undef (simulating silent connection loss) $Net::Jabber::Client::process_return_undef_remaining = 1; my $iterations = $bot->Start(); ok( $bot->IsConnected(), "Bot reconnected after silent disconnect (Process undef)" ); is( $bg_count, 2, "Background function ran after reconnection from undef" ); is( $Net::Jabber::Client::process_return_undef_remaining, 0, "Process undef counter exhausted" ); } # Test 9: Start() survives multiple reconnections in one run { my $bg_count = 0; my $bot = Net::Jabber::Bot->new( server => $server, conference_server => "conference.$server", port => 5222, username => 'test_username', password => 'test_pass', alias => 'multi_reconnect_bot', message_function => sub { }, background_function => sub { my ( $bot_obj, $counter ) = @_; $bg_count = $counter; $bot_obj->Stop() if $counter >= 3; }, loop_sleep_time => 0.01, process_timeout => 0.01, forums_and_responses => \%forums_and_responses, out_messages_per_second => 5, max_message_size => 800, max_messages_per_hour => 100, forum_join_grace => 0, ); # Two failures: first a die, then a undef return $Net::Jabber::Client::process_die_remaining = 1; $Net::Jabber::Client::process_return_undef_remaining = 1; my $iterations = $bot->Start(); ok( $bot->IsConnected(), "Bot survived multiple reconnections" ); is( $bg_count, 3, "Background function ran 3 times after multiple reconnections" ); } Net-Jabber-Bot-3.01/t/07-gtalk_connect.t000644 000765 000024 00000013331 15164031674 021313 0ustar00todd.rinaldostaff000000 000000 #!perl use strict; use warnings; use Test::More tests => 10; use Net::Jabber::Bot; # stuff for mock client object use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; # Test object my %forums_and_responses; $forums_and_responses{'test_forum1'} = ["jbot:", ""]; # Test 1: from_full attribute produces correct formatted string { my $bot = Net::Jabber::Bot->new( server => 'talk.google.com', conference_server => 'conference.talk.google.com', port => 5222, username => 'testuser', password => 'testpass', alias => 'testbot', forums_and_responses => \%forums_and_responses, ); is($bot->from_full, 'testuser@talk.google.com/' . $bot->resource, "from_full produces correctly formatted user\@server/resource string"); } # Test 2: from_full with lazy defaults for alias { my $bot = Net::Jabber::Bot->new( server => 'jabber.example.com', conference_server => 'conference.jabber.example.com', port => 5222, username => 'myuser', password => 'mypass', forums_and_responses => \%forums_and_responses, ); like($bot->from_full, qr/^myuser\@jabber\.example\.com\//, "from_full starts with username\@server/ when alias defaults"); } # Test 3: gtalk parameter enables TLS { my $bot = Net::Jabber::Bot->new( server => 'talk.google.com', conference_server => 'conference.talk.google.com', port => 5222, username => 'testuser', password => 'testpass', alias => 'testbot', gtalk => 1, forums_and_responses => \%forums_and_responses, ); is($bot->tls, 1, "gtalk => 1 enables TLS"); } # Test 4: gtalk parameter sets server_host to gmail.com { my $bot = Net::Jabber::Bot->new( server => 'talk.google.com', conference_server => 'conference.talk.google.com', port => 5222, username => 'testuser', password => 'testpass', alias => 'testbot', gtalk => 1, forums_and_responses => \%forums_and_responses, ); is($bot->server_host, 'gmail.com', "gtalk => 1 sets server_host to gmail.com"); } # Test 5: gtalk does not override explicit tls setting { my $bot = Net::Jabber::Bot->new( server => 'talk.google.com', conference_server => 'conference.talk.google.com', port => 5222, username => 'testuser', password => 'testpass', alias => 'testbot', gtalk => 1, tls => 0, forums_and_responses => \%forums_and_responses, ); # gtalk should force tls on regardless is($bot->tls, 1, "gtalk => 1 forces TLS even if tls => 0 was passed"); } # Test 6: gtalk does not override explicit server_host { my $bot = Net::Jabber::Bot->new( server => 'talk.google.com', conference_server => 'conference.talk.google.com', port => 5222, username => 'testuser', password => 'testpass', alias => 'testbot', gtalk => 1, server_host => 'custom.google.com', forums_and_responses => \%forums_and_responses, ); is($bot->server_host, 'custom.google.com', "gtalk => 1 does not override explicit server_host"); } # Test 7: without gtalk, tls defaults to 0 { my $bot = Net::Jabber::Bot->new( server => 'jabber.example.com', conference_server => 'conference.jabber.example.com', port => 5222, username => 'testuser', password => 'testpass', alias => 'testbot', forums_and_responses => \%forums_and_responses, ); is($bot->tls, 0, "Without gtalk, tls defaults to 0"); } # Test 8: without gtalk, server_host defaults to server { my $bot = Net::Jabber::Bot->new( server => 'jabber.example.com', conference_server => 'conference.jabber.example.com', port => 5222, username => 'testuser', password => 'testpass', alias => 'testbot', forums_and_responses => \%forums_and_responses, ); is($bot->server_host, 'jabber.example.com', "Without gtalk, server_host defaults to server value"); } # Test 9: gtalk => 0 does not change defaults { my $bot = Net::Jabber::Bot->new( server => 'jabber.example.com', conference_server => 'conference.jabber.example.com', port => 5222, username => 'testuser', password => 'testpass', alias => 'testbot', gtalk => 0, forums_and_responses => \%forums_and_responses, ); is($bot->tls, 0, "gtalk => 0 does not enable TLS"); } # Test 10: bot is a proper object { my $bot = Net::Jabber::Bot->new( server => 'talk.google.com', conference_server => 'conference.talk.google.com', port => 5222, username => 'testuser', password => 'testpass', alias => 'testbot', gtalk => 1, forums_and_responses => \%forums_and_responses, ); isa_ok($bot, "Net::Jabber::Bot"); } Net-Jabber-Bot-3.01/t/07-test_disconnect_public_api.t000644 000765 000024 00000004527 15160055734 024065 0ustar00todd.rinaldostaff000000 000000 #!perl use strict; use warnings; BEGIN { *CORE::GLOBAL::sleep = sub { }; } use Test::More tests => 9; use Net::Jabber::Bot; use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; my $bot_alias = 'make_test_bot'; my $server = 'talk.google.com'; my %forums_and_responses; $forums_and_responses{'test_forum1'} = [ "jbot:", "" ]; 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 => sub { }, background_function => sub { }, loop_sleep_time => 5, process_timeout => 5, forums_and_responses => \%forums_and_responses, ignore_server_messages => 1, ignore_self_messages => 1, out_messages_per_second => 5, max_message_size => 1000, max_messages_per_hour => 100, forum_join_grace => 0, ); isa_ok( $bot, "Net::Jabber::Bot" ); ok( $bot->IsConnected(), "Bot starts connected" ); # Disconnect the bot $bot->Disconnect(); ok( !$bot->IsConnected(), "Bot is disconnected" ); # Test that public API methods don't crash when disconnected. # Before this fix, each of these would die with: # "Can't call method ... on an undefined value" # because jabber_client is undef after Disconnect(). # ChangeStatus should return 0 (failure), not crash my $status_result = eval { $bot->ChangeStatus("available", "test status") }; ok( !$@, "ChangeStatus does not crash when disconnected" ) or diag("ChangeStatus died: $@"); # GetRoster should return empty list, not crash my @roster = eval { $bot->GetRoster() }; ok( !$@, "GetRoster does not crash when disconnected" ) or diag("GetRoster died: $@"); is( scalar @roster, 0, "GetRoster returns empty list when disconnected" ); # GetStatus should return 'unavailable', not crash my $get_status = eval { $bot->GetStatus("someone\@$server") }; ok( !$@, "GetStatus does not crash when disconnected" ) or diag("GetStatus died: $@"); is( $get_status, "unavailable", "GetStatus returns 'unavailable' when disconnected" ); # Process should return undef, not crash my $process_result = eval { $bot->Process(1) }; ok( !$@, "Process does not crash when disconnected" ) or diag("Process died: $@"); Net-Jabber-Bot-3.01/t/07-test_reconnect_and_leaks.t000644 000765 000024 00000005456 15156711514 023530 0ustar00todd.rinaldostaff000000 000000 #!perl use strict; use warnings; use Test::More tests => 13; use Net::Jabber::Bot; # stuff for mock client object use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; # Test object my $bot_alias = 'reconnect_test_bot'; my $server = 'talk.google.com'; my %forums_and_responses; $forums_and_responses{'test_forum1'} = [ "jbot:", "" ]; 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 => sub { }, background_function => sub { }, loop_sleep_time => 5, process_timeout => 5, forums_and_responses => \%forums_and_responses, out_messages_per_second => 5, max_message_size => 800, max_messages_per_hour => 100, forum_join_grace => 0, ); isa_ok( $bot, "Net::Jabber::Bot" ); # Test 1: IsConnected returns true when connected ok( $bot->IsConnected(), "Bot reports connected after init" ); ok( defined $bot->jabber_client, "jabber_client is defined when connected" ); # Test 2: IsConnected returns false after Disconnect $bot->Disconnect(); ok( !$bot->IsConnected(), "Bot reports NOT connected after Disconnect" ); # Test 3: ReconnectToServer successfully reconnects eval { $bot->ReconnectToServer(); }; ok( !$@, "ReconnectToServer does not die" ) or diag("ReconnectToServer died: $@"); ok( $bot->IsConnected(), "Bot reports connected after ReconnectToServer" ); ok( defined $bot->jabber_client, "jabber_client is defined after reconnect" ); # Test 4: Process works after reconnect my $process_result = $bot->Process(1); ok( defined $process_result, "Process works after reconnect" ); # Test 5: messages_sent_today does not leak old day entries { my $personal_address = "test_user\@$server/$bot_alias"; # Simulate sending messages "yesterday" by injecting an old day entry my $today_yday = ( localtime() )[7]; my $yesterday_yday = $today_yday > 0 ? $today_yday - 1 : 364; $bot->messages_sent_today->{$yesterday_yday} = { 0 => 50, 1 => 30 }; ok( exists $bot->messages_sent_today->{$yesterday_yday}, "Old day entry exists before sending" ); # Send a message - this should trigger cleanup of old day entries $bot->SendPersonalMessage( $personal_address, "test cleanup" ); ok( !exists $bot->messages_sent_today->{$yesterday_yday}, "Old day entry cleaned up after sending a message" ); ok( exists $bot->messages_sent_today->{$today_yday}, "Today's entry still exists" ); } # Test 6: Multiple disconnect/reconnect cycles work for my $cycle ( 1 .. 2 ) { $bot->Disconnect(); ok( !$bot->IsConnected(), "Disconnected in cycle $cycle" ); $bot->ReconnectToServer(); } Net-Jabber-Bot-3.01/t/07-test_from_parameter.t000644 000765 000024 00000006010 15156711514 022535 0ustar00todd.rinaldostaff000000 000000 #!perl use strict; use warnings; use Test::More tests => 9; use Net::Jabber::Bot; # stuff for mock client object use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; # Test object my $bot_alias = 'make_test_bot'; my $server = 'talk.google.com'; my $personal_address = "test_user\@$server/$bot_alias"; my %forums_and_responses; my $forum1 = 'test_forum1'; $forums_and_responses{$forum1} = [ "jbot:", "" ]; ok( 1, "Creating Net::Jabber::Bot object for from parameter tests" ); 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, background_function => \&background_checks, loop_sleep_time => 5, process_timeout => 5, forums_and_responses => \%forums_and_responses, ignore_server_messages => 1, ignore_self_messages => 1, out_messages_per_second => 5, max_message_size => 1000, max_messages_per_hour => 100, forum_join_grace => 0, ); isa_ok( $bot, "Net::Jabber::Bot" ); # Track the last MessageSend args for verification my @last_message_send_args; { no warnings 'redefine'; my $original_message_send = \&Net::Jabber::Client::MessageSend; *Net::Jabber::Client::MessageSend = sub { my $self = shift; @last_message_send_args = @_; $original_message_send->( $self, @_ ); }; } # Test 1: SendPersonalMessage with from parameter @last_message_send_args = (); my $from_jid = 'original_sender@example.com/resource'; my $result = $bot->SendPersonalMessage( $personal_address, "Hello with from", $from_jid ); ok( !defined $result, "SendPersonalMessage with from param succeeds" ); { my %args = @last_message_send_args; is( $args{from}, $from_jid, "from parameter passed through in SendPersonalMessage" ); } # Test 2: SendPersonalMessage without from parameter (backwards compatible) @last_message_send_args = (); $result = $bot->SendPersonalMessage( $personal_address, "Hello without from" ); ok( !defined $result, "SendPersonalMessage without from param succeeds" ); { my %args = @last_message_send_args; ok( !exists $args{from}, "No from parameter when not specified" ); } # Test 3: SendGroupMessage with from parameter @last_message_send_args = (); $result = $bot->SendGroupMessage( $forum1, "Group hello with from", $from_jid ); ok( !defined $result, "SendGroupMessage with from param succeeds" ); { my %args = @last_message_send_args; is( $args{from}, $from_jid, "from parameter passed through in SendGroupMessage" ); } # Test 4: SendGroupMessage without from parameter (backwards compatible) @last_message_send_args = (); $result = $bot->SendGroupMessage( $forum1, "Group hello without from" ); ok( !defined $result, "SendGroupMessage without from param succeeds" ); exit; sub new_bot_message { } sub background_checks { } Net-Jabber-Bot-3.01/t/07-test_message_chunking.t000644 000765 000024 00000010015 15163523660 023045 0ustar00todd.rinaldostaff000000 000000 #!perl use strict; use warnings; use Test::More; use Net::Jabber::Bot; use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; BEGIN { *CORE::GLOBAL::sleep = sub { }; } my $max_size = 200; my $server = 'test.example.com'; my $recipient = "user\@$server/res"; my $bot = Net::Jabber::Bot->new({ server => $server, conference_server => "conference.$server", port => 5222, username => 'test', password => 'test', alias => 'testbot', message_function => sub {}, forums_and_responses => { test => ["bot:"] }, max_message_size => $max_size, max_messages_per_hour => 10000, safety_mode => 0, forum_join_grace => 0, }); sub send_and_get_chunks { my ($msg) = @_; @{$bot->jabber_client->{message_queue}} = (); $bot->SendPersonalMessage($recipient, $msg); return map { $_->GetBody() } @{$bot->jabber_client->{message_queue}}; } sub all_chunks_within_limit { my (@chunks) = @_; for my $chunk (@chunks) { return 0 if length($chunk) > $max_size; } return 1; } # Short message — no splitting { my @chunks = send_and_get_chunks("Hello world"); is(scalar @chunks, 1, "Short message: single chunk"); is($chunks[0], "Hello world", "Short message: content preserved"); } # Exact max_size — no splitting needed { my $msg = "x" x $max_size; my @chunks = send_and_get_chunks($msg); is(scalar @chunks, 1, "Exact max_size: single chunk"); is(length($chunks[0]), $max_size, "Exact max_size: correct length"); } # Newline at exactly position max_size (was the off-by-one trigger) { my $msg = "x" x $max_size . "\n" . "y" x 50; my @chunks = send_and_get_chunks($msg); ok(all_chunks_within_limit(@chunks), "Newline at boundary: all chunks <= max_size"); is(join('', @chunks), $msg, "Newline at boundary: content preserved"); } # Newline just inside max_size { my $msg = "x" x ($max_size - 1) . "\n" . "y" x 50; my @chunks = send_and_get_chunks($msg); ok(all_chunks_within_limit(@chunks), "Newline inside limit: all chunks <= max_size"); is(length($chunks[0]), $max_size, "Newline inside limit: first chunk uses full max_size"); is(join('', @chunks), $msg, "Newline inside limit: content preserved"); } # Space at exactly position max_size { my $msg = "x" x $max_size . " " . "y" x 50; my @chunks = send_and_get_chunks($msg); ok(all_chunks_within_limit(@chunks), "Space at boundary: all chunks <= max_size"); is(join('', @chunks), $msg, "Space at boundary: content preserved"); } # No natural break points — hard chop { my $msg = "x" x 500; my @chunks = send_and_get_chunks($msg); is(scalar @chunks, 3, "No breaks: 500 chars split into 3 chunks"); ok(all_chunks_within_limit(@chunks), "No breaks: all chunks <= max_size"); is(join('', @chunks), $msg, "No breaks: content preserved"); } # Multiple newlines — prefers breaking at last newline within window { my $msg = "a" x 80 . "\n" . "b" x 80 . "\n" . "c" x 80; my @chunks = send_and_get_chunks($msg); ok(all_chunks_within_limit(@chunks), "Multiple newlines: all chunks <= max_size"); is(join('', @chunks), $msg, "Multiple newlines: content preserved"); # First chunk should break at the second newline (position 161) like($chunks[0], qr/\n$/, "Multiple newlines: first chunk ends with newline"); } # Mixed spaces and newlines { my $msg = "word " x 60; # 300 chars my @chunks = send_and_get_chunks($msg); ok(all_chunks_within_limit(@chunks), "Space-separated words: all chunks <= max_size"); is(join('', @chunks), $msg, "Space-separated words: content preserved"); } # Empty message — no chunks produced (nothing to send) { my @chunks = send_and_get_chunks(""); is(scalar @chunks, 0, "Empty message: no chunks sent"); } # Single newline { my @chunks = send_and_get_chunks("\n"); is(scalar @chunks, 1, "Single newline: one chunk"); is($chunks[0], "\n", "Single newline: content preserved"); } done_testing(); Net-Jabber-Bot-3.01/t/07-test_set_forum_subject.t000644 000765 000024 00000006610 15164031674 023263 0ustar00todd.rinaldostaff000000 000000 #!perl use strict; use warnings; use Test::More tests => 13; use Net::Jabber::Bot; # Mock client use FindBin; use lib "$FindBin::Bin/lib"; use MockJabberClient; BEGIN { *CORE::GLOBAL::sleep = sub { }; } my $bot_alias = 'make_test_bot'; my $server = 'talk.google.com'; my $conf_server = "conference.$server"; my %forums_and_responses; my $forum1 = 'test_forum1'; $forums_and_responses{$forum1} = [ "jbot:", "" ]; my $bot = Net::Jabber::Bot->new( server => $server, conference_server => $conf_server, port => 5222, username => 'test_username', password => 'test_pass', alias => $bot_alias, message_function => sub { }, background_function => sub { }, loop_sleep_time => 5, process_timeout => 5, forums_and_responses => \%forums_and_responses, ignore_server_messages => 1, ignore_self_messages => 1, out_messages_per_second => 5, max_message_size => 200, max_messages_per_hour => 100, forum_join_grace => 0, ); isa_ok( $bot, "Net::Jabber::Bot" ); # Track MessageSend calls my @sent_messages; { no warnings 'redefine'; my $original = \&Net::Jabber::Client::MessageSend; *Net::Jabber::Client::MessageSend = sub { my $self = shift; my %args = @_; push @sent_messages, \%args; $original->( $self, @_ ); }; } # Use full conference JID as recipient (mirrors how real code works) my $forum_jid = "$forum1\@$conf_server"; # Test 1: Normal subject setting @sent_messages = (); my $result = $bot->SetForumSubject( $forum_jid, "New Topic" ); ok( !defined $result, "SetForumSubject returns undef on success" ); is( scalar @sent_messages, 1, "Exactly one message sent" ); is( $sent_messages[0]{type}, 'groupchat', "Message sent as groupchat" ); is( $sent_messages[0]{subject}, 'New Topic', "Subject field is set correctly" ); like( $sent_messages[0]{body}, qr/Setting subject to New Topic/, "Body describes the subject change" ); # Test 2: Subject exceeding max_message_size is rejected early @sent_messages = (); my $long_subject = 'X' x 201; # max_message_size is 200 (safety capped from constructor) $result = $bot->SetForumSubject( $forum_jid, $long_subject ); is( $result, "Subject is too long!", "Returns error for oversized subject" ); is( scalar @sent_messages, 0, "No message sent for oversized subject" ); # Test 3: Subject exactly at max_message_size succeeds @sent_messages = (); my $exact_subject = 'Y' x 200; $result = $bot->SetForumSubject( $forum_jid, $exact_subject ); ok( !defined $result, "Subject at exact max size succeeds" ); is( $sent_messages[0]{subject}, $exact_subject, "Full subject preserved at max size" ); # Test 4: Subject one char below max_message_size succeeds @sent_messages = (); my $under_subject = 'Z' x 199; $result = $bot->SetForumSubject( $forum_jid, $under_subject ); ok( !defined $result, "Subject one below max size succeeds" ); # Test 5: SetForumSubject while disconnected # _send_individual_message returns "Server is down.\n" when not connected, # and SetForumSubject propagates that return value. $bot->Disconnect(); @sent_messages = (); $result = $bot->SetForumSubject( $forum_jid, "Should fail" ); like( $result, qr/Server is down/, "Returns error string when disconnected" ); is( scalar @sent_messages, 0, "No message sent when disconnected" ); exit; Net-Jabber-Bot-3.01/t/lib/MockJabberClient.pm000644 000765 000024 00000012640 15164031674 022333 0ustar00todd.rinaldostaff000000 000000 package 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. # Package variables for simulating connection/process failures in tests our $connect_fail_remaining = 0; our $process_die_remaining = 0; our $process_return_undef_remaining = 0; 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; $self->{muc_nicks} = {}; $self->{subscription_log} = []; $self->{muc_join_log} = []; $self->{presence_send_log} = []; $self->{sent_messages_log} = []; $self->{roster_jids} = []; $self->{presence_db} = {}; return $self; } # Read from array of messages and pass them to the message functions. sub Process { my $self = shift; my $timeout = shift or 0; # Simulate Process() dying (e.g., XML parse error, socket exception) if ($process_die_remaining > 0) { $process_die_remaining--; die "Simulated connection error\n"; } # Simulate Process() returning undef (silent connection loss) if ($process_return_undef_remaining > 0) { $process_return_undef_remaining--; return undef; } return if(!$self->{is_connected}); # Return undef if we're not connected. foreach my $message (@{$self->{message_queue}}) { next if(!defined $self->{message_callback}); $self->{message_callback}->($self->{SESSION}->{id}, $message); } @{$self->{message_queue}} = (); return 1; # undef means we lost connection. } sub PresenceSend { my $self = shift; my %args = @_; push @{$self->{presence_send_log}}, \%args; } 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; if ($connect_fail_remaining > 0) { $connect_fail_remaining--; return undef; # Simulate connection failure. } return 1; # 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 = @_; # Log the raw send arguments for test inspection push @{$self->{sent_messages_log}}, { %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 # In MUC (groupchat), real XMPP servers use the room nickname as the # resource in the from JID, not the XMPP resource. Use the stored MUC # nick when available for groupchat messages to match real behavior. my $from_resource = $self->{resource}; if ( ($arg_hash{'type'} || '') eq 'groupchat' ) { my $room_jid = "$forum\@$server"; $from_resource = $self->{muc_nicks}{$room_jid} || $from_resource; } my $from = "$forum\@$server/$from_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 { my $self = shift; my %args = @_; push @{$self->{muc_join_log}}, \%args; # Track the MUC nickname for each room so MessageSend can use it # in the from JID, matching real XMPP MUC server behavior. my $room = $args{room} || ''; my $server = $args{server} || ''; my $nick = $args{nick} || $self->{resource}; $self->{muc_nicks}{"$room\@$server"} = $nick; } sub Disconnect { my $self = shift; $self->{is_connected} = 0; } sub Send {;} # Used for IQ. need to see if we need to put something here. sub Subscription { my $self = shift; my %args = @_; push @{$self->{subscription_log}}, \%args; } sub RosterGet {;} sub RosterDB {;} sub RosterRequest {;} sub RosterDBJIDs { my $self = shift; return @{$self->{roster_jids}}; } sub PresenceDB {;} sub PresenceDBParse { my $self = shift; my $presence = shift; # Store presence by from JID for later query my $from = $presence->GetFrom(); $self->{presence_db}{$from} = $presence if defined $from; } sub PresenceDBQuery { my $self = shift; my $jid = shift; return $self->{presence_db}{$jid}; } sub VersionSend { my $self = shift; my %args = @_; $self->{last_version_send} = \%args; } 1;