Exemplo n.º 1
0
static void
_ipmipower_setup (void)
{
  int i;
  struct rlimit rlim;

  /* Make best effort to increase file descriptor limit, if it fails
   * for any reason, don't worry about it, its no big deal.
   */

  if (getrlimit (RLIMIT_NOFILE, &rlim) == 0)
    {
      rlim.rlim_cur = rlim.rlim_max;
      setrlimit (RLIMIT_NOFILE,&rlim);
    }

  if (ipmi_rmcpplus_init () < 0)
    {
      if (errno == EPERM)
        IPMIPOWER_ERROR (("ipmi_rmcpplus_init: incompatible crypto library"));
      else
        IPMIPOWER_ERROR (("ipmi_rmcpplus_init: %s", strerror (errno)));
      exit (EXIT_FAILURE);
    }

  /* Create TTY bufs */
  if (!(ttyin  = cbuf_create (IPMIPOWER_MIN_TTY_BUF, IPMIPOWER_MAX_TTY_BUF)))
    {
      IPMIPOWER_ERROR (("cbuf_create: %s", strerror (errno)));
      exit (EXIT_FAILURE);
    }
  cbuf_opt_set (ttyin, CBUF_OPT_OVERWRITE, CBUF_WRAP_MANY);

  if (!(ttyout = cbuf_create (IPMIPOWER_MIN_TTY_BUF, IPMIPOWER_MAX_TTY_BUF)))
    {
      IPMIPOWER_ERROR (("cbuf_create: %s", strerror (errno)));
      exit (EXIT_FAILURE);
    }
  cbuf_opt_set (ttyout, CBUF_OPT_OVERWRITE, CBUF_WRAP_MANY);

  for (i = 0; i < IPMIPOWER_MSG_TYPE_NUM_ENTRIES; i++)
    {
      if (!(output_hostrange[i] = hostlist_create (NULL)))
        {
          IPMIPOWER_ERROR (("hostlist_create: %s", strerror (errno)));
          exit (EXIT_FAILURE);
        }
    }

  memset (output_counts, '\0', sizeof (output_counts));
}
Exemplo n.º 2
0
static int command_patternadd(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct patterns_data* pdata = (struct patterns_data*) plugin->ptr;
	struct cbuffer* buf = cbuf_create(128);
	struct plugin_command_arg_data* arg1 = (struct plugin_command_arg_data*) list_get_first(cmd->args);
	struct plugin_command_arg_data* arg2 = (struct plugin_command_arg_data*) list_get_next(cmd->args);
	struct plugin_command_arg_data* arg3 = (struct plugin_command_arg_data*) list_get_next(cmd->args);
	struct plugin_command_arg_data* arg4 = (struct plugin_command_arg_data*) list_get_next(cmd->args);
  
	char* t = arg1->data.string;
	enum pattern_types type;
	
	if (!pattern_string_to_type(t, &type))
	{
		cbuf_append_format(buf, "*** %s: Wrong pattern type \"%s\". Available types are: MC, PM, NI, UA.", cmd->prefix, t);
	}
	else
	{
		enum auth_credentials mincred = arg2->data.credentials;
		enum auth_credentials maxcred = arg3->data.credentials;
		char* str = arg4->data.string;
	  
		int rc = sql_execute(pdata, null_callback, NULL, "INSERT INTO patterns VALUES(NULL, '%s', %d, '%s', '%s');", sql_escape_string(str), type, auth_cred_to_string(mincred), auth_cred_to_string(maxcred));
	  
		if (rc > 0)
			cbuf_append_format(buf, "*** %s: Added pattern \"%s\" to %s group.", cmd->prefix, str, pattern_type_to_string(type));
		else
			cbuf_append_format(buf, "*** %s: Unable to add pattern \"%s\".", cmd->prefix, str);
	}
	
	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);

	return 0;
}
Exemplo n.º 3
0
static int command_patternexlist(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct patterns_data* pdata = (struct patterns_data*) plugin->ptr;
	struct cbuffer* buf = cbuf_create(128);
	sqlite3_stmt *res;
	int error = 0;
	const char *tail;
	char *query = "SELECT * FROM pattern_exceptions;";

	cbuf_append_format(buf, "*** %s:\n", cmd->prefix);

	error = sqlite3_prepare_v2(pdata->db, query, strlen(query), &res, &tail);

	while (sqlite3_step(res) == SQLITE_ROW)
	{
		cbuf_append_format(buf, "ID: %d    Pattern ID: %d    Exception pattern: \"%s\"    Exempt credentials: %s-%s\n", sqlite3_column_int(res, 0), sqlite3_column_int(res, 2), (char*) sqlite3_column_text(res, 1), (char*) sqlite3_column_text(res, 3), (char*) sqlite3_column_text(res, 4));
	}

	sqlite3_finalize(res);

	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);

	return 0;
}
Exemplo n.º 4
0
static int command_releases(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct extras_data* extrasdata = (struct extras_data*) plugin->ptr;
	struct cbuffer* buf = cbuf_create(128);
	sqlite3_stmt *res;
	int error = 0;
	int rec_count = 0;
	const char *tail;
	char *query = "SELECT * FROM releases;";

	cbuf_append_format(buf, "*** %s:", cmd->prefix);
	
	error = sqlite3_prepare_v2(extrasdata->db, query, strlen(query), &res, &tail);
	
	while (sqlite3_step(res) == SQLITE_ROW)
	{
		cbuf_append_format(buf, "\nID: %d\nTitle: %s\nMagnet link: magnet:?xt=urn:tree:tiger:%s\nPublished: %s", sqlite3_column_int(res, 0), (char*) sqlite3_column_text(res, 1), (char*) sqlite3_column_text(res, 2), (char*) sqlite3_column_text(res, 3));
		rec_count++;
	}

	if (error != SQLITE_OK || rec_count < 1)
	{
		cbuf_append(buf, " No releases found.");
	}

	sqlite3_finalize(res);
	
	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);
	  
	return 0;
}
Exemplo n.º 5
0
static int command_hublist(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct extras_data* extrasdata = (struct extras_data*) plugin->ptr;
	struct cbuffer* buf = cbuf_create(128);
	sqlite3_stmt *res;
	int error = 0;
	int rec_count = 0;
	const char *tail;
	char *query = "SELECT * FROM hubs;";

	cbuf_append_format(buf, "*** %s:", cmd->prefix);
	
	error = sqlite3_prepare_v2(extrasdata->db, query, strlen(query), &res, &tail);
	
	while (sqlite3_step(res) == SQLITE_ROW)
	{
		cbuf_append_format(buf, "\nID: %d, Address: %s , Name: \"%s\"\n", sqlite3_column_int(res, 0), (char*) sqlite3_column_text(res, 1), (char*) sqlite3_column_text(res, 2));
		rec_count++;
	}

	if (error != SQLITE_OK || rec_count < 1)
	{
		cbuf_append(buf, " No hubs found in hublist.");
	}

	sqlite3_finalize(res);
	
	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);
	  
	return 0;
}
Exemplo n.º 6
0
void user_login(struct plugin_handle* plugin, struct plugin_user* user)
{
	struct chat_history_data* data = (struct chat_history_data*) plugin->ptr;
	struct cbuffer* buf = NULL;
	struct linked_list* found = (struct linked_list*) list_create();
	
	sql_execute(data, get_messages_callback, found, "SELECT from_nick,message, datetime(time, 'localtime') as time FROM chat_history ORDER BY time DESC LIMIT 0,%d;", (int) data->history_connect);

	if (data->history_connect > 0 && list_size(found) > 0)
	{
		buf = cbuf_create(MAX_HISTORY_SIZE);
		cbuf_append(buf, "Chat history:\n\n");
		struct chat_history_line* history_line;
		history_line = (struct chat_history_line*) list_get_last(found);
		while (history_line)
		{
			cbuf_append_format(buf, "[%s] <%s> %s\n", history_line->time, history_line->from, history_line->message);
			list_remove(found, history_line);
			hub_free(history_line);
			history_line = (struct chat_history_line*) list_get_last(found);
		}
		plugin->hub.send_message(plugin, user, cbuf_get(buf));
		cbuf_destroy(buf);
	}
	list_clear(found, &hub_free);
	list_destroy(found);
}
Exemplo n.º 7
0
static int command_patternexadd(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct patterns_data* pdata = (struct patterns_data*) plugin->ptr;
	struct cbuffer* buf = cbuf_create(128);
	struct plugin_command_arg_data* arg1 = (struct plugin_command_arg_data*) list_get_first(cmd->args);
	struct plugin_command_arg_data* arg2 = (struct plugin_command_arg_data*) list_get_next(cmd->args);
	struct plugin_command_arg_data* arg3 = (struct plugin_command_arg_data*) list_get_next(cmd->args);
	struct plugin_command_arg_data* arg4 = (struct plugin_command_arg_data*) list_get_next(cmd->args);
  
	int pattern_id = arg1->data.integer;
	enum auth_credentials mincred = arg2->data.credentials;
	enum auth_credentials maxcred = arg3->data.credentials;
	char* str = arg4->data.string;
  
	int rc = sql_execute(pdata, null_callback, NULL, "PRAGMA foreign_keys=ON; INSERT INTO pattern_exceptions VALUES(NULL, '%s', %d, '%s', '%s');", sql_escape_string(str), pattern_id, auth_cred_to_string(mincred), auth_cred_to_string(maxcred));
  
	if (rc > 0)
		cbuf_append_format(buf, "*** %s: Added pattern exception \"%s\" to pattern ID %d.", cmd->prefix, str, pattern_id);
	else
		cbuf_append_format(buf, "*** %s: Unable to add pattern exception \"%s\" to pattern ID %d.", cmd->prefix, str, pattern_id);
  
	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);

	return 0;
}
Exemplo n.º 8
0
/** Create and initialise a +Client+ structure to represent a single client.
 *
 * \param client_sock the socket associated to the client transmission
 *        (from libocomm).  The client_sock should be attached to an active
 *        client that has been accepted by the server socket.
 *
 * \param page_size the page size for the underlying memory store for
 *        buffering received measurements.
 * \param file_name save measurements to a file with this name.
 * \param server_port the port of the downstream OML server
 * \param server_address the address of the downstream OML Server
 *
 * \return a new Client structure
 */
Client*
client_new (Socket* client_sock, int page_size, char* file_name,
           int server_port, char* server_address)
{
  Client* self = (Client *)oml_malloc(sizeof(Client));
  memset(self, 0, sizeof(Client));

  self->state = C_HEADER;
  self->downstream_port = server_port;
  self->downstream_addr = oml_strndup (server_address, strlen (server_address));
  self->mbuf = mbuf_create ();
  self->headers = NULL;
  self->msg_start = dummy_read_msg_start;

  self->messages = msg_queue_create ();
  self->cbuf = cbuf_create (page_size);

  self->file = fopen(file_name, "wa");
  self->file_name =  oml_strndup (file_name, strlen (file_name));

  self->recv_socket = client_sock;

  /* FIXME:  Return value checking */
  pthread_mutex_init (&self->mutex, NULL);
  pthread_cond_init (&self->condvar, NULL);

  return self;
}
Exemplo n.º 9
0
cbuf_t 
Cbuf_create(int minsize, int maxsize) 
{
  cbuf_t c;
  if (!(c = cbuf_create(minsize, maxsize)))
    ierr_exit("Cbuf_create: %s", strerror(errno));
  cbuf_opt_set(c, CBUF_OPT_OVERWRITE, CBUF_WRAP_MANY);
  return c;
}
Exemplo n.º 10
0
/**
 * Send a status message back to the user who issued the !history command.
 */
static int command_status(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd, struct cbuffer* buf)
{
	struct cbuffer* msg = cbuf_create(cbuf_size(buf) + strlen(cmd->prefix) + 8);
	cbuf_append_format(msg, "*** %s: %s", cmd->prefix, cbuf_get(buf));
	plugin->hub.send_message(plugin, user, cbuf_get(msg));
	cbuf_destroy(msg);
	cbuf_destroy(buf);
	return 0;
}
Exemplo n.º 11
0
static int command_resettopic_handler(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct cbuffer* buf = cbuf_create(128);
	plugin->hub.set_description(plugin, NULL);
	cbuf_append_format(buf, "*** %s: Topic reset.", cmd->prefix);
	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);
	return 0;
}
Exemplo n.º 12
0
static int command_showtopic_handler(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct cbuffer* buf = cbuf_create(128);
	char* topic = plugin->hub.get_description(plugin);
	cbuf_append_format(buf, "*** %s: Current topic is: \"%s\"", cmd->prefix, topic);
	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);
	hub_free(topic);
	return 0;
}
Exemplo n.º 13
0
static int command_topic_handler(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct cbuffer* buf = cbuf_create(128);
	struct plugin_command_arg_data* arg = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_string);
	char* topic = arg ? arg->data.string : "";

	plugin->hub.set_description(plugin, topic);
	cbuf_append_format(buf, "*** %s: Topic set to \"%s\"", cmd->prefix, topic);
	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);
	return 0;
}
Exemplo n.º 14
0
static int zio_init_buffer (zio_t *zio)
{
    assert (zio != NULL);
    assert (zio->magic == ZIO_MAGIC);
    assert (zio->buf == NULL);

    if (!(zio->buf = cbuf_create (64, 1638400)))
        return (-1);

    cbuf_opt_set (zio->buf, CBUF_OPT_OVERWRITE, CBUF_NO_DROP);
    return (0);
}
Exemplo n.º 15
0
static int
_pstdout_state_init(pstdout_state_t pstate, const char *hostname)
{
    int rc;

    assert(pstate);

    memset(pstate, '\0', sizeof(struct pstdout_state));
    pstate->magic = PSTDOUT_STATE_MAGIC;
    pstate->hostname = (char *)hostname;

    if (!(pstate->p_stdout = cbuf_create(PSTDOUT_STATE_CBUF_MIN, PSTDOUT_STATE_CBUF_MAX)))
    {
        if (pstdout_debug_flags & PSTDOUT_DEBUG_STANDARD)
            fprintf(stderr, "cbuf_create: %s\n", strerror(errno));
        pstdout_errnum = PSTDOUT_ERR_INTERNAL;
        return -1;
    }
    if (!(pstate->p_stderr = cbuf_create(PSTDOUT_STATE_CBUF_MIN, PSTDOUT_STATE_CBUF_MAX)))
    {
        if (pstdout_debug_flags & PSTDOUT_DEBUG_STANDARD)
            fprintf(stderr, "cbuf_create: %s\n", strerror(errno));
        pstdout_errnum = PSTDOUT_ERR_INTERNAL;
        return -1;
    }
    pstate->buffer_stdout = NULL;
    pstate->buffer_stdout_len = 0;
    pstate->buffer_stderr = NULL;
    pstate->buffer_stderr_len = 0;
    pstate->no_more_external_output = 0;

    if ((rc = pthread_mutex_init(&(pstate->mutex), NULL)))
    {
        if (pstdout_debug_flags & PSTDOUT_DEBUG_STANDARD)
            fprintf(stderr, "pthread_mutex_lock: %s\n", strerror(rc));
        pstdout_errnum = PSTDOUT_ERR_INTERNAL;
        return -1;
    }
    return 0;
}
Exemplo n.º 16
0
void user_login(struct plugin_handle* plugin, struct plugin_user* user)
{
	struct chat_history_data* data = (struct chat_history_data*) plugin->ptr;
	struct cbuffer* buf = NULL;
	// size_t messages = 0;

	if (data->history_connect > 0 && list_size(data->chat_history) > 0)
	{
		buf = cbuf_create(MAX_HISTORY_SIZE);
		cbuf_append(buf, "Chat history:\n");
		get_messages(data, data->history_connect, buf);
		plugin->hub.send_message(plugin, user, cbuf_get(buf));
		cbuf_destroy(buf);
	}
}
Exemplo n.º 17
0
/**
 * The callback function for handling the !history command.
 */
static int command_history(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct chat_history_data* data = (struct chat_history_data*) plugin->ptr;
	struct cbuffer* buf = cbuf_create(MAX_HISTORY_SIZE);
	struct linked_list* found = (struct linked_list*) list_create();
	struct plugin_command_arg_data* arg = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_integer);
	int maxlines;

	if (arg)
		maxlines = arg->data.integer;
	else
		maxlines = data->history_default;

	sql_execute(data, get_messages_callback, found, "SELECT from_nick,message, datetime(time, 'localtime') as time FROM chat_history ORDER BY time DESC LIMIT 0,%d;", maxlines);

	size_t linecount = list_size(found);

	if (linecount > 0)
	{
		cbuf_append_format(buf, "*** %s: Chat History:\n\n", cmd->prefix);
		struct chat_history_line* history_line;
		history_line = (struct chat_history_line*) list_get_last(found);
		while (history_line)
		{
			cbuf_append_format(buf, "[%s] <%s> %s\n", history_line->time, history_line->from, history_line->message);
			list_remove(found, history_line);
			hub_free(history_line);
			history_line = (struct chat_history_line*) list_get_last(found);
		}
	}
	else
	{
		cbuf_append_format(buf, "*** %s: No messages found.", cmd->prefix);
	}

	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);
	list_clear(found, &hub_free);
	list_destroy(found);

	return 0;
}
Exemplo n.º 18
0
static int command_historycleanup(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct chat_history_data* data = (struct chat_history_data*) plugin->ptr;
	struct cbuffer* buf = cbuf_create(128);
	int rc = 0;

	rc = sql_execute(data, null_callback, NULL, "DELETE FROM chat_history;");
 
	if (!rc)
		cbuf_append_format(buf, "*** %s: Unable to clean chat history table.", cmd->prefix);
	else
		cbuf_append_format(buf, "*** %s: Cleaned chat history table.", cmd->prefix);

	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);

	sql_execute(data, null_callback, NULL, "VACUUM;");

	return 0;
}
Exemplo n.º 19
0
static int command_newsadd(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct extras_data* extrasdata = (struct extras_data*) plugin->ptr;
	struct cbuffer* buf = cbuf_create(128);
	struct plugin_command_arg_data* arg1 = (struct plugin_command_arg_data*) list_get_first(cmd->args);
  
	const char* news_text = sql_escape_string(arg1->data.string);
  
	int rc = sql_execute(extrasdata, null_callback, NULL, "INSERT INTO news (id, text) VALUES(NULL, '%s');", news_text);
  
	if (rc > 0)
		cbuf_append_format(buf, "*** %s: News updated.", cmd->prefix);
	else
		cbuf_append_format(buf, "*** %s: Unable to update news.", cmd->prefix);
  
	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);

	return 0;
}
Exemplo n.º 20
0
static int command_patternexdel(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct patterns_data* pdata = (struct patterns_data*) plugin->ptr;
	struct cbuffer* buf = cbuf_create(128);
	struct plugin_command_arg_data* args = (struct plugin_command_arg_data*) list_get_first(cmd->args);
	
	int id = args->data.integer;

	int rc = sql_execute(pdata, null_callback, NULL, "DELETE FROM pattern_exceptions WHERE id=%d;", id);
  
	if (rc > 0)
		cbuf_append_format(buf, "*** %s: Deleted pattern exception with ID %d.", cmd->prefix, id);
	else
		cbuf_append_format(buf, "*** %s: Unable to delete pattern exception with id %d.", cmd->prefix, id);
  
	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);

	return 0;
}
Exemplo n.º 21
0
static int command_patterntest(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct patterns_data* pdata = (struct patterns_data*) plugin->ptr;
	struct cbuffer* buf = cbuf_create(128);
	struct plugin_command_arg_data* arg1 = (struct plugin_command_arg_data*) list_get_first(cmd->args);
	struct plugin_command_arg_data* arg2 = (struct plugin_command_arg_data*) list_get_next(cmd->args);
	
	int id = arg1->data.integer;
	char* str = arg2->data.string;
	
	sqlite3_stmt *res;
	int error = 0;
	const char *tail;
	char query[80];

	cbuf_append_format(buf, "*** %s: ", cmd->prefix);

	int n = sprintf(query, "SELECT regexp FROM patterns WHERE id=%d LIMIT 1;", id);

	error = sqlite3_prepare_v2(pdata->db, query, n, &res, &tail);

	if (sqlite3_step(res) == SQLITE_ROW)
	{
		if(pattern_match(str, (char*) sqlite3_column_text(res, 0)))
			cbuf_append_format(buf, "Tested string \"%s\" matches pattern \"%s\".", str, sqlite3_column_text(res, 0));
		else
			cbuf_append_format(buf, "Tested string \"%s\" does not match pattern \"%s\".", str, sqlite3_column_text(res, 0));
	}
	else
	{
		cbuf_append_format(buf, "Pattern ID \"%d\" not found.", id);
	}

	sqlite3_finalize(res);

	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);

	return 0;
}
Exemplo n.º 22
0
static int command_userlogcleanup(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct log_data* ldata = (struct log_data*) plugin->ptr;
	struct cbuffer* buf = cbuf_create(128);
	struct plugin_command_arg_data* arg = (struct plugin_command_arg_data*) list_get_first(cmd->args);
	
	int days = arg->data.integer;

	int rc = sql_execute(ldata, null_callback, NULL, "DELETE FROM userlog WHERE time < DATETIME('NOW', 'localtime', '-%d days');", days);
	
	if (rc > 0)
		cbuf_append_format(buf, "*** %s: Cleaned log entries older than %d days.", cmd->prefix, days);
	else
		cbuf_append_format(buf, "*** %s: Unable to clean log table.", cmd->prefix);
  
	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);

	sql_execute(ldata, null_callback, NULL, "VACUUM;");

	return 0;
}
Exemplo n.º 23
0
/**
 * The callback function for handling the !history command.
 */
static int command_history(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct cbuffer* buf;
	struct chat_history_data* data = (struct chat_history_data*) plugin->ptr;
	struct plugin_command_arg_data* arg = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_integer);
	int maxlines;

	if (!list_size(data->chat_history))
		return command_status(plugin, user, cmd, cbuf_create_const("No messages."));

	if (arg)
		maxlines = arg->data.integer;
	else
		maxlines = data->history_default;
	
	buf = cbuf_create(MAX_HISTORY_SIZE);
	cbuf_append_format(buf, "*** %s: Chat History:\n", cmd->prefix);
	get_messages(data, maxlines, buf);

	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);
	return 0;
}
Exemplo n.º 24
0
static int command_releaseadd(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct extras_data* extrasdata = (struct extras_data*) plugin->ptr;
	struct cbuffer* buf = cbuf_create(128);
	struct plugin_command_arg_data* arg1 = (struct plugin_command_arg_data*) list_get_first(cmd->args);
	struct plugin_command_arg_data* arg2 = (struct plugin_command_arg_data*) list_get_next(cmd->args);

	char* tth = strdup(sql_escape_string(arg1->data.string));
	char* title = strdup(sql_escape_string(arg2->data.string));
  
	int rc = sql_execute(extrasdata, null_callback, NULL, "INSERT INTO releases (id, title, tth) VALUES(NULL, '%s', '%s');", title, tth);
  
	if (rc > 0)
		cbuf_append_format(buf, "*** %s: Added \"%s\" to releases.", cmd->prefix, title);
	else
		cbuf_append_format(buf, "*** %s: Unable to add \"%s\" to releases.", cmd->prefix, title);
  
	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);
	hub_free(tth);
	hub_free(title);

	return 0;
}
Exemplo n.º 25
0
/*
 * Initialize scheduler log with
 * prog = program name to tag error messages with
 * opt  = log_options_t specifying max log levels for syslog, stderr, and file
 * fac  = log facility for syslog (unused if syslog level == LOG_QUIET)
 * logfile = logfile name if logfile level > LOG_QUIET
 */
static int
_sched_log_init(char *prog, log_options_t opt, log_facility_t fac, 
		char *logfile)
{
	int rc = 0;

	if (!sched_log) {
		sched_log = (log_t *)xmalloc(sizeof(log_t));
		atfork_install_handlers();
	}

	if (prog) {
		xfree(sched_log->argv0);
		sched_log->argv0 = xstrdup(xbasename(prog));
	} else if (!sched_log->argv0) {
		const char *short_name;
		short_name = strrchr((const char *) default_name, '/');
		if (short_name) 
			short_name++;
		else
			short_name = default_name;
		sched_log->argv0 = xstrdup(short_name);
	}

	if (!sched_log->fpfx)
		sched_log->fpfx = xstrdup("");

	sched_log->opt = opt;

	if (sched_log->buf) {
		cbuf_destroy(sched_log->buf);
		sched_log->buf = NULL;
	}
	if (sched_log->fbuf) {
		cbuf_destroy(sched_log->fbuf);
		sched_log->fbuf = NULL;
	}

	if (sched_log->opt.buffered) {
		sched_log->buf  = cbuf_create(128, 8192);
		sched_log->fbuf = cbuf_create(128, 8192);
	}

	if (sched_log->opt.syslog_level > LOG_LEVEL_QUIET)
		sched_log->facility = fac;

	if (logfile) {
		FILE *fp;

		fp = safeopen(logfile, "a", SAFEOPEN_LINK_OK);

		if (!fp) {
			rc = errno;
			goto out;
		}

		if (sched_log->logfp)
			fclose(sched_log->logfp); /* Ignore errors */

		sched_log->logfp = fp;
	}

	if (sched_log->logfp) {
		int fd;
		if ((fd = fileno(sched_log->logfp)) < 0)
			sched_log->logfp = NULL;
		else
			fd_set_close_on_exec(fd);
	}

	sched_log->initialized = 1;
 out:
	return rc;
}
Exemplo n.º 26
0
/*
 * Initialize log with
 * prog = program name to tag error messages with
 * opt  = log_options_t specifying max log levels for syslog, stderr, and file
 * fac  = log facility for syslog (unused if syslog level == LOG_QUIET)
 * logfile =
 *        logfile name if logfile level > LOG_QUIET
 */
static int
_log_init(char *prog, log_options_t opt, log_facility_t fac, char *logfile )
{
	int rc = 0;

	if (!log)  {
		log = (log_t *)xmalloc(sizeof(log_t));
		log->logfp = NULL;
		log->argv0 = NULL;
		log->buf   = NULL;
		log->fbuf  = NULL;
		log->fpfx  = NULL;
		atfork_install_handlers();
	}

	if (prog) {
		if (log->argv0)
			xfree(log->argv0);
		log->argv0 = xstrdup(xbasename(prog));
	} else if (!log->argv0) {
		const char *short_name = strrchr(default_name, '/');
		if (short_name)
			short_name++;
		else
			short_name = default_name;
		log->argv0 = xstrdup(short_name);
	}

	if (!log->fpfx)
		log->fpfx = xstrdup("");

	log->opt = opt;

	if (log->buf) {
		cbuf_destroy(log->buf);
		log->buf = NULL;
	}
	if (log->fbuf) {
		cbuf_destroy(log->fbuf);
		log->fbuf = NULL;
	}

	if (log->opt.buffered) {
		log->buf  = cbuf_create(128, 8192);
		log->fbuf = cbuf_create(128, 8192);
	}

	if (log->opt.syslog_level > LOG_LEVEL_QUIET)
		log->facility = fac;

	if (logfile && (log->opt.logfile_level > LOG_LEVEL_QUIET)) {
		FILE *fp;

		fp = safeopen(logfile, "a", SAFEOPEN_LINK_OK);

		if (!fp) {
			char *errmsg = NULL;
			xslurm_strerrorcat(errmsg);
			fprintf(stderr,
				"%s: log_init(): Unable to open logfile"
			        "`%s': %s\n", prog, logfile, errmsg);
			xfree(errmsg);
			rc = errno;
			goto out;
		}

		if (log->logfp)
			fclose(log->logfp); /* Ignore errors */

		log->logfp = fp;
	}

	if (log->logfp) {
		int fd;
		if ((fd = fileno(log->logfp)) < 0)
			log->logfp = NULL;
		else
			fd_set_close_on_exec(fd);
	}

	log->initialized = 1;
 out:
	return rc;
}
Exemplo n.º 27
0
static int command_userlog(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct log_data* ldata = (struct log_data*) plugin->ptr;
	struct cbuffer* buf = cbuf_create(128);
	size_t argnum = list_size(cmd->args);
	struct plugin_command_arg_data* arg1 = NULL;
	struct plugin_command_arg_data* arg2 = NULL;
	struct plugin_command_arg_data* arg3 = NULL;
	
	if (argnum == 3)
	{
		arg1 = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_integer);
		arg2 = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_string);
		arg3 = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_string);
	}
	else
	{
		if (argnum == 2)
		{
			cbuf_append_format(buf, "*** %s: Missing search pattern.", cmd->prefix);
			plugin->hub.send_message(plugin, user, cbuf_get(buf));
			cbuf_destroy(buf);
			return 0;
		}
		if (argnum == 1)
			arg1 = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_integer);
	}

	int lines = arg1 ? arg1->data.integer : 20;
	char* column = arg2 ? arg2->data.string : "";
	char* search = arg3 ? arg3->data.string : "";
	size_t column_len = strlen(column);
	size_t search_len = strlen(search);
	char query[1024];
	sqlite3_stmt *res;
	int error = 0;
	const char *tail;
	size_t count = 0;

	if (lines > 200)
		lines = 200;

	if (search_len && column_len)
	{
		if(!check_column(column))
		{
			cbuf_append_format(buf, "*** %s: Invalid column. Valid columns are: nick, cid, addr, credentials, useragent, message, all.", cmd->prefix);
			plugin->hub.send_message(plugin, user, cbuf_get(buf));
			cbuf_destroy(buf);
			return 0;
		}

		if (strcmp(column, "all") == 0)
		{
			sprintf(query, "SELECT * FROM userlog WHERE nick LIKE '%%%s%%' OR cid LIKE '%%%s%%' OR credentials LIKE '%%%s%%' OR useragent LIKE '%%%s%%' OR addr LIKE '%%%s%%' OR message LIKE '%%%s%%' ORDER BY time DESC LIMIT %d;", search, search, search, search, search, search, lines);
			cbuf_append_format(buf, "*** %s: Search_ing for \"%s\" in all columns.", cmd->prefix, search);
		}
		else
		{
			sprintf(query, "SELECT * FROM userlog WHERE %s LIKE '%%%s%%' ORDER BY time DESC LIMIT %d;", column, search, lines);
			cbuf_append_format(buf, "*** %s: Searching for \"%s\" in column \"%s\".", cmd->prefix, search, column);
		}
	}
	else
	{
		sprintf(query, "SELECT * FROM userlog ORDER BY time DESC LIMIT %d;", lines);
		cbuf_append_format(buf, "*** %s: ", cmd->prefix);
	}

	error = sqlite3_prepare_v2(ldata->db, query, strlen(query), &res, &tail);
    
	while (sqlite3_step(res) == SQLITE_ROW)
	{
		cbuf_append_format(buf, "\n[%s] %s, %s [%s] [%s] \"%s\" - %s", (char*) sqlite3_column_text(res, 6), (char*) sqlite3_column_text(res, 1), (char*) sqlite3_column_text(res, 0), (char*) sqlite3_column_text(res, 3), (char*) sqlite3_column_text(res, 2), (char*) sqlite3_column_text(res, 4), (char*) sqlite3_column_text(res, 5));
		count++;
	}

	if (error || count == 0)
	{
		if (search_len && column_len)
			cbuf_append(buf, "\n");
		cbuf_append(buf, "No log entries found.");
	}
	else
		cbuf_append_format(buf, "\n\n%zd entr%s shown", count, count != 1 ? "ies" : "y");

	sqlite3_finalize(res);
  
	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);
    
	return 0;
}
Exemplo n.º 28
0
int
main (void) {

    const char input_string[] = "Hello Base Encoding/Decoding World!!!";
    const char long_string[] =
        "\t0. This is a very long message\n"
        "\t1. This is a very long message\n"
        "\t2. This is a very long message\n"
        "\t3. This is a very long message\n"
        "\t4. This is a very long message\n"
        "\t5. This is a very long message\n"
        "\t6. This is a very long message\n"
        "\t7. This is a very long message\n"
        "\t8. This is a very long message\n"
        "\t9. This is a very long message\n"
        "\t0. This is a very long message\n"
        "\t1. This is a very long message\n"
        "\t2. This is a very long message\n"
        "\t3. This is a very long message\n"
        "\t4. This is a very long message\n"
        "\t5. This is a very long message\n"
        "\t6. This is a very long message\n"
        "\t7. This is a very long message\n"
        "\t8. This is a very long message\n"
        "\t9. This is a very\nlong message\n";
    char *cache = (char *)NULL;

    cbuffer_t *in_single = (cbuffer_t *)NULL;
    cbuffer_t *in_encode = (cbuffer_t *)NULL;
    cbuffer_t *out_decode = (cbuffer_t *)NULL;

    printf ("base 64 chunk size: in=%d, out=%d\n",
            base_encode_chunk_sz (6),
            base_decode_chunk_sz (6));

    printf ("base 32 chunk size: in=%d, out=%d\n",
            base_encode_chunk_sz (5),
            base_decode_chunk_sz (5));

    printf ("base 16 chunk size: in=%d, out=%d\n",
            base_encode_chunk_sz (4),
            base_decode_chunk_sz (4));

    printf ("base 64 buffer size: in=%d, out=%d\n",
            base_encode_buffer_sz (6),
            base_decode_buffer_sz (6));

    printf ("base 32 buffer size: in=%d, out=%d\n",
            base_encode_buffer_sz (5),
            base_decode_buffer_sz (5));

    printf ("base 16 buffer size: in=%d, out=%d\n",
            base_encode_buffer_sz (4),
            base_decode_buffer_sz (4));

    printf ("==================================================\n");

    in_single = cbuf_create ((size_t)(strlen (input_string) + 1));

    if (in_single != (cbuffer_t *)NULL) {

        printf ("input: %s (len:%d; %p)\n", input_string,
                strlen (input_string), (void *)in_single);

        cbuf_clean (in_single);
        cbuf_import (in_single, input_string, strlen (input_string));

        // base16 encoding/decoding
        in_encode = caf_base16_encode (in_single);
        if (in_encode != (cbuffer_t *)NULL) {
            // display encoding
            cache = (char *)xmalloc (in_encode->sz + 1);
            memset (cache, 0, in_encode->sz + 1);
            memcpy (cache, in_encode->data, in_encode->sz);
            printf ("base16 encode: %s\n", cache);
            xfree (cache);
            // decoding and display decoding
            out_decode = caf_base16_decode (in_encode);
            if (out_decode != (cbuffer_t *)NULL) {
                cache = (char *)xmalloc (out_decode->iosz + 1);
                memset (cache, 0, out_decode->sz + 1);
                memcpy (cache, out_decode->data, out_decode->sz);
                printf ("base16 decode: %s (len:%d; blen:%d)\n", cache,
                        strlen (cache), (int)out_decode->iosz);
                cbuf_delete (out_decode);
                xfree (cache);
            }
            cbuf_delete (in_encode);
        }

        printf ("==================================================\n");

        // base32 encoding/decoding
        in_encode = caf_base32_encode (in_single);
        if (in_encode != (cbuffer_t *)NULL) {
            // display encoding
            cache = (char *)xmalloc (in_encode->sz + 1);
            memset (cache, 0, in_encode->sz + 1);
            memcpy (cache, in_encode->data, in_encode->sz);
            printf ("base32 encode: %s\n", cache);
            xfree (cache);
            // decoding and display decoding
            out_decode = caf_base32_decode (in_encode);
            if (out_decode != (cbuffer_t *)NULL) {
                cache = (char *)xmalloc (out_decode->iosz + 1);
                memset (cache, 0, out_decode->sz + 1);
                memcpy (cache, out_decode->data, out_decode->sz);
                printf ("base32 decode: %s (len:%d; blen:%d)\n", cache,
                        strlen (cache), (int)out_decode->iosz);
                cbuf_delete (out_decode);
                xfree (cache);
            }
            cbuf_delete (in_encode);
        }

        printf ("==================================================\n");

        // base64 encoding/decoding
        in_encode = caf_base64_encode (in_single);
        if (in_encode != (cbuffer_t *)NULL) {
            // display encoding
            cache = (char *)xmalloc (in_encode->sz + 1);
            memset (cache, 0, in_encode->sz + 1);
            memcpy (cache, in_encode->data, in_encode->sz);
            printf ("base64 encode: %s\n", cache);
            xfree (cache);
            // decoding and display decoding
            out_decode = caf_base64_decode (in_encode);
            if (out_decode != (cbuffer_t *)NULL) {
                cache = (char *)xmalloc (out_decode->iosz + 1);
                memset (cache, 0, out_decode->sz + 1);
                memcpy (cache, out_decode->data, out_decode->sz);
                printf ("base64 decode: %s (len:%d; blen:%d)\n", cache,
                        strlen (cache), (int)out_decode->iosz);
                cbuf_delete (out_decode);
                xfree (cache);
            }
            cbuf_delete (in_encode);
        }

        printf ("==================================================\n");

        cbuf_delete (in_single);

    }


    in_single = cbuf_create ((size_t)(strlen (long_string) + 1));

    if (in_single != (cbuffer_t *)NULL) {

        printf ("input:\n%s (len:%d; %p)\n", long_string,
                strlen (long_string), (void *)in_single);

        cbuf_clean (in_single);
        cbuf_import (in_single, long_string, strlen (long_string));

        // base16 encoding/decoding
        in_encode = caf_base16_encode (in_single);
        if (in_encode != (cbuffer_t *)NULL) {
            // display encoding
            cache = (char *)xmalloc (in_encode->sz + 1);
            memset (cache, 0, in_encode->sz + 1);
            memcpy (cache, in_encode->data, in_encode->sz);
            printf ("base16 encode: %s\n", cache);
            xfree (cache);
            // decoding and display decoding
            out_decode = caf_base16_decode (in_encode);
            if (out_decode != (cbuffer_t *)NULL) {
                cache = (char *)xmalloc (out_decode->iosz + 1);
                memset (cache, 0, out_decode->sz + 1);
                memcpy (cache, out_decode->data, out_decode->sz);
                printf ("base16 decode: %s (len:%d; blen:%d)\n", cache,
                        strlen (cache), (int)out_decode->iosz);
                cbuf_delete (out_decode);
                xfree (cache);
            }
            cbuf_delete (in_encode);
        }

        printf ("==================================================\n");

        // base32 encoding/decoding
        in_encode = caf_base32_encode (in_single);
        if (in_encode != (cbuffer_t *)NULL) {
            // display encoding
            cache = (char *)xmalloc (in_encode->sz + 1);
            memset (cache, 0, in_encode->sz + 1);
            memcpy (cache, in_encode->data, in_encode->sz);
            printf ("base32 encode: %s\n", cache);
            xfree (cache);
            // decoding and display decoding
            out_decode = caf_base32_decode (in_encode);
            if (out_decode != (cbuffer_t *)NULL) {
                cache = (char *)xmalloc (out_decode->iosz + 1);
                memset (cache, 0, out_decode->sz + 1);
                memcpy (cache, out_decode->data, out_decode->sz);
                printf ("base32 decode: %s (len:%d; blen:%d)\n", cache,
                        strlen (cache), (int)out_decode->iosz);
                cbuf_delete (out_decode);
                xfree (cache);
            }
            cbuf_delete (in_encode);
        }

        printf ("==================================================\n");

        // base64 encoding/decoding
        in_encode = caf_base64_encode (in_single);
        if (in_encode != (cbuffer_t *)NULL) {
            // display encoding
            cache = (char *)xmalloc (in_encode->sz + 1);
            memset (cache, 0, in_encode->sz + 1);
            memcpy (cache, in_encode->data, in_encode->sz);
            printf ("base64 encode: %s\n", cache);
            xfree (cache);
            // decoding and display decoding
            out_decode = caf_base64_decode (in_encode);
            if (out_decode != (cbuffer_t *)NULL) {
                cache = (char *)xmalloc (out_decode->iosz + 1);
                memset (cache, 0, out_decode->sz + 1);
                memcpy (cache, out_decode->data, out_decode->sz);
                printf ("base64 decode: %s (len:%d; blen:%d)\n", cache,
                        strlen (cache), (int)out_decode->iosz);
                cbuf_delete (out_decode);
                xfree (cache);
            }
            cbuf_delete (in_encode);
        }

        printf ("==================================================\n");

        cbuf_delete (in_single);

    }

    return 0;
}