rendered paste bodyimport socketimport osimport sysfrom pysqlite2 import dbapi2 as sqlitemy_nick = 'got_root?'network = 'irc.freenode.net'port = 6667databasedir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'brain.db')channel = '#ubuntu-pl'irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM )irc.connect((network, port))irc.recv(4096)irc.send ( 'NICK %s\r\n' % my_nick )irc.send ( 'USER none none none :irssi\r\n' )irc.send ( 'JOIN %s\r\n' % channel )irc.send ( 'PRIVMSG %s : Witam \r\n' % channel )def send_msg(message): irc.send('PRIVMSG %s :>>> %s \r\n' % (channel, message) )class DataBase(object): def __init__(self): self.con = sqlite.connect(databasedir) self.cur = self.con.cursor() def show_all(self): self.cur.execute('SELECT command FROM knowledge') command_list = '|' for word in self.cur.fetchall(): command_list += '%s|' % word send_msg(command_list) def show_command(self, cmd): self.cur.execute("SELECT message FROM knowledge WHERE command=:cmd", locals()) if self.cur.fetchone(): send_msg('%s >> %s' % (nick, self.cur.fetchall() ) ) else: send_msg('%s >> Nic mi o tym nie wiadomo...' % nick) def add_command(self, command, msg): self.cur.execute('INSERT INTO knowledge(command, message) VALUES (?, ?)', (command, msg)) self.con.commit() send_msg('%s >> Komenda %s zostala przyjeta do bazy' % (nick, command) )while True: data = irc.recv ( 4096 ) if data.find ( 'PING' ) != -1: irc.send ( 'PONG ' + data.split() [ 1 ] + '\r\n' ) elif data.find ( 'PRIVMSG' ) != -1 : nick = data.split ( '!' ) [ 0 ].replace ( ':', '' ) message = ':'.join ( data.split ( ':' ) [ 2: ] ) destination = ''.join ( data.split ( ':' ) [ :2 ] ).split ( ' ' ) [ -2 ] if '%s: show_commands'%my_nick in message : DataBase().show_all() elif '%s: show:' % my_nick in message : a = len(my_nick) + 7 DataBase().show_command(message[a:]) elif ('%s: add_command:' % my_nick and '|') in message : a = len(my_nick) + 14 command, message = message[a:].split('|') DataBase().add_command(command, message) elif '%s: help' % my_nick in message: send_msg('''%s >> Aby pokazac liste dostepnych komend wpisz %s: show_commands, aby dodac komende wpisz %s: add_command:<komenda>|<tekst>, aby mnie zabic wpisz %s: killim. ''' % (nick, my_nick, my_nick, my_nick) ) elif '%s: killim' % my_nick in message : irc.close() elif my_nick in message[:9]: send_msg('Jestem botem ktory moze Ci pomoc. Aby uzyskac wiecej informacji wpisz %s:help' % my_nick) print nick + ' | ', message[:-1]