Moontalk server and client (provided by many parties)
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

150 Zeilen
3.5KB

  1. #!/usr/bin/ruby
  2. # written by Emil.
  3. # Licensed under the GPLv3 only.
  4. #
  5. # SHORT GUIDE
  6. #
  7. # Press enter to check for new messages, blank lines from you won't be sent.
  8. #
  9. # -serv specifies server,
  10. # -port specifies port,
  11. # -name specifies your display name,
  12. #
  13. # Type "BYE" to stop it after it connects to the moon.
  14. # Type Enter and nothing else to refresh the messages.
  15. # Type messages and send them, hopefully everyone hears you just fine.
  16. #
  17. # Commands (which always begin with a forward slash, case doesn't matter):
  18. #
  19. # Commands must be near the beginning of messages, and will preform their respective action.
  20. #
  21. # BYE, QUIT - exits
  22. # SAY - says something verbatim
  23. # RAW - send a raw message (no prefix)
  24. # CLEAR - clears the terminal
  25. # NICK... - sets the nick
  26. # RECONNECT - reconnects
  27. # DELAY... - sets delays
  28. #
  29. # /nick anon9999
  30. require 'socket'
  31. require 'readline'
  32. @serv = '7ks473deh6ggtwqsvbqdurepv5i6iblpbkx33b6cydon3ajph73sssad.onion'
  33. @port = 50000
  34. @name = "anonymous"
  35. def read_from(socket)
  36. begin # probably too large of a read size...
  37. puts socket.read_nonblock(2 << 16)
  38. rescue
  39. end
  40. end
  41. def raw(socket, msg)
  42. socket.puts(msg)
  43. end
  44. def post(socket, msg)
  45. update_prefix
  46. raw(socket, @prefix + msg)
  47. end
  48. def update_prefix
  49. date = Time.now.utc.strftime("%Y/%m/%d %k:%M")
  50. @prefix = "<#{date} #{@name}> "
  51. end
  52. def connect
  53. puts "Connecting to " + @serv + ":" + @port.to_s + " as " + @name
  54. socket = TCPSocket.new(@serv, @port)
  55. if not socket
  56. puts "Failed to connect."
  57. exit 1
  58. end
  59. return socket
  60. end
  61. def main
  62. begin
  63. h = { "-name" => 1, "-nick" => 1, "-serv" => 2, "-port" => 3 }
  64. ARGV.each_with_index do |element,index|
  65. case h[element]
  66. when h["-name"]
  67. @name = ARGV[index + 1]
  68. when h["-serv"]
  69. @serv = ARGV[index + 1]
  70. when h["-port"]
  71. @port = ARGV[index + 1].to_i
  72. end
  73. end
  74. rescue
  75. end
  76. loop do
  77. begin
  78. socket = connect
  79. delay = 0
  80. h = { "BYE" => 1, "QUIT" => 1, "SAY" => 2, "CLEAR" => 3, "NAME" => 4, "NICK" => 4,
  81. "RC" => 5, "RECONNECT" => 5, "DELAY" => 6, "RAW" => 7 }
  82. update_prefix
  83. while msg = Readline.readline(@prefix, true)
  84. if not msg.empty?
  85. msg.strip!
  86. word = msg.split(/^\/([\w]*)\W+?(.*)?$/)[1..]
  87. if not word.empty?
  88. case h[word[0].upcase]
  89. when h["BYE"]
  90. post(socket, "BYE")
  91. exit 0
  92. when h["CLEAR"]
  93. puts "\e[1;1H\e[2J"
  94. when h["NICK"]
  95. if not word[1].empty?
  96. @name = word[1]
  97. end
  98. when h["RC"]
  99. puts "reconnecting..."
  100. socket.close
  101. socket = connect
  102. when h["DELAY"]
  103. delay = word[1].to_i
  104. when h["SAY"]
  105. if not word.length > 1
  106. word.push("")
  107. end
  108. post(socket, word[1])
  109. when h["RAW"]
  110. begin
  111. raw(socket, (msg.match(/\/?raw (.*)/i)[1]))
  112. puts "raw message sent"
  113. rescue
  114. puts "didn't send that"
  115. end
  116. else
  117. post(socket, msg)
  118. end
  119. else
  120. post(socket, msg)
  121. end
  122. end
  123. read_from(socket)
  124. sleep delay
  125. update_prefix
  126. end
  127. rescue (Errno::ECONNREFUSED Errno::EPIPE)
  128. puts "\n THE HOUSE IS ON FIRE."
  129. ensure
  130. puts "\nSTOPPING NOW."
  131. exit 1
  132. end
  133. end
  134. end
  135. main