| 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 logging |
|---|
| 45 | |
|---|
| 46 | try: |
|---|
| 47 | import xmpp |
|---|
| 48 | except ImportError: |
|---|
| 49 | pass |
|---|
| 50 | |
|---|
| 51 | from AlertDevice import * |
|---|
| 52 | |
|---|
| 53 | DEFAULT_JABBER_HOST = "localhost" |
|---|
| 54 | DEFAULT_JABBER_PORT = 5223 |
|---|
| 55 | |
|---|
| 56 | class JabberAlertDevice(AlertDevice): |
|---|
| 57 | def __init__(self,to,jabberInfo,alerts,areas): |
|---|
| 58 | super(JabberAlertDevice,self).__init__(alerts, areas) |
|---|
| 59 | self.__jabberInfo = jabberInfo |
|---|
| 60 | self.__to = to |
|---|
| 61 | self.a = alerts |
|---|
| 62 | self.log = logging.getLogger('JabberAlertDevice') |
|---|
| 63 | |
|---|
| 64 | self.log.setLevel(logging.INFO) |
|---|
| 65 | |
|---|
| 66 | def display(self): |
|---|
| 67 | print self.__str__() |
|---|
| 68 | |
|---|
| 69 | def __str__(self): |
|---|
| 70 | str = "JabberAlertDevice\n" + \ |
|---|
| 71 | "\tTo: " + self.__to + "\n" + \ |
|---|
| 72 | self.__jabberInfo.__str__() + \ |
|---|
| 73 | super(JabberAlertDevice,self).__str__() |
|---|
| 74 | return str |
|---|
| 75 | |
|---|
| 76 | def send(self,alert): |
|---|
| 77 | jid=xmpp.protocol.JID(self.__jabberInfo.user + '@' + self.__jabberInfo.host) |
|---|
| 78 | cl=xmpp.Client(jid.getDomain(),debug=[]) |
|---|
| 79 | conres = cl.connect() |
|---|
| 80 | |
|---|
| 81 | if not conres: |
|---|
| 82 | self.log.error("Unable to connect to server %s!" % self.__jabberInfo.host) |
|---|
| 83 | return |
|---|
| 84 | |
|---|
| 85 | if conres<>'tls': |
|---|
| 86 | self.log.error("Unable to estabilish secure connection - TLS failed!") |
|---|
| 87 | return |
|---|
| 88 | |
|---|
| 89 | authres = cl.auth(jid.getNode(),self.__jabberInfo.password) |
|---|
| 90 | |
|---|
| 91 | if not authres: |
|---|
| 92 | self.log.error("Unable to authorize on %s - check login/password." % jid) |
|---|
| 93 | return |
|---|
| 94 | |
|---|
| 95 | if authres<>'sasl': |
|---|
| 96 | self.log.warn("Warning: unable to perform SASL auth os %s. Old authentication method used!" % jid) |
|---|
| 97 | |
|---|
| 98 | cl.send(xmpp.protocol.Message(self.to,alert.detailed,None, alert.headline + "(" + alert.getAreas() + ")")) |
|---|
| 99 | |
|---|
| 100 | class JabberAlertDeviceInfo(object): |
|---|
| 101 | def __init__(self): |
|---|
| 102 | self.__host = "" |
|---|
| 103 | self.__port = 0 |
|---|
| 104 | self.__user = "" |
|---|
| 105 | self.__password = "" |
|---|
| 106 | |
|---|
| 107 | def display(self): |
|---|
| 108 | print self.__str__() |
|---|
| 109 | |
|---|
| 110 | def setHost(self, host): |
|---|
| 111 | self.__host = host |
|---|
| 112 | |
|---|
| 113 | def getHost(self): |
|---|
| 114 | return self.__host |
|---|
| 115 | |
|---|
| 116 | def _setUser(self, user): |
|---|
| 117 | self.__user = user |
|---|
| 118 | |
|---|
| 119 | def _getUser(self): |
|---|
| 120 | return self.__user |
|---|
| 121 | |
|---|
| 122 | def setPassword(self, password): |
|---|
| 123 | self.__password = password |
|---|
| 124 | |
|---|
| 125 | def getPassword(self): |
|---|
| 126 | return self.__password |
|---|
| 127 | |
|---|
| 128 | def setPort(self, port): |
|---|
| 129 | self.__port = port |
|---|
| 130 | |
|---|
| 131 | def getPort(self): |
|---|
| 132 | return self.__port |
|---|
| 133 | |
|---|
| 134 | host = property(getHost,setHost,None) |
|---|
| 135 | port = property(getPort,setPort,None) |
|---|
| 136 | user = property(_getUser,_setUser,None) |
|---|
| 137 | password = property(getPassword,setPassword,None) |
|---|
| 138 | |
|---|
| 139 | def __str__(self): |
|---|
| 140 | return "Jabber Info\n" + \ |
|---|
| 141 | "\tHost: " + self.host + "\n" + \ |
|---|
| 142 | "\tPort: " + str(self.port) + "\n" + \ |
|---|
| 143 | "\tUser: " + self.user + "\n" + \ |
|---|
| 144 | "\tPassword: " + self.password + "\n" |
|---|