Skip to content
cmd.cc 3.35 KiB
Newer Older
Seblu's avatar
Seblu committed
/*
  This file is part of SLC.
  Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>

  SLC is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 2 of the License.

  SLC is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with SLC; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "slc.hh"
#include "engine.hh"
#include "screen.hh"
#include "options.hh"
#include "sll/connection.hh"

namespace cmd {

  /*!
  ** Exec command if @param s is not a command.
  */
  void exec(const char *s) {
    // string is a local command
    if (*s == '/') {
      if (!strcmp(s, "/exit")) {
	S << INF_DATA << "Exiting.\n";
	exit(0);
      }
      else if (!strcmp(s, "/logout"))
	cmd::logout();
      else if (!strcmp(s, "/login"))
	cmd::login();
      else if (!strcmp(s, "/help"))
	cmd::help();
      else if (!strncmp(s, "/user", 5))
	cmd::user(s);
      else if (!strncmp(s, "/pass", 5))
	cmd::pass(s);
      else
	S << WAR_DATA << s << ": Unkown command.\n";
    }
    // String is a remote command
    else {
      if (C.connected()) {
	C.sendln(s);
	S << ">> " << s << "\n";
      }
      else
	S << "Unable to send: " << s << ". Try to connect with /login !\n";
    }
  }


  /**
   * Log into server
   *
   */
  void login() {
    if (C.connected()) {
      S << INF_DATA << "Already connected. Please disconnect before.\n";
      return;
    }
    S << INF_DATA << "Login.\n";
    E.login();
  }


  /**
   * Log out of server
   *
   */
  void logout() {
    if (!C.connected()) {
      S << INF_DATA << "Not connected. Please connect before.\n";
      return;
    }
    S << INF_DATA << "Logout.\n";
    C.disconnect();
  }

  /**
   * Print cmd help
   *
   */
  void help() {
    S << INF_DATA << "Commands:\n";
    S << INF_DATA << "/exit: Quit.\n";
    S << INF_DATA << "/login: Connect and try to login on server.\n";
    S << INF_DATA << "/logout: Disconnect from server.\n";
    S << INF_DATA << "/user <name>: Change username.\n";
    S << INF_DATA << "/pass <pass>: Change password.\n";
    S << INF_DATA << "/help: Print this help.\n";
  }

  /**
   * Change user name to @param s
   */
  void user(const char *s) {
    if (C.connected()) {
      S << INF_DATA << "Your are connected. Please deconnect before.\n";
      return;
    }

    // extract name
    string login = s;
    login.erase(0, 6);

    if (login.empty()) {
      S << WAR_DATA << "Empty username.\n";
      return;
    }

    // set new name
    O.login = login;

    // display
    S << INF_DATA << "Username changed to `" << O.login << "'.\n";
  }

  /**
   * Change pass name @param s
   */
  void pass(const char *s) {
    if (C.connected()) {
      S << INF_DATA << "Your are connected. Please deconnect before.\n";
      return;
    }

    // extract name
    string pass = s;
    pass.erase(0, 6);

    if (pass.empty()) {
      S << WAR_DATA << "Empty password.\n";
      return;
    }

    // set new name
    O.pass = pass;

    // display
    S << INF_DATA << "Password changed.\n";
  }

}