root/trunk/jjigw/config.py

Revision 165, 4.8 kB (checked in by jajcus, 2 years ago)

- addresses updated

Line 
1 #!/usr/bin/python -u
2 #
3 #  Jajcus' Jabber to IRC Gateway
4 #  Copyright (C) 2004  Jacek Konieczny <jajcus@jajcus.net>
5 #
6 #  This program is free software; you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation; either version 2 of the License, or
9 #  (at your option) any later version.
10 #
11 #  This program is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License along
17 #  with this program; if not, write to the Free Software Foundation, Inc.,
18 #  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20
21 import libxml2
22 from pyxmpp.jid import JID
23 from common import JJIGWFatalError,nick8_re,nick_re,normalize
24 import os
25
26 class ConnectConfig:
27     def __init__(self,node):
28         self.node=node
29         self.host=node.xpathEval("host")[0].getContent()
30         self.port=int(node.xpathEval("port")[0].getContent())
31         self.secret=node.xpathEval("secret")[0].getContent()
32
33 class SPIdentDConfig:
34     def __init__(self,node):
35         node=node.xpathEval("socket")[0]
36         self.socket=node.getContent()
37
38 class ServerConfig:
39     def __init__(self,node):
40         self.host=node.getContent()
41         self.port=node.prop("port")
42         try:
43             self.port=int(self.port)
44             if self.port<1 or self.port>65535:
45                 raise ValueError
46         except ValueError:
47             print >>sys.stderr,"Bad port value: %r, using default: 6667" % (self.port,)
48             self.port=6667
49         self.bindport=node.prop("bindport")
50         if not self.bindport:
51             self.bindport=0
52         self.bind=node.prop("bind")
53     def __repr__(self):
54         return "<ServerConfig %s:%s/>" % (self.host,self.port)
55
56 class ChannelConfig:
57     def __init__(self,node):
58         self.name=node.getContent()
59         self.encoding=node.prop("encoding")
60         self.description=node.prop("description")
61         self.browseable=node.prop("browseable")
62
63 class NetworkConfig:
64     def __init__(self,node):
65         self.node=node
66         self.jid=JID(node.prop("jid"))
67         servers=node.xpathEval("server")
68         self.servers=[]
69         for s in servers:
70             self.servers.append(ServerConfig(s))
71         channels=node.xpathEval("channel")
72         self.channels={}
73         if channels:
74             for c in channels:
75                 ch=ChannelConfig(c)
76                 self.channels[normalize(ch.name)]=ch
77         self.default_encoding=node.prop("encoding")
78         self.nicks_8bit=node.prop("nicks_8bit")
79         self.name=node.prop("name")
80         self.max_nick_length=int(node.prop("max_nick_length"))
81         self.max_channel_length=int(node.prop("max_nick_length"))
82         self.password=node.prop("password")
83     def get_servers(self):
84         r=self.servers
85         self.servers=self.servers[-1:]+self.servers[:-1]
86         return r
87     def get_channel_config(self,channel):
88         return self.channels.get(normalize(channel))
89     def valid_nick(self,s,strict=1):
90         if self.nicks_8bit:
91             m=nick8_re.match(s)
92         else:
93             m=nick_re.match(s)
94         if not m:
95             return 0
96         if not strict:
97             return 1
98         if len(s)<=self.max_nick_length:
99             return 1
100         return 0
101
102 class Config:
103     def __init__(self,config_dir,data_dir):
104         self.doc=None
105         self.config_dir=config_dir
106         self.data_dir=data_dir
107         os.chdir(data_dir)
108         libxml2.initializeCatalog()
109         libxml2.loadCatalog(os.path.join(data_dir,"catalog.xml"))
110         parser=libxml2.createFileParserCtxt(os.path.join(config_dir,"jjigw.xml"))
111         parser.validate(1)
112         parser.parseDocument()
113         if not parser.isValid():
114             raise JJIGWFatalError,"Invalid configuration"
115         self.doc=parser.doc()
116         self.connect=ConnectConfig(self.doc.xpathEval("jjigw/connect")[0])
117         self.jid=None
118         self.networks={}
119         for n in self.doc.xpathEval("jjigw/network"):
120             network=NetworkConfig(n)
121             if not self.jid:
122                 self.jid=network.jid
123             self.networks[network.jid.domain]=network
124         spidentd=self.doc.xpathEval("jjigw/spidentd")
125         if spidentd:
126             self.spidentd=SPIdentDConfig(spidentd[0])
127         else:
128             self.spidentd=None
129         self.admins=[]
130         for n in self.doc.xpathEval("jjigw/admin"):
131             self.admins.append(JID(n.getContent()))
132     def get_network(self,jid):
133         if isinstance(jid,JID):
134             return self.networks[jid.domain]
135         else:
136             return self.networks[jid]
137     def __del__(self):
138         if self.doc:
139             self.doc.freeDoc()
140
141 # vi: sts=4 et sw=4
142
Note: See TracBrowser for help on using the browser.