rjp-jabber_cat 0.0.2 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/bin/jabber_cat.rb +123 -61
  2. metadata +1 -1
data/bin/jabber_cat.rb CHANGED
@@ -3,19 +3,28 @@ require 'optparse'
3
3
  require 'socket'
4
4
  require 'xmpp4r'
5
5
  require 'xmpp4r/framework/bot'
6
+ require 'xmpp4r/muc/helper/simplemucclient'
6
7
  include Jabber
7
8
 
9
+ def log(x)
10
+ if $options[:verbose] then
11
+ puts(*x)
12
+ end
13
+ end
14
+
8
15
  $options = {
9
16
  :host => 'localhost',
10
17
  :port => 9999,
11
18
  :whoto => nil,
12
19
  :config => ENV['HOME'] + '/.jabber_cat',
13
20
  :verbose => nil,
14
- :debug => 0
21
+ :debug => 0,
22
+ :keyfile => nil,
23
+ :muc => nil
15
24
  }
16
25
 
17
26
  OptionParser.new do |opts|
18
- opts.banner = "Usage: twittermoo.rb [-p port] [-h host] [-w jid]"
27
+ opts.banner = "Usage: twittermoo.rb [-p port] [-h host] [-w jid] [-v] [-d N] [-m] [-k file]"
19
28
 
20
29
  opts.on("-p", "--port N", Integer, "irccat port") do |p|
21
30
  $options[:port] = p
@@ -41,6 +50,14 @@ OptionParser.new do |opts|
41
50
  $options[:debug] = p
42
51
  end
43
52
 
53
+ opts.on("-k", "--key filename", String, "Shared key file") do |p|
54
+ $options[:keyfile] = p
55
+ end
56
+
57
+ opts.on("-m", "--muc", "Treat the destination as a MUC") do |v|
58
+ $options[:muc] = v
59
+ end
60
+
44
61
  end.parse!
45
62
 
46
63
  if $options[:whoto].nil? then # debug = 1 if not already set
@@ -50,8 +67,16 @@ end
50
67
  # TODO handle failing here with exceptions
51
68
  config = YAML::load(open($options[:config]))
52
69
 
70
+ # TODO this won't work at all because $options has symbols, config has strings
53
71
  unless config['options'].nil? then
54
- $options.merge!(config['options'])
72
+ config['options'].each { |k,v|
73
+ $options[k.to_sym] = v
74
+ }
75
+ end
76
+
77
+ if $options[:keyfile] then
78
+ puts "loading secret key from #{$options[:keyfile]}"
79
+ $options[:secret_key] = File.open($options[:keyfile]).read.chomp
55
80
  end
56
81
 
57
82
  # make sure we always have a filters list
@@ -65,26 +90,44 @@ end
65
90
 
66
91
  server = TCPServer.new($options[:host], $options[:port])
67
92
 
68
- x = Thread.new do
93
+ x = Thread.new do
69
94
  loop do
70
95
  ignore = nil
71
96
  s = server.accept
72
97
  line = s.gets.chomp.gsub(/\r/,'')
98
+ if $options[:verbose] then
99
+ puts "[#{line}] received"
100
+ end
101
+
102
+ log "R #{line}"
103
+
104
+ if $options[:secret_key] then
105
+ log "K #{$options[:secret_key]}"
106
+ # check the line matches our secret shared key
107
+ match = "%/#{$options[:secret_key]}/%"
108
+ unless line.gsub!(%r{^#{match} }, '') then
109
+ log "line ignored because secret key doesn't match"
110
+ ignore = true
111
+ end
112
+ log "L #{line}"
113
+ else
114
+ # avoid information leakage due to misconfiguration
115
+ # if a line looks like it starts with a secret key, remove it
116
+ line.gsub!(%r{^%/.*?/% }, '')
117
+ end
73
118
 
74
119
  config['filters'].each { |f|
75
120
  if line =~ /#{f}/ then
76
121
  if $options[:verbose] then
77
- puts "[#{line}] filtered by [#{f}]"
122
+ log "F #{f} =~ #{line}"
78
123
  end
79
124
  ignore = true
80
125
  end
81
126
  }
82
127
 
128
+
83
129
  if ignore.nil? then
84
- if $options[:debug] > 0 then
85
- puts "got line [#{line}]"
86
- puts "sending it to #{$options[:whoto]}"
87
- end
130
+ log "sending it to #{$options[:whoto]}"
88
131
  if $options[:debug] > 0 then
89
132
  puts "<#{$options[:whoto]}> #{line}"
90
133
  else
@@ -96,67 +139,86 @@ x = Thread.new do
96
139
  end
97
140
  end
98
141
 
99
- if $options[:debug] > 0 then
100
- puts "creating jabber connection now"
142
+ # skip jabber entirely if we have high enough debugging
143
+ if $options[:debug] > 3 then
144
+ x.join
145
+ exit
101
146
  end
102
147
 
103
- if $options[:debug] > 1 then
104
- Jabber::debug = true
105
- end
106
-
107
- subscription_callback = lambda { |item,pres|
108
- name = pres.from
109
- if item != nil && item.iname != nil
110
- name = "#{item.iname} (#{pres.from})"
111
- end
112
- case pres.type
113
- when :subscribe then puts("Subscription request from #{name}")
114
- when :subscribed then puts("Subscribed to #{name}")
115
- when :unsubscribe then puts("Unsubscription request from #{name}")
116
- when :unsubscribed then puts("Unsubscribed from #{name}")
117
- else raise "The Roster Helper is buggy!!! subscription callback with type=#{pres.type}"
118
- end
119
- $bot.set_presence(nil, "Waiting for socket tickling...")
120
- }
148
+ #### JABBER
121
149
 
122
150
  # settings
123
151
  myJID = JID.new(config['myjid'])
124
152
  myPassword = config['mypass']
125
153
 
126
- $bot = Jabber::Framework::Bot.new(myJID, myPassword)
127
- class << $bot
128
- def accept_subscription_from?(jid)
129
- if jid == $options[:whoto] then
130
- true
131
- else
132
- false
133
- end
134
- end
135
- end
136
-
137
- $bot.set_presence(nil, "Waiting for socket tickling...")
138
-
139
- $bot.roster.add_update_callback { |olditem,item|
140
- if [:from, :none].include?(item.subscription) && item.ask != :subscribe && item.jid == $options[:whoto]
141
- if $options[:debug] > 0 then
142
- puts("Subscribing to #{item.jid}")
143
- end
144
- item.subscribe
145
- end
146
- }
154
+ log "creating jabber connection now"
147
155
 
148
- $bot.roster.add_subscription_callback(0, nil, &subscription_callback)
156
+ if $options[:debug] > 1 then
157
+ Jabber::debug = true
158
+ end
149
159
 
150
- $bot.roster.groups.each { |group|
151
- $bot.roster.find_by_group(group).each { |item|
152
- if [:from, :none].include?(item.subscription) && item.ask != :subscribe && item.jid == $options[:whoto] then
153
- if $options[:debug] > 0 then
154
- puts "subscribing to #{item.jid}"
155
- end
156
- item.subscribe
160
+ if $options[:muc] then # can't distinguish MUC JID from normal JID
161
+ cl = Jabber::Client.new(Jabber::JID.new(myJID))
162
+ cl.connect
163
+ cl.auth(myPassword)
164
+ m = Jabber::MUC::SimpleMUCClient.new(cl)
165
+ class << m
166
+ def send_message(junk, body)
167
+ say(body)
157
168
  end
158
- }
159
- }
160
-
169
+ end
170
+ m.join($options[:whoto])
171
+ $bot = m
172
+ else
173
+ subscription_callback = lambda { |item,pres|
174
+ name = pres.from
175
+ if item != nil && item.iname != nil
176
+ name = "#{item.iname} (#{pres.from})"
177
+ end
178
+ case pres.type
179
+ when :subscribe then puts("Subscription request from #{name}")
180
+ when :subscribed then puts("Subscribed to #{name}")
181
+ when :unsubscribe then puts("Unsubscription request from #{name}")
182
+ when :unsubscribed then puts("Unsubscribed from #{name}")
183
+ else raise "The Roster Helper is buggy!!! subscription callback with type=#{pres.type}"
184
+ end
185
+ $bot.set_presence(nil, "Waiting for socket tickling...")
186
+ }
187
+
188
+ $bot = Jabber::Framework::Bot.new(myJID, myPassword)
189
+ class << $bot
190
+ def accept_subscription_from?(jid)
191
+ if jid == $options[:whoto] then
192
+ true
193
+ else
194
+ false
195
+ end
196
+ end
197
+ end
198
+
199
+ $bot.set_presence(nil, "Waiting for socket tickling...")
200
+
201
+ $bot.roster.add_update_callback { |olditem,item|
202
+ if [:from, :none].include?(item.subscription) && item.ask != :subscribe && item.jid == $options[:whoto]
203
+ if $options[:debug] > 0 then
204
+ puts("Subscribing to #{item.jid}")
205
+ end
206
+ item.subscribe
207
+ end
208
+ }
209
+
210
+ $bot.roster.add_subscription_callback(0, nil, &subscription_callback)
211
+
212
+ $bot.roster.groups.each { |group|
213
+ $bot.roster.find_by_group(group).each { |item|
214
+ if [:from, :none].include?(item.subscription) && item.ask != :subscribe && item.jid == $options[:whoto] then
215
+ if $options[:debug] > 0 then
216
+ puts "subscribing to #{item.jid}"
217
+ end
218
+ item.subscribe
219
+ end
220
+ }
221
+ }
222
+ end
161
223
 
162
224
  x.join
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rjp-jabber_cat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Partington