Package gozerbot :: Module config
[hide private]
[frames] | no frames]

Source Code for Module gozerbot.config

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