forked from googleprojectzero/SkCodecFuzzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmms.py
82 lines (60 loc) · 2.27 KB
/
mms.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
import datetime
import glob
import logging
import os
import re
import requests
import time
class MmsClient:
def __init__(self, hostname, username, password, mms_in_dir):
self.hostname = hostname
self.username = username
self.password = password
self.mms_in_dir = mms_in_dir
# Disable non-essential logging messages from the HTTP-related modules.
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
def Send(self, phone_number, files, subject=None, text=None,
receipt_wait_time=-1):
formatted_files = []
for attachment in files:
formatted_files.append(("MMSFile", attachment))
# Set the expiry date to the night of the following day.
tomorrow = datetime.date.today() + datetime.timedelta(days = 1)
tomorrow = tomorrow.strftime("%d-%b-%Y")
http_data = {"PhoneNumber" : phone_number,
"MMSHEADERS" : ("X-Mms-Expiry: %s 03:00:00\n" % tomorrow) +
"X-Mms-Priority: low"}
if subject != None:
http_data["MMSSubject"] = subject
if text != None:
http_data["MMSText"] = text
if receipt_wait_time != -1:
http_data["MMSDeliveryReport"] = "Yes"
old_mms_in_files = glob.glob(os.path.join(self.mms_in_dir, "*.HDR"))
r = requests.post(self.hostname,
auth = (self.username, self.password),
files = formatted_files,
data = http_data)
assert(r.status_code == 200)
assert(r.text.find("Message Submitted") != -1)
m = re.search("MMSMessageID=.*@", r.text)
msgid = m.group(0)[13:-1]
logging.debug("Sent message with Message-Id: %s" % msgid)
if receipt_wait_time == -1:
return True
deadline = time.time() + receipt_wait_time
while time.time() < deadline:
time.sleep(1)
cur_mms_in_files = glob.glob(os.path.join(self.mms_in_dir, "*.HDR"))
for mms_in in cur_mms_in_files:
if mms_in in old_mms_in_files:
continue
with open(mms_in, "r") as f:
data = f.read()
if data.find("Message-id: %s" % msgid) == -1:
continue
if data.find("Status: Retrieved") == -1:
return False
return True
return False