-
Notifications
You must be signed in to change notification settings - Fork 21
telecommServer improvements #394
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
Open
amopremcak
wants to merge
7
commits into
labrad:master
Choose a base branch
from
amopremcak:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f779cdd
Added instructions for usage. Added optional password in the send_sms…
amopremcak 0acbd32
Restoring white spaces that were removed somehow.
amopremcak b45a14a
More white space corrections.
amopremcak d8d7fc1
Removed space from keyword argument.
amopremcak b464b52
Changed regContent to keys as it is more descriptive
amopremcak ab5613e
Removed long line from textMessage call
amopremcak a6c5c3f
Misread request on keys variable, fixed.
amopremcak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,36 @@ 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') | ||
| resp = yield p.send() | ||
| self.smsUsers=dict(resp['userlist']) | ||
| self.domain = resp['domain'] | ||
| self.smtpServer = resp['server'] | ||
| keys = resp['regcontent'][1] | ||
| if 'password' in keys: | ||
| 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) | ||
|
|
||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
.gitignoreso 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.expanduserto 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.