source: repository/lib/Metabrik/File/Text.pm

Last change on this file was 895:4f5647eb9e58, checked in by GomoR <gomor@…>, 8 months ago
  • UPDATE: forensic::sysmon: every ps* Command now output a source and a target ARRAY
  • bugfix: forensic::sysmon: correctly escapes chars on building the .csv whitelisting state
  • new: forensic::sysmon: list_file_created_processes Command
  • new: remote::sysmon: redeploy Command
  • update: string::javascript: catch alert and ActiveXObject functions from deobfuscation Command
  • bugfix: antivirus::clamav: make update works
  • update: client::elasticsearch: show_indices Command to support an additional Arg to display some indices only (use string as a regex)
  • optimization: client::elasticsearch: import_from_csv Command to use pre-compiled regexes
  • bugfix: client::elasticsearch: set default snapshot and repository names for snapshot Commands and allow to use an index string instead of an ARRAY as Arg
  • new: client::imap: total, read_next_with_subject and parse Commands
  • update: client::tcpdump: added datadir Attribute so output pcap are put in the right place (i/o / directory)
  • bugfix: email::message: parse Command to return email headers too
  • bugfix: file::base64: use ascii encoding by default
  • new: file::base64: decode_from_string and encode_from_string Commands
  • update: forensic::pcap: more messages and statistics on importing a pcap to ES
File size: 3.5 KB
Line 
1#
2# $Id$
3#
4# file::text Brik
5#
6package Metabrik::File::Text;
7use strict;
8use warnings;
9
10use base qw(Metabrik::File::Write);
11
12sub brik_properties {
13   return {
14      revision => '$Revision$',
15      tags => [ qw(unstable read write) ],
16      author => 'GomoR <GomoR[at]metabrik.org>',
17      license => 'http://opensource.org/licenses/BSD-3-Clause',
18      attributes => {
19         input => [ qw(file) ],
20         output => [ qw(file) ],
21         as_array => [ qw(0|1) ],
22         strip_crlf => [ qw(0|1) ],
23         encoding => [ qw(utf8|ascii) ],  # Inherited
24         _fr => [ qw(INTERNAL) ],
25      },
26      # encoding: see `perldoc Encode::Supported' for other types
27      attributes_default => {
28         encoding => 'utf8',
29         as_array => 0,
30         strip_crlf => 0,
31      },
32      commands => {
33         read => [ qw(input) ],
34         read_line => [ qw(input count|OPTIONAL) ],
35         read_split_by_blank_line => [ qw(input) ],
36         write => [ qw($data|$data_ref|$data_list output) ],
37      },
38      require_modules => {
39         'Metabrik::File::Read' => [ ],
40      },
41   };
42}
43
44sub _open {
45   my $self = shift;
46   my ($input) = @_;
47
48   my $fr = Metabrik::File::Read->new_from_brik_init($self) or return;
49   $fr->input($input);
50   $fr->encoding($self->encoding);
51   $fr->as_array($self->as_array);
52   $fr->strip_crlf($self->strip_crlf);
53
54   $fr->open or return;
55
56   return $fr;
57}
58
59#
60# Read everything available
61#
62sub read {
63   my $self = shift;
64   my ($input) = @_;
65
66   $input ||= $self->input;
67   $self->brik_help_run_undef_arg('read', $input) or return;
68
69   my $fr = $self->_open($input) or return;
70   my $data = $fr->read or return;
71   $fr->close;
72
73   return $data;
74}
75
76#
77# Just return next available line
78#
79sub read_line {
80   my $self = shift;
81   my ($input, $count) = @_;
82
83   $input ||= $self->input;
84   $count ||= 1;
85   $self->brik_help_run_undef_arg('read_line', $input) or return;
86   $self->brik_help_run_file_not_found('read_line', $input) or return;
87
88   my $fr = $self->_fr;
89   if (! $fr) {
90      $fr = $self->_open($input) or return;
91      $self->_fr($fr);
92   }
93
94   if ($fr->eof) {
95      $fr->close;
96      $self->_fr(undef);
97      return 0;
98   }
99
100   my $data;
101   my @lines = ();
102   if ($count > 1) {
103      for (1..$count) {
104         $data = $fr->read_line;
105         push @lines, $data;
106      }
107   }
108   else {
109      $data = $fr->read_line;
110   }
111
112   return $count > 1 ? \@lines : $data;
113}
114
115#
116# Will read everything until eof
117#
118sub read_split_by_blank_line {
119   my $self = shift;
120   my ($input) = @_;
121
122   $input ||= $self->input;
123   $self->brik_help_run_undef_arg('read_split_by_blank_line', $input) or return;
124
125   my $fr = $self->_open($input) or return;
126
127   my @chunks = ();
128   while (my $this = $fr->read_until_blank_line) {
129      push @chunks, $this;
130      last if $fr->eof;
131   }
132
133   $fr->close;
134
135   return \@chunks;
136}
137
138sub write {
139   my $self = shift;
140   my ($data, $output) = @_;
141
142   $output ||= $self->output;
143   $self->brik_help_run_undef_arg('write', $data) or return;
144   $self->brik_help_run_undef_arg('write', $output) or return;
145
146   $self->open($output) or return;
147   # We check definedness because if we write 0 byte write will return 0
148   my $r = $self->SUPER::write($data);
149   if (! defined($r)) {
150      return;
151   }
152   $self->close;
153
154   return 1;
155}
156
1571;
158
159__END__
160
161=head1 NAME
162
163Metabrik::File::Text - file::text Brik
164
165=head1 COPYRIGHT AND LICENSE
166
167Copyright (c) 2014-2017, Patrice E<lt>GomoRE<gt> Auffret
168
169You may distribute this module under the terms of The BSD 3-Clause License.
170See LICENSE file in the source distribution archive.
171
172=head1 AUTHOR
173
174Patrice E<lt>GomoRE<gt> Auffret
175
176=cut
Note: See TracBrowser for help on using the repository browser.