Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- libircbot.py:
- import socket
- import time
- class libircbot(object):
- def __init__(self):
- self.socketObj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- self.logObj = open('./ircbot.log', 'a+')
- self.buffer = []
- self.console = ''
- def sendToConsole(self, data):
- self.console += str(data)
- def sendToLog(self, data):
- self.logObj.write(data)
- def getRawData(self):
- data = self.socketObj.recv(4096)
- #self.sendToLog(time.strftime('[%H:%I:%S][-->] ') + data + "\r\n")
- #self.sendToConsole(time.strftime('[%H:%I:%S][-->] ') + data + "\r\n")
- return data
- def sendRawData(self, data):
- self.socketObj.send(data[:510] + "\r\n")
- self.sendToLog(time.strftime('[%H:%I:%S][<--] ') + data + "\r\n")
- self.sendToConsole(time.strftime('[%H:%I:%S][<--] ') + data + "\r\n")
- def setNick(self, nick):
- self.sendRawData('NICK ' + nick)
- def setName(self, ident, name):
- self.sendRawData('USER ' + ident + ' 0 0 :' + name)
- def joinChans(self, chans):
- for chan in chans:
- self.sendRawData('JOIN ' + chan)
- def pong(self, ping):
- self.sendRawData('PONG ' + ping)
- def closeConnection(self):
- self.socketObj.close()
- def closeLogfile(self):
- self.logObj.close()
- ircbot.py:
- #!/usr/bin/env python
- import sys
- import time
- from libircbot import *
- from config import *
- irc = libircbot()
- try:
- irc.socketObj.connect((config['host'], config['port']))
- except IOError:
- print IOError
- irc.setNick(config['nick'])
- irc.setName(config['ident'], config['name'])
- while 1:
- tmp = irc.getRawData().split("\r\n")
- tmp.remove('')
- irc.buffer.extend(tmp)
- print irc.buffer
- for line in irc.buffer:
- data = line.rstrip().split(' ')
- if data[0][:1] == ':':
- sender = data.pop(0)
- if data[0] == 'PING':
- irc.pong(data[1][1:])
- if data[0] == '001':
- irc.joinChans(config['chans'])
- print irc.console
- time.sleep(1)
- irc.buffer = []
- masticore@ubuntu:/media/WD Passport_/portfolio/python/ircbot$ ./ircbot.py
- ['NOTICE AUTH :*** Looking up your hostname']
- [21:09:11][<--] NICK ubrukeligBot2
- [21:09:11][<--] USER lamer 0 0 :lamer
- ['NOTICE AUTH :*** Checking Ident', 'PING :1658817574', 'NOTICE AUTH :*** Found your hostname']
- [21:09:11][<--] NICK ubrukeligBot2
- [21:09:11][<--] USER lamer 0 0 :lamer
- [21:09:11][<--] NICK ubrukeligBot2
- [21:09:11][<--] USER lamer 0 0 :lamer
- [21:09:13][<--] PONG 1658817574
- [21:09:11][<--] NICK ubrukeligBot2
- [21:09:11][<--] USER lamer 0 0 :lamer
- [21:09:13][<--] PONG 1658817574
- ['NOTICE AUTH :*** No ident response']
- [21:09:11][<--] NICK ubrukeligBot2
- [21:09:11][<--] USER lamer 0 0 :lamer
- [21:09:13][<--] PONG 1658817574
- Traceback (most recent call last):
- File "./ircbot.py", line 19, in <module>
- tmp.remove('')
- ValueError: list.remove(x): x not in list
Add Comment
Please, Sign In to add comment