A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

connection.py 3.0 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):
  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. `data` is
  51. a Data object (or anything with chan and nick attributes)."""
  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)