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

servermap 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #! /usr/bin/env python
  2. #
  3. # Example program using irclib.py.
  4. #
  5. # Copyright (C) 1999-2002 Joel Rosdahl
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2.1 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this library; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. #
  21. # Joel Rosdahl <joel@rosdahl.net>
  22. #
  23. # servermap connects to an IRC server and finds out what other IRC
  24. # servers there are in the net and prints a tree-like map of their
  25. # interconnections.
  26. #
  27. # Example:
  28. #
  29. # % ./servermap irc.dal.net somenickname
  30. # Connecting to server...
  31. # Getting links...
  32. #
  33. # 26 servers (18 leaves and 8 hubs)
  34. #
  35. # splitrock.tx.us.dal.net
  36. # `-vader.ny.us.dal.net
  37. # |-twisted.ma.us.dal.net
  38. # |-sodre.nj.us.dal.net
  39. # |-glass.oh.us.dal.net
  40. # |-distant.ny.us.dal.net
  41. # | |-algo.se.eu.dal.net
  42. # | | |-borg.se.eu.dal.net
  43. # | | | `-ced.se.eu.dal.net
  44. # | | |-viking.no.eu.dal.net
  45. # | | |-inco.fr.eu.dal.net
  46. # | | |-paranoia.se.eu.dal.net
  47. # | | |-gaston.se.eu.dal.net
  48. # | | | `-powertech.no.eu.dal.net
  49. # | | `-algo-u.se.eu.dal.net
  50. # | |-philly.pa.us.dal.net
  51. # | |-liberty.nj.us.dal.net
  52. # | `-jade.va.us.dal.net
  53. # `-journey.ca.us.dal.net
  54. # |-ion.va.us.dal.net
  55. # |-dragons.ca.us.dal.net
  56. # |-toronto.on.ca.dal.net
  57. # | `-netropolis-r.uk.eu.dal.net
  58. # | |-traced.de.eu.dal.net
  59. # | `-lineone.uk.eu.dal.net
  60. # `-omega.ca.us.dal.net
  61. import irclib
  62. import sys
  63. if len(sys.argv) != 3:
  64. print "Usage: servermap <server[:port]> <nickname>"
  65. sys.exit(1)
  66. links = []
  67. def on_connect(connection, event):
  68. sys.stdout.write("\nGetting links...")
  69. sys.stdout.flush()
  70. connection.links()
  71. def on_passwdmismatch(connection, event):
  72. print "Password required."
  73. sys.exit(1)
  74. def on_links(connection, event):
  75. global links
  76. links.append((event.arguments()[0],
  77. event.arguments()[1],
  78. event.arguments()[2]))
  79. def on_endoflinks(connection, event):
  80. global links
  81. print "\n"
  82. m = {}
  83. for (to_node, from_node, desc) in links:
  84. if from_node != to_node:
  85. m[from_node] = m.get(from_node, []) + [to_node]
  86. if connection.get_server_name() in m:
  87. if len(m[connection.get_server_name()]) == 1:
  88. hubs = len(m) - 1
  89. else:
  90. hubs = len(m)
  91. else:
  92. hubs = 0
  93. print "%d servers (%d leaves and %d hubs)\n" % (len(links), len(links)-hubs, hubs)
  94. print_tree(0, [], connection.get_server_name(), m)
  95. connection.quit("Using irclib.py")
  96. def on_disconnect(connection, event):
  97. sys.exit(0)
  98. def indent_string(level, active_levels, last):
  99. if level == 0:
  100. return ""
  101. s = ""
  102. for i in range(level-1):
  103. if i in active_levels:
  104. s = s + "| "
  105. else:
  106. s = s + " "
  107. if last:
  108. s = s + "`-"
  109. else:
  110. s = s + "|-"
  111. return s
  112. def print_tree(level, active_levels, root, map, last=0):
  113. sys.stdout.write(indent_string(level, active_levels, last)
  114. + root + "\n")
  115. if root in map:
  116. list = map[root]
  117. for r in list[:-1]:
  118. print_tree(level+1, active_levels[:]+[level], r, map)
  119. print_tree(level+1, active_levels[:], list[-1], map, 1)
  120. s = sys.argv[1].split(":", 1)
  121. server = s[0]
  122. if len(s) == 2:
  123. try:
  124. port = int(s[1])
  125. except ValueError:
  126. print "Error: Erroneous port."
  127. sys.exit(1)
  128. else:
  129. port = 6667
  130. nickname = sys.argv[2]
  131. irc = irclib.IRC()
  132. sys.stdout.write("Connecting to server...")
  133. sys.stdout.flush()
  134. try:
  135. c = irc.server().connect(server, port, nickname)
  136. except irclib.ServerConnectionError, x:
  137. print x
  138. sys.exit(1)
  139. c.add_global_handler("welcome", on_connect)
  140. c.add_global_handler("passwdmismatch", on_passwdmismatch)
  141. c.add_global_handler("links", on_links)
  142. c.add_global_handler("endoflinks", on_endoflinks)
  143. c.add_global_handler("disconnect", on_disconnect)
  144. irc.process_forever()