/*
 * broadcast.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_broadcast(char *a);

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s BROADCAST <message>\nSend a message to ALL connected users.\n"
	   , BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Send a message to ALL connected users");
  dl->level = 950;
#if 0
  dl->func = m_broadcast;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_broadcast");
#endif
  return 1;
}

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

#define MSGLEN 430
MODULE_PUBLIC void m_broadcast(char *a)
{
  char message[MSGLEN+1];
  int nr = 0;
  int i, len;
  struct activeuser *u;
  struct dbaseuser *db;
  ListElem *el;

  db = sender_au->dbuser;
  if(db) {
    log("From: %s broadcast [%s]\n", db->nick, a);
  } else
    return;
  if(!is_oper()) {
    say_sender("You are not an Oper.");
    log("To: %s Is not an Oper\n", db->nick);
    return;
  }
  i=0;
  len = sprintf(message, "BROADCAST from %s: ", sender_au->nick);

  strncpy(&message[len], a, MSGLEN-len);
  message[MSGLEN] = '\0';
  if(!strlen(&message[len])) {
    say_sender("No message was given.");
    return;
  }

  el = GetHead(auser_list);
  while(el) {
    u = (struct activeuser *)(el->data);
    say_y(u->yxx, message);
    nr++;
    //    if(nr > 100) break;
    el = GetSucc(el);
  }
/*
  sprintf(message, "Message sent to %d users", nr);
  say_sender( message);
*/
  return;
}
