Class: Atig::Gateway::Session

Inherits:
Net::IRC::Server::Session
  • Object
show all
Includes:
Util
Defined in:
lib/atig/gateway/session.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExceptionUtil

daemon, safe

Constructor Details

#initialize(*args) ⇒ Session

Returns a new instance of Session.



41
42
43
44
45
# File 'lib/atig/gateway/session.rb', line 41

def initialize(*args)
  super
  @tmpdir = @opts.tmpdir
  @on_message = nil
end

Class Method Details

.class_writer(*ids) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/atig/gateway/session.rb', line 28

def self.class_writer(*ids)
  ids.each do|id|
    module_eval <<END
def #{id}=(arg)
  @@#{id} = arg
end
END
  end
end

Instance Method Details

#[](name) ⇒ Object



72
73
74
# File 'lib/atig/gateway/session.rb', line 72

def [](name)
  @channels[name]
end

#channel(name, opts = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/atig/gateway/session.rb', line 60

def channel(name,opts={})
  opts.update(:session => self,
              :name    => name,
              :filters => @ifilters,
              :prefix  => @prefix,
              :nick    => @nick,
              :opts    => @opts)
  channel = Channel.new opts
  @channels[name] = channel
  channel
end

#ctcp_action(*commands, &block) ⇒ Object



80
81
82
83
84
# File 'lib/atig/gateway/session.rb', line 80

def ctcp_action(*commands, &block)
  commands.each do |command|
    @ctcp_actions[command] = block
  end
end

#on_ctcp(target, mesg) ⇒ Object



201
202
203
204
205
206
207
208
# File 'lib/atig/gateway/session.rb', line 201

def on_ctcp(target, mesg)
  if mesg.respond_to? :encoding!
    mesg.encoding! "UTF-8"
  end
  type, mesg = mesg.split(" ", 2)
  method = "on_ctcp_#{type.downcase}".to_sym
  send(method, target, mesg) if respond_to? method, true
end

#on_ctcp_action(target, mesg) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/atig/gateway/session.rb', line 210

def on_ctcp_action(target, mesg)
  command, *args = mesg.split(" ")
  last_match = nil
  commond = command.to_s.downcase
  _, action = @ctcp_actions.find{|define, f|
    r = (define === command)
    last_match = Regexp.last_match
    r
  }
  if action then
    safe {
      action.call(target, mesg, last_match || command, args)
    }
  else
    self[target].notify "[atig.rb] CTCP ACTION COMMANDS:"
    @ctcp_actions.keys.each do |c|
      self[target].notify c.to_s
    end
  end
end

#on_disconnectedObject



178
179
180
# File 'lib/atig/gateway/session.rb', line 178

def on_disconnected
  (@thread_group.list - [Thread.current]).each {|t| t.kill }
end

#on_invite(m) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/atig/gateway/session.rb', line 231

def on_invite(m)
  nick, channel = *m.params
  if not nick.screen_name? or @db.me.screen_name.casecmp(nick).zero?
    post server_name, ERR_NOSUCHNICK, nick, "No such nick: #{nick}" # or yourself
    return
  end

  unless @channels.key? channel
    post server_name, ERR_NOSUCHNICK, nick, "No such channel: #{channel}"
    return
  end

  @api.delay(0){|api| @channels[channel].on_invite(api, nick) }
end

#on_kick(m) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/atig/gateway/session.rb', line 246

def on_kick(m)
  channel, nick, msg = *m.params

  if not nick.screen_name? or @db.me.screen_name.casecmp(nick).zero?
    post server_name, ERR_NOSUCHNICK, nick, "No such nick: #{nick}" # or yourself
    return
  end

  unless @channels.key? channel
    post server_name, ERR_NOSUCHNICK, nick, "No such channel: #{channel}"
    return
  end

  @api.delay(0){|api| @channels[channel].on_kick(api, nick) }
end

#on_message(m) ⇒ Object



100
101
102
103
# File 'lib/atig/gateway/session.rb', line 100

def on_message(m)
  GC.start
  @on_message.call(m) if @on_message
end

#on_privmsg(m) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/atig/gateway/session.rb', line 182

def on_privmsg(m)
  target, mesg = *m.params
  m.ctcps.each {|ctcp| on_ctcp(target, ctcp) } if m.ctcp?

  case
  when mesg.empty?
    return
  when mesg.sub!(/\A +/, "")
    on_ctcp_action(target, mesg)
  when target[0] != ?#
    channel target
    on_ctcp_action(target, "dm #{target} #{mesg}")
  when (@opts.old_style_reply and mesg =~ /\A@(?>([A-Za-z0-9_]{1,15}))[^A-Za-z0-9_]/)
    on_ctcp_action(target, "reply #{$1} #{mesg}")
  else
    on_ctcp_action(target, "status #{mesg}")
  end
end

#on_topic(m) ⇒ Object



271
272
273
274
# File 'lib/atig/gateway/session.rb', line 271

def on_topic(m)
  channel,topic = *m.params
  on_ctcp_action(channel, "topic #{topic}")
end

#on_user(m) ⇒ Object



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
# File 'lib/atig/gateway/session.rb', line 105

def on_user(m)
  super
  @thread_group = ThreadGroup.new
  @thread_group.add Thread.current
  @ctcp_actions = {}
  @channels     = {}
  load_config

  @opts = Atig::Option.parse @real
  context = OpenStruct.new(:log=>@log, :opts=>@opts)

  oauth = OAuth.new(context, @nick)
  unless oauth.verified? then
    channel = channel '#oauth'
    channel.join_me
    channel.notify "Please approve me at #{oauth.url}"
    callcc{|cc|
      @on_message = lambda{|x|
        if x.command.downcase == 'privmsg' then
          _, mesg = *x.params
          if oauth.verify(mesg.strip)
            channel.part_me "Verified"
            save_config
            @on_message = nil
            cc.call
          end
        end
        return true
      }
      return
    }
  end

  log :debug, "initialize Twitter"
  twitter = Twitter.new   context, oauth.access
  search  = SearchTwitter.new context
  if @opts.stream
    unless @channels.key?("##{@nick}")
      ch = channel("##{@nick}")
      ch.join_me
    end
  end
  stream  = Stream.new context, @channels["##{@nick}"], oauth.access if @opts.stream
  @api    = Scheduler.new context, twitter, search, stream

  log :debug, "initialize filter"
  @ifilters = run_new @@ifilters, context
  @ofilters = run_new @@ofilters, context

  @api.delay(0) do|t|
    me  = t.post "account/update_profile"
    unless me then
      log :info, <<END
Failed to access API.
Please check Twitter Status <http://status.twitter.com/> and try again later.
END
      finish
    end
    @prefix = prefix me
    @user   = @prefix.user
    @host   = @prefix.host

    post server_name, MODE, @nick, "+o"

    @db = Atig::Db::Db.new context, :me=>me, :size=> 100, :tmpdir => @tmpdir
    run_new @@commands, context, self, @api, @db
    run_new @@agents  , context, @api, @db
    run_new @@channels, context, self, @db

    @db.statuses.add :user => me, :source => :me, :status => me.status
  end
end

#on_who(m) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/atig/gateway/session.rb', line 276

def on_who(m)
  channel  = m.params[0]

  unless @channels.key? channel
    post server_name, ERR_NOSUCHNICK, nick, "No such channel: #{channel}"
    return
  end
  @channels[channel].on_who do|user|
    #     "<channel> <user> <host> <server> <nick>
    #         ( "H" / "G" > ["*"] [ ( "@" / "+" ) ]
    #             :<hopcount> <real name>"
    prefix = prefix(user)
    server = 'twitter.com'
    mode   = case prefix.nick
             when @nick                     then "~"
             else                                "+"
             end
    real = user.name
    post server_name, RPL_WHOREPLY, @nick, channel, prefix.user, prefix.host, server, prefix.nick, "H*#{mode}", "1 #{real}"
  end
  post server_name, RPL_ENDOFWHO, @nick, channel
end

#on_whois(m) ⇒ Object



262
263
264
265
266
267
268
269
# File 'lib/atig/gateway/session.rb', line 262

def on_whois(m)
  nick = m.params[0]
  unless nick.screen_name?
    post server_name, ERR_NOSUCHNICK, nick, "No such nick/channel"
    return
  end
  on_ctcp_action(nil, "whois #{nick}")
end

#output_message(query) ⇒ Object



76
77
78
# File 'lib/atig/gateway/session.rb', line 76

def output_message(query)
  @ofilters.inject(query) {|x, f| f.call x }
end

#post(*args) ⇒ Object



47
48
49
# File 'lib/atig/gateway/session.rb', line 47

def post(*args)
  super
end

#prefix(u) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/atig/gateway/session.rb', line 86

def prefix(u)
  nick = u.screen_name
  nick = "@#{nick}" if @opts.athack
  user = "id=%.9d" % u.id
  host = "twitter"
  host += "/protected" if u.protected

  Net::IRC::Prefix.new("#{nick}!#{user}@#{host}")
end

#topic(entry) ⇒ Object



96
97
98
# File 'lib/atig/gateway/session.rb', line 96

def topic(entry)
  @channels.each{|_, ch| ch.topic entry }
end

#update_status(ret, target, msg = '') ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/atig/gateway/session.rb', line 51

def update_status(ret, target, msg='')
  @db.transaction do|db|
    db.statuses.add(:source => :me, :status => ret, :user => ret.user )
  end

  msg = "(#{msg})" unless msg.empty?
  self[target].notify "Status updated #{msg}"
end