64a2dea22aa86f3da7b42341de7a2beb8779de7f
[ircu2.10.12-pk.git] / tools / convert-conf.py
1 #!/usr/bin/env python
2 #
3 # IRC - Internet Relay Chat, tools/convert-conf.py
4 # Copyright (C) 2002 Alex Badea <vampire@p16.pub.ro>
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 1, or (at your option)
9 # 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
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #
20 #
21 # Configuration file converter from 2.10.11 to 2.10.12 format
22 # Usage:
23 #   convert-conf.py < old.conf > new.conf
24 #
25 # $Id: convert-conf.py,v 1.1 2002-04-09 22:40:56 vampire Exp $
26 #
27
28 import sys
29 from string import *
30
31 if len(sys.argv) > 1:
32         f = open(sys.argv[1], "r")
33 else:
34         f = sys.stdin
35
36 servers = {}
37 jupes = []
38 feats = []
39
40 def do_uline(parts):
41         if not servers.has_key(parts[1]):
42                 servers[parts[1]] = ['*', 0, 0, 0]
43         servers[parts[1]][1] = 1
44         if len(parts[2]):
45                 jupes.append(parts[2])
46
47 def do_hline(parts):
48         if not servers.has_key(parts[3]):
49                 servers[parts[3]] = ['*', 0, 0, 0]
50         servers[parts[3]][0] = parts[1]
51         servers[parts[3]][2] = 1
52
53 def do_lline(parts):
54         if not servers.has_key(parts[3]):
55                 servers[parts[3]] = ['*', 0, 0, 0]
56         servers[parts[3]][0] = parts[1]
57         servers[parts[3]][3] = 1
58
59 def do_pline(parts):
60         print "#", join(parts, ":")
61         print "Port {"
62         print "\tport = %s;" % parts[4]
63         if len(parts[1]):
64                 print "\tmask = \"%s\";" % parts[1]
65         if len(parts[2]):
66                 print "\tvhost = \"%s\";" % parts[2]
67         if count(parts[3], 'S'):
68                 print "\tserver = yes;"
69         if count(parts[3], 'H'):
70                 print "\thidden = yes;"
71         print "};"
72         print
73
74 def do_fline(parts):
75         feats.append((parts[1], parts[2]))
76
77 cvtmap = {
78         'M': ('General', ('name', 'vhost', 'description', '', '!numeric'), ''),
79         'A': ('Admin', ('location', 'contact', 'contact'), ''),
80         'Y': ('Class', ('name', '!pingfreq', '!connectfreq', '!maxlinks', '!sendq'), ''),
81         'I': ('Client', ('ip', 'password', 'host', '', 'class'), ''),
82         'T': ('motd', ('host', 'file'), ''),
83         'U': do_uline,
84         'H': do_hline,
85         'L': do_lline,
86         'K': ('Kill', ('host', 'reason'), ''),
87         'k': ('Kill', ('host', 'reason'), ''),
88         'C': ('Connect', ('host', 'password', 'name', '!port', 'class'), ''),
89         'D': ('CRULE', ('server', '', 'rule'), '\tall = yes;'),
90         'd': ('CRULE', ('server', '', 'rule'), ''),
91         'O': ('Operator', ('host', 'password', 'name', '', 'class'), '\tlocal = no;'),
92         'o': ('Operator', ('host', 'password', 'name', '', 'class'), '\tlocal = yes;'),
93         'P': do_pline,
94         'F': do_fline
95 }
96
97 for line in f.readlines():
98         line = strip(line)
99         parts = split(line, ":")
100         if not len(parts):
101                 continue
102         if not cvtmap.has_key(parts[0]):
103                 continue
104         if callable(cvtmap[parts[0]]):
105                 cvtmap[parts[0]](parts)
106                 continue
107         (block, items, extra) = cvtmap[parts[0]]
108         print "#", line
109         print block, "{"
110         idx = 1
111         for item in items:
112                 if idx >= len(parts):
113                         break
114                 if len(parts[idx]) and len(item):
115                         if item[0] == '!':
116                                 print "\t%s = %s;" % (item[1:], parts[idx])
117                         else:
118                                 print "\t%s = \"%s\";" % (item, parts[idx])
119                 idx = idx + 1
120         if len(extra):
121                 print extra
122         print "};"
123         print
124
125 for server in servers.keys():
126         (mask, uw, hub, leaf) = servers[server]
127         print "Server {"
128         print "\tname = \"%s\";" % server
129         print "\tmask = \"%s\";" % mask
130         if uw: print "\tuworld = yes;"
131         if hub: print "\thub = yes;"
132         if leaf: print "\tleaf = yes;"
133         print "};"
134         print
135
136 if len(jupes):
137         print "Jupe {"
138         for nick in jupes:
139                 print "\tnick = \"%s\";" % nick
140         print "};"
141         print
142
143 if len(feats):
144         print "features {"
145         for (name, value) in feats:
146                 print "\t\"%s\" = \"%s\";" % (name, value)
147         print "};"
148         print