/*****************************************************************
 * PTlink IRCd is (C) CopyRight PTlink Coders Team 1999-2000     *
 * http://www.ptlink.net/Coders - coders@PTlink.net              *
 * This program is distributed under GNU Public License          *
 * Please read the file COPYING for copyright information.       *
 *****************************************************************
 
 File: m_ircops.c
 Desc: list online ircops
 Author: Lamego@PTlink.net
  
*/

#include "m_commands.h"
#include "client.h"
#include "common.h"
#include "ircd.h"
#include "numeric.h"
#include "s_serv.h"
#include "irc_string.h"
#include "send.h"
#include "conf_vars.h"
#include "listener.h"

#include <string.h> 	/* for strchr() */

/*
** m_ircops() By ScOrP|On
** Rewritten by HAMLET
** rererewritten by pier (showing vcops and NOT showing +a)
**   Lists online IRCOps
**
*/
int m_ircops(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
{
  struct Client *acptr;
  char buf[BUFSIZE];
  int locals = 0, helpers = 0;

  if (!IRCopsForAll && !IsPrivileged(cptr))
    {
      sendto_one(sptr, form_str(ERR_NOPRIVILEGES), me.name, parv[0]);
      return 0;
    }
	
if (!IsWebchat(sptr)) {
  strcpy(buf, "Nick                           Status            Server");
  sendto_one(sptr, form_str(RPL_IRCOPS), MENAME(sptr), parv[0], buf);
  strcpy(buf, "-------------------------------------------------------");
  sendto_one(sptr, form_str(RPL_IRCOPS), MENAME(sptr), parv[0], buf);
}
  for (acptr = GlobalClientList; acptr; acptr = acptr->next)
	{

    if (!acptr->user) continue;

    if (!IsService(acptr) && !IsSAdmin(acptr) && (IsHelper(acptr) ||
    (IsVAny(acptr) && sptr->user && sptr->user->vs && acptr->user->vs
    && match(acptr->user->vs, sptr->user->vs)) ))
        {
          ircsprintf(buf, "%-30s  %-6s  %-8s  %s",
            acptr->name ? acptr->name : "<unknown>",
            (IsAdmin(acptr) || IsVAdmin(acptr)) ? "Admin" : (IsAnOper(acptr) || IsVCop(acptr)) ? "Ircop" : "Staff",
            acptr->user->away ? "(AWAY)" : IsHelper(acptr) ? "(Helper)" : "",
            IsVAny(acptr) ? acptr->user->vs : "");
	  
          sendto_one(sptr, form_str(RPL_IRCOPS), MENAME(sptr), parv[0], buf);
          if (IsHelper(acptr)) helpers++;
          else if (IsVAny(acptr)) locals++;
    	}
    }
   
  ircsprintf(buf, "Total: \2%d\2 Helpful looking %s found - \2%d\2 Global Helper%s, \2%d\2 Local Ircop%s/Admin%s", helpers+locals,
                        (helpers+locals) > 1 ? "people" : "person",
                        helpers, helpers > 1 ? "s" : "", locals, locals > 1 ? "s" : "",locals > 1 ? "s" : "");
  sendto_one(sptr, form_str(RPL_IRCOPS), MENAME(sptr), parv[0], buf);

/*strcpy(buf, "============================================");
  sendto_one(sptr, form_str(RPL_IRCOPS), MENAME(sptr), parv[0], buf);*/

  sendto_one(sptr, form_str(RPL_ENDOFIRCOPS), MENAME(sptr), parv[0]);
  return 0;
}

/*
 ** m_staff
 ** Written by Pier
 ** This will show ALL IRC OPERATORS to BraSmIRC's
 ** SERVICES ROOTS--HardMAn
 */
int m_staff(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
{
  struct Client *acptr;
  char *status;
  char buf[BUFSIZE];
  int locals = 0, globals = 0, virtuals = 0, helpers = 0;

  if (!IsTechAdmin(cptr))
    {
      return 0;
    }
	
if (!IsWebchat(sptr)) {
  strcpy(buf, "Nick                   Status                            Server");
  sendto_one(sptr, form_str(RPL_IRCOPS), MENAME(sptr), parv[0], buf);
  strcpy(buf, "---------------------------------------------------------------");
  sendto_one(sptr, form_str(RPL_IRCOPS), MENAME(sptr), parv[0], buf);
}

  for (acptr = GlobalClientList; acptr; acptr = acptr->next)
	{
    
         if (!acptr->user) continue;
 	  if (!IsService(acptr) && (IsAnOper(acptr) || IsVAny (acptr) || IsHelper(acptr)) )
 {

	  if (IsRoot(acptr)) 
		status = "Root Administrator";
	  else if (IsTechAdmin(acptr)) 
		status = "Technical Administrator";
	  else if (IsNetAdmin(acptr)) 
		status = "Network Administrator";
	  else if (IsSAdmin(acptr)) 
		status = "Services Administrator";
	  else if (IsAdmin(acptr)) 
		status = "Server Administrator";		
	  else if(IsAnOper(acptr))
		status = "IRC Operator"; 
	  else if(IsHelper(acptr))
		status = "IRC Helper"; 
	  else if(IsVAdmin(acptr))
		status = "Virtual Administrator"; 
	  else if(IsVCop(acptr))
		status = "Virtual Operator"; 
	  else
		status = "I dont know..";  	


    if (acptr->user && acptr->user->vs && IsVAny(acptr))
      {
        ircsprintf(buf, "%-21s  %-22s  %-8s  %s", acptr->name ? acptr->name : "<unknown>", status,
                              acptr->user->away ? "(AWAY)" : "", acptr->user->vs);
      }
    else
      {
      ircsprintf(buf, "%-21s  %-22s  %-8s  %s", acptr->name ? acptr->name : "<unknown>", status,
                            acptr->user->away ? "(AWAY)" : "", acptr->user->server);
      }
	                                                                                                   
        sendto_one(sptr, form_str(RPL_IRCOPS), MENAME(sptr), parv[0], buf);

          if (IsRoot(acptr) || IsTechAdmin(acptr) || IsNetAdmin(acptr) || IsSAdmin(acptr)) globals++;
          else if (IsHelper(acptr)) helpers++;
          else if (IsAnOper(acptr) || IsAdmin(acptr)) locals++;
          else if (IsVAny(acptr)) virtuals++;
    	}
    }
 
  ircsprintf(buf, "Total: \2%d\2 IRCOp%s connected - \2%d\2 Globa%s, \2%d\2 Helpe%s, \2%d\2 Loca%s, \2%d\2 Virtua%s", 
                         globals+locals+virtuals+helpers,
                        (globals+locals+virtuals+helpers) > 1 ? "s" : "",
                        globals, globals > 1 ? "ls" : "l",
                        helpers, helpers > 1 ? "rs" : "r",
                        locals, locals > 1 ? "ls" : "l",
                        virtuals, virtuals > 1 ? "ls" : "l");
  sendto_one(sptr, form_str(RPL_IRCOPS), MENAME(sptr), parv[0], buf);
  sendto_one(sptr, form_str(RPL_ENDOFIRCOPS), me.name, parv[0]);
  return 0;
}


