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

irccat 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. def on_connect(connection, event):
  12. if irclib.is_channel(target):
  13. connection.join(target)
  14. else:
  15. while 1:
  16. line = sys.stdin.readline()
  17. if not line:
  18. break
  19. connection.privmsg(target, line)
  20. connection.quit("Using irclib.py")
  21. def on_join(connection, event):
  22. while 1:
  23. line = sys.stdin.readline()
  24. if not line:
  25. break
  26. connection.privmsg(target, line)
  27. connection.quit("Using irclib.py")
  28. if len(sys.argv) != 4:
  29. print "Usage: irccat <server[:port]> <nickname> <target>"
  30. print "\ntarget is a nickname or a channel."
  31. sys.exit(1)
  32. def on_disconnect(connection, event):
  33. sys.exit(0)
  34. s = sys.argv[1].split(":", 1)
  35. server = s[0]
  36. if len(s) == 2:
  37. try:
  38. port = int(s[1])
  39. except ValueError:
  40. print "Error: Erroneous port."
  41. sys.exit(1)
  42. else:
  43. port = 6667
  44. nickname = sys.argv[2]
  45. target = sys.argv[3]
  46. irc = irclib.IRC()
  47. try:
  48. c = irc.server().connect(server, port, nickname)
  49. except irclib.ServerConnectionError, x:
  50. print x
  51. sys.exit(1)
  52. c.add_global_handler("welcome", on_connect)
  53. c.add_global_handler("join", on_join)
  54. c.add_global_handler("disconnect", on_disconnect)
  55. irc.process_forever()