root/StormSiren/EmailAlertDevice.py

Revision 177:e7e54a896aa3, 4.2 KB (checked in by cfreeze@…, 3 years ago)

clean up the execution device to actually be usable on the commandline

  • Property exe set to *
Line 
1#!/usr/bin/env python
2
3"""
4StormSiren
5Copyright (C) 2008  Chris Freeze <cfreeze/cfreeze_com\>
6
7Redistribution and use in source and binary forms, with or without
8modification, are permitted provided that the following conditions
9are met:
10
111. Redistributions of source code must retain the above copyright
12   notice, this list of conditions and the following disclaimer.
132. 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
17THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27"""
28
29"""
30This is StormSiren a program inspired by the StormSiren application
31written by Roy McManus <slorf/users_sourceforge_net>.  Much like the
32original StormSiren written by Roy McManus <slorf/users_sourceforge_net>,
33this program is a simple script capable of scanning and providing
34notifications of weather bulletins issued by the National
35Weather Service.  StormSiren supports a wide range of paging devices
36and filtering of alert types per alert device.  While inspired by
37StormSiren, StormSiren is a complete rewrite that is capable of using
38the new CAP/XML feeds provided at http://www.weather.gov/alerts/.
39
40For more information there is see the README.TXT file located in the root
41of this directory.
42"""
43
44import smtplib
45
46from AlertDevice import *
47
48EMAIL_ALERT_DEVICE_VERSION = "1.0"
49EMAIL_SUBJECT_LEADER = "[StormSiren] "
50DEFAULT_SMTP_FROM = "noreply@example.com"
51XMAILER_TAG = "StormSiren/" + EMAIL_ALERT_DEVICE_VERSION
52
53DEFAULT_SMTP_HOST = "localhost"
54DEFAULT_SMTP_PORT = 25
55DEFAULT_SMTP_FROM = "noreply@example.com"
56
57class 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
114class 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
Note: See TracBrowser for help on using the browser.