| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 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 |
|
|---|
| 142 |
|
|---|