A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
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.

94 lines
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. import socket
  3. import threading
  4. class BrokenSocketException(Exception):
  5. """A socket has broken, because it is not sending data. Raised by
  6. Connection.get()."""
  7. pass
  8. class Connection(object):
  9. """A class to interface with IRC."""
  10. def __init__(self, host=None, port=None, nick=None, ident=None,
  11. realname=None, logger=None):
  12. self.host = host
  13. self.port = port
  14. self.nick = nick
  15. self.ident = ident
  16. self.realname = realname
  17. self.logger = logger
  18. # A lock to prevent us from sending two messages at once:
  19. self.lock = threading.Lock()
  20. def connect(self):
  21. """Connect to our IRC server."""
  22. self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  23. try:
  24. self.sock.connect((self.host, self.port))
  25. except socket.error:
  26. self.logger.critical("Couldn't connect to IRC server", exc_info=1)
  27. exit(1)
  28. self.send("NICK %s" % self.nick)
  29. self.send("USER %s %s * :%s" % (self.ident, self.host, self.realname))
  30. def close(self):
  31. """Close our connection with the IRC server."""
  32. try:
  33. self.sock.shutdown(socket.SHUT_RDWR) # shut down connection first
  34. except socket.error:
  35. pass # ignore if the socket is already down
  36. self.sock.close()
  37. def get(self, size=4096):
  38. """Receive (i.e. get) data from the server."""
  39. data = self.sock.recv(4096)
  40. if not data:
  41. # Socket isn't giving us any data, so it is dead or broken:
  42. raise BrokenSocketException()
  43. return data
  44. def send(self, msg):
  45. """Send data to the server."""
  46. # Ensure that we only send one message at a time with a blocking lock:
  47. with self.lock:
  48. self.sock.sendall(msg + "\r\n")
  49. self.logger.debug(msg)
  50. def say(self, target, msg):
  51. """Send a private message to a target on the server."""
  52. message = "".join(("PRIVMSG ", target, " :", msg))
  53. self.send(message)
  54. def reply(self, data, msg):
  55. """Send a private message as a reply to a user on the server."""
  56. message = "".join((chr(2), data.nick, chr(0x0f), ": ", msg))
  57. self.say(data.chan, message)
  58. def action(self, target, msg):
  59. """Send a private message to a target on the server as an action."""
  60. message = "".join((chr(1), "ACTION ", msg, chr(1)))
  61. self.say(target, message)
  62. def notice(self, target, msg):
  63. """Send a notice to a target on the server."""
  64. message = "".join(("NOTICE ", target, " :", msg))
  65. self.send(message)
  66. def join(self, chan):
  67. """Join a channel on the server."""
  68. message = " ".join(("JOIN", chan))
  69. self.send(message)
  70. def part(self, chan):
  71. """Part from a channel on the server."""
  72. message = " ".join(("PART", chan))
  73. self.send(message)
  74. def mode(self, chan, level, msg):
  75. """Send a mode message to the server."""
  76. message = " ".join(("MODE", chan, level, msg))
  77. self.send(message)