From c9f1c5406a867154269f051c5b73e67721113ff9 Mon Sep 17 00:00:00 2001 From: Alexander Opremcak Date: Sat, 6 Feb 2016 19:51:17 -0600 Subject: [PATCH 1/3] Added some instructions and an optional password for authentication purposes. --- Telecomm/telecommServer.py | 50 +++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/Telecomm/telecommServer.py b/Telecomm/telecommServer.py index f6292d25..5f633763 100644 --- a/Telecomm/telecommServer.py +++ b/Telecomm/telecommServer.py @@ -32,6 +32,27 @@ #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 +# [("USERNAME_1","TENDIGITPHONENUMBER@txt.att.net"),... +# ,("USERNAME_M","TENDIGITPHONENUMBER@vtext.com")] +# Note that the domain name depends on the user's +# mobile phone provider. "USERNAME_" should 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 which +# is determined by 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 with different domains. +# 6) Call the send_mail (send_sms) setting as +# desired. from labrad import types as T, util from labrad.server import LabradServer, setting, Signal @@ -47,10 +68,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 +83,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 +109,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 +151,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 +159,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 fce8ce03cc1c43a66fd4d3af05c9c4c768184967 Mon Sep 17 00:00:00 2001 From: Alexander Opremcak Date: Sat, 6 Feb 2016 20:06:29 -0600 Subject: [PATCH 2/3] removed blank space --- Telecomm/telecommServer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Telecomm/telecommServer.py b/Telecomm/telecommServer.py index 5f633763..6b55b45f 100644 --- a/Telecomm/telecommServer.py +++ b/Telecomm/telecommServer.py @@ -151,7 +151,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 53408ec70f916c324f02248e8a22f94714477677 Mon Sep 17 00:00:00 2001 From: Alexander Opremcak Date: Sat, 6 Feb 2016 20:12:43 -0600 Subject: [PATCH 3/3] Bumped minor version number. --- Telecomm/telecommServer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Telecomm/telecommServer.py b/Telecomm/telecommServer.py index 6b55b45f..b8f6d6cb 100644 --- a/Telecomm/telecommServer.py +++ b/Telecomm/telecommServer.py @@ -17,7 +17,7 @@ ### BEGIN NODE INFO [info] name = Telecomm Server -version = 1.1 +version = 1.2 description = [startup]