twitter2jabber 0.0.4 → 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.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to twitter2jabber version 0.0.4
5
+ This documentation refers to twitter2jabber version 0.0.5
6
6
 
7
7
 
8
8
  == DESCRIPTION
data/Rakefile CHANGED
@@ -16,7 +16,7 @@ begin
16
16
  :homepage => %q{http://twitter2jabber.rubyforge.org/},
17
17
  :files => FileList['lib/**/*.rb', 'bin/*'].to_a,
18
18
  :extra_files => FileList['[A-Z]*', 'sample/**/*'].to_a,
19
- :dependencies => %w[twitter xmpp4r-simple highline]
19
+ :dependencies => %w[twitter xmpp4r-simple highline shorturl]
20
20
  }
21
21
  }}
22
22
  rescue LoadError
data/TODO CHANGED
@@ -1,4 +1,5 @@
1
1
  - persistent cache! (friends_timeline: since/since_id?)
2
+ - OAuth (register: http://twitter.com/oauth_clients/new)
2
3
  - better interrupt handling (in loop mode)
3
4
  - daemonize after asking for credentials (in loop mode)
4
5
  - additional commands?
@@ -4,7 +4,7 @@ class Twitter2Jabber
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 0
7
- TINY = 4
7
+ TINY = 5
8
8
 
9
9
  class << self
10
10
 
@@ -30,6 +30,7 @@ require 'erb'
30
30
  require 'rubygems'
31
31
  require 'twitter'
32
32
  require 'xmpp4r-simple'
33
+ require 'shorturl'
33
34
 
34
35
  require 'twitter2jabber/version'
35
36
 
@@ -54,7 +55,7 @@ class Twitter2Jabber
54
55
  new(options).run(recipients, &block)
55
56
  end
56
57
 
57
- attr_reader :id, :verbose, :debug, :twitter, :jabber, :filter, :formats, :templates
58
+ attr_reader :id, :verbose, :debug, :twitter, :jabber, :filter, :formats, :templates, :_erb
58
59
 
59
60
  def initialize(options, &block)
60
61
  [:twitter, :jabber].each { |client|
@@ -77,11 +78,16 @@ class Twitter2Jabber
77
78
  ].inject({}) { |hash, template|
78
79
  hash.update(File.extname(template).sub(/\A\./, '') => File.read(template))
79
80
  }
81
+
82
+ @_erb = Hash.new { |hash, format|
83
+ template = templates[format]
84
+ hash[format] = template && ERB.new(template)
85
+ }
80
86
  end
81
87
 
82
88
  def run(recipients = [], seen = {}, flag = true, &block)
83
89
  deliver_tweets(recipients, seen, &block) if flag
84
- post_messages
90
+ post_messages(recipients)
85
91
  end
86
92
 
87
93
  def loop(recipients = [], pause = nil, &block)
@@ -94,16 +100,16 @@ class Twitter2Jabber
94
100
  # sleep at least one second
95
101
  pause = 1 if pause < 1
96
102
 
97
- i, seen = 0, Hash.new { |h, k| h[k] = true; false }
103
+ i, seen = 1, Hash.new { |h, k| h[k] = true; false }
98
104
 
99
- trap(:INT) { i = nil }
100
-
101
- while i
102
- i += 1
105
+ trap(:INT) { i = -1 }
103
106
 
107
+ while i > 0
104
108
  run(recipients, seen, i % ratio == 1, &block)
105
109
 
106
110
  sleep pause
111
+
112
+ i += 1
107
113
  end
108
114
  end
109
115
 
@@ -127,9 +133,12 @@ class Twitter2Jabber
127
133
  }
128
134
  end
129
135
 
130
- def post_messages
136
+ def post_messages(recipients = [])
137
+ allowed = %r{\A(?:#{recipients.map { |r| Regexp.escape(r) }.join('|')})\z}i
138
+
131
139
  jabber.received_messages { |msg|
132
140
  next unless msg.type == :chat
141
+ next unless msg.from.bare.to_s =~ allowed
133
142
 
134
143
  logj msg.id
135
144
 
@@ -178,10 +187,8 @@ class Twitter2Jabber
178
187
  msg = Jabber::Message.new.set_type(:chat)
179
188
 
180
189
  formats.each { |format|
181
- if template = templates[format]
182
- msg.add_element format_element(format) {
183
- ERB.new(template).result(binding)
184
- }
190
+ if erb = _erb[format]
191
+ msg.add_element(format_element(format, erb.result(binding)))
185
192
  end
186
193
  }
187
194
 
@@ -189,35 +196,41 @@ class Twitter2Jabber
189
196
  end
190
197
 
191
198
  # cf. <http://devblog.famundo.com/articles/2006/10/18/ruby-and-xmpp-jabber-part-3-adding-html-to-the-messages>
192
- def format_element(format)
199
+ def format_element(format, text)
193
200
  body = REXML::Element.new('body')
194
- REXML::Text.new(yield, format != 'html', body, true, nil, /.^/)
195
201
 
196
202
  case format
197
203
  when 'html'
204
+ REXML::Text.new(process_html(text), false, body, true, nil, /.^/)
205
+
198
206
  html = REXML::Element.new('html').add_namespace(JABBER_NS)
199
207
  html.add(body.add_namespace(XHTML_NS))
200
208
  html
201
209
  else
210
+ REXML::Text.new(text, true, body, true, nil, /.^/)
202
211
  body
203
212
  end
204
213
  end
205
214
 
215
+ def process_html(text)
216
+ text.gsub(/((?:\A|\s)@)(\w+)/, '\1<a href="http://twitter.com/\2">\2</a>')
217
+ end
218
+
206
219
  def handle_command(body, from, execute = true)
207
220
  case body
208
221
  when /\Ahe?(?:lp)?\z/i
209
222
  deliver(from, <<-HELP) if execute
210
- h[e[lp]] -- Print this help
223
+ h[e[lp]] -- Print this help
211
224
 
212
- de[bug] -- Print debug mode
213
- de[bug] on|off -- Turn debug mode on/off
225
+ de[bug] -- Print debug mode
226
+ de[bug] on|off -- Turn debug mode on/off
214
227
 
215
- bl[ock] #ID -- Block ID
216
- fa[v[orite]] #ID -- Create favorite #ID
228
+ bl[ock] #ID -- Block ID
229
+ fa[v[orite]] #ID -- Create favorite #ID
217
230
 
218
- re[ply] #ID: ... -- Reply to ID
219
- le[n[gth]] ... -- Determine length
220
- ... -- Update status
231
+ re[ply] #ID[:] [!] STATUS -- Reply to ID (Force if too long)
232
+ le[n[gth]] STATUS -- Determine length
233
+ [!] STATUS -- Update status (Force if too long)
221
234
 
222
235
  (Note: Message body must be shorter than #{MAX_LENGTH} characters)
223
236
  HELP
@@ -256,9 +269,17 @@ le[n[gth]] ... -- Determine length
256
269
  options[:in_reply_to_status_id] = $1
257
270
  end
258
271
 
272
+ if body.sub!(/\A!\s+/, '')
273
+ force = true
274
+ end
275
+
276
+ body.gsub!(/https?:\/\/\S+/) { |match|
277
+ match.length < 30 ? match : ShortURL.shorten(match)
278
+ }
279
+
259
280
  return body unless execute
260
281
 
261
- if body.length <= MAX_LENGTH
282
+ if force || body.length <= MAX_LENGTH
262
283
  update(body, options)
263
284
  else
264
285
  deliver(from, "MSG TOO LONG (> #{MAX_LENGTH}): #{body}")
@@ -1,7 +1,3 @@
1
- <%= user.name %>
2
- (<a href="http://twitter.com/<%= user.screen_name %>"><%= user.screen_name %></a>)
3
- [<%= tweet.created_at %>]:
1
+ <%= user.name %> (<a href="http://twitter.com/<%= user.screen_name %>"><%= user.screen_name %></a>) [<%= tweet.created_at %>]:
4
2
  <br /><br />
5
- <img src="<%= user.profile_image_url %>" alt="[<%= user.screen_name %>]" />
6
- <%= tweet.text %>
7
- | #<%= tweet.id %>
3
+ <img src="<%= user.profile_image_url %>" alt="[<%= user.screen_name %>]" /> <%= tweet.text %> | #<%= tweet.id %>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitter2jabber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Wille
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-02 00:00:00 +02:00
12
+ date: 2009-08-03 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -42,6 +42,16 @@ dependencies:
42
42
  - !ruby/object:Gem::Version
43
43
  version: "0"
44
44
  version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: shorturl
47
+ type: :runtime
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
45
55
  description: Twitter-to-Jabber gateway.
46
56
  email: jens.wille@uni-koeln.de
47
57
  executables: