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.

128 Zeilen
3.2KB

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