class TermtterIrcGateway

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/plugins/irc_gw.rb, line 71
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

Public Instance Methods

call(statuses, event, indent = 0) click to toggle source
# File lib/plugins/irc_gw.rb, line 102
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) click to toggle source
# File lib/plugins/irc_gw.rb, line 173
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_ids() click to toggle source
# File lib/plugins/irc_gw.rb, line 222
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) click to toggle source
# File lib/plugins/irc_gw.rb, line 206
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) click to toggle source
# File lib/plugins/irc_gw.rb, line 183
def log(str)
  str.each_line do |line|
    post server_name, NOTICE, main_channel, line
  end
end
main_channel() click to toggle source
# File lib/plugins/irc_gw.rb, line 69
def main_channel; '#termtter' end
on_message(m) click to toggle source
# File lib/plugins/irc_gw.rb, line 133
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) click to toggle source
# File lib/plugins/irc_gw.rb, line 150
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) click to toggle source
Calls superclass method
# File lib/plugins/irc_gw.rb, line 141
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_name() click to toggle source
# File lib/plugins/irc_gw.rb, line 67
def server_name; 'termtter' end
server_version() click to toggle source
# File lib/plugins/irc_gw.rb, line 68
def server_version; '0.0.0' end
sync_commands() click to toggle source
# File lib/plugins/irc_gw.rb, line 197
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_friends() click to toggle source
# File lib/plugins/irc_gw.rb, line 189
def sync_friends
  previous_friends = @members
  new_friends = Termtter::Client.following_friends
  diff = new_friends - previous_friends
  join_members(diff)
  @members += diff
end