source: perl/lib/BarnOwl/Logging.pm @ dca6255

Last change on this file since dca6255 was dca6255, checked in by Jason Gross <jgross@mit.edu>, 8 years ago
Moved log file name generation to perl I don't think that the class/personal distinction is the best for general protocols, but I don't know what should replace it. I've made class-logging default to only zephyr (a slight change from previous behavior, where jabber MUC's would be logged to $classlogpath, as well as all non-private non-login messages from other protocols), but made logging path generation overridable. TODO: Decide whether or not to filter out more 'bad' characters. Perhaps we should remove '!' because it indicates history in some shells and makes things obnoxious, or '~' becase it indicates homedirs in many shells. * '/' is for separating directories, and we don't want to accidentally make subdirectories We first NFKC for zephyrs, and then apply lc. The zephyr servers apply case-folded NFKC (so says http://zephyr.1ts.org/browser/zephyr/server/zstring.c). We should probably use Unicode::CaseFold instead of lc. I'm also not sure what the order case-adjustment and normalization should be. We first NFKC, then apply lc, to jabbers, as per http://xmpp.org/internet-drafts/attic/draft-ietf-xmpp-nodeprep-03.html (though I can't actually find anything that specifies the case-folding algorithm, nor the ordering). We now use lc instead of g_utf8_strdown to normalize AIM screennames.
  • Property mode set to 100644
File size: 2.3 KB
Line 
1use strict;
2use warnings;
3
4package BarnOwl::Logging;
5
6=head1 BarnOwl::Logging
7
8=head1 DESCRIPTION
9
10C<BarnOwl::Logging> implements the internals of logging.  All customizations
11to logging should be done in the appropriate subclass of L<BarnOwl::Message>.
12
13=head2 USAGE
14
15Modules wishing to customize how messages are logged should override the
16relevant subroutines in the appropriate subclass of L<BarnOwl::Message>.
17
18=head2 EXPORTS
19
20None by default.
21
22=cut
23
24use Exporter;
25
26our @EXPORT_OK = qw();
27
28our %EXPORT_TAGS = (all => [@EXPORT_OK]);
29
30use File::Spec;
31
32=head2 sanitize_filename BASE_PATH FILENAME
33
34Sanitizes C<FILENAME> and concatenates it with C<BASE_PATH>.
35
36In any filename, C<"/"> and any control characters (characters which
37match C<[:cntrl:]> get replaced by underscores.  If the resulting
38filename is empty or equal to C<"."> or C<"..">, it is replaced with
39C<"weird">.
40
41=cut
42
43sub sanitize_filename {
44    my $base_path = BarnOwl::Internal::makepath(shift);
45    my $filename = shift;
46    $filename =~ s/[[:cntrl:]\/]/_/g;
47    if ($filename eq '' || $filename eq '.' || $filename eq '..') {
48        $filename = 'weird';
49    }
50    # The original C code also removed characters less than '!' and
51    # greater than or equal to '~', marked file names beginning with a
52    # non-alphanumeric or non-ASCII character as 'weird', and rejected
53    # filenames longer than 35 characters.
54    return File::Spec->catfile($base_path, $filename);
55}
56
57=head2 get_filenames MESSAGE
58
59Returns a list of filenames in which to log the passed message.
60
61This method calls C<log_filenames> on C<MESSAGE> to determine the list
62of filenames to which C<MESSAGE> gets logged.  All filenames are
63relative to C<MESSAGE->log_base_path>.  If C<MESSAGE->log_to_all_file>
64returns true, then the filename C<"all"> is appended to the list of
65filenames.
66
67Filenames are sanitized by C<sanitize_filename>.
68
69=cut
70
71sub get_filenames {
72    my ($m) = @_;
73    my @filenames = $m->log_filenames;
74    push @filenames, 'all' if $m->log_to_all_file;
75    return map { sanitize_filename($m->log_base_path, $_) } @filenames;
76}
77
78# For ease of use in C
79sub get_filenames_as_string {
80    my @rtn;
81    foreach my $filename (BarnOwl::Logging::get_filenames(@_)) {
82        $filename =~ s/\n/_/g;
83        push @rtn, $filename;
84    }
85    return join("\n", @rtn);
86}
87
881;
Note: See TracBrowser for help on using the repository browser.