def make_bindings(window)

  window.bind('Control-h', proc { help_keys(window)} )
  window.bind('Control-s', proc { status_player(window)} )
  window.bind('Control-r', proc { results_player(window)} )
  window.bind('Control-m', proc { match_player(window)} )
  window.bind('Control-d', proc { decline_player(window)} )
  window.bind('Control-t', proc { tell_user(window)} )
#    window.bind ('Control-o', proc { observe_pro_game(window)} )

  register_command('set_auto_reply', proc { |*params| set_auto_reply(*params) } )
  register_command('cancel_auto_reply', proc { cancel_auto_reply } )
  register_command('chat', proc { |player| chat(player) } )
  register_command('match', proc { |player,*args| open_match_window(player,*args) } )
  register_command('auto_load', proc { |game| set_auto_load(game) } )
  @auto_reply_set=false

  user_make_bindings(window)
end

def user_make_bindings(window)
end

def help_keys(window)
  putline "\n"
  putline "rubygo predefined key bindings:\n"

  putline "Control-h : this help\n"
  putline "Control-s : status of last player requesting a match\n"
  putline "Control-r : results of that player\n"
  putline "Control-m : send 'match' to that player\n"
  putline "Control-d : decline match\n"
  putline "Control-t : reply to the last player talking to you\n"
#   putline "Control-o : observe last pro game started\n"

  putline "\n rubygo predefined commands:\n"
  putline "  set_auto_reply <text>     : reply to a user when your are not there\n"
  putline "  cancel_auto_reply         : guess ?\n"
  putline "  chat <player | channel>   : talk with a player/channel\n"
  putline "  match <player>            : open match window\n"
  putline "  auto_load <game>          :  load/observe an adjourned game\n"


  user_do_help(window)
end

def user_do_help(window)
end

def status_player(window)
  return if @player_requesting_match.nil?
  send_command('stats '+ @player_requesting_match)
end

def results_player(window)
  return if @player_requesting_match.nil?
  send_command('results '+ @player_requesting_match)
end

def match_player(window)
  return if @use_match_to_respond.nil?
  window.set_cmd(@use_match_to_respond)
end

def decline_player(window)
  return if @use_decline_to_respond.nil?
  window.set_cmd(@use_decline_to_respond)
end

def tell_user(window)
  if @user_to_tell
    window.set_cmd('tell '+ @user_to_tell)
  elsif @player_requesting_match
    window.set_cmd('tell '+ @player_requesting_match)
  end
end

def cancel_auto_reply
  @auto_reply_set=false
  putline("auto_reply canceled\n")
end

def set_auto_reply(*params)
  if params.empty?
    text=Defaults::DEFS['ReplyText']
  else
    text = params.join(' ')
    @text_auto_reply= text
  end
  putline("auto_reply set to: \n" + text + "\n")
  @auto_reply_set=true
end

def auto_reply
  return unless @auto_reply_set
  send_command('tell '+ @user_to_tell + ' ' + @text_auto_reply) 
end

def chat(player)
  $chatobserver.new_chat(player) unless $chatobserver.nil?
end

def  open_match_window(player,*args)

  if args.empty?
    GoMatch.new('match '+ player,player)
  else
    $igsClient.send('match ' + player + ' ' + args.join(' '))
  end

end

def set_auto_load(game)
  @game_to_load= game
  game =~ /(\S+)-(\S+)/
  if $1 == $user or $2 == $user
    @player_to_wait= ($1== $user ? $2 : $1)
    putline("waiting for #@player_to_wait to connect\n",'tell')
  else
    @game_to_observe= Array.new unless @game_to_observe
    @game_to_observe.push( game)
    putline("waiting for #{game} to restart\n",'tell')
  end

end

def make_vars(code,line)
      #debug "#{code}:#{line}" 
  case code
  when  IGSProtocol::VERSION # connected
    init_session
   #   $igsClient.send('user')
  when IGSProtocol::TELL
    if line =~ /\*(\S+)\*:/
      @user_to_tell = $1
    end
  when IGSProtocol::SHOUT
    if m = /\{(\S+) \[.+\] has connected/.match(line)
        if  @player_to_wait and m[1] ==  @player_to_wait
          putline("#@player_to_wait is connected\n")
  $igsClient.send("load #@game_to_load")
  @player_to_wait= nil
end
  #{Game 24: zyy vs gti @ Move 198}
elsif m = /Game (\d+): (\S+) vs (\S+) @ Move/.match(line)
  game= $2+'-'+$3
  if @game_to_observe and @game_to_observe.include?( game)
    putline("game #{game} has restarted\n")
    send_command('observe '+$1)
    @game_to_observe.delete(game)
  end
end
when IGSProtocol::INFO
  if line =~ /Match.* requested with (\S+) as .*/
    @player_requesting_match = $1
  elsif  line =~ /Use <(.+)> or <(.+)> to respond/
    @use_match_to_respond = $1
    @use_decline_to_respond = $2
  elsif line =~ /You have .* message/
    Misc.info(line,"IGS MailBox")
  end
when IGSProtocol::SAVED
  unless line =~ /Found/
    line.gsub(/\S+-\S+/) { | game |
      game =~ /(\S+)-(\S+)/
      @buf_stored << $1  if $1 != $user
      @buf_stored << $2  if $2 != $user
    }
  else
    list="You have games stored with:\n"
    @buf_stored.each { |user|
      list << user << "\n"
      $chatobserver.notify(user)
    }
    list << "waiting for player(s) to connect"
    unless @buf_stored.empty?
      Misc.info(list,"Games stored")
      @buf_stored = []
    end
  end
end

user_make_vars(code,line)
end

def  user_make_vars(code,line)
end

def init_session

  @buf_stored = []
  $igsClient.send("id RubyGo-#$version #{RUBY_VERSION} #{RUBY_PLATFORM}")

  $igsClient.send('user '+$user)
  $igsClient.send('stored')
  Defaults::NOTES.each {|key,val|# debug "#{key} -> #{val}" 
    $chatobserver.notify(key) if val['notify'] == '1' and key != $user
  }

  $igsClient.send('igs') 

  user_init_session
end

def user_init_session
end


user_scripts=File.join($home,'scripts.rb')

if test(?r,user_scripts)
  load user_scripts
  
  $stderr.puts "user scripts loaded : " + user_scripts
else
  $stderr.puts "user scripts not found"
end



