rjp-jabber_cat 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/bin/jabber_cat.rb +158 -0
  2. metadata +62 -0
data/bin/jabber_cat.rb ADDED
@@ -0,0 +1,158 @@
1
+ require 'rubygems'
2
+ require 'optparse'
3
+ require 'socket'
4
+ require 'xmpp4r'
5
+ require 'xmpp4r/framework/bot'
6
+ include Jabber
7
+
8
+ $options = {
9
+ :host => 'localhost',
10
+ :port => 9999,
11
+ :whoto => nil,
12
+ :config => ENV['HOME'] + '/.jabber_cat',
13
+ :verbose => nil,
14
+ :debug => 0
15
+ }
16
+
17
+ OptionParser.new do |opts|
18
+ opts.banner = "Usage: twittermoo.rb [-p port] [-h host] [-w jid]"
19
+
20
+ opts.on("-p", "--port N", Integer, "irccat port") do |p|
21
+ $options[:port] = p
22
+ end
23
+
24
+ opts.on("-h", "--host HOST", String, "host") do |p|
25
+ $options[:host] = p
26
+ end
27
+
28
+ opts.on("-w", "--whoto jid", String, "JID") do |p|
29
+ $options[:whoto] = p
30
+ end
31
+
32
+ opts.on("-c", "--config CONFIG", String, "config file") do |p|
33
+ $options[:config] = p
34
+ end
35
+
36
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
37
+ $options[:verbose] = v
38
+ end
39
+
40
+ opts.on("-d", "--debug N", Integer, "debug level") do |p|
41
+ $options[:debug] = p
42
+ end
43
+
44
+ end.parse!
45
+
46
+ if $options[:whoto].nil? then # debug = 1 if not already set
47
+ $options[:debug] = $options[:debug] || 1
48
+ end
49
+
50
+ # TODO handle failing here with exceptions
51
+ config = YAML::load(open($options[:config]))
52
+
53
+ # make sure we always have a filters list
54
+ if config['filters'].nil? then
55
+ config['filters'] = []
56
+ end
57
+
58
+ if $options[:debug] > 0 then
59
+ puts "listening to socket on #{$options[:host]}:#{$options[:port]}"
60
+ end
61
+
62
+ server = TCPServer.new($options[:host], $options[:port])
63
+
64
+ x = Thread.new do
65
+ loop do
66
+ ignore = nil
67
+ s = server.accept
68
+ line = s.gets.chomp.gsub(/\r/,'')
69
+
70
+ config['filters'].each { |f|
71
+ if line =~ /#{f}/ then
72
+ if $options[:verbose] then
73
+ puts "[#{line}] filtered by [#{f}]"
74
+ end
75
+ ignore = true
76
+ end
77
+ }
78
+
79
+ if ignore.nil? then
80
+ if $options[:debug] > 0 then
81
+ puts "got line [#{line}]"
82
+ puts "sending it to #{$options[:whoto]}"
83
+ end
84
+ if $options[:debug] > 0 then
85
+ puts "<#{$options[:whoto]}> #{line}"
86
+ else
87
+ $bot.send_message($options[:whoto], line)
88
+ end
89
+ end
90
+
91
+ s.close
92
+ end
93
+ end
94
+
95
+ if $options[:debug] > 0 then
96
+ puts "creating jabber connection now"
97
+ end
98
+
99
+ if $options[:debug] > 1 then
100
+ Jabber::debug = true
101
+ end
102
+
103
+ subscription_callback = lambda { |item,pres|
104
+ name = pres.from
105
+ if item != nil && item.iname != nil
106
+ name = "#{item.iname} (#{pres.from})"
107
+ end
108
+ case pres.type
109
+ when :subscribe then puts("Subscription request from #{name}")
110
+ when :subscribed then puts("Subscribed to #{name}")
111
+ when :unsubscribe then puts("Unsubscription request from #{name}")
112
+ when :unsubscribed then puts("Unsubscribed from #{name}")
113
+ else raise "The Roster Helper is buggy!!! subscription callback with type=#{pres.type}"
114
+ end
115
+ $bot.set_presence(nil, "Waiting for socket tickling...")
116
+ }
117
+
118
+ # settings
119
+ myJID = JID.new(config['myjid'])
120
+ myPassword = config['mypass']
121
+
122
+ $bot = Jabber::Framework::Bot.new(myJID, myPassword)
123
+ class << $bot
124
+ def accept_subscription_from?(jid)
125
+ if jid == $options[:whoto] then
126
+ true
127
+ else
128
+ false
129
+ end
130
+ end
131
+ end
132
+
133
+ $bot.set_presence(nil, "Waiting for socket tickling...")
134
+
135
+ $bot.roster.add_update_callback { |olditem,item|
136
+ if [:from, :none].include?(item.subscription) && item.ask != :subscribe && item.jid == $options[:whoto]
137
+ if $options[:debug] > 0 then
138
+ puts("Subscribing to #{item.jid}")
139
+ end
140
+ item.subscribe
141
+ end
142
+ }
143
+
144
+ $bot.roster.add_subscription_callback(0, nil, &subscription_callback)
145
+
146
+ $bot.roster.groups.each { |group|
147
+ $bot.roster.find_by_group(group).each { |item|
148
+ if [:from, :none].include?(item.subscription) && item.ask != :subscribe && item.jid == $options[:whoto] then
149
+ if $options[:debug] > 0 then
150
+ puts "subscribing to #{item.jid}"
151
+ end
152
+ item.subscribe
153
+ end
154
+ }
155
+ }
156
+
157
+
158
+ x.join
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rjp-jabber_cat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rob Partington
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: xmpp4r
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0.4"
24
+ version:
25
+ description:
26
+ email: zimpenfish@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - bin/jabber_cat.rb
35
+ has_rdoc: false
36
+ homepage: http://rjp.github.com/jabber_cat
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.2.0
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: Simple Socket-to-Jabber gateway
61
+ test_files: []
62
+