693e21c24513761a928368c9cb72918330aa1e6a
[mir.git] / dbscripts / smsreceiver.pl
1 #!/usr/bin/perl
2
3 use DBI;
4
5 $0=~m#[^/]+$#;
6 $cmdpath=$`;
7 if ($cmdpath eq "") { $cmdpath="./"; }
8
9 unshift(@INC,$cmdpath."lib");
10 require "property.pl";
11 require "log.pl";
12
13 ### configure here
14
15 $config_file=$cmdpath."smsreceiver.config";
16
17 ### no configuration beyond this line
18
19 $props=&Property::read($config_file);
20
21 ### connect logfile
22
23 tie *LOGFILE, 'Logfile', $$props{'public'}{'logfile'}, 'SMSRECEIVER';
24
25 print LOGFILE "Connecting to ".$$props{'db'}{'dsn'}." as user ".$$props{'db'}{'user'}."\n";
26
27
28 ### connect to db
29 $dbh=DBI->connect($$props{'db'}{'dsn'},$$props{'db'}{'user'},$$props{'db'}{'passwd'});
30 if (!defined $dbh) {
31         print LOGFILE "Error while connecting DB\n";
32         exit(0);
33         }
34 $table=$$props{'mail'}{'table'};        
35
36 ### prepare
37 $mo_insert_q="insert into $table (produced,type_sms,message,created) values (?,?,?,NOW())";
38 $mo_insert_sth=$dbh->prepare($mo_insert_q);
39
40 $headermode=1;
41 while (<>) {
42         if($headermode==1) {
43                 if (/^Subject: (.*)/i) {
44                         $subject=$1;
45                         }
46                 if (/^From: (.*)/i) {
47                         $from = $1;
48                         }
49                 if (/^Content-type:\s+(.*)/i) {
50                         $content_type=$1;
51                         }
52                 if (/^$/) {
53                         $headermode=0;
54                         }             
55     }
56         else {
57           push(@contentdata,$_);
58         }
59 }
60
61 ### filter out mime-crap
62
63 if ((defined $content_type)&&($content_type!~m#^text/plain#i)) {
64         print LOGFILE "Got mime-mail: $from ($subject) ".localtime(time)."\n";
65         print LOGFILE "Content: ".join('',@contentdata)."\n";
66         exit();
67         }
68
69 ### try to identify sms
70
71 if ($from=~/^\+?\d+\@/) {
72         ### coming from an emailaccount with a telefon number as user-part in From:
73         
74         ### sms is in first line of body
75         $text=shift(@contentdata);
76         
77         $type=1;
78         }
79 else {
80         $text=join('',@contentdata);
81         $type=2;
82         }
83
84 print LOGFILE "$type: $text\n";
85 $mo_insert_sth->execute(0,$type,$text);