An IRC bot built for tubes
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

103 linhas
2.9KB

  1. # See the LICENSE file for licensing information
  2. '''The bot itself.'''
  3. import re
  4. from time import sleep
  5. from . import tubes
  6. from . import sed
  7. class Queue:
  8. size = 0
  9. data = []
  10. head = 0
  11. tail = 0
  12. count = 0
  13. def __init__(self, size):
  14. self.size = size
  15. self.data = [None]*size
  16. def enqueue(self, s):
  17. if not self.full():
  18. self.count += 1
  19. self.data[self.tail] = s
  20. self.tail = (self.tail + 1) % self.size
  21. def dequeue(self):
  22. if not self.empty():
  23. self.count -= 1
  24. s = self.data[self.head]
  25. self.head = (self.head + 1) % self.size
  26. return s
  27. def full(self):
  28. return self.size == self.count
  29. def empty(self):
  30. return self.head == self.count
  31. def find(self, s, f=0):
  32. i = self.tail-1
  33. while True:
  34. if i == -1:
  35. i = self.size-1
  36. if re.search(s, self.data[i], f):
  37. return self.data[i]
  38. i -= 1
  39. if i == self.tail-1 or self.data[i] is None:
  40. return False
  41. def main():
  42. nick = 'seddy'
  43. server = 'lain.church'
  44. channel = '#lain'
  45. gecos = 'A text processing bot'
  46. history = Queue(48)
  47. tube = tubes.Tube(server)
  48. ping = re.compile('PING :(.*)')
  49. parse_message = re.compile('.* PRIVMSG {} :(.*)'.format(channel))
  50. slash_sed = re.compile('^s/.*/.*')
  51. comma_sed = re.compile('^s,.*,.*')
  52. slash_parser = re.compile('(?<!\\\\)/')
  53. comma_parser = re.compile('(?<!\\\\),')
  54. tube.identify(nick, gecos)
  55. sleep(5)
  56. tube.join(channel)
  57. with open(tube.incoming, 'r', encoding='utf-8') as receive:
  58. for line in receive:
  59. if re.match(ping, line):
  60. tube.pong(ping.split(line)[1])
  61. continue
  62. tmp = parse_message.match(line)
  63. try:
  64. message = tmp.group(1)
  65. except:
  66. continue
  67. if history.full():
  68. history.dequeue()
  69. if not slash_sed.match(message) and not comma_sed.match(message):
  70. history.enqueue(message)
  71. if slash_sed.match(message):
  72. response = sed.seddy(message, history, slash_parser)
  73. if response:
  74. response = re.sub('\\\\/', '/', response)
  75. if history.full():
  76. history.dequeue()
  77. history.enqueue(response)
  78. tube.privmsg(response, channel)
  79. elif comma_sed.match(message):
  80. response = sed.seddy(message, history, comma_parser)
  81. if response:
  82. response = re.sub('\\\\,', ',', response)
  83. if history.full():
  84. history.dequeue()
  85. history.enqueue(response)
  86. tube.privmsg(response, channel)