Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use 5.012;
- use strict;
- use File::Copy;
- my $sourcedir="/var/spool/sms/incoming";
- my $destdir = "/home/pi/sms-inbox";
- my $movedir = "/home/pi/sms-inbox/source";
- my $maxsize = 1024 * 100; # refuse xml files bigger than 100 KB
- opendir(my $dir,$sourcedir);
- while(my $sourcename = readdir $dir) {
- $sourcename =~ /^\.+$/ and next;
- # ignore items which are not files (like dir or symlink)
- -f "$sourcedir/$sourcename" or next;
- my $filepath = "$sourcedir/$sourcename";
- my @stat = stat($filepath);
- $stat[7] > $maxsize and warn "ignoring $sourcename: bigger than $maxsize" and next;
- print "opening $filepath...\n";
- open(my $smsfile, "<$filepath") or warn "Error opening $sourcename" and next;
- my $from;
- my $sent;
- my $text="";
- my $textmode=0;
- while(<$smsfile>) {
- if($textmode) {
- $text .= $_;
- } else {
- /^From:\s+(.+)$/ and $from = $1;
- /^Sent:\s+(.+)$/ and $sent = $1;
- /^$/ and $textmode = 1;
- }
- }
- close($smsfile);
- if($from and $sent and $text) {
- my $date = $sent;
- $date =~ s/:/-/g;
- $date =~ s/ /_/g;
- my $destname = "${from}_${date}.csv";
- $text =~ s/"/\\"/;
- my $smsfilepath = "$destdir/$destname";
- open(my $outfile, ">$smsfilepath") or warn "cannot open $smsfilepath: $!" and next;
- print $outfile "\"$from\",\"$sent\",\"$text\"";
- close $outfile;
- move($filepath, "$movedir/$sourcename" ) or warn "Error moving $filepath to $movedir: $!";
- #unlink $filepath or warn "Error deleting $filepath: $!";
- }
- }
- closedir($dir);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement