irclib -- Internet Relay Chat (IRC) protocol client library

irccat2 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #! /usr/bin/env python
  2. #
  3. # Example program using irclib.py.
  4. #
  5. # This program is free without restrictions; do anything you like with
  6. # it.
  7. #
  8. # Joel Rosdahl <joel@rosdahl.net>
  9. import irclib
  10. import sys
  11. class IRCCat(irclib.SimpleIRCClient):
  12. def __init__(self, target):
  13. irclib.SimpleIRCClient.__init__(self)
  14. self.target = target
  15. def on_welcome(self, connection, event):
  16. if irclib.is_channel(self.target):
  17. connection.join(self.target)
  18. else:
  19. self.send_it()
  20. def on_join(self, connection, event):
  21. self.send_it()
  22. def on_disconnect(self, connection, event):
  23. sys.exit(0)
  24. def send_it(self):
  25. while 1:
  26. line = sys.stdin.readline()
  27. if not line:
  28. break
  29. self.connection.privmsg(self.target, line)
  30. self.connection.quit("Using irclib.py")
  31. def main():
  32. if len(sys.argv) != 4:
  33. print "Usage: irccat2 <server[:port]> <nickname> <target>"
  34. print "\ntarget is a nickname or a channel."
  35. sys.exit(1)
  36. s = sys.argv[1].split(":", 1)
  37. server = s[0]
  38. if len(s) == 2:
  39. try:
  40. port = int(s[1])
  41. except ValueError:
  42. print "Error: Erroneous port."
  43. sys.exit(1)
  44. else:
  45. port = 6667
  46. nickname = sys.argv[2]
  47. target = sys.argv[3]
  48. c = IRCCat(target)
  49. try:
  50. c.connect(server, port, nickname)
  51. except irclib.ServerConnectionError, x:
  52. print x
  53. sys.exit(1)
  54. c.start()
  55. if __name__ == "__main__":
  56. main()