A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

connection.py 2.9 KiB

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