Guest User

Untitled

a guest
Jun 19th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. libircbot.py:
  2.  
  3. import socket
  4. import time
  5.  
  6. class libircbot(object):
  7.  
  8. def __init__(self):
  9. self.socketObj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  10. self.logObj = open('./ircbot.log', 'a+')
  11. self.buffer = []
  12. self.console = ''
  13.  
  14. def sendToConsole(self, data):
  15. self.console += str(data)
  16.  
  17. def sendToLog(self, data):
  18. self.logObj.write(data)
  19.  
  20. def getRawData(self):
  21. data = self.socketObj.recv(4096)
  22. #self.sendToLog(time.strftime('[%H:%I:%S][-->] ') + data + "\r\n")
  23. #self.sendToConsole(time.strftime('[%H:%I:%S][-->] ') + data + "\r\n")
  24. return data
  25.  
  26. def sendRawData(self, data):
  27. self.socketObj.send(data[:510] + "\r\n")
  28. self.sendToLog(time.strftime('[%H:%I:%S][<--] ') + data + "\r\n")
  29. self.sendToConsole(time.strftime('[%H:%I:%S][<--] ') + data + "\r\n")
  30.  
  31. def setNick(self, nick):
  32. self.sendRawData('NICK ' + nick)
  33.  
  34. def setName(self, ident, name):
  35. self.sendRawData('USER ' + ident + ' 0 0 :' + name)
  36.  
  37. def joinChans(self, chans):
  38. for chan in chans:
  39. self.sendRawData('JOIN ' + chan)
  40.  
  41. def pong(self, ping):
  42. self.sendRawData('PONG ' + ping)
  43.  
  44. def closeConnection(self):
  45. self.socketObj.close()
  46.  
  47. def closeLogfile(self):
  48. self.logObj.close()
  49.  
  50. ircbot.py:
  51.  
  52. #!/usr/bin/env python
  53.  
  54. import sys
  55. import time
  56. from libircbot import *
  57. from config import *
  58.  
  59. irc = libircbot()
  60.  
  61. try:
  62. irc.socketObj.connect((config['host'], config['port']))
  63. except IOError:
  64. print IOError
  65.  
  66. irc.setNick(config['nick'])
  67. irc.setName(config['ident'], config['name'])
  68. while 1:
  69. tmp = irc.getRawData().split("\r\n")
  70. tmp.remove('')
  71. irc.buffer.extend(tmp)
  72. print irc.buffer
  73. for line in irc.buffer:
  74. data = line.rstrip().split(' ')
  75. if data[0][:1] == ':':
  76. sender = data.pop(0)
  77. if data[0] == 'PING':
  78. irc.pong(data[1][1:])
  79. if data[0] == '001':
  80. irc.joinChans(config['chans'])
  81. print irc.console
  82. time.sleep(1)
  83. irc.buffer = []
  84.  
  85.  
  86.  
  87. masticore@ubuntu:/media/WD Passport_/portfolio/python/ircbot$ ./ircbot.py
  88. ['NOTICE AUTH :*** Looking up your hostname']
  89. [21:09:11][<--] NICK ubrukeligBot2
  90. [21:09:11][<--] USER lamer 0 0 :lamer
  91.  
  92. ['NOTICE AUTH :*** Checking Ident', 'PING :1658817574', 'NOTICE AUTH :*** Found your hostname']
  93. [21:09:11][<--] NICK ubrukeligBot2
  94. [21:09:11][<--] USER lamer 0 0 :lamer
  95.  
  96. [21:09:11][<--] NICK ubrukeligBot2
  97. [21:09:11][<--] USER lamer 0 0 :lamer
  98. [21:09:13][<--] PONG 1658817574
  99.  
  100. [21:09:11][<--] NICK ubrukeligBot2
  101. [21:09:11][<--] USER lamer 0 0 :lamer
  102. [21:09:13][<--] PONG 1658817574
  103.  
  104. ['NOTICE AUTH :*** No ident response']
  105. [21:09:11][<--] NICK ubrukeligBot2
  106. [21:09:11][<--] USER lamer 0 0 :lamer
  107. [21:09:13][<--] PONG 1658817574
  108.  
  109. Traceback (most recent call last):
  110. File "./ircbot.py", line 19, in <module>
  111. tmp.remove('')
  112. ValueError: list.remove(x): x not in list
Add Comment
Please, Sign In to add comment