#!/usr/bin/perl
use Mail::IMAPClient;
my @lines = `gnokii --getsms ME 0 100`;
my $msg_nr = 0;
my $msg_status = "gelesen";
my $msg_text = "";
my @messages = ();
my %message = ();
# parse all messages
foreach (@lines) {
chomp;
if (/^(\d). Eingehende Nachricht \((.*?)\)/) {
if (exists($message{'status'})) {
my %m = %message;
push(@messages, \%m);
}
undef(%message);
%message = ();
$msg_nr = $1;
$msg_status = $2;
$msg_text = "";
$message{'status'} = $msg_status;
} elsif (/^Date\/time: (.*?)$/) {
$msg_date = $1;
$message{'datetime'} = $msg_date;
} elsif (/^Absender: (.*?) SMS Center: (.*?)/) {
$msg_sender = $1;
$message{'sender'} = $msg_sender;
} elsif (/^Text:/) {
} else {
$msg_text .= $_;
$message{'text'} = $msg_text;
}
}
if (exists($message{'status'})) {
my %m = %message;
push(@messages, \%m);
#print "push ".%message."\n\n";
}
$imap = Mail::IMAPClient->new( Server => 'my.impapserver.com:143',
User => ' myuser',
Password => 'mypass')
# module uses eval, so we use $@ instead of $!
or die "IMAP Failure: $@";
#print "\nFolders: " . join(", ",$imap->folders),".\n";
# save unread messages to IMAP
foreach (@messages) {
if ($$_{'status'} eq 'unread') {
my $header_datetime = $$_{'datetime'};
$header_datetime =~ s/^(\d\d)\/(\d\d)/$2\/$1/;
my $mail = <<MAIL;
To: mail@adress.here
From: mail@adress.here
Subject: SMS von $$_{'sender'}
Date: $header_datetime;
Content-Type: text/plain; charset="iso-8859-1"
$$_{'text'}
$imap->append_string("INBOX", $mail) or die "Could not append_string: $@\n";;
}
}
$imap->logout();
2008-12-14
This little Skript saves unread short text messages from a connected phone to an IMAP folder. The script connects to the phone via gnokii, an incredible piece of software to access mobile phones over a serial connection like USB or Bluetooth. So you have to setup gnokii first and see wether the "gnokii --getsms" command is working. The script only checks the first 100 slots of the SMS memory, no deletion is done, you will have to do it manually or increase the value in the script. I parse for german strings in the gnokii output, so maybe you will have to adapt the strings in the script to your phones language. Then change the values of the parameters for the URL, user and password of the IMAP server and add it to your cron. No need to carry around the phone any more (ok, except for calling someone...). It works for my BenQ EF81, but not for recent Nokia Series40 or Series60 models, unfortunately, as they lack the AT commands to read the SMS memory. Anyway, here's the script:
#!/usr/bin/perl
use Mail::IMAPClient;
my @lines = `gnokii --getsms ME 0 100`;
my $msg_nr = 0;
my $msg_status = "gelesen";
my $msg_text = "";
my @messages = ();
my %message = ();
# parse all messages
foreach (@lines) {
chomp;
if (/^(\d). Eingehende Nachricht \((.*?)\)/) {
if (exists($message{'status'})) {
my %m = %message;
push(@messages, \%m);
}
undef(%message);
%message = ();
$msg_nr = $1;
$msg_status = $2;
$msg_text = "";
$message{'status'} = $msg_status;
} elsif (/^Date\/time: (.*?)$/) {
$msg_date = $1;
$message{'datetime'} = $msg_date;
} elsif (/^Absender: (.*?) SMS Center: (.*?)/) {
$msg_sender = $1;
$message{'sender'} = $msg_sender;
} elsif (/^Text:/) {
} else {
$msg_text .= $_;
$message{'text'} = $msg_text;
}
}
if (exists($message{'status'})) {
my %m = %message;
push(@messages, \%m);
#print "push ".%message."\n\n";
}
$imap = Mail::IMAPClient->new( Server => 'my.impapserver.com:143',
User => ' myuser',
Password => 'mypass')
# module uses eval, so we use $@ instead of $!
or die "IMAP Failure: $@";
#print "\nFolders: " . join(", ",$imap->folders),".\n";
# save unread messages to IMAP
foreach (@messages) {
if ($$_{'status'} eq 'unread') {
my $header_datetime = $$_{'datetime'};
$header_datetime =~ s/^(\d\d)\/(\d\d)/$2\/$1/;
my $mail = <<MAIL;
To: mail@adress.here
From: mail@adress.here
Subject: SMS von $$_{'sender'}
Date: $header_datetime;
Content-Type: text/plain; charset="iso-8859-1"
$$_{'text'}
$imap->append_string("INBOX", $mail) or die "Could not append_string: $@\n";;
}
}
$imap->logout();
Save SMS to IMAP via gnokii using Perl
Save SMS to IMAP via gnokii using Perl
This little Skript saves unread short text messages from a connected phone to an IMAP folder. The script connects to the phone via gnokii, an incredible piece of software to access mobile phones over a serial connection like USB or Bluetooth. So you have to setup gnokii first and see wether the "gnokii --getsms" command is working. The script only checks the first 100 slots of the SMS memory, no deletion is done, you will have to do it manually or increase the value in the script. I parse for german strings in the gnokii output, so maybe you will have to adapt the strings in the script to your phones language. Then change the values of the parameters for the URL, user and password of the IMAP server and add it to your cron. No need to carry around the phone any more (ok, except for calling someone...). It works for my BenQ EF81, but not for recent Nokia Series40 or Series60 models, unfortunately, as they lack the AT commands to read the SMS memory. Anyway, here's the script:
Labels:
Mobile,
Perl,
Programmieren,
Technik
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment