<commit_before>/**
 * Copyright (c) <NAME>, 2010
 * See LICENSE for license.
 */

#include "perl_callbacks.h"
#include "embedperl.h"
#include <assert.h>

#define MAX_EMBED_PERL 10
// #define DEBUG

// Totally thread-unsafe...
static EmbedPerl *ePerl[MAX_EMBED_PERL];
static int numEmbedPerl = 0;

extern "C" {
void xs_init(pTHX);

/***** CALLBACKS *****/

#define FOR_EVERY_EMBED(UNIQUEID,CALLBACK) \
  for( int i = 0; i < numEmbedPerl; ++i ) \
     if( strcmp(ePerl[i]->uniqueid(),UNIQUEID)==0 && ePerl[i]->CALLBACK )

void emoteEmbed(const char *uniqueid, const char *receiver, const char *message)
{
#ifdef DEBUG
  printf("***Callback*** Send emote to %s (on %s): %s\n", receiver, uniqueid, message);
#endif
  FOR_EVERY_EMBED(uniqueid,emoteCallback) {
    ePerl[i]->emoteCallback( uniqueid, receiver, message, ePerl[i]->data );
    return;
  }
  fprintf(stderr,"[CallbackError] No handler for Emote callback (uniqueid=%s)\n", uniqueid);
}

void privmsgEmbed(const char *uniqueid, const char *receiver, const char *message)
{
#ifdef DEBUG
  printf("***Callback*** Send privmsg to %s (on %s): %s\n", receiver, uniqueid, message);
#endif
  FOR_EVERY_EMBED(uniqueid,privmsgCallback) {
    ePerl[i]->privmsgCallback( uniqueid, receiver, message, ePerl[i]->data );
    return;
  }
  fprintf(stderr,"[CallbackError] No handler for Privmsg callback (uniqueid=%s)\n", uniqueid);
}

const char *getPropertyEmbed(const char *uniqueid, const char *variable)
{
  FOR_EVERY_EMBED(uniqueid,getPropertyCallback) {
    return ePerl[i]->getPropertyCallback( uniqueid, variable, ePerl[i]->data );
  }
}

void setPropertyEmbed(const char *uniqueid, const char *variable, const char *value)
{
  FOR_EVERY_EMBED(uniqueid,setPropertyCallback) {
    ePerl[i]->setPropertyCallback( uniqueid, variable, value, ePerl[i]->data );
    return;
  }
}

void unsetPropertyEmbed(const char *uniqueid, const char *variable)
{
  FOR_EVERY_EMBED(uniqueid,unsetPropertyCallback) {
    ePerl[i]->unsetPropertyCallback( uniqueid, variable, ePerl[i]->data );
    return;
  }
}

const char *getNickEmbed(const char *uniqueid)
{
  FOR_EVERY_EMBED(uniqueid,getNickCallback) {
    return ePerl[i]->getNickCallback( ePerl[i]->data );
  }
}

void sendWhoisEmbed(const char *uniqueid, const char *who)
{
  FOR_EVERY_EMBED(uniqueid,sendWhoisCallback) {
    ePerl[i]->sendWhoisCallback( who, ePerl[i]->data );
    return;
  }
}

void joinEmbed(const char *uniqueid, const char *channel)
{
  FOR_EVERY_EMBED(uniqueid,joinCallback) {
    ePerl[i]->joinCallback( channel, ePerl[i]->data );
    return;
  }
}

void partEmbed(const char *uniqueid, const char *channel)
{
  FOR_EVERY_EMBED(uniqueid,partCallback) {
    ePerl[i]->partCallback( channel, ePerl[i]->data );
    return;
  }
}

/***** END CALLBACKS ****/
}

EmbedPerl::EmbedPerl(const char *uniqueid)
: emoteCallback(0)
, privmsgCallback(0)
{
  if( MAX_EMBED_PERL == numEmbedPerl )
  {
    fprintf(stderr, "Too many EmbedPerls. Raise MAX_EMBED_PERL\n");
    abort();
  }

  for( int i = 0; i < numEmbedPerl; ++i )
  {
    if( strcmp(ePerl[i]->uniqueid(), uniqueid) != 0 )
    {
      fprintf(stderr,"EmbedPerl created for a duplicate uniqueid.\n");
      abort();
    }
  }
  uniqueid_ = (char*)malloc(strlen(uniqueid));
  strcpy(uniqueid_, uniqueid);
  ePerl[numEmbedPerl++] = this;

  perl = perl_alloc();
  perl_construct(perl);

  char *argv[] = {"", "dazeus2_api_emulate.pl"};
  perl_parse(perl, xs_init, 1, argv, NULL);

  init();
}

EmbedPerl::~EmbedPerl()
{
#ifdef DEBUG
  fprintf(stderr, "EmbedPerl destructor\n");
#endif
  perl_destruct(perl);<commit_msg>Fix: Abort() if two uniqueids are the same, instead of different.<commit_after>/**
 * Copyright (c) <NAME>, 2010
 * See LICENSE for license.
 */

#include "perl_callbacks.h"
#include "embedperl.h"
#include <assert.h>

#define MAX_EMBED_PERL 10
// #define DEBUG

// Totally thread-unsafe...
static EmbedPerl *ePerl[MAX_EMBED_PERL];
static int numEmbedPerl = 0;

extern "C" {
void xs_init(pTHX);

/***** CALLBACKS *****/

#define FOR_EVERY_EMBED(UNIQUEID,CALLBACK) \
  for( int i = 0; i < numEmbedPerl; ++i ) \
     if( strcmp(ePerl[i]->uniqueid(),UNIQUEID)==0 && ePerl[i]->CALLBACK )

void emoteEmbed(const char *uniqueid, const char *receiver, const char *message)
{
#ifdef DEBUG
  printf("***Callback*** Send emote to %s (on %s): %s\n", receiver, uniqueid, message);
#endif
  FOR_EVERY_EMBED(uniqueid,emoteCallback) {
    ePerl[i]->emoteCallback( uniqueid, receiver, message, ePerl[i]->data );
    return;
  }
  fprintf(stderr,"[CallbackError] No handler for Emote callback (uniqueid=%s)\n", uniqueid);
}

void privmsgEmbed(const char *uniqueid, const char *receiver, const char *message)
{
#ifdef DEBUG
  printf("***Callback*** Send privmsg to %s (on %s): %s\n", receiver, uniqueid, message);
#endif
  FOR_EVERY_EMBED(uniqueid,privmsgCallback) {
    ePerl[i]->privmsgCallback( uniqueid, receiver, message, ePerl[i]->data );
    return;
  }
  fprintf(stderr,"[CallbackError] No handler for Privmsg callback (uniqueid=%s)\n", uniqueid);
}

const char *getPropertyEmbed(const char *uniqueid, const char *variable)
{
  FOR_EVERY_EMBED(uniqueid,getPropertyCallback) {
    return ePerl[i]->getPropertyCallback( uniqueid, variable, ePerl[i]->data );
  }
}

void setPropertyEmbed(const char *uniqueid, const char *variable, const char *value)
{
  FOR_EVERY_EMBED(uniqueid,setPropertyCallback) {
    ePerl[i]->setPropertyCallback( uniqueid, variable, value, ePerl[i]->data );
    return;
  }
}

void unsetPropertyEmbed(const char *uniqueid, const char *variable)
{
  FOR_EVERY_EMBED(uniqueid,unsetPropertyCallback) {
    ePerl[i]->unsetPropertyCallback( uniqueid, variable, ePerl[i]->data );
    return;
  }
}

const char *getNickEmbed(const char *uniqueid)
{
  FOR_EVERY_EMBED(uniqueid,getNickCallback) {
    return ePerl[i]->getNickCallback( ePerl[i]->data );
  }
}

void sendWhoisEmbed(const char *uniqueid, const char *who)
{
  FOR_EVERY_EMBED(uniqueid,sendWhoisCallback) {
    ePerl[i]->sendWhoisCallback( who, ePerl[i]->data );
    return;
  }
}

void joinEmbed(const char *uniqueid, const char *channel)
{
  FOR_EVERY_EMBED(uniqueid,joinCallback) {
    ePerl[i]->joinCallback( channel, ePerl[i]->data );
    return;
  }
}

void partEmbed(const char *uniqueid, const char *channel)
{
  FOR_EVERY_EMBED(uniqueid,partCallback) {
    ePerl[i]->partCallback( channel, ePerl[i]->data );
    return;
  }
}

/***** END CALLBACKS ****/
}

EmbedPerl::EmbedPerl(const char *uniqueid)
: emoteCallback(0)
, privmsgCallback(0)
{
  if( MAX_EMBED_PERL == numEmbedPerl )
  {
    fprintf(stderr, "Too many EmbedPerls. Raise MAX_EMBED_PERL\n");
    abort();
  }

  for( int i = 0; i < numEmbedPerl; ++i )
  {
    if( strcmp(ePerl[i]->uniqueid(), uniqueid) == 0 )
    {
      fprintf(stderr,"EmbedPerl created for a duplicate uniqueid.\n");
      abort();
    }
  }
  uniqueid_ = (char*)malloc(strlen(uniqueid));
  strcpy(uniqueid_, uniqueid);
  ePerl[numEmbedPerl++] = this;

  perl = perl_alloc();
  perl_construct(perl);

  char *argv[] = {"", "dazeus2_api_emulate.pl"};
  perl_parse(perl, xs_init, 1, argv, NULL);

  init();
}

EmbedPerl::~EmbedPerl()
{
#ifdef DEBUG
  fprintf(stderr, "EmbedPerl destructor\n");
#endif
  perl_destruct(perl);
{'fraction_non_alphanumeric': 0.15351486524034455, 'fraction_numerical': 0.004167824395665463, 'mean_word_length': 4.796296296296297, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4302370', 'n_tokens_mistral': 2774, 'n_tokens_neox': 2681, 'n_words': 557}