palbo-lijab 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/lijab/out.rb ADDED
@@ -0,0 +1,173 @@
1
+ require 'lijab/input'
2
+ require 'lijab/term/ansi'
3
+ require 'monitor'
4
+ require 'readline'
5
+ require 'readline/extra'
6
+
7
+ include Term
8
+
9
+ module Lijab
10
+
11
+ module Out
12
+
13
+ @monitor = Monitor.new
14
+ @time = Time.now
15
+
16
+ module_function
17
+
18
+ def put(s="\n", redisplay_input=false)
19
+ #clear_infoline()
20
+ puts "#{ANSI.clearline}#{s}"
21
+ InputHandler::redisplay_input if redisplay_input
22
+ end
23
+
24
+ def notice_if_day_changed(redisplay_input=true)
25
+ t = Time.now
26
+ if @time.day != t.day
27
+ ft = @time.strftime('%Y-%M-%d')
28
+ @time = t
29
+ puts "#{ANSI.clearline}** day changed -- #{ft} -> #{Date.today}".green
30
+ end
31
+ end
32
+
33
+ def format_time(time=nil, format=:datetime_format)
34
+ return "" unless time
35
+ time = Time.now if time == :now
36
+
37
+ "#{time.strftime(Config.opts[format])} "
38
+ end
39
+
40
+ def format_message_in(from, text, colors, time)
41
+ "#{ANSI.clearline}#{time}#{from} <- ".colored(*colors) + text
42
+ end
43
+
44
+ def format_message_out(to, text, colors, time)
45
+
46
+ prefix = "#{time}#{to} -> "
47
+ indent = " " * prefix.length
48
+
49
+ lines = text.to_a
50
+ s = []
51
+
52
+ s << "#{ANSI.clearline}#{prefix.colored(*colors)}#{lines.shift.chomp}"
53
+ lines.each do |l|
54
+ s << "#{ANSI.clearline}#{indent}#{l.chomp}"
55
+ end
56
+
57
+ s.join("\n")
58
+ end
59
+
60
+ def message_in(from, text, colors=[])
61
+ @monitor.synchronize do
62
+ clear_infoline()
63
+ InputHandler::delete_typed
64
+
65
+ notice_if_day_changed()
66
+
67
+ print "\a" if Config.opts[:terminal_bell_on_message]
68
+ puts format_message_in(from, text, colors, format_time(:now))
69
+
70
+ InputHandler::redisplay_input
71
+ end
72
+ end
73
+
74
+ def message_out(to, text, colors=[])
75
+ @monitor.synchronize do
76
+ InputHandler::delete_last_typed
77
+
78
+ notice_if_day_changed(false)
79
+
80
+ puts format_message_out(to, text, colors, format_time(:now))
81
+ end
82
+ end
83
+
84
+ def presence(from, presence, colors=[])
85
+ @monitor.synchronize do
86
+ clear_infoline()
87
+ InputHandler::delete_typed
88
+
89
+ notice_if_day_changed()
90
+
91
+ print "#{ANSI.clearline}"
92
+ print "** #{format_time(:now)}#{from} (#{presence.priority || 0}) is now ".colored(*colors)
93
+ puts presence.pretty(true)
94
+
95
+ InputHandler::redisplay_input
96
+ end
97
+ end
98
+
99
+ def subscription(from, type, msg=nil, colors=[])
100
+ @monitor.synchronize do
101
+ clear_infoline()
102
+ InputHandler::delete_typed
103
+
104
+ notice_if_day_changed()
105
+
106
+ time = format_time(:now)
107
+ case type
108
+ when :subscribe
109
+ indent = ' ' * time.length
110
+ s = "** #{time}subscription request from #{from} received:\n"
111
+ s += %{"#{msg}"\n} if msg
112
+ s += "** See '/help requests' to see how to handle requests."
113
+ when :subscribed
114
+ s = "** #{time}#{from} has subscribed to you"
115
+ when :unsubscribed
116
+ s = "** #{time}#{from} has unsubscribed from you"
117
+ end
118
+
119
+ puts "#{ANSI.clearline}#{s}"
120
+
121
+ InputHandler::redisplay_input
122
+ end
123
+ end
124
+
125
+ def history(*log_entries)
126
+ log_entries.each do |e|
127
+ contact = Main.contacts[Jabber::JID.new(e[:target])]
128
+ target_s = contact ? contact.simple_name : e[:target]
129
+ colors = contact ? [contact.color] : []
130
+ time = format_time(e[:time].localtime, :history_datetime_format)
131
+
132
+ if e[:direction] == :from
133
+ colors << :bold
134
+ m = method(:format_message_in)
135
+ else
136
+ m = method(:format_message_out)
137
+ end
138
+
139
+ puts m.call(target_s, e[:msg], colors, time)
140
+ end
141
+ end
142
+
143
+ def error(s, redisplay_input=true)
144
+ puts "#{ANSI.cleartoeol}error: #{s}".red.bold
145
+ InputHandler::redisplay_input if redisplay_input
146
+ end
147
+
148
+ def infoline(s)
149
+ @monitor.synchronize do
150
+ print "#{ANSI.savepos}#{ANSIMove.down(1)}#{ANSI.clearline}"
151
+ print s
152
+ print "#{ANSI.restorepos}"
153
+ STDOUT.flush
154
+ end
155
+ end
156
+
157
+ def clear_infoline
158
+ @monitor.synchronize do
159
+ print "#{ANSI.savepos}\n#{ANSI.clearline}#{ANSI.restorepos}"
160
+ STDOUT.flush
161
+ end
162
+ end
163
+
164
+ def make_infoline
165
+ @monitor.synchronize do
166
+ print "\n\r#{ANSI.cleartoeol}#{ANSIMove.up(1)}"
167
+ STDOUT.flush
168
+ end
169
+ end
170
+
171
+ end
172
+
173
+ end
@@ -0,0 +1,20 @@
1
+
2
+ module Term
3
+ module ANSIMove
4
+ module_function
5
+ def to(line, col) "\033[#{line};#{col}H" end
6
+ def up(lines) "\033[#{lines}A" end
7
+ def down(lines) "\033[#{lines}B" end
8
+ def fwd(cols) "\033[#{cols}C" end
9
+ def back(cols) "\033[#{cols}D" end
10
+ end
11
+ module ANSI
12
+ module_function
13
+ def savepos() "\033[s" end
14
+ def restorepos() "\033[u" end
15
+ def title(s) "\033]2;#{s}\007" end
16
+ def clear() "\033[2J" end
17
+ def cleartoeol() "\033[K" end
18
+ def clearline() "\r#{cleartoeol}" end
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+
2
+ module Lijab
3
+ VERSION = '0.1.0'
4
+ end
@@ -0,0 +1,45 @@
1
+ require 'xmpp4r'
2
+
3
+ unless Jabber::Message.method_defined?(:chat_state)
4
+ class Jabber::Message
5
+ CHAT_STATES = %w(active composing gone inactive paused).freeze
6
+
7
+ # Returns the current chat state, or nil if no chat state is set
8
+ def each_elements(*els, &block)
9
+ els.inject([ ]) do |res, e|
10
+ res + each_element(e, &block)
11
+ end
12
+ end
13
+
14
+ def chat_state
15
+ each_elements(*CHAT_STATES) { |el| return el.name.to_sym }
16
+ return nil
17
+ end
18
+
19
+ ##
20
+ # Sets the chat state :active, :composing, :gone, :inactive, :paused
21
+ def chat_state=(s)
22
+ s = s.to_s
23
+ raise InvalidChatState,
24
+ "Chat state must be one of #{CHAT_STATES.join(', ')}" unless CHAT_STATES.include?(s)
25
+ CHAT_STATES.each { |state| delete_elements(state) }
26
+ add_element(REXML::Element.new(s).add_namespace('http://jabber.org/protocol/chatstates'))
27
+ end
28
+
29
+ CHAT_STATES.each do |state|
30
+ define_method("#{state}?") do
31
+ chat_state == state.to_sym
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ class Jabber::Message
38
+ ##
39
+ # Sets the message's chat state
40
+ def set_chat_state(s)
41
+ self.chat_state = s
42
+ self
43
+ end
44
+ end
45
+
data/lib/lijab.rb ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'lijab/main'
4
+
5
+ Lijab::Main.run!
6
+ Thread.stop
7
+
@@ -0,0 +1,7 @@
1
+
2
+ require 'readline_extra.so'
3
+
4
+ module Readline
5
+ Extra = :"oh yeah"
6
+ end
7
+
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: palbo-lijab
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pablo Flouret
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-04 00:00:00 -07:00
13
+ default_executable: lijab
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: file-tail
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: term-ansicolor
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: xmpp4r
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description:
46
+ email: quuxbaz@gmail.com
47
+ executables:
48
+ - lijab
49
+ extensions:
50
+ - ext/extconf.rb
51
+ extra_rdoc_files: []
52
+
53
+ files:
54
+ - ext/extconf.rb
55
+ - ext/readline_extra.c
56
+ - lib/lijab/commands/contacts.rb
57
+ - lib/lijab/commands/options.rb
58
+ - lib/lijab/commands/simple.rb
59
+ - lib/lijab/commands/status.rb
60
+ - lib/lijab/commands/subscription.rb
61
+ - lib/lijab/commands.rb
62
+ - lib/lijab/config.rb
63
+ - lib/lijab/contacts.rb
64
+ - lib/lijab/history.rb
65
+ - lib/lijab/hooks.rb
66
+ - lib/lijab/input.rb
67
+ - lib/lijab/main.rb
68
+ - lib/lijab/out.rb
69
+ - lib/lijab/term/ansi.rb
70
+ - lib/lijab/version.rb
71
+ - lib/lijab/xmpp4r/message.rb
72
+ - lib/lijab.rb
73
+ - lib/readline/extra.rb
74
+ - bin/lijab
75
+ has_rdoc: false
76
+ homepage: http://github.com/palbo/lijab
77
+ post_install_message:
78
+ rdoc_options: []
79
+
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 1.8.0
87
+ version:
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: "0"
93
+ version:
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.2.0
98
+ signing_key:
99
+ specification_version: 2
100
+ summary: Extensible line oriented jabber client
101
+ test_files: []
102
+