import socket from pysqlite2 import dbapi2 as sqlite import os, sys, string mypath = os.path.dirname(os.path.abspath(__file__)) databasedir = os.path.join(mypath, 'brain.db') ############################ my_nick = 'py_boot' network = 'irc.freenode.net' port = 6667 channel = '#test_py' ############################ 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 Spieprzaj_dziadu spieprzaj_dziadu Spieprzaj_dziadu :Python IRC\r\n' ) irc.send ( 'JOIN %s\r\n' % channel ) irc.send ( 'PRIVMSG %s : Witam, jestem testowym botem irca. Aby uzyskac wiecej informacji wpisz py_boot: help. \r\n' % channel ) class DataBase(object): databasedir = os.path.join( os.path.abspath(sys.argv[0]), 'brain.db') def __init__(self): self.con = sqlite.connect(databasedir) self.cur = self.con.cursor() def show_commands(self): self.cur.execute('SELECT command FROM knowledge') commands_list = '' for output in self.cur.fetchall(): commands_list += ' |*| %s ' % output commands_list += ' |*|' irc.send('PRIVMSG %s : %s\r\n' % (channel, commands_list) ) def write_to(self, command, text): self.cur.execute('INSERT INTO knowledge (command, text) VALUES (?, ?)', (command, text)) self.con.commit() def show_cmd(self, msg): self.cur.execute("SELECT text FROM knowledge WHERE command = ?", (msg, ) ) output = '' for output in self.cur.fetchall(): commands_list += ' | %s' % output if output != '' : irc.send('PRIVMSG %s : %s --> %s \r\n' % (channel, nick, output ) ) else: irc.send('PRIVMSG %s : No %s command found you dirty fucker! RTFM \r\n' % (channel, msg) ) 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 'py_boot: show_commands' in message : try: DataBase().show_commands() except: print '\n\n********************\nCOS POSZLO NIE TAK PRZY POKAZYWANIU BAZY! \n********************\n\n' elif 'py_boot: command:' in message[ :17 ] : try: message = message[17:] DataBase().show_cmd(message) except: irc.send('PRIVMSG %s : %s >> ERROR \r\n' % (channel, nick)) elif 'py_boot: add_command:' in message : try: a, b = message[21:].split('|') DataBase().write_to(a, b) except: print '************\nERROR\n**************' elif 'py_boot == exit' in message : irc.close() elif 'py_boot: help' in message : irc.send('PRIVMSG %s :>>> Aby wyswietlic spis temotow, wpisz py_boot: show_commands. Aby dodac nowy wpis, py_boot: add_command:temat|wiadomosc. Aby mnie wywalic wcisnij Alt+F4 i wpisz py_boot == exit \r\n' % channel) print nick + ' | ', message[:-1]