A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

connection.py 2.9 KiB

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