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.

49 Zeilen
1.8KB

  1. '''Wrappers for interacting with tubes.'''
  2. import os
  3. class Tube:
  4. '''Object for interacting with tubes.'''
  5. def __init__(self, server):
  6. '''Takes a string containing a server name, e.g. chat.freenode.net, and
  7. gets the correct filenames for tubes' named pipes for that server.
  8. '''
  9. self.incoming = os.path.join('/tmp', '{}.in'.format(server))
  10. self.outgoing = os.path.join('/tmp', '{}.out'.format(server))
  11. def identify(self, nick, gecos):
  12. '''Identify ourselves to the ircd.'''
  13. with open(self.outgoing, 'w', encoding='utf-8') as send:
  14. send.write('NICK {}\r\n'.format(nick))
  15. send.write('USER {} * 8 :{}\r\n'.format(nick, gecos))
  16. def join(self, channel):
  17. '''Join a channel.'''
  18. with open(self.outgoing, 'w', encoding='utf-8') as send:
  19. send.write('JOIN {}\r\n'.format(channel))
  20. def notice(self, message, channel):
  21. '''Send a notice.'''
  22. with open(self.outgoing, 'w', encoding='utf-8') as send:
  23. send.write('NOTICE {} :{}\r\n'.format(channel, message))
  24. def privmsg(self, message, channel):
  25. '''Send a message.'''
  26. if message[:7] == '\x01ACTION':
  27. with open(self.outgoing, 'w', encoding='utf-8') as send:
  28. sanitised = ''.join(c for c in message if c.isprintable())
  29. send.write('PRIVMSG {} :\x01{}\x01\r\n'.format(channel, sanitised))
  30. else:
  31. with open(self.outgoing, 'w', encoding='utf-8') as send:
  32. sanitised = message.replace('\n', ' ',).replace('\r', '')
  33. send.write('PRIVMSG {} :{}\r\n'.format(channel, sanitised))
  34. def pong(self, reply):
  35. '''Reply to a ping.'''
  36. with open(self.outgoing, 'w', encoding='utf-8') as send:
  37. send.write('PONG {0}\r\n'.format(reply))