public
Description: numbr5 is alive - our friendly #roro bot
Homepage: http://faces.rubyonrails.com.au
Clone URL: git://github.com/lachie/numbr5.git
numbr5 / ror_au.example.rb
100644 355 lines (286 sloc) 8.449 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
require 'hpricot'
require 'open-uri'
require 'pp'
require 'insult'
require 'ri'
require 'bitchslap'
require 'active_support'
require 'seer'
 
 
module Numbr5
  module Commands
    
    extend TimeHelper
    
    def tumblr_client
      t = Tumblr::Client.new
      t.signin($config.tumblr_user, $config.tumblr_pass)
      t
    end
    
    # pinched from erb
    def h(s)
      s.to_s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
    end
    
    help "get a snippet of ancient unix wisdom"
    C :f do |client,text,nick,chan|
      f = $debug ? "no fortune :'(" : `fortune`.split($/)
      f[0] = "#{nick}: #{f[0]}"
      f
    end
    
    help "[to_nick] insult to_nick, old skool (default to_nick=you)"
    C :i do |client,text,nick,chan|
      Insulter.insult(text.blank? ? nick : text)
    end
    
    C :s do |client,text,nick,chan|
      cooked = (text || '').gsub(/^[\W_]+/,'').gsub(/[\W_]+$/,'')
      
      return if cooked.blank?
      
      if seen = client.seer[cooked]
        msg = "last saw #{text} #{seen[:note]}"
        msg << " #{distance_of_time_in_words(seen[:at].getlocal,Time.now)} ago" if seen[:at]
        
        [ msg ]
      else
        text.strip == $config.nick ? 'myself' : text
        ["I've nevah seen #{text}"]
      end
    end
    
    help "ri [method]"
    C :ri do |client,text,nick,chan|
      blurb = Ri.find(text)
      ["ri for #{text} sent to #{nick}",privmsg(nick.strip, blurb)]
    end
    
    help "bs [to_nick]"
    C :bs do |client,text,nick,chan|
      BitchSlap.bitchslap(text.blank? ? nick : text, nick)
    end
    
    def parse_quote(text)
 
      if text[/(\d+),(\d+)(.*?)$/]
        lines = $1
        back = $2
        title = $3
      elsif text[/(\d+)(.*?)$/]
        back = 0
        lines = $1
        title = $2
      end
      
      [
        (lines.to_i rescue 0),
        (back.to_i rescue 0),
        (title.lstrip rescue '')
      ]
    end
    
    help "quote your peeps (no args)"
    C :q do |client,text,nick,chan|
      client.make_session(nick,Sessions::Quoting,text,chan)
    end
    
    help "num_lines[,back] [title] - quote n lines, starting m lines back"
    C :qx do |client,text,nick,chan|
      begin
        lines,back,title = parse_quote(text)
        conversation = client.messages[back,lines].reverse.map {|l| l * ': '} * $/
        
        unless $offline
          post_link = tumblr_client.create_tumble('conversation',{
                 'conversation' => conversation,
                 'title' => "#{title} (quoted by #{nick} on #{chan})"
                })
              
          "#{nick}: posted to #{post_link}"
        else
          "no intarweb :'( ... #{nick} posted #{conversation}"
        end
      rescue
        "#{nick}: failed to post your quote: #{$!}"
      end
    end
    
    
    
    help "who is that? - ?w [nick]"
      C :w do |client,text,nick,chan|
        begin
          f = Faces::Client.new
          f.url_for_nick(text)
        rescue
          puts "?w failed: #{$!}"
          ["#{text} not found.",privmsg(text.strip,"hey, #{text}, sign up to http://faces.rubyonrails.com.au so ppl can find out who you are, owe you beers etc.")]
        end
    end
    
    help "post: I can understand some different kinds of input - http://link [caption] (flickr photo pages," +
      "youtube video pages, just a link)" +
      " - quotes (you quoting someone else, you quoting you)"
    C() do |client,text,nick,chan|
      return "unknown command #{text}" if text.strip[0] == ??
      
      begin
        
        unless $offline
          t = tumblr_client
          post_link = t.create_automatic_tumble(text,nick,chan)
          "#{nick}: posted to #{post_link}"
        else
          "no intarweb :'("
        end
      rescue
        "#{nick}: I couldn't post your tumble :( #{$!}"
      end
    end
    
  end
  
  module Actions
    
    OWE_RE = /^
(thanks|owes)
\s+
([\w_\^]+)
\s+
for
\s+
(.*?)
$/x
    A(OWE_RE) do |client,text,nick,chan|
      begin
        f = Faces::Client.new
        text[OWE_RE]
 
        response = f.thank(nick,$2,$3)
 
        "#{nick}: #{response}"
      rescue
        "#{nick}: i couldn't owe your beer: #{$!}"
      end
    end
  end
end
 
module Tumblr
  module Interpreters
    
    # pinched from erb
    def h(s)
      s.to_s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
    end
    
    FLICKR_RE = /
^
http:\/\/
(?:\w+\.|)
flickr\.com\/photos\/
\w+
\/
(\d+)
/x
    
    help ""
    I FLICKR_RE do |client,text,nick,chan|
      url,caption = text.split(/\s+/,2)
      
      url[FLICKR_RE]
      photo_id = $1
      
      doc = Hpricot(open("http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=#{$config.flickr_api_key}&photo_id=#{photo_id}"))
      source = (doc % "size[3]")['source']
      
      ['photo',{
        :source => source,
        :caption => "<a href='#{url}'>#{h(caption)}</a> (#{nick} on #{chan})"
      }]
    end
    
    YOUTUBE_RE = /
^
http:\/\/
.*?
youtube\.com
/x
    I YOUTUBE_RE do |client,text,nick,chan|
      url,caption = text.split(/\s+/,2)
      ['video',{
        :embed => url,
        :caption => "<a href='#{url}'>#{h(caption)}</a> (#{nick} on #{chan})"
      }]
    end
    
    IMAGE_EXT_RE = /(png|jpe?g|gif)$/
    
    I /^http:\/\// do |client,text,nick,chan|
      url,caption = text.split(/\s+/,2)
      
      if url[IMAGE_EXT_RE]
        ['photo', {
          'source' => url,
          'description' => "#{h(caption)} (#{nick} on #{chan})" }]
      else
        ['link', {
          'url' => url,
          'description' => "#{h(caption)} (#{nick} on #{chan})" }]
      end
    end
    
    I(/^([\w_]+): /) do |client,text,nick,chan|
      text[pattern]
      quoted = $1
      text.sub!(pattern,'')
      
      ['quote',{
        'quote' => h(text),
        'source' => "#{quoted} quoted by #{nick} on #{chan}"
      }]
    end
    
    I() do |client,text,nick,chan|
      ['quote',{
        'quote' => h(text),
        'source' => "#{nick} on #{chan}"
      }]
    end
  end
end
 
module Numbr5
  module Sessions
 
    class Quoting < Base
      
      attr_accessor :start, :end
      
      def initialize(*args)
        super
        @messages = @client.messages.dup
        
        @start ||= 1
        @end ||= max_span
      end
      
      def prompt
        # "quotr: n-m to set start and end lines, [s]how n to show window of 10 lines at offset #, [p]ost, [q]uit"
        "quotr: n-m to set start and end lines, [s]how the lines, [r]eset the span, [p]ost, [q]uit"
      end
      
      def max_span
        [10,@messages.size].min
      end
      
      def span_start
        @messages.length - max_span
      end
      
      def window_end
        span_start + @end - 1
      end
      
      def window_start
        span_start + @start - 1
      end
      
      def start=(start)
        @start = start < 1 ? 1 : start
      end
      
      def end=(e)
        @end = e > max_span ? max_span : e
      end
      
      def span
        @messages[window_start..window_end]
      end
      
      def window_messages
        i = 0
        span.compact.map do |m|
          i += 1
          "#{i} #{m * ' '}"
        end
      end
      
      def do_start
        puts "calling start"
        [ "quotr", window_messages(), prompt ].flatten.map {|m| msg(m)}
      end
      
      def post
        conversation = span.map {|l| l * ': '} * $/
        
        post_link = tumblr_client.create_tumble('conversation', {
          'conversation' => conversation,
          'title' => "(quoted by #{@nick} on #{@chan})"
        })
      end
    
      def process(client,text,nick,chan)
        case text
        when /^q/
          quit!
          "bye..."
          
        when /^s/
          [window_messages,prompt].flatten
          
        when /(\d+)\s*-\s*(\d+)/
          @start = $1.to_i
          @end = $2.to_i
          [window_messages,prompt].flatten
        
        when /^r/
          @start ||= 0
          @end ||= max_span
          [window_messages,prompt].flatten
          
        when /^p/
          link = self.post
          quit!
          ["posted to #{link}, bye...",chan_msg("#{@nick}: posted quote to #{link}")]
          
        end
      end
    end
  end
end