| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | """ |
|---|
| 4 | StormSiren |
|---|
| 5 | Copyright (C) 2008 Chris Freeze <cfreeze/cfreeze_com\> |
|---|
| 6 | |
|---|
| 7 | Redistribution and use in source and binary forms, with or without |
|---|
| 8 | modification, are permitted provided that the following conditions |
|---|
| 9 | are met: |
|---|
| 10 | |
|---|
| 11 | 1. Redistributions of source code must retain the above copyright |
|---|
| 12 | notice, this list of conditions and the following disclaimer. |
|---|
| 13 | 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 14 | notice, this list of conditions and the following disclaimer in the |
|---|
| 15 | documentation and/or other materials provided with the distribution. |
|---|
| 16 | |
|---|
| 17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|---|
| 18 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|---|
| 19 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|---|
| 20 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
|---|
| 21 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|---|
| 22 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|---|
| 26 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 27 | """ |
|---|
| 28 | |
|---|
| 29 | """ |
|---|
| 30 | This is StormSiren a program inspired by the StormSiren application |
|---|
| 31 | written by Roy McManus <slorf/users_sourceforge_net>. Much like the |
|---|
| 32 | original StormSiren written by Roy McManus <slorf/users_sourceforge_net>, |
|---|
| 33 | this program is a simple script capable of scanning and providing |
|---|
| 34 | notifications of weather bulletins issued by the National |
|---|
| 35 | Weather Service. StormSiren supports a wide range of paging devices |
|---|
| 36 | and filtering of alert types per alert device. While inspired by |
|---|
| 37 | StormSiren, StormSiren is a complete rewrite that is capable of using |
|---|
| 38 | the new CAP/XML feeds provided at http://www.weather.gov/alerts/. |
|---|
| 39 | |
|---|
| 40 | For more information there is see the README.TXT file located in the root |
|---|
| 41 | of this directory. |
|---|
| 42 | """ |
|---|
| 43 | |
|---|
| 44 | import smtplib |
|---|
| 45 | |
|---|
| 46 | from AlertDevice import * |
|---|
| 47 | |
|---|
| 48 | EMAIL_ALERT_DEVICE_VERSION = "1.0" |
|---|
| 49 | EMAIL_SUBJECT_LEADER = "[StormSiren] " |
|---|
| 50 | DEFAULT_SMTP_FROM = "noreply@example.com" |
|---|
| 51 | XMAILER_TAG = "StormSiren/" + EMAIL_ALERT_DEVICE_VERSION |
|---|
| 52 | |
|---|
| 53 | DEFAULT_SMTP_HOST = "localhost" |
|---|
| 54 | DEFAULT_SMTP_PORT = 25 |
|---|
| 55 | DEFAULT_SMTP_FROM = "noreply@example.com" |
|---|
| 56 | |
|---|
| 57 | class EmailAlertDevice(AlertDevice): |
|---|
| 58 | def __init__(self, to, textwidth, smtpInfo, alerts, areas): |
|---|
| 59 | super(EmailAlertDevice,self).__init__(alerts, areas) |
|---|
| 60 | self.__smtpInfo = smtpInfo |
|---|
| 61 | self.__to = to |
|---|
| 62 | self.__textwidth = textwidth |
|---|
| 63 | |
|---|
| 64 | def display(self): |
|---|
| 65 | print self.__str__() |
|---|
| 66 | |
|---|
| 67 | def __str__(self): |
|---|
| 68 | str = "EmailAlertDevice\n" + \ |
|---|
| 69 | "To: " + self.to + "\n" |
|---|
| 70 | |
|---|
| 71 | if(self.__textwidth > 0): |
|---|
| 72 | str += "TextWidth: %i" % self.__textwidth |
|---|
| 73 | |
|---|
| 74 | str += self.__smtpInfo.__str__() + \ |
|---|
| 75 | super(EmailAlertDevice,self).__str__() |
|---|
| 76 | |
|---|
| 77 | return str |
|---|
| 78 | |
|---|
| 79 | def send(self,alert): |
|---|
| 80 | subject = EMAIL_SUBJECT_LEADER + " - " + alert.headline + " (" + alert.getAreas() + ")" |
|---|
| 81 | alert_body = "" |
|---|
| 82 | |
|---|
| 83 | if(self.__textwidth > 0): |
|---|
| 84 | email_body = alert.getDetailedWithFormat(self.__textwidth) |
|---|
| 85 | else: |
|---|
| 86 | email_body = alert.getDetailed() |
|---|
| 87 | |
|---|
| 88 | self.email(email_body,subject) |
|---|
| 89 | |
|---|
| 90 | def email(self,text,subject): |
|---|
| 91 | msg = 'From: ' + self.__smtpInfo.msg_from + '\n' + \ |
|---|
| 92 | 'To: ' + self.to + '\n' + \ |
|---|
| 93 | 'X-Mailer: ' + XMAILER_TAG + '\n' + \ |
|---|
| 94 | 'Subject: ' + subject + '\n\n\n' |
|---|
| 95 | |
|---|
| 96 | msg += text |
|---|
| 97 | |
|---|
| 98 | mailhost = smtplib.SMTP(self.__smtpInfo.host, self.__smtpInfo.port) |
|---|
| 99 | |
|---|
| 100 | if (self.__smtpInfo.user and self.__smtpInfo.password): |
|---|
| 101 | mailhost.login(self.__smtpInfo.user, self.__smtpInfo.password) |
|---|
| 102 | |
|---|
| 103 | mailhost.sendmail(self.__smtpInfo.msg_from, self.to, msg) |
|---|
| 104 | mailhost.quit |
|---|
| 105 | |
|---|
| 106 | def setTo(self,to): |
|---|
| 107 | self.__to = to |
|---|
| 108 | |
|---|
| 109 | def getTo(self): |
|---|
| 110 | return self.__to |
|---|
| 111 | |
|---|
| 112 | to = property(getTo,setTo,None) |
|---|
| 113 | |
|---|
| 114 | class EmailAlertDeviceInfo: |
|---|
| 115 | def __init__(self, efrom = DEFAULT_SMTP_FROM, host = DEFAULT_SMTP_HOST, port = DEFAULT_SMTP_PORT, user = None, password = None): |
|---|
| 116 | self.__from = efrom |
|---|
| 117 | self.__host = host |
|---|
| 118 | self.__port = port |
|---|
| 119 | self.__user = user |
|---|
| 120 | self.__password = password |
|---|
| 121 | |
|---|
| 122 | def display(self): |
|---|
| 123 | print self.__str__() |
|---|
| 124 | |
|---|
| 125 | def __str__(self): |
|---|
| 126 | str = "Email Device Info\n" + \ |
|---|
| 127 | "\tHost: " + self.host + "\n" + \ |
|---|
| 128 | "\tPort: " + self.port + "\n" |
|---|
| 129 | if self.user: |
|---|
| 130 | str += "\tUser: " + self.user + "\n" |
|---|
| 131 | if self.password: |
|---|
| 132 | str += "\tPassword: " + self.password + "\n" |
|---|
| 133 | |
|---|
| 134 | return str |
|---|