A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
4.2 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 by Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. import socket
  23. import threading
  24. __all__ = ["BrokenSocketException", "Connection"]
  25. class BrokenSocketException(Exception):
  26. """A socket has broken, because it is not sending data. Raised by
  27. Connection.get()."""
  28. pass
  29. class Connection(object):
  30. """A class to interface with IRC."""
  31. def __init__(self, host=None, port=None, nick=None, ident=None,
  32. realname=None, logger=None):
  33. self.host = host
  34. self.port = port
  35. self.nick = nick
  36. self.ident = ident
  37. self.realname = realname
  38. self.logger = logger
  39. # A lock to prevent us from sending two messages at once:
  40. self.lock = threading.Lock()
  41. def connect(self):
  42. """Connect to our IRC server."""
  43. self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  44. try:
  45. self.sock.connect((self.host, self.port))
  46. except socket.error:
  47. self.logger.critical("Couldn't connect to IRC server", exc_info=1)
  48. exit(1)
  49. self.send("NICK %s" % self.nick)
  50. self.send("USER %s %s * :%s" % (self.ident, self.host, self.realname))
  51. def close(self):
  52. """Close our connection with the IRC server."""
  53. try:
  54. self.sock.shutdown(socket.SHUT_RDWR) # shut down connection first
  55. except socket.error:
  56. pass # ignore if the socket is already down
  57. self.sock.close()
  58. def get(self, size=4096):
  59. """Receive (i.e. get) data from the server."""
  60. data = self.sock.recv(4096)
  61. if not data:
  62. # Socket isn't giving us any data, so it is dead or broken:
  63. raise BrokenSocketException()
  64. return data
  65. def send(self, msg):
  66. """Send data to the server."""
  67. # Ensure that we only send one message at a time with a blocking lock:
  68. with self.lock:
  69. self.sock.sendall(msg + "\r\n")
  70. self.logger.debug(msg)
  71. def say(self, target, msg):
  72. """Send a private message to a target on the server."""
  73. message = "".join(("PRIVMSG ", target, " :", msg))
  74. self.send(message)
  75. def reply(self, data, msg):
  76. """Send a private message as a reply to a user on the server."""
  77. message = "".join((chr(2), data.nick, chr(0x0f), ": ", msg))
  78. self.say(data.chan, message)
  79. def action(self, target, msg):
  80. """Send a private message to a target on the server as an action."""
  81. message = "".join((chr(1), "ACTION ", msg, chr(1)))
  82. self.say(target, message)
  83. def notice(self, target, msg):
  84. """Send a notice to a target on the server."""
  85. message = "".join(("NOTICE ", target, " :", msg))
  86. self.send(message)
  87. def join(self, chan):
  88. """Join a channel on the server."""
  89. message = " ".join(("JOIN", chan))
  90. self.send(message)
  91. def part(self, chan):
  92. """Part from a channel on the server."""
  93. message = " ".join(("PART", chan))
  94. self.send(message)
  95. def mode(self, chan, level, msg):
  96. """Send a mode message to the server."""
  97. message = " ".join(("MODE", chan, level, msg))
  98. self.send(message)