An IRC bot built for tubes
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

138 行
3.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 = '/tmp/{0}.in'.format(server)
  9. send = '/tmp/{0}.out'.format(server)
  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 msg_replace(find, replace, msg, f, n=1):
  48. try:
  49. if msg[:7] == '\x01ACTION':
  50. res = '\x01ACTION {0}'.format(re.sub(find, replace, msg[8:], count=n, flags=f))
  51. else:
  52. res = re.sub(find, replace, msg, count=n, flags=f)
  53. except:
  54. return False
  55. return res
  56. def seddy(sed, history, parser):
  57. f = 0
  58. regex = parser.split(sed)
  59. if len(regex) < 4:
  60. return False
  61. if 'i' in regex[3]:
  62. f |= re.I
  63. try:
  64. msg = history.find(regex[1], f)
  65. except:
  66. return False
  67. if "g" in regex[3]:
  68. res = msg_replace(regex[1], regex[2], msg, 0, f)
  69. else:
  70. res = msg_replace(regex[1], regex[2], msg, 1, f)
  71. if res:
  72. res = res.replace('\0', re.search(regex[1], msg, f).group(0))
  73. return res
  74. def notice(msg, channel):
  75. with open(send, 'w', encoding='utf-8') as g:
  76. g.write('NOTICE ' + channel + ' :' + msg + '\r\n')
  77. def privmsg(msg, channel):
  78. if msg[:7] == '\x01ACTION':
  79. with open(send, 'w', encoding='utf-8') as g:
  80. g.write('PRIVMSG ' + channel + ' :' + '\x01' +
  81. ''.join(c for c in msg if c.isprintable()) + '\x01\r\n')
  82. else:
  83. with open(send, 'w', encoding='utf-8') as g:
  84. g.write('PRIVMSG ' + channel + ' :' +
  85. msg.replace('\n', ' ',).replace('\r', '') + '\r\n')
  86. if __name__ == "__main__":
  87. history = Queue(48)
  88. with open(send, 'w', encoding='utf-8') as f:
  89. f.write('NICK ' + nick + '\r\n')
  90. f.write('USER ' + nick + ' * 8 :' + gecos + '\r\n')
  91. sleep(5)
  92. f.write('JOIN ' + channel + '\r\n')
  93. with open(receive, 'r', encoding='utf-8') as f:
  94. for line in f:
  95. if 'PING' in line:
  96. with open(send, 'w', encoding='utf-8') as g:
  97. g.write('PONG {0}\r\n'.format(ping.split(line)[1]))
  98. continue
  99. m = parse_msg.match(line)
  100. try:
  101. channel = m.group(1)
  102. msg = m.group(2)
  103. except:
  104. continue
  105. if history.full():
  106. history.dequeue()
  107. if 'PRIVMSG' in line and not is_sed.match(msg):
  108. history.enqueue(msg)
  109. if '.bots' in msg[:5] or '.bot ' + nick in msg[:5 + len(nick)]:
  110. notice("I was written to correct your mistakes.")
  111. if '.source ' + nick in msg[:8 + len(nick)]:
  112. notice('[Python] https://github.com/sys-fs/seddy')
  113. elif is_sed.match(msg):
  114. res = seddy(msg, history)
  115. if res:
  116. privmsg(re.sub('\\\\/', '/', foo))