Class: TermtterIrcGateway

Inherits:
Net::IRC::Server::Session
  • Object
show all
Defined in:
lib/plugins/irc_gw.rb

Constant Summary collapse

@@listners =
[]
@@last_statuses =
[]

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TermtterIrcGateway

Returns a new instance of TermtterIrcGateway.



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
# File 'lib/plugins/irc_gw.rb', line 72

def initialize(*args)
  super
  @@listners << self
  @members = Set.new
  @commands = []

  Termtter::Client.register_hook(:collect_user_names_for_irc_gw, :point => :pre_filter) do |statuses, event|
    new_users = []
    statuses.each do |s|
      screen_name = s.user.screen_name
      next if screen_name == config.user_name
      next unless friends_ids.include? s.user.id
      next if @members.include? screen_name
      @members << screen_name
      new_users << screen_name
    end
    join_members(new_users)
  end

  Termtter::Client.register_command(
    :name => :collect_friends,
    :help => 'Collect friends for IRC.',
    :exec => lambda {|arg|
      sync_friends
    })

  Termtter::Client.register_hook(:collect_commands_for_irc_gw, :point => :post_command) do |text|
    sync_commands if text =~ /plug/
  end
end

Instance Method Details

#call(statuses, event, indent = 0) ⇒ Object



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
# File 'lib/plugins/irc_gw.rb', line 103

def call(statuses, event, indent = 0)
  if event == :update_friends_timeline
    msg_type = PRIVMSG
  else
    time_format = Termtter::Client.time_format_for statuses
    msg_type = NOTICE
  end
  statuses.each do |s|
    typable_id = Termtter::Client.data_to_typable_id(s.id)
    time = Time.parse(s.created_at).strftime(time_format) if time_format
    reply_to_status_id_str =
      if s.in_reply_to_status_id
        "(reply to #{Termtter::Client.data_to_typable_id(s.in_reply_to_status_id)})"
      else
        nil
      end

    padding = indent > 0 ? '' : nil

    post s.user.screen_name, msg_type, main_channel, [time, padding, CGI.unescapeHTML(s.text), typable_id, reply_to_status_id_str].compact.join(' ')
    if config.plugins.stdout.show_reply_chain && s.in_reply_to_status_id && indent < config.plugins.stdout.max_indent_level
      begin
        if reply = Termtter::API.twitter.cached_status(s.in_reply_to_status_id)
          call([reply], event, indent+1)
        end
      rescue Rubytter::APIError
      end
    end
  end
end

#execute_command(command) ⇒ Object



174
175
176
177
178
179
180
181
182
# File 'lib/plugins/irc_gw.rb', line 174

def execute_command(command)
  command.encode!('utf-8', 'utf-8') if command.respond_to? :encode!
  original_confirm = config.confirm
  config.confirm = false
  post '#termtter', NOTICE, main_channel, '> ' + command
  Termtter::Client.execute(command)
ensure
  config.confirm = original_confirm
end

#friends_idsObject



223
224
225
226
227
228
229
# File 'lib/plugins/irc_gw.rb', line 223

def friends_ids
  if !@friends_ids || !@friends_ids_expire ||@friends_ids_expire < Time.now
    @friends_ids = Termtter::API.twitter.friends_ids(:screen_name => config.user_name)
    @friends_ids_expire = Time.now + 3600
  end
  @friends_ids
end

#join_members(members) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/plugins/irc_gw.rb', line 207

def join_members(members)
  params = []
  max_params_count = 3
  members.each do |member|
    prefix = Prefix.new("#{member}!#{member}@localhost")
    next if prefix.extract.empty?
    post prefix, JOIN, main_channel
    params << prefix.nick
    next if params.size < max_params_count

    post server_name, MODE, main_channel, "+#{"v" * params.size}", *params
    params = []
  end
  post server_name, MODE, main_channel, "+#{"v" * params.size}", *params unless params.empty?
end

#log(str) ⇒ Object



184
185
186
187
188
# File 'lib/plugins/irc_gw.rb', line 184

def log(str)
  str.each_line do |line|
    post server_name, NOTICE, main_channel, line
  end
end

#main_channelObject



70
# File 'lib/plugins/irc_gw.rb', line 70

def main_channel; '#termtter' end

#on_message(m) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/plugins/irc_gw.rb', line 134

def on_message(m)
  termtter_command = m.command.downcase + ' ' + m.params.join(' ')
  return unless Termtter::Client.find_command(termtter_command)
  execute_command(termtter_command)
rescue Exception => e
  Termtter::Client.handle_error(e)
end

#on_privmsg(m) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/plugins/irc_gw.rb', line 151

def on_privmsg(m)
  target, message = *m.params
  if message =~ / +\//
    termtter_command = message.gsub(/ +\//, '')
    return unless Termtter::Client.find_command(termtter_command)
    execute_command(termtter_command)
    return
  end
  config.plugins.irc_gw.command_regexps and
  config.plugins.irc_gw.command_regexps.each do |rule|
    if message =~ rule
      command = message.scan(rule).first.join(' ')
      next unless Termtter::Client.find_command(command)
      execute_command(command)
      return
    end
  end
  execute_command('update ' + message)
  post @prefix, TOPIC, main_channel, message
rescue Exception => e
  Termtter::Client.handle_error(e)
end

#on_user(m) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/plugins/irc_gw.rb', line 142

def on_user(m)
  super
  @user = m.params.first
  post @prefix, JOIN, main_channel
  post server_name, MODE, main_channel, "+o", @prefix.nick
  sync_commands
  self.call(@@last_statuses || [], :update_friends_timeline)
end

#server_nameObject



68
# File 'lib/plugins/irc_gw.rb', line 68

def server_name; 'termtter' end

#server_versionObject



69
# File 'lib/plugins/irc_gw.rb', line 69

def server_version; '0.0.0' end

#sync_commandsObject



198
199
200
201
202
203
204
205
# File 'lib/plugins/irc_gw.rb', line 198

def sync_commands
  previous_commands = @commands
  new_commands = (
    Termtter::Client.commands.keys + Termtter::Client.commands.values.map(&:aliases)
    ).flatten.uniq.compact
  join_members(new_commands - previous_commands)
  @commands = new_commands
end

#sync_friendsObject



190
191
192
193
194
195
196
# File 'lib/plugins/irc_gw.rb', line 190

def sync_friends
  previous_friends = @members
  new_friends = Termtter::Client.following_friends
  diff = new_friends - previous_friends
  join_members(diff)
  @members += diff
end