Guest User

Untitled

a guest
Mar 16th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. require 'optparse'
  2. require 'stringio'
  3. require 'rubygems'
  4. require 'twitter'
  5.  
  6. module Twsh
  7. class Logout < StandardError; end
  8. class TwshError < StandardError; end
  9.  
  10. class Shell
  11. class << self
  12. def login(argv, env)
  13. new argv, env
  14. end
  15. end
  16.  
  17. def initialize(argv, env)
  18. username = nil
  19. password = nil
  20.  
  21. OptionParser.new do |opt|
  22. opt.on('-u VAL') {|v| username = v}
  23. opt.on('-p VAL') {|v| password = v}
  24. opt.parse! argv
  25. end
  26.  
  27. fail if username.nil? || password.nil?
  28.  
  29. @client = Twitter::Base.new Twitter::HTTPAuth.new username, password
  30. input = Input.new self
  31. logged_in = true
  32.  
  33. while logged_in
  34. begin
  35. output = execute input.read
  36. puts output if output
  37. rescue Logout
  38. logged_in = false
  39. rescue Interrupt
  40. puts
  41. logged_in = false
  42. rescue TwshError => e
  43. puts e.message
  44. end
  45. end
  46. end
  47.  
  48. attr_reader :client
  49.  
  50. def execute(argv)
  51. return if argv.empty?
  52. command = argv.shift
  53. Command.new(self).execute command, argv
  54. end
  55. end
  56.  
  57. class Command
  58. @@commands = {}
  59.  
  60. class << self
  61. def setup
  62. fail unless block_given?
  63. yield new
  64. end
  65. end
  66.  
  67. def initialize(twsh = nil)
  68. @twsh = twsh
  69. end
  70.  
  71. def on(*commands, &process)
  72. fail unless block_given?
  73. commands.each {|command| @@commands[command] = process}
  74. end
  75.  
  76. def execute(command, argv)
  77. process = @@commands[command]
  78.  
  79. if process
  80. args = []
  81. args << @twsh if process.arity >= 1
  82. args << argv if process.arity >= 2
  83. output = nil
  84.  
  85. StringIO.open '', 'w' do |io|
  86. orig = $stdout
  87. $stdout = io
  88. process.call *args
  89. $stdout = orig
  90. output = io.string unless io.string.empty?
  91. end
  92.  
  93. output
  94. else
  95. `#{command} #{argv.join ' '}`.chomp
  96. end
  97. end
  98. end
  99.  
  100. class Input
  101. def initialize(twsh)
  102. @twsh = twsh
  103. end
  104.  
  105. def read
  106. read_with_context
  107. end
  108.  
  109. private
  110.  
  111. def read_with_context(context = nil, argv = [])
  112. print context ? "#{context}> " : argv.empty? ? 'twsh% ' : '> '
  113. context, argv, continue = parse gets, context, argv
  114. continue ? read_with_context(context, argv) : argv
  115. end
  116.  
  117. def parse(input, context = nil, argv = [])
  118. escape = false
  119. continue = false
  120. v = !argv.empty? ? argv.pop : ''
  121.  
  122. new = lambda do
  123. argv << v unless v.empty?
  124. v = ''
  125. end
  126.  
  127. input.each_byte do |c|
  128. if escape
  129. escape = false
  130. case c
  131. when LINE_FEED
  132. continue = true
  133. v << c if context
  134. when ?n
  135. v << "\n" if quoted? context
  136. when ?t
  137. v << "\t" if quoted? context
  138. when ?f
  139. v << "\f" if quoted? context
  140. when ?v
  141. v << "\v" if quoted? context
  142. else
  143. v << c
  144. end
  145. else
  146. case c
  147. when ?\n
  148. v << c if context
  149. when ?\s
  150. if context
  151. v << c
  152. else
  153. new.call
  154. end
  155. when ?'
  156. if context == :quote
  157. new.call
  158. context = nil
  159. elsif context
  160. v << c
  161. else
  162. context = :quote
  163. end
  164. when ?"
  165. if context == :dquote
  166. new.call
  167. context = nil
  168. elsif context
  169. v << c
  170. else
  171. context = :dquote
  172. end
  173. when ?`
  174. if context == :bquote
  175. v = @twsh.execute(parse(v)[1])
  176. context = nil
  177. elsif context
  178. v << c
  179. else
  180. context = :bquote
  181. end
  182. when ?\\
  183. escape = true
  184. else
  185. v << c
  186. end
  187. end
  188. end
  189.  
  190. new.call
  191. [context, argv, continue || !context.nil?]
  192. end
  193.  
  194. def quoted?(context)
  195. context == :quote || context == :dquote
  196. end
  197. end
  198. end
  199.  
  200. Twsh::Command.setup do |c|
  201. c.on('exit', 'quit', 'logout') { fail Twsh::Logout }
  202.  
  203. c.on 'echo' do |twsh, argv|
  204. twsh.client.update argv.join ' '
  205. nil
  206. end
  207.  
  208. c.on 'head' do |twsh, argv|
  209. n = 5
  210.  
  211. OptionParser.new do |opt|
  212. opt.on('-n VAL', Integer) {|v| n = v}
  213. opt.parse! argv
  214. end
  215.  
  216. screen_name = argv.shift
  217. iterator = lambda {|status| puts "#{status.user.screen_name}: #{status.text}"}
  218.  
  219. if screen_name
  220. twsh.client.user_timeline(:screen_name => screen_name, :count => n).each &iterator
  221. else
  222. twsh.client.home_timeline(:count => n).each &iterator
  223. end
  224.  
  225. nil
  226. end
  227. end
  228.  
  229. Twsh::Shell.login ARGV, ENV
Add Comment
Please, Sign In to add comment