~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to gozerbot/compat/config.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2009-09-14 09:00:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090914090029-uval0ekt72kmklxw
Tags: 0.9.1.3-3
Changed dependency on python-setuptools to python-pkg-resources
(Closes: #546435) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# gozerbot/config.py
 
2
#
 
3
#
 
4
 
 
5
""" this is where the config dict lives .. use pickle to persist config data
 
6
    .. use this pickle on start until the config file has changed  """
 
7
 
 
8
__copyright__ = 'this file is in the public domain'
 
9
 
 
10
from gozerbot.datadir import datadir
 
11
import os, pickle, subprocess
 
12
 
 
13
# version string
 
14
ver = 'GOZERBOT 0.8.1.1 RELEASE'
 
15
 
 
16
def diffdict(dictfrom, dictto):
 
17
    """ check for differences between two dicts """
 
18
    temp = {}
 
19
    for i in dictto.iteritems():
 
20
        if dictfrom.has_key(i[0]):
 
21
            if dictfrom[i[0]] != i[1]:
 
22
                temp.setdefault(i[0], i[1])
 
23
        else:
 
24
            temp.setdefault(i[0], i[1])
 
25
    return temp
 
26
 
 
27
class Config(dict):
 
28
 
 
29
    """ config object is a dict """
 
30
 
 
31
    def __init__(self, ddir, *args, **kw):
 
32
        dict.__init__(self, *args, **kw)
 
33
        self.dir = str(ddir)
 
34
        self['dbtype'] = 'mysql'
 
35
 
 
36
    def __getitem__(self, item):
 
37
        """ get config item .. return None if not available"""
 
38
        if not self.has_key(item):
 
39
            return None
 
40
        else:
 
41
            return dict.__getitem__(self, item)
 
42
 
 
43
    def set(self, item, value):
 
44
        """ set a config item """
 
45
        dict.__setitem__(self, item, value)
 
46
        self.save()
 
47
 
 
48
    def load(self):
 
49
        """ load the config file """
 
50
        frompickle = {}
 
51
        picklemodtime = None
 
52
        # first do reload of the data/config file
 
53
        self.reload()
 
54
        # see if there is a configpickle
 
55
        try:
 
56
            picklemodtime = os.stat(self.dir + os.sep + 'configpickle')[8]
 
57
            configpickle = open(self.dir + os.sep + 'configpickle', 'r')
 
58
            frompickle = pickle.load(configpickle)
 
59
            configpickle.close()
 
60
        except OSError:
 
61
            return
 
62
        except:
 
63
            pass
 
64
        # see if data/config is more recent than the configpickle
 
65
        configmodtime = os.stat(self.dir + os.sep + 'config')[8]
 
66
        if picklemodtime and picklemodtime > configmodtime:
 
67
            # if so update the config dict with the pickled data
 
68
            # a = diffdict(self, frompickle)
 
69
            self.update(frompickle)
 
70
        # set version
 
71
        if self['dbenable']:
 
72
            self['version'] = ver + ' ' + self['dbtype'].upper()
 
73
        else:
 
74
            self['version'] = ver
 
75
    
 
76
    def save(self):
 
77
        """ save config data to pickled file """
 
78
        picklefile = open(self.dir + os.sep + 'configpickle', 'w')
 
79
        pickle.dump(self, picklefile)
 
80
        picklefile.close()
 
81
 
 
82
    def reload(self):
 
83
        """ use execfile to reload data/config """
 
84
        try:
 
85
            execfile(self.dir + os.sep + 'config', self)
 
86
        except IOError:
 
87
            self.defaultconfig()
 
88
        # remove builtin data
 
89
        try:
 
90
            del self['__builtins__']
 
91
        except:
 
92
            pass
 
93
        # set version
 
94
        if self['dbenable']:
 
95
            self['version'] = ver + ' ' + self['dbtype'].upper()
 
96
        else:
 
97
            self['version'] = ver
 
98
 
 
99
    def defaultconfig(self):
 
100
        """ init default config values if no config file is found """
 
101
        self['loglevel'] = 100
 
102
        self['jabberenable'] = 0
 
103
        self['ircdisable'] = 0
 
104
        self['stripident'] = 1
 
105
        self['owneruserhost'] = ['bart@127.0.0.1', ]
 
106
        self['nick'] = 'gozerbot'
 
107
        self['server'] = 'localhost'
 
108
        self['port'] = 6667
 
109
        self['ipv6'] = 0
 
110
        self['username'] = 'gozerbot'
 
111
        self['realname'] = 'GOZERBOT'
 
112
        self['defaultcc'] = "!"
 
113
        self['nolimiter'] = 0
 
114
        self['quitmsg'] = 'http://gozerbot.org'
 
115
        self['dbenable'] = 0
 
116
        self['udp'] = 0
 
117
        self['partyudp'] = 0
 
118
        self['mainbotname'] = 'main'
 
119
        self['addonallow'] = 0
 
120
        self['allowedchars'] = []
 
121
 
 
122
configtxt = """# config
 
123
#
 
124
#
 
125
 
 
126
__copyright__ = 'this file is in the public domain'
 
127
 
 
128
# gozerdata dir umask
 
129
umask = 0700
 
130
 
 
131
# logging level .. the higher this value is the LESS the bot logs
 
132
loglevel = 10
 
133
 
 
134
## jabber section:
 
135
 
 
136
jabberenable = 0
 
137
jabberowner = 'bartholo@localhost'
 
138
jabberhost = 'localhost'
 
139
jabberuser = 'gozerbot@localhost'
 
140
jabberpass = 'pass'
 
141
jabberoutsleep = 0.1
 
142
 
 
143
## irc section:
 
144
 
 
145
ircdisable = 0
 
146
 
 
147
# stripident .. enable stripping of ident from userhost
 
148
stripident = 1
 
149
 
 
150
# userhost of owner .. make sure this matches your client's userhost
 
151
# if it doesn't match you will get an userhost denied message when you
 
152
# try to send commands to the bot
 
153
owneruserhost = ['bart@127.0.0.1', ]
 
154
 
 
155
# the nick the bot tries to use, only used if no nick is set
 
156
# otherwise the bot will use the last nick used by the !nick command
 
157
nick = 'gozerbot'
 
158
 
 
159
# alternick
 
160
#alternick = 'gozerbot2'
 
161
 
 
162
# server to connect to
 
163
server = 'localhost'
 
164
 
 
165
# irc port to connect to 
 
166
port = 6667
 
167
 
 
168
# ircd password for main bot
 
169
#password = 'bla'
 
170
 
 
171
# ipv6
 
172
ipv6 = 0
 
173
 
 
174
# bindhost .. uncomment and edit to use
 
175
#bindhost = 'localhost'
 
176
 
 
177
# bots username
 
178
username = 'gozerbot'
 
179
 
 
180
# realname
 
181
realname = 'GOZERBOT'
 
182
 
 
183
# default control character
 
184
defaultcc = "!"
 
185
 
 
186
# no limiter
 
187
nolimiter = 0
 
188
 
 
189
# quit message
 
190
quitmsg = 'http://gozerbot.org'
 
191
 
 
192
# nickserv .. set pass to enable nickserv ident
 
193
nickservpass = ""
 
194
nickservtxt = ['set unfiltered on', ]
 
195
 
 
196
## if you want to use a database:
 
197
 
 
198
dbenable = 0 # set to 1 to enable
 
199
dbtype = 'mysql' # one of mysql or sqlite
 
200
dbname = "gb_db"
 
201
dbhost = "localhost"
 
202
dbuser = "bart"
 
203
dbpasswd = "mekker2"
 
204
dboldstyle = False # set to True if mysql database is <= 4.1
 
205
 
 
206
## if you want to use udp:
 
207
 
 
208
# udp
 
209
udp = 0 # set to 1 to enable
 
210
partyudp = 0
 
211
udpipv6 = 0
 
212
udphost = 'localhost'
 
213
udpport = 5500
 
214
udpmasks = ['192.168*', ]
 
215
udpallow = ['127.0.0.1', ]
 
216
udpallowednicks = ['#dunkbots', 'dunker']
 
217
udppassword = 'mekker'
 
218
udpseed = "" # set this to 16 char wide string if you want to encrypt the data
 
219
udpstrip = 1 # strip all chars < char(32)
 
220
udpsleep = 0 # sleep in sendloop .. can be used to delay packet traffic
 
221
 
 
222
# tcp
 
223
tcp = 0 # set to 1 to enable
 
224
partytcp = 0
 
225
tcpipv6 = 0
 
226
tcphost = 'localhost'
 
227
tcpport = 5500 
 
228
tcpmasks = ['192.168*', ]
 
229
tcpallow = ['127.0.0.1', ]
 
230
tcpallowednicks = ['#dunkbots', 'dunker', 'dunker@jabber.xs4all.nl']
 
231
tcppassword = 'mekker'
 
232
tcpseed = "bla1234567890bla"
 
233
# set this to 16 char wide string if you want to encrypt the data
 
234
tcpstrip = 1
 
235
tcpsleep = 0   
 
236
 
 
237
## other stuff:
 
238
 
 
239
# plugin server 
 
240
pluginserver = 'http://gozerbot.org'
 
241
 
 
242
# upgradeurl .. only needed if mercurial repo changed
 
243
#upgradeurl = 'http://gozerbot.org/hg/gozerbot'
 
244
 
 
245
# mail related
 
246
mailserver = None
 
247
mailfrom = None
 
248
 
 
249
# collective boot server
 
250
collboot = "gozerbot.org:8088"
 
251
 
 
252
# name of the main bot
 
253
mainbotname = 'main'
 
254
 
 
255
# allowed character for strippedtxt
 
256
allowedchars = []
 
257
 
 
258
# set to 1 to allow addons
 
259
addonallow = 0
 
260
 
 
261
# enable loadlist
 
262
loadlist = 0
 
263
"""
 
264
 
 
265
def writeconfig():
 
266
    """ wtite default config file to datadir/config """
 
267
    if not os.path.isfile(datadir + os.sep + 'config'):
 
268
        cfgfile = open(datadir + os.sep + 'config', 'w')
 
269
        cfgfile.write(configtxt)
 
270
        cfgfile.close()
 
271
 
 
272
loadlist = """
 
273
core
 
274
misc
 
275
irc
 
276
not
 
277
grep
 
278
reverse
 
279
count
 
280
chanperm
 
281
choice
 
282
fleet
 
283
ignore
 
284
upgrade
 
285
job
 
286
reload
 
287
rest
 
288
tail
 
289
user
 
290
googletalk
 
291
all
 
292
at
 
293
backup
 
294
install
 
295
reload
 
296
tell
 
297
reverse
 
298
to
 
299
underauth
 
300
userstate
 
301
alias
 
302
nickserv
 
303
"""
 
304
 
 
305
def writeloadlist():
 
306
    """ write loadlist to datadir """
 
307
    if not os.path.isfile(datadir + os.sep + 'loadlist'):
 
308
        cfgfile = open(datadir + os.sep + 'loadlist', 'w')
 
309
        cfgfile.write(loadlist)
 
310
        cfgfile.close()
 
311
 
 
312
# create the config dict and load it from file
 
313
#config = Config(datadir)