#!/usr/bin/env python3
import socket, ssl, os, io, time, sys
address=''
port=993
user=''
password=''
encoding='utf-8'
count=0
latest_recent_time=time.time()
write_file="/home/jusss/lab/mail.log"
a_socket = socket.socket()
ssl_socket = ssl.wrap_socket(a_socket)
try:
ssl_socket.connect((address,port))
except socket.gaierror as e:
log=open(write_file,'a')
log.write(e.__str__())
log.close()
time.sleep(600)
os.system("/home/jusss/lab/mail-notify-1.py")
os._exit()
ssl_socket.write(('a_tag login ' + user + ' ' + password + '\r\n').encode(encoding))
ssl_socket.write('a_tag select inbox\r\n'.encode(encoding))
ssl_socket.write('a_tag idle\r\n'.encode(encoding))
while True:
ssl_socket.settimeout(180)
try:
recv_msg=ssl_socket.read().decode(encoding)[:-2]
except socket.timeout as e:
log1=open(write_file,'a')
log1.write(e.__str__())
log1.close()
time.sleep(300)
os.system("/home/jusss/lab/mail-notify-1.py")
os._exit()
# print(recv_msg)
if recv_msg.find('RECENT') > -1:
if count > 0:
os.system("mplayer -noconsolecontrols -really-quiet /home/jusss/sounds/new-email.mp3 2>/dev/null")
latest_recent_time=time.time()
else:
count=count+1
if time.time() - latest_recent_time > 600:
latest_recent_time=time.time()
ssl_socket.write('done\r\n'.encode(encoding))
recv_msg=ssl_socket.read().decode(encoding)[:-2]
# print(recv_msg)
ssl_socket.write('a_tag status inbox (unseen)\r\n'.encode(encoding))
recv_msg=ssl_socket.read().decode(encoding)[:-2]
# print(recv_msg)
ssl_socket.write('a_tag idle\r\n'.encode(encoding))
if recv_msg[recv_msg.find('UNSEEN')+7] != '0':
os.system("mplayer -noconsolecontrols -really-quiet /home/jusss/sounds/new-email.mp3 2>/dev/null")
# ssl_socket.write('a_tag status inbox (unseen)\r\n'.encode(encoding))
# * STATUS "inbox" (UNSEEN 0)
# idle need to be end with 'done'
[jusss@localhost lab]$ ps ax|grep mail
8162 pts/0 S 0:00 python3 ./mail-notify-0.py
8163 pts/0 S 0:00 python3 /home/jusss/lab/mail-notify-1.py
8165 pts/0 S+ 0:00 grep mail
[jusss@localhost lab]$ ps ax|grep mail
8162 pts/0 S 0:00 python3 ./mail-notify-0.py
8163 pts/0 S 0:00 python3 /home/jusss/lab/mail-notify-1.py
8167 pts/0 S+ 0:00 grep mail
[jusss@localhost lab]$ ps ax|grep mail
8162 pts/0 S 0:00 python3 ./mail-notify-0.py
8163 pts/0 S 0:00 python3 /home/jusss/lab/mail-notify-1.py
8168 pts/0 S 0:00 python3 /home/jusss/lab/mail-notify-0.py
8170 pts/0 S+ 0:00 grep mail
[jusss@localhost lab]$ ps ax|grep mail
8162 pts/0 S 0:00 python3 ./mail-notify-0.py
8163 pts/0 S 0:00 python3 /home/jusss/lab/mail-notify-1.py
8168 pts/0 S 0:00 python3 /home/jusss/lab/mail-notify-0.py
8171 pts/0 S 0:00 python3 /home/jusss/lab/mail-notify-1.py
8173 pts/0 S+ 0:00 grep mail