An IRC bot built for tubes
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

122 Zeilen
3.0KB

  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. try:
  59. res = re.sub(regex[1], regex[2], msg, flags=f)
  60. except:
  61. res = False
  62. else:
  63. try:
  64. res = re.sub(regex[1], regex[2], msg, 1, f)
  65. except:
  66. res = False
  67. return res
  68. def notice(msg):
  69. send.write('NOTICE ' + channel + ' :' + msg + '\r\n')
  70. send.flush()
  71. def privmsg(msg):
  72. send.write('PRIVMSG ' + channel + ' :' + msg + '\r\n')
  73. send.flush()
  74. if __name__ == "__main__":
  75. history = Queue(48)
  76. send.write('NICK ' + nick + '\r\n')
  77. send.flush()
  78. send.write('USER ' + nick + ' * 8 :' + gecos + '\r\n')
  79. send.flush()
  80. send.write('JOIN ' + channel + '\r\n')
  81. send.flush()
  82. for line in receive:
  83. msg = parse_msg.search(line)
  84. if msg is None:
  85. if 'PING' in line:
  86. send.write('PONG\r\n')
  87. send.flush()
  88. continue
  89. else:
  90. msg = msg.group(1)
  91. if history.full():
  92. history.dequeue()
  93. if 'PRIVMSG' in line and not is_sed.match(msg):
  94. history.enqueue(msg)
  95. if '.bots' in msg[:5] or '.bot ' + nick in msg[:5 + len(nick)]:
  96. notice("I was written to correct your mistakes.")
  97. if '.source ' + nick in msg[:8 + len(nick)]:
  98. notice('[Python] https://github.com/sys-fs/seddy')
  99. elif is_sed.match(msg):
  100. foo = seddy(msg, history)
  101. if foo != False:
  102. privmsg(re.sub('\\\\/', '/', foo))