An IRC bot built for tubes
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

115 řádky
2.8KB

  1. #!/usr/bin/python3
  2. # See the LICENSE file for licensing information
  3. import re
  4. nick = 'seddy'
  5. server = 'chat.freenode.net'
  6. channel = '#example'
  7. gecos = 'A text processing bot'
  8. receive = open('/tmp/' + server + '.in', 'r')
  9. send = open('/tmp/' + server + '.out', 'w')
  10. parse_msg = re.compile(' :(.*)')
  11. parse_sed = re.compile('(?<!\\\\)/')
  12. is_sed = re.compile('^s/.*/.*')
  13. class Queue:
  14. size = 0
  15. data = []
  16. head = 0
  17. tail = 0
  18. count = 0
  19. def __init__(self, size):
  20. self.size = size
  21. self.data = [None]*size
  22. def enqueue(self, s):
  23. if not self.full():
  24. self.count += 1
  25. self.data[self.tail] = s
  26. self.tail = (self.tail + 1) % self.size
  27. def dequeue(self):
  28. if not self.empty():
  29. self.count -= 1
  30. s = self.data[self.head]
  31. self.head = (self.head + 1) % self.size
  32. return s
  33. def full(self):
  34. return self.size == self.count
  35. def empty(self):
  36. return self.head == self.count
  37. def find(self, s):
  38. i = self.tail-1
  39. while True:
  40. if i == -1:
  41. i = self.size-1
  42. if re.search(s, self.data[i]):
  43. return self.data[i]
  44. i -= 1
  45. if i == self.tail-1:
  46. return False
  47. def seddy(sed, history):
  48. regex = parse_sed.split(sed)
  49. if len(regex) < 4:
  50. return False
  51. msg = history.find(regex[1])
  52. f = 0
  53. if msg == False:
  54. return False
  55. if 'i' in regex[3]:
  56. f |= re.I
  57. if "g" in regex[3]:
  58. return re.sub(regex[1], regex[2], msg, flags=f)
  59. else:
  60. return re.sub(regex[1], regex[2], msg, 1)
  61. def notice(msg):
  62. send.write('NOTICE ' + channel + ' :' + msg + '\r\n')
  63. send.flush()
  64. def privmsg(msg):
  65. send.write('PRIVMSG ' + channel + ' :' + msg + '\r\n')
  66. send.flush()
  67. if __name__ == "__main__":
  68. history = Queue(48)
  69. send.write('NICK ' + nick + '\r\n')
  70. send.flush()
  71. send.write('USER ' + nick + ' * 8 :' + gecos + '\r\n')
  72. send.flush()
  73. send.write('JOIN ' + channel + '\r\n')
  74. send.flush()
  75. for line in receive:
  76. msg = parse_msg.search(line)
  77. if msg is None:
  78. continue
  79. else:
  80. msg = msg.group(1)
  81. if history.full():
  82. history.dequeue()
  83. if 'PRIVMSG' in line and not is_sed.match(msg):
  84. history.enqueue(msg)
  85. if 'PING' in line:
  86. send.write('PONG\r\n')
  87. send.flush()
  88. if '.bots' in msg[:5] or '.bot ' + nick in msg[:5 + len(nick)]:
  89. notice("I was written to correct your mistakes.")
  90. if '.source ' + nick in msg[:8 + len(nick)]:
  91. notice('[Python] https://github.com/sys-fs/seddy')
  92. elif is_sed.match(msg):
  93. foo = seddy(msg, history)
  94. if foo != False:
  95. privmsg(re.sub('\\\\/', '/', foo))