-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
telecommServer improvements #394
base: master
Are you sure you want to change the base?
Changes from 3 commits
f779cdd
0acbd32
b45a14a
d8d7fc1
b464b52
ab5613e
a6c5c3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,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","[email protected]"),... | ||
# ,("USERNAMEM","[email protected]")] | ||
# 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,8 +66,9 @@ | |
SMS_KEY = 'smsUsers' | ||
DOMAIN_KEY = 'domain' | ||
SERVER_KEY = 'smtpServer' | ||
PASSWORD_KEY = 'password' | ||
|
||
def textMessage(recipients, subject, msg, domain, server, username='LabRAD',attempts=2): | ||
def textMessage(recipients, subject, msg, domain, server, username='LabRAD', password=None, attempts=2): | ||
"""Send a text message to one or more recipients | ||
|
||
INPUTS: | ||
|
@@ -61,9 +81,9 @@ 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: | ||
|
@@ -88,6 +108,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,27 +150,35 @@ def _refreshConnectionData(self): | |
reg = cxn.registry | ||
p = reg.packet() | ||
p.cd(REGISTRY_PATH) | ||
p.dir(key = 'regcontent') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No space around |
||
p.get(SMS_KEY, key='userlist') | ||
p.get(DOMAIN_KEY, key='domain') | ||
p.get(SERVER_KEY, key='server') | ||
resp = yield p.send() | ||
self.smsUsers=dict(resp['userlist']) | ||
self.domain = resp['domain'] | ||
self.smtpServer = resp['server'] | ||
regContent = resp['regcontent'][1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Long line? |
||
return (success, failures) | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if there's a better place to store the password.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the registry more than a plain text file, because one has to jump through some additional loops in order to find it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, the difference is that the registry is accessible over LabRAD, so putting a password in the registry means that everyone allowed on LabRAD is allowed to see that password. LabRAD isn't really set up to manage permissions like that, so I think it unadvised to store passwords in the registry.
Can you just put in in a yaml or text file and have the server load it?
@maffoo @kunalq thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm inclined to agree with @DanielSank -- storing a password in the registry isn't ideal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the concern is managing permissions, then would storing the password in a text or yaml file really improve things? Do you guys have a single machine dedicated to using this server? In that case you could create some password file, store it in Telecomm folder and just make sure it is in the git ignore file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Presumably your computers have access control.
No, but that's not really the point. The point is to not use LabRAD as an access control system, because it's not (yet) designed for that (although @maffoo has been making great strides in that direction).
It's better to distribute the passwords some other way; whether by talking to each other or using some other software tool is irrelevant as long as it's as secure. This leaves the question of how to get the telecomm server to find out about this password.
We recently had to deal with a very similar situation regarding access to an SQL database through some scripts in our own code repo. My first solution was exactly what you're suggesting: put a password file in the same repo but add it to
.gitignore
so it doesn't wind up in version control. Again, how you tell other what password to put in that file is entirely up to you. However, that was a bit limiting and kind of dangerous, so now we have users put the passwords in a file in their home directory, and our scripts simply look there. You can useos.path.expanduser
to get the home directory on Mac/Linux/Windows.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I was working on database stuff where I used to work, we transitioned from storing username/password in plain text to windows authentication which is pretty nice, but everyone was running windows 7. Also, we were using MS SQL Server so I don't know if windows authentication was even an option for you guys.
Let me know what you guys want to do with the password stuff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We certainly need a cross-platform solution. I suppose one option would be to put a key in the registry indicating how to get the password, i.e. via a file at a particular location, or fro Windows authentication, etc.