From f779cdd4f75765f9dfb0cbb8f732763120e26a62 Mon Sep 17 00:00:00 2001 From: amopremcak Date: Tue, 27 Dec 2016 13:28:34 -0600 Subject: [PATCH 1/7] Added instructions for usage. Added optional password in the send_sms setting for authentication purposes. --- Telecomm/telecommServer.py | 50 +++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/Telecomm/telecommServer.py b/Telecomm/telecommServer.py index f6292d25..84014cb6 100644 --- a/Telecomm/telecommServer.py +++ b/Telecomm/telecommServer.py @@ -19,11 +19,9 @@ name = Telecomm Server version = 1.1 description = - [startup] cmdline = %PYTHON% %FILE% timeout = 20 - [shutdown] message = 987654321 timeout = 20 @@ -32,6 +30,25 @@ #TODO #Document input/output values +#Instructions for usage: +# 1) In the registry, create the directory /Servers/Telecomm +# 2) Create a key named 'smsUsers' with value +# [("USERNAME1","TENDIGITPHONENUMBER@txt.att.net"),... +# ,("USERNAMEM","TENDIGITPHONENUMBER@vtext.com")] +# Note that the domain name depends on the user's +# mobile phone provider. "USERNAME" must be ALL caps. +# 3) Create a key named 'domain' with a value that +# matches the domain name of the email address +# from which you wish to send messages. +# e.g. domain = "physics.someschool.edu" +# 4) Create a key named 'smtpServer' whose value is +# the smtp server you wish to use. +# 5) (Optional) Create a key named 'password' +# corresponding to the login credentials of +# the account you plan to send messages from. +# Note that some smtp servers require you to +# authenticate before you can send messages +# to email addresses outside of its domain. from labrad import types as T, util from labrad.server import LabradServer, setting, Signal @@ -47,10 +64,11 @@ SMS_KEY = 'smsUsers' DOMAIN_KEY = 'domain' SERVER_KEY = 'smtpServer' +PASSWORD_KEY = 'password' -def textMessage(recipients, subject, msg, domain, server, username='LabRAD',attempts=2): - """Send a text message to one or more recipients +def textMessage(recipients, subject, msg, domain, server, username='LabRAD', password=None, attempts=2): + """Send a text message to one or more recipients INPUTS: recipients - str or [str,str...]: List of names of labmembers to whom you want to send the message. These names must be in the @@ -61,11 +79,10 @@ def textMessage(recipients, subject, msg, domain, server, username='LabRAD',atte """ if not isinstance(recipients,list): recipients = [recipients] - return email(recipients, subject, msg, domain, server, username, attempts=attempts) + return email(recipients, subject, msg, domain, server, username, password, attempts=attempts) -def email(toAddrs, subject, msg, domain, server, username='LabRAD', attempts=2, noisy=False): +def email(toAddrs, subject, msg, domain, server, username='LabRAD', password=None, attempts=2, noisy=False): """Send an email to one or more recipients - INPUTS: toAddrs - str or [str...]: target address or list of target addresses subject - str: Subject of the message @@ -88,6 +105,9 @@ def email(toAddrs, subject, msg, domain, server, username='LabRAD', attempts=2, message = header+msg #Get connection to smtp server and send message server = smtplib.SMTP(server) + if password is not None: + server.starttls() + server.login(fromAddr, password) result = server.sendmail(fromAddr, toAddrs, message) #Update the toAddrs list to include only recipients for which mail sending failed toAddrs = result.keys() @@ -127,6 +147,7 @@ def _refreshConnectionData(self): reg = cxn.registry p = reg.packet() p.cd(REGISTRY_PATH) + p.dir(key = 'regcontent') p.get(SMS_KEY, key='userlist') p.get(DOMAIN_KEY, key='domain') p.get(SERVER_KEY, key='server') @@ -134,20 +155,27 @@ def _refreshConnectionData(self): self.smsUsers=dict(resp['userlist']) self.domain = resp['domain'] self.smtpServer = resp['server'] + regContent = resp['regcontent'][1] + if 'password' in regContent: + p.get(PASSWORD_KEY, key='password') + resp = yield p.send() + self.password = resp['password'] + else: + self.password = None print 'Refresh complete.' @setting(10, toAddrs=['s{email address of recipient}','*s{array of recipient email addresses}'], subject='s', msg='s', username='s', returns='b{success}*s{failures}') def send_mail(self, c, toAddrs, subject, msg, username='LabRAD'): - success, failures = email(toAddrs, subject, msg, self.domain, self.smtpServer, username) + success, failures = email(toAddrs, subject, msg, self.domain, self.smtpServer, username, self.password) return (success, failures) - @setting(11, subject='s', msg='s', recipients=['*s','s'], returns='b{success}*s{failures}') - def send_sms(self, c, subject, msg, recipients): + @setting(11, subject='s', msg='s', recipients=['*s','s'], username='s', returns='b{success}*s{failures}') + def send_sms(self, c, subject, msg, recipients, username='LabRAD'): if not isinstance(recipients,list): recipients = [recipients] recipients = [self.smsUsers[name.upper()] for name in recipients if name.upper() in self.smsUsers] - success, failures = textMessage(recipients, subject, msg, self.domain, self.smtpServer) + success, failures = textMessage(recipients, subject, msg, self.domain, self.smtpServer, username, self.password) return (success, failures) From 0acbd322192ca403b074267aa33017b3b504d51f Mon Sep 17 00:00:00 2001 From: amopremcak Date: Tue, 27 Dec 2016 13:35:01 -0600 Subject: [PATCH 2/7] Restoring white spaces that were removed somehow. --- Telecomm/telecommServer.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Telecomm/telecommServer.py b/Telecomm/telecommServer.py index 84014cb6..60bc1618 100644 --- a/Telecomm/telecommServer.py +++ b/Telecomm/telecommServer.py @@ -18,10 +18,12 @@ [info] name = Telecomm Server version = 1.1 -description = +description = + [startup] cmdline = %PYTHON% %FILE% timeout = 20 + [shutdown] message = 987654321 timeout = 20 @@ -66,9 +68,9 @@ SERVER_KEY = 'smtpServer' PASSWORD_KEY = 'password' - def textMessage(recipients, subject, msg, domain, server, username='LabRAD', password=None, attempts=2): """Send a text message to one or more recipients + INPUTS: recipients - str or [str,str...]: List of names of labmembers to whom you want to send the message. These names must be in the @@ -83,6 +85,7 @@ def textMessage(recipients, subject, msg, domain, server, username='LabRAD', pas def email(toAddrs, subject, msg, domain, server, username='LabRAD', password=None, attempts=2, noisy=False): """Send an email to one or more recipients + INPUTS: toAddrs - str or [str...]: target address or list of target addresses subject - str: Subject of the message From b45a14ab6df1586ff21c710becb634484547eb53 Mon Sep 17 00:00:00 2001 From: amopremcak Date: Tue, 27 Dec 2016 13:37:20 -0600 Subject: [PATCH 3/7] More white space corrections. --- Telecomm/telecommServer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Telecomm/telecommServer.py b/Telecomm/telecommServer.py index 60bc1618..3cb9b625 100644 --- a/Telecomm/telecommServer.py +++ b/Telecomm/telecommServer.py @@ -18,7 +18,7 @@ [info] name = Telecomm Server version = 1.1 -description = +description = [startup] cmdline = %PYTHON% %FILE% From d8d7fc11156d73d8ce77691c1d1a2d02a66ce5c9 Mon Sep 17 00:00:00 2001 From: amopremcak Date: Thu, 5 Jan 2017 20:30:50 -0600 Subject: [PATCH 4/7] Removed space from keyword argument. --- Telecomm/telecommServer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Telecomm/telecommServer.py b/Telecomm/telecommServer.py index 3cb9b625..a30e6e44 100644 --- a/Telecomm/telecommServer.py +++ b/Telecomm/telecommServer.py @@ -150,7 +150,7 @@ def _refreshConnectionData(self): reg = cxn.registry p = reg.packet() p.cd(REGISTRY_PATH) - p.dir(key = 'regcontent') + p.dir(key='regcontent') p.get(SMS_KEY, key='userlist') p.get(DOMAIN_KEY, key='domain') p.get(SERVER_KEY, key='server') From b464b52ce540839ea26fb72a1a22385d91d324d3 Mon Sep 17 00:00:00 2001 From: amopremcak Date: Thu, 5 Jan 2017 20:34:37 -0600 Subject: [PATCH 5/7] Changed regContent to keys as it is more descriptive --- Telecomm/telecommServer.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Telecomm/telecommServer.py b/Telecomm/telecommServer.py index a30e6e44..ce8d82ad 100644 --- a/Telecomm/telecommServer.py +++ b/Telecomm/telecommServer.py @@ -150,7 +150,7 @@ def _refreshConnectionData(self): reg = cxn.registry p = reg.packet() p.cd(REGISTRY_PATH) - p.dir(key='regcontent') + p.dir(key='keys') p.get(SMS_KEY, key='userlist') p.get(DOMAIN_KEY, key='domain') p.get(SERVER_KEY, key='server') @@ -158,8 +158,8 @@ def _refreshConnectionData(self): self.smsUsers=dict(resp['userlist']) self.domain = resp['domain'] self.smtpServer = resp['server'] - regContent = resp['regcontent'][1] - if 'password' in regContent: + keys = resp['keys'][1] + if 'password' in keys: p.get(PASSWORD_KEY, key='password') resp = yield p.send() self.password = resp['password'] From ab5613ebc4d26f49767ff0f5670abb7b17c67c79 Mon Sep 17 00:00:00 2001 From: amopremcak Date: Thu, 5 Jan 2017 20:39:02 -0600 Subject: [PATCH 6/7] Removed long line from textMessage call --- Telecomm/telecommServer.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Telecomm/telecommServer.py b/Telecomm/telecommServer.py index ce8d82ad..952239a8 100644 --- a/Telecomm/telecommServer.py +++ b/Telecomm/telecommServer.py @@ -178,7 +178,8 @@ def send_sms(self, c, subject, msg, recipients, username='LabRAD'): if not isinstance(recipients,list): recipients = [recipients] recipients = [self.smsUsers[name.upper()] for name in recipients if name.upper() in self.smsUsers] - success, failures = textMessage(recipients, subject, msg, self.domain, self.smtpServer, username, self.password) + success, failures = textMessage(recipients, subject, msg, self.domain, self.smtpServer, + username, self.password) return (success, failures) From a6c5c3f9049dd7617787b305e91c9ed2c479d4b8 Mon Sep 17 00:00:00 2001 From: amopremcak Date: Thu, 5 Jan 2017 20:46:34 -0600 Subject: [PATCH 7/7] Misread request on keys variable, fixed. --- Telecomm/telecommServer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Telecomm/telecommServer.py b/Telecomm/telecommServer.py index 952239a8..0c796e9e 100644 --- a/Telecomm/telecommServer.py +++ b/Telecomm/telecommServer.py @@ -150,7 +150,7 @@ def _refreshConnectionData(self): reg = cxn.registry p = reg.packet() p.cd(REGISTRY_PATH) - p.dir(key='keys') + p.dir(key='regcontent') p.get(SMS_KEY, key='userlist') p.get(DOMAIN_KEY, key='domain') p.get(SERVER_KEY, key='server') @@ -158,7 +158,7 @@ def _refreshConnectionData(self): self.smsUsers=dict(resp['userlist']) self.domain = resp['domain'] self.smtpServer = resp['server'] - keys = resp['keys'][1] + keys = resp['regcontent'][1] if 'password' in keys: p.get(PASSWORD_KEY, key='password') resp = yield p.send()