-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.py
99 lines (84 loc) · 2.57 KB
/
check.py
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
import glob
import re
import os
import sys
from subprocess import call
from subprocess import check_call
logText = ""
success = True
call(["getmail",])
newmails = glob.glob("/home/pi/scanMail/new/*.nodepi")
scandir = "/home/pi/nodescan/"
for mail in newmails:
try:
# get sender
with open(mail, 'r') as f:
first_line = f.readline()
sender = re.search("<([\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4})>", first_line).group(1)
logText += "Successful parse:\n"
logText += sender + "/n"
except:
logText += "Error in parsing %s\n" % mail
success = False
break
try:
# get image
cmd = "munpack -C /home/pi/nodescan " + mail
os.system(cmd)
logText += "Succesfull outputting image munpack \n"
except:
logText += "Error in outputting image munpack \n"
success = False
break
images = glob.glob("/home/pi/nodescan/*.jpg") + glob.glob("/home/pi/nodescan/*.JPG")
# if image then process
if len(images) > 0:
try:
check_call(["python", scandir + "scan.py", "--image", images[0]])
logText += "Successfully used scan.py \n"
except:
logText += "Problem with scan.py \n"
success = False
break
try:
# send back the ok message with the file attached
cmd = "mutt -s 'Voila ton scan !' -i /home/pi/nodescan/ok_msg.txt " + sender + " -a /home/pi/*.pdf < /dev/null"
os.system(cmd)
logText += "Successfully send the scaned image \n")
except:
logText += "Problem sending mail \n")
success = False
break
else:
# there is no image to process
# send back the "no image attached" error message
cmd = "mutt -s 'Euh, elle est ou ton image ?' -i /home/pi/nodescan/error_empty_msg.txt " + sender + " < /dev/null"
os.system(cmd)
logText += "no image in mail \n")
success = False
break
try:
# clean
os.remove(mail)
os.system("rm -f /home/pi/nodescan/*.JPG /home/pi/nodescan/*.jpg /home/pi/nodescan/*.desc /home/pi/*.pdf")
os.system("rm -f /home/pi/scanMail/sent/cur/*")
logText += "Successfully cleaned files \n"
except:
logText += "Problem in cleaning files \n"
success = False
break
if success != False:
# the image was not correctly processed
# send back the "file not processed" error message
tmp = open("tmp.txt", "w")
tmp.write(logText)
tmp.close()
cmd = "mutt -s 'Problem de scan...' -i /home/pi/nodescan/tmp.txt " + sender + " < /dev/null"
os.system(cmd)
# clean
os.remove(mail)
os.system("rm -f /home/pi/nodescan/*.JPG /home/pi/nodescan/*.jpg /home/pi/nodescan/*.desc /home/pi/*.pdf")
os.system("rm -f /home/pi/scanMail/sent/cur/*")
logs = open("log.txt", "a")
logs.write(logText)
logs.close()