Skip to content

Instantly share code, notes, and snippets.

@vansteki
Created August 8, 2012 15:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vansteki/3295743 to your computer and use it in GitHub Desktop.
Save vansteki/3295743 to your computer and use it in GitHub Desktop.
ruby play ptt XD
#origin: http://godspeedlee.sg1004.myweb.hinet.net/ruby/ptt1/play-ptt-example1.rb
#Big5查表: http://cccii.ncl.edu.tw/unicode/cu3.asp
require 'net/telnet'
if ARGV.size != 2 then
print("play-ptt-example1.rb ID PASSWORD\n")
exit(0)
end
WaitForInput = '(?>\s+)(?>\x08+)'
AnsiSetDisplayAttr = '\x1B\[(?>(?>(?>\d+;)*\d+)?)m'
AnsiCursorHome = '\x1B\[(?>(?>\d+;\d+)?)H'
# '請(按任意鍵繼續)'
PressAnyKey = '\xAB\xF6\xA5\xF4\xB7\x4E\xC1\xE4\xC4\x7E\xC4\xF2'
# '請(按任意鍵繼續) ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄' \xA2\x65 -> ▄
PressAnyKeyToContinue = "#{PressAnyKey}(?>\\s*)#{AnsiSetDisplayAttr}(?>(?:\\xA2\\x65)+)\s*#{AnsiSetDisplayAttr}"
Big5Code = '[\xA1-\xF9][\x40-\xF0]'
tn = Net::Telnet.new('Host' => "ptt.cc", # 這個不用解釋吧?
'Port' => 23, # 通常是 23
'Timeout' => 5, # connect, read/write timeout
'Waittime' => 1 # 暫且不用理解這個參數
)
tn.waitfor(/guest.+new(?>[^:]+):(?>\s*)#{AnsiSetDisplayAttr}#{WaitForInput}\Z/){
|s| print(s)
}
# 帳號
tn.cmd( 'String' => ARGV[0],
# "您的密碼: "(注意多一個空白字元)
'Match' => /\xB1\x4B\xBD\x58:(?>\s*)\Z/) {
|s| print(s)
}
tn.cmd( 'String' => ARGV[1],
'Match' => /#{PressAnyKeyToContinue}\Z/){
|s| print(s)
}
# 相當於按下 Enter
tn.print("\n")
# Cursor Home
# [呼叫器]
tn.waitfor(/\[\xA9\x49\xA5\x73\xBE\xB9\]#{AnsiSetDisplayAttr}.+#{AnsiCursorHome}\Z/){ |s| print(s) }
tn.print("s")
tn.waitfor(/\):(?>\s*)#{AnsiSetDisplayAttr}(?>\s*)#{AnsiSetDisplayAttr}.*#{AnsiCursorHome}\Z/){
|s| print(s)
}
# (b)進板畫面
ArticleList = '\(b\)' +
"#{AnsiSetDisplayAttr}" +
'\xB6\x69\xAA\x4F\xB5\x65\xAD\xB1\s*' +
"#{AnsiSetDisplayAttr}#{AnsiCursorHome}"
lines = tn.cmd( "String" => "test",
"Match" => /(?>#{PressAnyKeyToContinue}|#{ArticleList})\Z/ ) do |s|
print(s)
end
if /#{PressAnyKeyToContinue}\Z/ =~ lines
tn.print("\n")
tn.waitfor(/#{ArticleList}\Z/){ |s| print(s) }
end
Big5Code = '[\xA1-\xF9][\x40-\xF0]'
tn.print("\x10")
tn.waitfor(/\((?>\d+)-(?>\d+)(?>(?:#{Big5Code})+)\).*#{AnsiCursorHome}\Z/m){
|s| print(s)
}
tn.cmd( "String" => "1",
"Match" => /\xBC\xD0\xC3\x44\xA1\x47.*#{AnsiCursorHome}\Z/){
|s| print(s)
}
tn.cmd( "String" => "this is a test!",
"Match" => /1\s*:\s*1\s*#{AnsiSetDisplayAttr}#{AnsiCursorHome}\Z/){
|s| print(s)
}
tn.print("This article is posted by Ruby!")
tn.waitfor(/#{AnsiCursorHome}#{AnsiSetDisplayAttr}(?>\d+)(?>(?:\s*:\s*\d+)?)#{AnsiSetDisplayAttr}#{AnsiCursorHome}\Z/){
|s| print(s)
}
AnsiEraseEOL = '\x1B\[K'
tn.print("\x18")
tn.waitfor(/(?>(?:#{AnsiEraseEOL}\r?\n)+)(?>(?:#{AnsiEraseEOL})?)#{AnsiCursorHome}\Z/){
|s| print(s)
}
Signature = '\xC3\xB1\xA6\x57\xC0\xC9\.(?>\d+).+' + "#{AnsiCursorHome}"
# 存檔
lines = tn.cmd( "String" => "s", "Match" => /(?:#{PressAnyKeyToContinue}|#{Signature})\Z/m) do |s|
print(s)
end
# 假如有簽名檔...
if /#{Signature}\Z/m =~ lines
# 選擇簽名檔 1
tn.cmd( "String" => "1",
"Match" => /#{PressAnyKeyToContinue}\Z/ ) do |s|
print(s)
end
end
# 按任意鍵離開
tn.print("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment