root/StormSiren/JabberAlertDevice.py

Revision 187:b82b5be8e615, 4.3 KB (checked in by chris@…, 3 years ago)

moving the CAP 1.1 work in the default branch

  • 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 logging
45
46try:
47        import xmpp
48except ImportError:
49        pass
50
51from AlertDevice import *
52
53DEFAULT_JABBER_HOST = "localhost"
54DEFAULT_JABBER_PORT = 5223
55
56class 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
100class 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"
Note: See TracBrowser for help on using the browser.