diff --git a/doc/CHANGELOG b/doc/CHANGELOG index eef6da1..de3ebd9 100644 --- a/doc/CHANGELOG +++ b/doc/CHANGELOG @@ -3,6 +3,7 @@ Auto IRC Bot 3.0: Change Log 3.0 Indev =============================================================================== + * Added slog() to API::Log for logging to IRC. * Added a Greet module for greeting users on join. * Added PostgreSQL support. Official modules do not support this yet. * database:format now a required configuration value. diff --git a/etc/example.conf b/etc/example.conf index 254dbdc..3423426 100644 --- a/etc/example.conf +++ b/etc/example.conf @@ -107,6 +107,9 @@ database { # Locale. locale "en_US"; +# Logging to IRC. Uncomment this and set it to setup an IRC logchan. +# logchan "Freenode/#logchan"; + # Expire logs in days. 0 disables. expire_logs 15; diff --git a/lib/API/Log.pm b/lib/API/Log.pm index 37ae18a..94bef92 100644 --- a/lib/API/Log.pm +++ b/lib/API/Log.pm @@ -11,7 +11,7 @@ use Exporter; use base qw(Exporter); use API::Std qw(conf_get); -our @EXPORT_OK = qw(println dbug alog); +our @EXPORT_OK = qw(println dbug alog slog); # Print with the system newline appended. @@ -107,6 +107,39 @@ sub expire_logs return 1; } +# Subroutine for logging to an IRC logchan. +sub slog +{ + my ($msg) = @_; + + # Check if logging to channel is enabled. + if (conf_get('logchan')) { + # It is, continue. + + # Split the network and channel. + my ($net, $chan) = split '/', (conf_get('logchan'))[0][0]; + $chan = lc $chan; + + # Check if we're connected to the network. + if (!defined $Auto::SOCKET{$net}) { + dbug 'WARNING: slog(): Unable to log to IRC: Not connected to network.'; + alog 'WARNING: slog(): Unable to log to IRC: Not connected to network.'; + return; + } + + # Check if we're in the channel. + if (!defined $Parser::IRC::botchans{$net}{$chan}) { + dbug 'WARNING: slog(): Unable to log to IRC: Not in channel.'; + alog 'WARNING: slog(): Unable to log to IRC: Not in channel.'; + return; + } + + # Log to IRC. + API::IRC::privmsg($net, $chan, "\002LOG:\002 $msg"); + } + + return 1; +} 1; # vim: set ai sw=4 ts=4: