/*
 * teshline.c for tesh NT
 * $Id: teshline.c,v 1.18 2002/06/14 14:49:20 tesh Exp $
 * 010710 cb
 *
 * (C) 2001 Christian Jacobi
 * (C) 2001 Christoph Berg
 *
 * This program 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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.
 */

#include <signal.h>

#include "teshline.h"

// not defined in Linux' readline.h
extern char **completion_matches PARAMS((char *, rl_compentry_func_t *));


char *user_generator(char *text, int state) {
	static int n, len;
	//static char *user[] = {"cj", "cb", "jochen", "kempest", NULL};
	static char **user = NULL;

	if(!state) {
		n = 0;
		len = strlen(text);
		if(user)
			freelist(user);
		user = complete_name(text);
	}
	for(; user[n]; n++) {
		if(!strncmp(text, user[n], len))
			return strdup(user[n++]);
			// hier n++, da das n++ oben nicht mehr ausgeführt wird
	}
	return NULL;
}

char *article_generator(char *text, int state) {
	static int n, len;
	static char **article = NULL;

	if(!state) {
		n = 0;
		len = strlen(text);
		if(article)
			freelist(article);
		article = complete_article(text);
	}
	for(; article[n]; n++) {
		if(!strncmp(text, article[n], len))
			return strdup(article[n++]);
			// hier n++, da das n++ oben nicht mehr ausgeführt wird
	}
	return NULL;
}

char **generator_mux(char *text, int start, int end) {
	char **matches = NULL;

	// at the beginning: complete user names
	if(start == 0) {
		matches = completion_matches(text, (rl_compentry_func_t *) user_generator);
	}

	// return matches, fallback to article names if NULL
	return matches;
}

int tante_clear_screen(int count, int key) {
	// wipe screen, hipe history
	int i;
	rl_save_prompt();
	i = rl_clear_screen(count, key);
	rl_message("");
	rl_restore_prompt();
	help();
	clear_history();
	return i;
}

void init_signals() {
	// signal(SIGHUP, SIG_IGN);
	signal(SIGINT, SIG_IGN);
	signal(SIGQUIT, SIG_IGN);
	// signal(SIGABRT, SIG_IGN);
	// signal(SIGTERM, SIG_IGN);
	signal(SIGTSTP, SIG_IGN);
}

void init_readline() {
	// standard fallback completion function
	rl_completion_entry_function = (rl_compentry_func_t *)article_generator;
	// try this first
	rl_attempted_completion_function = (CPPFunction *)generator_mux;

	rl_bind_key(CTRL('L'), (Function *)tante_clear_screen);

	stifle_history(MAX_HISTORY_LINES);
}

char *get_tante_str(char *prompt) {
	static char *line = NULL;

	if(line != NULL) {
		free(line);
		line = NULL;
	}

	line = readline(prompt);

	if(line && *line) {
		add_history(line);
		stifle_history(MAX_HISTORY_LINES);
	}
	if(line == NULL) { // ^D pressed
		puts("");
		return "";
	}

	return line;
}

char *static_strdup(char *s) {
	static char *l = NULL;
	if(l != NULL) {
		free(l);
	}
	l = strdup(s);
	return l;
}

int check_tante_pw(struct db_kunde *kunde) {
	char *pw, *c;
	pw = getpass("  Passwort: ");
	c = crypt(pw, kunde->pw);
	// printf("  pw: %s db: %s crypt: %s\n", pw, kunde->pw, c);
	if (!strcmp(c, kunde->pw)) {
		if(kunde->locked) {
			printf("  Account gesperrt.\n");
			printf("  Bitte beim Tante-Emma-Team melden. (Oder Mail an tante@wjpserver.cs.uni-sb.de)\n");
			return 0;
		}
		return 1;
	}
	c = crypt(pw, SUPERVISOR_PASSWORD);
	return (!strcmp(c, SUPERVISOR_PASSWORD));
}

int check_super_pw() {
	char *pw, *c;
#ifdef DEBUG
	return 1;
#endif
	pw = getpass("  Root-Passwort: ");
	c = crypt(pw, SUPERVISOR_PASSWORD);
	return (!strcmp(c, SUPERVISOR_PASSWORD));
}
