<commit_before>/***************************************************************************//**

  @file         cbot_irc.c

  @author       <NAME>

  @date         Created Thursday, 23 July 2015

  @brief        CBot implementation for IRC!

  @copyright    Copyright (c) 2015, <NAME>.  Released under the Revised
                BSD License.  See LICENSE.txt for details.

*******************************************************************************/

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

#ifdef LIBIRCCLIENT_LOCAL
#include "libircclient.h"
#include "libirc_rfcnumeric.h"
#else
#include <libircclient/libircclient.h>
#include <libircclient/libirc_rfcnumeric.h>
#endif
#include "libstephen/ll.h"
#include "libstephen/ad.h"
#include "libstephen/cb.h"

#include "cbot_handlers.h"
#include "cbot_private.h"
#include "cbot_irc.h"

char *chan = "#cbot";

void log_event(irc_session_t *session, const char *event,
               const char *origin, const char **params, unsigned int count)
{
  size_t i;
  printf("Event \"%s\", origin: \"%s\", params: %d [", event, origin, count);
  for (i = 0; i < count; i++) {
    if (i != 0) {
      fputc('|', stdout);
    }
    printf("%s", params[i]);
  }
  printf("]\n");
}

void event_numeric(irc_session_t *session, unsigned int event,
                   const char *origin, const char **params, unsigned int count)
{
  char buf[24];
  sprintf(buf, "%d", event);
  log_event(session, buf, origin, params, count);
}

/*
  Run once we are connected to the server.
 */
void event_connect(irc_session_t *session, const char *event,
                   const char *origin, const char **params, unsigned int count)
{
  irc_cmd_join(session, chan, NULL);
  log_event(session, event, origin, params, count);
}

void cbot_irc_send(const cbot_t *cbot, const char *dest, const char *format, ...)
{
  va_list va;
  cbuf cb;
  irc_session_t *session = cbot->backend;
  va_start(va, format);
  cb_init(&cb, 1024);
  cb_vprintf(&cb, format, va);
  irc_cmd_msg(session, dest, cb.buf);
  cb_destroy(&cb);
  va_end(va);
}

void cbot_irc_me(const cbot_t *cbot, const char *dest, const char *format, ...)
{
  va_list va;
  cbuf cb;
  irc_session_t *session = cbot->backend;
  va_start(va, format);
  cb_init(&cb, 1024);
  cb_vprintf(&cb, format, va);
  irc_cmd_me(session, dest, cb.buf);
  cb_destroy(&cb);
  va_end(va);
}

void cbot_irc_op(const cbot_t *cbot, const char *channel, const char *person)
{
  irc_session_t *session = cbot->backend;
  cbuf cb;
  cb_init(&cb, 256);
  cb_printf(&cb, "+o %s", person);
  irc_cmd_channel_mode(session, channel, cb.buf);
  cb_destroy(&cb);
}

void cbot_irc_join(const cbot_t *cbot, const char *channel, const char *password)
{
  irc_session_t *session = cbot->backend;
  irc_cmd_join(session, channel, password);
}

void event_channel(irc_session_t *session, const char *event,
                   const char *origin, const char **params, unsigned int count)
{
  log_event(session, event, origin, params, count);
  if (count >= 2 && params[1] != NULL) {
    cbot_handle_channel_message(irc_get_ctx(session), params[0], origin, params[1]);
    printf("Event handled by CBot.\n");
  }
}

void event_action(irc_session_t *session, const char *event,
                  const char *origin, const char **params, unsigned int count)
{
  log_event(session, event, origin, params, count);
  cbot_t *bot = irc_get_ctx(session);
  cbot_event_t ircevent = {
    .bot = bot,
    .type = CBOT_CHANNEL_ACTION,
    .channel = params[0],
    .username = origin,
    .message = params[1],
  };
  cbot_handle_event(bot, ircevent);
  printf("Event handled by CBot.\n");
}

void event_join(irc_session_t *session, const char *event,
                const char *origin, const char **params, unsigned int count)
{
  log_event(session, event, origin, params, count);
  cbot_t *bot = irc_get_ctx(session);
  cbot_event_t ircevent = {
    .bot = bot,
    .type = CBOT_JOIN,
    .channel = params[0],
    .username = origin,
    .message = NULL,
  };
  cbot_handle_event(bot, ircevent);
  printf("Event handled by CBot.\n");
}

void event_part(irc_session_t *session, const char *event,
                const char *origin, const char **params, unsigned int count)
{
  log_event(session, event, origin, params, count);
  cbot_t *bot = irc_get_ctx(session);
  cbot_event_t ircevent = {
    .bot = bot,
    .type = CBOT_PART,
    .channel = params[0],
    .username = origin,
    .message = NULL,
  };
  cbot_handle_event(bot, ircevent);
  printf("Event handled by CBot.\n");
}

void help()
{
  puts("usage: cbot irc [options] plugins");
  puts("options:");
  puts("  --hash HASH        set the hash chain tip (required)");
  puts("  --name [name]      set the bot's name");
  puts("  --host [host]      set the irc server hostname");
  puts("  --port [port]      set the irc port number");
  puts("  --chan [chan]      set the irc channel");
  puts("  --plugin-dir [dir] set the plugin directory");
  puts("  --help             show this help and exit");
  puts("plugins:");
  puts("  list of names of plugins within plugin-dir (don't include \".so\").");
  exit(EXIT_FAILURE);
}

void run_cbot_irc(int argc, char *argv[])
{
  irc_callbacks_t callbacks;
  irc_session_t *session;
  cbot_t *cbot;
  smb_ad args;
  char *name = "cbot";
  char *host = "irc.case.edu";
  char *port = "6667";
  char *plugin_dir = "bin/release/plugin";<commit_msg>Update help<commit_after>/***************************************************************************//**

  @file         cbot_irc.c

  @author       <NAME>

  @date         Created Thursday, 23 July 2015

  @brief        CBot implementation for IRC!

  @copyright    Copyright (c) 2015, <NAME>.  Released under the Revised
                BSD License.  See LICENSE.txt for details.

*******************************************************************************/

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

#ifdef LIBIRCCLIENT_LOCAL
#include "libircclient.h"
#include "libirc_rfcnumeric.h"
#else
#include <libircclient/libircclient.h>
#include <libircclient/libirc_rfcnumeric.h>
#endif
#include "libstephen/ll.h"
#include "libstephen/ad.h"
#include "libstephen/cb.h"

#include "cbot_handlers.h"
#include "cbot_private.h"
#include "cbot_irc.h"

char *chan = "#cbot";

void log_event(irc_session_t *session, const char *event,
               const char *origin, const char **params, unsigned int count)
{
  size_t i;
  printf("Event \"%s\", origin: \"%s\", params: %d [", event, origin, count);
  for (i = 0; i < count; i++) {
    if (i != 0) {
      fputc('|', stdout);
    }
    printf("%s", params[i]);
  }
  printf("]\n");
}

void event_numeric(irc_session_t *session, unsigned int event,
                   const char *origin, const char **params, unsigned int count)
{
  char buf[24];
  sprintf(buf, "%d", event);
  log_event(session, buf, origin, params, count);
}

/*
  Run once we are connected to the server.
 */
void event_connect(irc_session_t *session, const char *event,
                   const char *origin, const char **params, unsigned int count)
{
  irc_cmd_join(session, chan, NULL);
  log_event(session, event, origin, params, count);
}

void cbot_irc_send(const cbot_t *cbot, const char *dest, const char *format, ...)
{
  va_list va;
  cbuf cb;
  irc_session_t *session = cbot->backend;
  va_start(va, format);
  cb_init(&cb, 1024);
  cb_vprintf(&cb, format, va);
  irc_cmd_msg(session, dest, cb.buf);
  cb_destroy(&cb);
  va_end(va);
}

void cbot_irc_me(const cbot_t *cbot, const char *dest, const char *format, ...)
{
  va_list va;
  cbuf cb;
  irc_session_t *session = cbot->backend;
  va_start(va, format);
  cb_init(&cb, 1024);
  cb_vprintf(&cb, format, va);
  irc_cmd_me(session, dest, cb.buf);
  cb_destroy(&cb);
  va_end(va);
}

void cbot_irc_op(const cbot_t *cbot, const char *channel, const char *person)
{
  irc_session_t *session = cbot->backend;
  cbuf cb;
  cb_init(&cb, 256);
  cb_printf(&cb, "+o %s", person);
  irc_cmd_channel_mode(session, channel, cb.buf);
  cb_destroy(&cb);
}

void cbot_irc_join(const cbot_t *cbot, const char *channel, const char *password)
{
  irc_session_t *session = cbot->backend;
  irc_cmd_join(session, channel, password);
}

void event_channel(irc_session_t *session, const char *event,
                   const char *origin, const char **params, unsigned int count)
{
  log_event(session, event, origin, params, count);
  if (count >= 2 && params[1] != NULL) {
    cbot_handle_channel_message(irc_get_ctx(session), params[0], origin, params[1]);
    printf("Event handled by CBot.\n");
  }
}

void event_action(irc_session_t *session, const char *event,
                  const char *origin, const char **params, unsigned int count)
{
  log_event(session, event, origin, params, count);
  cbot_t *bot = irc_get_ctx(session);
  cbot_event_t ircevent = {
    .bot = bot,
    .type = CBOT_CHANNEL_ACTION,
    .channel = params[0],
    .username = origin,
    .message = params[1],
  };
  cbot_handle_event(bot, ircevent);
  printf("Event handled by CBot.\n");
}

void event_join(irc_session_t *session, const char *event,
                const char *origin, const char **params, unsigned int count)
{
  log_event(session, event, origin, params, count);
  cbot_t *bot = irc_get_ctx(session);
  cbot_event_t ircevent = {
    .bot = bot,
    .type = CBOT_JOIN,
    .channel = params[0],
    .username = origin,
    .message = NULL,
  };
  cbot_handle_event(bot, ircevent);
  printf("Event handled by CBot.\n");
}

void event_part(irc_session_t *session, const char *event,
                const char *origin, const char **params, unsigned int count)
{
  log_event(session, event, origin, params, count);
  cbot_t *bot = irc_get_ctx(session);
  cbot_event_t ircevent = {
    .bot = bot,
    .type = CBOT_PART,
    .channel = params[0],
    .username = origin,
    .message = NULL,
  };
  cbot_handle_event(bot, ircevent);
  printf("Event handled by CBot.\n");
}

void help()
{
  puts("usage: cbot irc [options] plugins");
  puts("required flags:");
  puts("  --hash HASH        set the hash chain tip (required)");
  puts("  --plugin-dir [dir] set the plugin directory");
  puts("  --name [name]      set the bot's name");
  puts("optional flags:");
  puts("  --host [host]      set the irc server hostname");
  puts("  --password [pass]  set the irc server password");
  puts("  --port [port]      set the irc port number");
  puts("  --chan [chan]      set the irc channel");
  puts("  --help             show this help and exit");
  puts("plugins:");
  puts("  list of names of plugins within plugin-dir (don't include \".so\").");
  exit(EXIT_FAILURE);
}

void run_cbot_irc(int argc, char *argv[])
{
  irc_callbacks_t callbacks;
  irc_session_t *session;
  cbot_t *cbot;
  smb_ad args;
  char *name = "cbot";
  char *host = "irc.case.edu";
  char *port = "6667";
  char *plugin_dir = "bin/release/plugin";
{'fraction_non_alphanumeric': 0.1679172767203514, 'fraction_numerical': 0.006771595900439239, 'mean_word_length': 3.5708908406524467, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 29, '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': '3165657', 'n_tokens_mistral': 3957, 'n_tokens_neox': 3812, 'n_words': 1043}