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 = '#aaa' ############################ 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. Nie mam zamiaru przeszkadzac, jesli chcesz mnie wylaczyc wpisz py_boot == exit . 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 irc.send('PRIVMSG %s : %s >> %s' % (channel, nick, commands_list) ) def write_to(self, command, text): self.cur.execute('INSERT INTO knowledge (command, text) VALUES (?, ?)', (command, text)) self.con.commit() 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: add_command:' in message : try: a, b = message[21:].split('|') DataBase().write_to(a, b) except: print '************\nERROR\n**************' if 'py_boot == exit' in message : irc.exit() if destination == 'PyIRC': destination = 'PRIVATE' print nick + ' | ', message[:-1]