/*
 * opchan.c
 * (C) Christian Magnusson 2000, Email: d1mag@dtek.chalmers.se
 * This program is copyright, and covered by the Gnu Public License.
 * The Q bot.
 */

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "settings.h"
#include "bottypes.h"
#include "globals.h"
#include "dl.h"

void m_whoami(char *a);

MODULE_PUBLIC int _init_whoami(void *a, void *b)
{
  dl_command_entry_t *dl = a;
  if(!a) return 0;

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s WHOAMI\nThe bot should tell you your current global auth level (equivalent to the old #cservice levels) and may give other info too.\n"
	   , BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Gives the bot's opinion on who you are.");
  dl->level = 0;
#if 0
  dl->func = m_whoami;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_whoami");
#endif
  return 1;
}

MODULE_PUBLIC int _remove_whoami(char *a, char *b)
{
  remove_command("whoami");
  return 1;
}

MODULE_PUBLIC void m_whoami(char *a)
{
  struct dbaseuser *p;
  struct chanpriv *cp;
  struct chanmod *cm;
  char buffer[512];

  if(!sender_au) return;

  p = NULL;
  if(sender_au->userid) {
    p = sender_au->dbuser;
  }
  if(p) {
    cp = p->privs;
    while(cp) {
      sprintf(buffer,"Access level ");
      createmodes(cp->chanlevel,buffer+strlen(buffer));
      sprintf(buffer+strlen(buffer)," on channel %s.",cp->channel);
      say_sender(buffer);
      cp = cp->next;
    }
  }
  sprintf(buffer,"Global auth level: %d",sender_au->auth);
  say_sender(buffer);
  sprintf(buffer,"Current modes: ");
  createmodes(sender_au->mode,buffer+strlen(buffer));
  say_sender(buffer);
  if(sender_au->oper == 1) {
    say_sender( "You are an IRCop.");
  }
  if(sender_au->userid) {
    if(p) {
      say_sender_("You have authed as userid: %d nick: %s",
	      sender_au->userid, p->nick);
      if(p->email) {
	say_sender_("E-mail: %s", p->email);
      } else {
	say_sender("E-mail: (not set)");
      }
    } else {
      /* this shouldn't happen (in case if account was deleted after auth */
      say_sender_("You have authed as userid %d.", sender_au->userid);
      say_sender("E-mail: (not set)");
    }
  } else {
    say_sender( "You have NOT authed");
  }
  cm = sender_au->chanmodes;
  while(cm) {
    sprintf(buffer,"You are on %s.",cm->channel);
    say_sender(buffer);
    cm = cm->next;
  }

  return;
}
