Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions lib/Net/Jabber/Bot.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,18 @@ sub _send_individual_message {
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];

Expand All @@ -1050,16 +1062,6 @@ sub _send_individual_message {
return "Too many messages ($messages_this_hour)\n";
}

if ( !$self->IsConnected ) {
$subject = "" if ( !defined $subject ); # Keep warning messages quiet.
$message_chunk = "" if ( !defined $message_chunk ); # Keep warning messages quiet.

ERROR( "Can't send: Jabber server is down. Tried to send: \n" . "To: $recipient\n" . "Subject: $subject\n" . "Type: $message_type\n" . "Message sent:\n" . "$message_chunk" );

# Send 1 panic message out to jabber if this is our last message before quieting down.
return "Server is down.\n";
}

# Strip out anything that's not a printable character except new line, we want to be able to send multiline message, aren't we?
# Now with unicode support?
$message_chunk =~ s/[^\r\n[:print:]]+/./xmsg;
Expand Down
63 changes: 63 additions & 0 deletions t/07-test_disconnect_message_count.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!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" );
Loading