-
Notifications
You must be signed in to change notification settings - Fork 5
/
backup-comm-n9e
executable file
·160 lines (135 loc) · 3.74 KB
/
backup-comm-n9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/perl
use strict;
use warnings;
use Date::Format;
use Date::Parse;
my $baseDir = "/home/user/MyDocs";
my $usage = "
Usage: $0 sms|call [file]
backup texts or calls to file, or to a file in default backup dir
uses n9Export and develsh
";
sub convertSmsFile($$);
sub convertSms($@);
sub convertCallFile($$);
sub convertCall($);
sub formatDate($);
sub run(@);
sub main(@){
if(`whoami` eq "root\n"){
exec "su", "user", "-c", "source /etc/profile; $0 @_";
}
my $type = shift() || '';
my $file = shift();
die $usage if $type !~ /^(sms|call)$/ or @_ > 0;
my $destDir = "$baseDir/backup-$type";
run "mkdir", "-p", $destDir;
my $nowS = `date +%s`;
chomp $nowS;
my $tmpRes = "$destDir/n9Export-$type-$nowS-res";
my @cmd = ("n9Export");
push @cmd, "-c" if $type =~ /^(call)$/;
push @cmd, $tmpRes;
open FH, "-|", "develsh -c '@cmd' 2>&1";
my $dotsAbove = 0;
$| = 1; #autoflush
while(<FH>){
my $line = $_;
if($line =~ /Not cleaning up obsolete resources/){
print ".";
$dotsAbove = 1;
}elsif($line =~ /The task queue's background thread stalled/i){
die "ugh libqtcontacts\nthread stall error\n";
}else{
print "\n" if $dotsAbove;
print $line;
$dotsAbove = 0;
}
}
close FH;
die "\"@cmd\" failed\n" if $? != 0;
if(not defined $file){
my $name = `date +%Y_%m_%d-%s`;
chomp $name;
$file = "$destDir/$name.$type";
}
convertSmsFile $tmpRes, $file if $type =~ /sms/;
convertCallFile $tmpRes, $file if $type =~ /call/;
die "Error: comm file does not exist: $file\n" if not -f $file;
}
sub convertSmsFile($$){
my ($srcFile, $destFile) = @_;
my @lines = `cat $srcFile`;
my @sms;
my $smsLine = undef;
my @extraLines;
for my $line(@lines){
my $dateFmt = '\d+-\d+-\d+T\d+:\d+:\d+Z';
if($line =~ /^[^;]*;(IN|OUT);$dateFmt;$dateFmt;/){
push @sms, convertSms $smsLine, @extraLines if defined $smsLine;
$smsLine = $line;
@extraLines = ();
}elsif($line =~ /^ /){
push @extraLines, $line;
}else{
die "malformed sms line: $line\n";
}
}
push @sms, convertSms $smsLine, @extraLines if defined $smsLine;
open FH, "> $destFile" or die "Could not write to $destFile\n";
print FH @sms;
close FH;
}
sub convertSms($@){
my ($smsLine, @extraLines) = @_;
if($smsLine =~ /^([^;]*);(IN|OUT);([^;]*);([^;]*);(.*)/){
my ($number, $inOut, $date, $otherDate, $msg) = ($1, $2, $3, $4, $5);
chomp $msg;
chomp foreach @extraLines;
s/^ // foreach @extraLines;
$msg = join "\n", ($msg, @extraLines);
$msg =~ s/"/""/g;
$msg = "\"$msg\"";
my $type = $inOut =~ /IN/ ? "1" : "2";
my $newDate = formatDate $date;
return "$number,$type,$newDate,$msg\n";
}else{
die "malformed n9Export line: $smsLine";
}
}
sub convertCallFile($$){
my ($srcFile, $destFile) = @_;
my @lines = `cat $srcFile`;
my @calls;
for my $line(@lines){
push @calls, convertCall $line;
}
open FH, "> $destFile" or die "Could not write to $destFile\n";
print FH @calls;
close FH;
}
sub convertCall($){
my $oldLine = shift;
if($oldLine =~ /^(.*);(IN|OUT);(OK|MISSED);(.*);(.*)/){
my ($number, $inOut, $status, $start, $end) = ($1, $2, $3, $4, $5);
my $type = $inOut =~ /IN/ ? "1" : "0";
$type = "2" if $status =~ /MISSED/;
my $newStart = formatDate $start;
my $newEnd = formatDate $end;
my $prefix = "/org/freedesktop/Telepathy/Account/ring/tel/ring";
return "$prefix,$number,$type,$newStart,$newEnd\n";
}else{
die "malformed n9Export line: $oldLine";
}
}
sub formatDate($){
my $date = shift;
my $time = str2time $date;
my @localtime = localtime($time);
return strftime "%Y-%m-%d %H:%M:%S", @localtime;
}
sub run(@){
print "@_\n";
system @_;
}
&main(@ARGV);