Skip to content

Commit

Permalink
Merge branch 'master' of github.com:aj00200/BBot
Browse files Browse the repository at this point in the history
  • Loading branch information
aj00200 committed Mar 11, 2012
2 parents b37b17f + 3ece5d7 commit 93aecc9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
38 changes: 38 additions & 0 deletions api/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'''This api module safely stores data given to it by modules. This data
will not be lost of the bot shuts down and should be properly saved,
even if the bot crashes.
Data for this module is stored in ~/.BBot/storage/.
'''
import os
import json

# Variables
STORAGE_PATH = os.path.join(os.path.expanduser('~'),
'.BBot', 'storage', '')
registered = {}

# Functions
def register(network, module):
# Check if database exists, create if missing
if network in registered:
if module not in registered[network]:
registered[network][module] = Database()
else:
registered[network] = {
module: Database()
}

# Return the database
return registered[network][module]

def load():
'''Load databases from ~/.BBot/storage/.'''
for storagepath in os.listdir(STORAGE_PATH):
network = storagepath.replace('.json', '')
registered[network] = json.load(file(STORAGE_PATH + storagepath))

def save():
'''Save the databases in a file with the name of the network.'''
for network in registered:
filename = network + '.json'
json.dump(registered[network], file(STORAGE_PATH + filename, 'w'))
28 changes: 21 additions & 7 deletions backends/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ def __init__(self, address, port, use_ssl):
self.set_socket(self.sock)

def load_modules(self):
'''Load all the modules which are set in the config'''
'''Load all the modules which are set in the config.'''
for module in config.modules:
self.load_module(module)

def handle_error(self):
'''Print a traceback when an error happens'''
'''Print a traceback when an error happens.'''
traceback.print_exc()

def load_module(self, module):
'''Load a module for the network'''
'''Load a module for the network.'''
try:
self.modules[module] = getattr(__import__('modules.'+module), module).Module(self.__address__)
return True
Expand All @@ -78,12 +78,17 @@ def load_module(self, module):
return False

def unload_module(self, module):
'''Unload a module for the network.'''
if module in self.modules:
self.output('Removing module %s' % module)
self.modules[module].destroy()
del self.modules[module]

def reload_module(self, module):
'''Reload a module by calling the unload_module method followed
by a reload of the file and finally running the load_module
method to reload the module for the network.
'''
self.unload_module(module)
try:
reload(getattr(__import__('modules.'+module), module)).Module(self.__address__)
Expand All @@ -102,10 +107,14 @@ def get_data(self):

def found_terminator(self):
data = self.get_data()
# Check if we should ignore this message
if re.search(config.ignore, data.lower()):
return
command = data.split(' ', 2)[1]

self.output('R%s' % data)

# Take an accion based on the command
command = data.split(' ', 2)[1]
if data[:4] == 'PING':
self.push('PONG %s\r\n' % data[5:])

Expand Down Expand Up @@ -176,20 +185,25 @@ def collect_incoming_data(self, data):
self.data += data

def output(self, message):
'''Print a message and display the network name'''
'''Print a message and display the network name.'''
print('[%s] %s' % (self.netname, message))

def connect(address, port = 6667, use_ssl = False):
'''Connect to an IRC network
'''Connect to an IRC network:
address - The network address of the IRC network
port - On optional argument that specifies the port to connect on
ssl - A boolean argument specifying wether or not to use SSL'''
ssl - A boolean argument specifying wether or not to use SSL
'''
print('[*] Connecting to %s:%s; SSL: %s' % (address, port, use_ssl))
connections[address] = Connection(address, port, use_ssl)
connections[address].load_modules()

class User(str):
'''An object which stores data on a user. It subclasses the str
object to maintain backwards compatibility.
'''
pass

def loop():
'''Start the backend loop.'''
asyncore.loop()
2 changes: 1 addition & 1 deletion bbot-makeconf
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Please note that this setting takes no effect on servers that
perform ident checks and there's an active identd running.''')
config.set('main', 'ident', raw_input('ident: '))

# Nickname
# Real Name
print('''
What do you want your bot's realname to be?
This is only shown in the WHOIS.''')
Expand Down

0 comments on commit 93aecc9

Please sign in to comment.