/* GADMIN-PROFTPD - An easy to use GTK+ frontend for the ProFTPD standalone server.
 * Copyright (C) 2001 - 2008 Magnus Loef <magnus-swe@telia.com> 
 *
 * 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 3 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
*/



#include "../config.h"
#include <gtk/gtk.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "widgets.h"
#include "allocate.h"
#include "commands.h"
#include "chars_are_digits.h"
#include "kick_button_clicked.h"

extern int activated;

extern int MAX_READ_POPEN;



void kick_button_clicked(struct w *widgets)
{
    /* Kick a user by name or pid */
    FILE *fp;
    char *get_buffer, *kick_name, *kick_pid, *kick;
    int user_kick=0, pid_kick=0;
    G_CONST_RETURN gchar *username;
    int avoid_warnings = 0;

    if( ! activated )
      return;

    username = gtk_entry_get_text(GTK_ENTRY(widgets->kick_entry));
    
    if( strlen(username) == 0 )
      return;

    /* Get users pid and name */
    if((fp=popen("ftpwho -v", "r"))==NULL)
      return;

    fflush(fp);

    kick_name = allocate(4096);
    kick_pid = allocate(4096);

    get_buffer = allocate(MAX_READ_POPEN+1);
    while(fgets(get_buffer, MAX_READ_POPEN, fp)!=NULL)
    {
	kick_pid[0]='\0'; kick_name[0]='\0';
	sscanf(get_buffer, "%s %s", kick_pid, kick_name);

	if( kick_name[strlen(kick_name)-1]=='\n' || kick_name[strlen(kick_name)-1]=='\r' )
	  kick_name[strlen(kick_name)-1]='\0';

	/* kill this user by name */
	if( strcmp(username, kick_name) == 0 && chars_are_digits(kick_pid) )
	{
	    user_kick=1;
	    break;
	}
	/* Kill this user by pid (supplied number is the same as any left most word in the output) */
	if( strcmp(username, kick_pid) == 0 && chars_are_digits(kick_pid)  )
	{
	    pid_kick=1;
	    break;
	}	
    }
    pclose(fp);
    free(get_buffer);
    free(kick_name);


    if( user_kick )
    {
	/* Kill the the users pid */
	kick = allocate(8192);
	sprintf(kick, "kill -15 %s", kick_pid);
	avoid_warnings = system(kick);
	free(kick);
    }
    else
    if( pid_kick )
    {
	/* Kill the the users pid */
	kick = allocate(8192);
	sprintf(kick, "kill -15 %s", username);
	avoid_warnings = system(kick);
	free(kick);
    }
    else
      {
//	  strcpy(info_buffer, _("User or pid not found.\n"));
//	  info_window = create_info_window();
//          gtk_widget_show(info_window);
      }
    free(kick_pid);
}
