An IRC bot built for tubes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.9KB

  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(channel + ' :(.*)')
  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, flags=0):
  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], flags):
  43. return self.data[i]
  44. i -= 1
  45. if i == self.tail-1 or self.data[i] is None:
  46. return False
  47. def seddy(sed, history):
  48. flag = 0
  49. regex = parse_sed.split(sed)
  50. if len(regex) < 4:
  51. return False
  52. if 'i' in regex[3]:
  53. flag |= re.I
  54. msg = history.find(regex[1], flag)
  55. if msg == False:
  56. return False
  57. if "g" in regex[3]:
  58. return re.sub(regex[1], regex[2], msg, flags=flag)
  59. else:
  60. return re.sub(regex[1], regex[2], msg, 1, flag)
  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. if 'PING' in line:
  79. send.write('PONG\r\n')
  80. send.flush()
  81. continue
  82. else:
  83. msg = msg.group(1)
  84. if history.full():
  85. history.dequeue()
  86. if 'PRIVMSG' in line and not is_sed.match(msg):
  87. history.enqueue(msg)
  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))