| 1 | use strict; |
|---|
| 2 | use warnings; |
|---|
| 3 | |
|---|
| 4 | package BarnOwl::Logging; |
|---|
| 5 | |
|---|
| 6 | =head1 BarnOwl::Logging |
|---|
| 7 | |
|---|
| 8 | =head1 DESCRIPTION |
|---|
| 9 | |
|---|
| 10 | C<BarnOwl::Logging> implements the internals of logging. All customizations |
|---|
| 11 | to logging should be done in the appropriate subclass of L<BarnOwl::Message>. |
|---|
| 12 | |
|---|
| 13 | =head2 USAGE |
|---|
| 14 | |
|---|
| 15 | Modules wishing to customize how messages are logged should override the |
|---|
| 16 | relevant subroutines in the appropriate subclass of L<BarnOwl::Message>. |
|---|
| 17 | |
|---|
| 18 | Modules wishing to log errors sending outgoing messages should call |
|---|
| 19 | L<BarnOwl::Logging::log_outgoing_error> with the message that failed |
|---|
| 20 | to be sent. |
|---|
| 21 | |
|---|
| 22 | =head2 EXPORTS |
|---|
| 23 | |
|---|
| 24 | None by default. |
|---|
| 25 | |
|---|
| 26 | =cut |
|---|
| 27 | |
|---|
| 28 | use Exporter; |
|---|
| 29 | |
|---|
| 30 | our @EXPORT_OK = qw(); |
|---|
| 31 | |
|---|
| 32 | our %EXPORT_TAGS = (all => [@EXPORT_OK]); |
|---|
| 33 | |
|---|
| 34 | use File::Spec; |
|---|
| 35 | |
|---|
| 36 | $BarnOwl::Hooks::newMessage->add("BarnOwl::Logging::log"); |
|---|
| 37 | |
|---|
| 38 | =head2 sanitize_filename BASE_PATH FILENAME |
|---|
| 39 | |
|---|
| 40 | Sanitizes C<FILENAME> and concatenates it with C<BASE_PATH>. |
|---|
| 41 | |
|---|
| 42 | In any filename, C<"/"> and any control characters (characters which |
|---|
| 43 | match C<[:cntrl:]> get replaced by underscores. If the resulting |
|---|
| 44 | filename is empty or equal to C<"."> or C<"..">, it is replaced with |
|---|
| 45 | C<"weird">. |
|---|
| 46 | |
|---|
| 47 | =cut |
|---|
| 48 | |
|---|
| 49 | sub sanitize_filename { |
|---|
| 50 | my $base_path = BarnOwl::Internal::makepath(shift); |
|---|
| 51 | my $filename = shift; |
|---|
| 52 | $filename =~ s/[[:cntrl:]\/]/_/g; |
|---|
| 53 | if ($filename eq '' || $filename eq '.' || $filename eq '..') { |
|---|
| 54 | $filename = 'weird'; |
|---|
| 55 | } |
|---|
| 56 | # The original C code also removed characters less than '!' and |
|---|
| 57 | # greater than or equal to '~', marked file names beginning with a |
|---|
| 58 | # non-alphanumeric or non-ASCII character as 'weird', and rejected |
|---|
| 59 | # filenames longer than 35 characters. |
|---|
| 60 | return File::Spec->catfile($base_path, $filename); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | =head2 get_filenames MESSAGE |
|---|
| 64 | |
|---|
| 65 | Returns a list of filenames in which to log the passed message. |
|---|
| 66 | |
|---|
| 67 | This method calls C<log_filenames> on C<MESSAGE> to determine the list |
|---|
| 68 | of filenames to which C<MESSAGE> gets logged. All filenames are |
|---|
| 69 | relative to C<MESSAGE->log_base_path>. If C<MESSAGE->log_to_all_file> |
|---|
| 70 | returns true, then the filename C<"all"> is appended to the list of |
|---|
| 71 | filenames. |
|---|
| 72 | |
|---|
| 73 | Filenames are sanitized by C<sanitize_filename>. |
|---|
| 74 | |
|---|
| 75 | =cut |
|---|
| 76 | |
|---|
| 77 | sub get_filenames { |
|---|
| 78 | my ($m) = @_; |
|---|
| 79 | my @filenames = $m->log_filenames; |
|---|
| 80 | push @filenames, 'all' if $m->log_to_all_file; |
|---|
| 81 | return map { sanitize_filename($m->log_base_path, $_) } @filenames; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | =head2 should_log_message MESSAGE |
|---|
| 85 | |
|---|
| 86 | Determines whether or not the passed message should be logged. |
|---|
| 87 | |
|---|
| 88 | To customize the behavior of this method, override |
|---|
| 89 | L<BarnOwl::Message::should_log>. |
|---|
| 90 | |
|---|
| 91 | =cut |
|---|
| 92 | |
|---|
| 93 | sub should_log_message { |
|---|
| 94 | my ($m) = @_; |
|---|
| 95 | # If there's a logfilter and this message matches it, log. |
|---|
| 96 | # pass quiet=1, because we don't care if the filter doesn't exist |
|---|
| 97 | return 1 if BarnOwl::message_matches_filter($m, BarnOwl::getvar('logfilter'), 1); |
|---|
| 98 | # otherwise we do things based on the logging variables |
|---|
| 99 | # skip login/logout messages if appropriate |
|---|
| 100 | return 0 if $m->is_loginout && BarnOwl::getvar('loglogins') eq 'off'; |
|---|
| 101 | # check direction |
|---|
| 102 | return 0 if $m->is_outgoing && BarnOwl::getvar('loggingdirection') eq 'in'; |
|---|
| 103 | return 0 if $m->is_incoming && BarnOwl::getvar('loggingdirection') eq 'out'; |
|---|
| 104 | return $m->should_log; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | =head2 log MESSAGE |
|---|
| 108 | |
|---|
| 109 | Call this method to (potentially) log a message. |
|---|
| 110 | |
|---|
| 111 | To customize the behavior of this method for your messages, override |
|---|
| 112 | L<BarnOwl::Message::log>, L<BarnOwl::Message::should_log>, |
|---|
| 113 | L<BarnOwl::Message::log_base_path>, and/or |
|---|
| 114 | L<BarnOwl::Message::log_filenames>. |
|---|
| 115 | |
|---|
| 116 | =cut |
|---|
| 117 | |
|---|
| 118 | sub log { |
|---|
| 119 | my ($m) = @_; |
|---|
| 120 | return unless defined $m; |
|---|
| 121 | return unless BarnOwl::Logging::should_log_message($m); |
|---|
| 122 | my $log_text = $m->log; |
|---|
| 123 | foreach my $filename (BarnOwl::Logging::get_filenames($m)) { |
|---|
| 124 | BarnOwl::Logging::enqueue_text($log_text, $filename); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | =head2 log_outgoing_error MESSAGE |
|---|
| 129 | |
|---|
| 130 | Call this method to (potentially) log an error in sending an |
|---|
| 131 | outgoing message. Errors get logged to the same file(s) as |
|---|
| 132 | successful messages. |
|---|
| 133 | |
|---|
| 134 | To customize the behavior of this method for your messages, override |
|---|
| 135 | L<BarnOwl::Message::log_outgoing_error>, |
|---|
| 136 | L<BarnOwl::Message::should_log>, |
|---|
| 137 | L<BarnOwl::Message::log_base_path>, and/or |
|---|
| 138 | L<BarnOwl::Message::log_filenames>. |
|---|
| 139 | |
|---|
| 140 | =cut |
|---|
| 141 | |
|---|
| 142 | sub log_outgoing_error { |
|---|
| 143 | my ($m) = @_; |
|---|
| 144 | return unless BarnOwl::Logging::should_log_message($m); |
|---|
| 145 | my $log_text = $m->log_outgoing_error; |
|---|
| 146 | foreach my $filename (BarnOwl::Logging::get_filenames($m)) { |
|---|
| 147 | BarnOwl::Logging::enqueue_text($log_text, $filename); |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | 1; |
|---|