Skip to content
engine.cc 2.15 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 "sll/connection.hh"
#include "sll/cypher.hh"

#include "engine.hh"
#include "screen.hh"
#include "options.hh"

#include <string.h>
#include <openssl/sha.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/evp.h>
#include <openssl/md5.h>

void Engine::on() {
  // Create graphic env
  S.init();

  // Show login screen
  S.login();

  // Showing dialog screen
  S.dialog();

  // connect and auth
  login();

  // Treat keyboards events
  S.eventsloop();
}

void Engine::off() {
  // Close connection
  try { C.disconnect(); }
  catch (...) {}
}

/**
 * Connect and auth to server
 *
 */
void Engine::login() {
  try {

    S << INF_DATA << "Trying to connect to " << O.server << " on " << O.port << ".\n";
    C.connect(O.server.c_str(), O.port);

    S << INF_DATA << "Trying to auth as " << O.login << ".\n";
    auth();
  }
  catch (const Error &e) {
    try {C.disconnect(); }
    catch (...) {}

    if (e.code() == ERR_NET || e.code() == ERR_AUTH)
      S << WAR_DATA << e.message() << ".\n";
    else
      throw;
  }
}

/**
 * Try authenticate to server
 *
 */
void Engine::auth() {
  char buf[MAX_LINE_SIZE];

  snprintf(buf, MAX_LINE_SIZE, "USER %s", O.login.c_str());
  C.sendln(buf);

  snprintf(buf, MAX_LINE_SIZE, "PASS %s",
	   Cypher::sha1_64(O.pass.c_str(), O.pass.length()).c_str());
  C.sendln(buf);

  string valid = C.recvln();

  if (valid != "OK")
    throw Error(ERR_AUTH, "Authentication refused");
}