[AC_MSG_ERROR([libircclient header not found])])
AC_CHECK_LIB([ircclient],[irc_create_session],[],
[AC_MSG_ERROR([libircclient library not found])])
+AC_CHECK_HEADER([mysql/mysql.h],[],
+ [AC_MSG_ERROR([Mysql header not found])])
+AC_CHECK_LIB([mysqlclient],[mysql_real_connect],[],
+ [AC_MSG_ERROR([Mysql library not found])])
# Checks for typedefs, structures, and compiler characteristics.
AM_CFLAGS = -I$(top_srcdir)/lib
bin_PROGRAMS = thales
-thales_SOURCES = main.c cmd.h cmd.c conf.h conf.c worker.h \
- log_worker.h log_worker.c error.h workers_registration.c \
- workers_registration.h irc.h irc.c defaults.c defaults.h \
+thales_SOURCES = main.c cmd.h cmd.c worker.h \
+ error.h irc.h irc.c defaults.c defaults.h \
sentry.h mysql_sentry.h mysql_sentry.c
thales_LDADD = $(top_builddir)/lib/libthales.a
+check-syntax:
+ $(COMPILE) -o /dev/null -S ${CHK_SOURCES}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
+#include <envz.h>
#include "utility.h"
static void
}
void
-parse_cmdopts (struct irc_options *opts, int argc, char **argv)
+parse_cmd_options (struct irc_options *opts, struct config_options *config_opts,
+ int argc, char **argv)
{
- const char *optstr = "hvs:p:C:n:";
+ const char *optstr = "hdvs:p:C:n:";
const struct option longopts[] = {
{"help", no_argument, NULL, 'h'},
{"server", required_argument, NULL, 's'},
{"port", required_argument, NULL, 'p'},
+ {"debug", no_argument, NULL, 'd'},
{"version", no_argument, NULL, 'v'},
{"nick", required_argument, NULL, 'n'},
{"config", required_argument, NULL, 'C'},
print_version ();
exit (EXIT_SUCCESS);
case 'C':
- opts->conf_filename = optarg;
+ config_opts->conf_filename = optarg;
break;
case 's':
opts->server = optarg;
break;
case 'p':
- opts->port = atoi (optarg);
+ opts->port = (unsigned short int) atoi (optarg); // Port in two byte integral.
break;
case 'n':
opts->nick = optarg;
break;
+ case 'd':
+ config_opts->debug = true;
case '?':
exit (EXIT_FAILURE);
}
return NULL;
}
-static void
+static inline void
fill_envz(char **envz, size_t *envz_len, FILE *stream)
{
char *line = NULL;
#ifndef CMD_H
#define CMD_H
+#include <stdbool.h>
+#include <stdio.h>
+
struct irc_options {
const char *server;
const char *nick;
unsigned short int port;
};
+struct config_options {
+ bool debug;
+ const char *conf_filename;
+};
+
FILE* default_config_file(void);
-void parse_cmd_options(struct irc_options *opts, int argc, char **argv);
+void parse_cmd_options(struct irc_options *irc_opts, struct config_options *config_opts,
+ int argc, char **argv);
void parse_mysql_options(struct mysql_options *opts, FILE *stream);
#endif
--- /dev/null
+#ifndef INIT_QUERIES_H
+#define INIT_QUERIES_H
+static const char *channel_tbl =
+ "CREATE TABLE IF NOT EXISTS channel ("
+ "chanid int unsigned NOT NULL auto_increment,"
+ "channel varchar(33) binary NOT NULL default '',"
+ "topic text,"
+ "topicauthor varchar(31) default NULL,"
+ "topictime datetime default NULL,"
+ "PRIMARY KEY (chanid),"
+ "UNIQUE KEY channel (channel))";
+
+static const char *user_tbl =
+ "CREATE TABLE IF NOT EXISTS user ("
+ "nickid int unsigned NOT NULL auto_increment,"
+ "nick varchar(31) NOT NULL default '',"
+ "realname varchar(51) NOT NULL default '',"
+ "hostname varchar(64) NOT NULL default '',"
+ "hiddenhostname varchar(64) NOT NULL default '',"
+ "username varchar(11) NOT NULL default '',"
+ "swhois varchar(32) NOT NULL default '',"
+ "connecttime datetime NOT NULL default '0000-00-00 00:00:00',"
+ "servid int unsigned NOT NULL default '0',"
+ "away enum('Y','N') NOT NULL default 'N',"
+ "awaymsg text,"
+ "online enum('Y','N') NOT NULL DEFAULT 'Y',"
+ "lastquit datetime default NULL,"
+ "PRIMARY KEY (nickid),"
+ "UNIQUE KEY nick (nick),"
+ "KEY servid (servid))";
+
+static const char * server_tbl =
+ "CREATE TABLE IF NOT EXISTS server ("
+ "servid int unsigned NOT NULL auto_increment,"
+ "server varchar(64) NOT NULL default '',"
+ "connecttime datetime default NULL,"
+ "PRIMARY KEY (servid),"
+ "UNIQUE KEY server (server))";
+
+static const char *presence_tbl =
+ "CREATE TABLE IF NOT EXISTS presence ("
+ "nickid int unsigned NOT NULL default '0',"
+ "chanid int unsigned NOT NULL default '0',"
+ "servid int unsigned NOT NULL default '0',"
+ "PRIMARY KEY (nickid,chanid,servid),"
+ "KEY nickid (nickid),"
+ "KEY chanid (chanid))";
+
+/* Stupid restriction of C */
+static inline const char **const
+init_queries ()
+{
+ enum { query_count = 5 };
+ static const char *queries[query_count] = { 0 };
+ queries[0] = user_tbl;
+ queries[1] = presence_tbl;
+ queries[2] = server_tbl;
+ queries[3] = channel_tbl;
+ queries[4] = NULL;
+ return queries;
+}
+
+
+
+
+
+
+#endif
function_name (irc_session_t * session, const char *event, \
const char *origin, const char **params, unsigned int count)
-struct context
-{
- const struct cmd_options *opts;
- const struct list_head *workers;
+struct context {
+ SENTRY *sentry;
+ const struct irc_options *opts;
};
static bool
EVENT_CALLBACK (event_connect)
{
const struct context *ctx = irc_get_ctx (session);
- for (const char **chan = ctx->opts->channels; *chan; ++chan)
+ for (char **chan = ctx->opts->channels; *chan; ++chan)
irc_cmd_join (session, *chan, NULL);
}
};
bool
-start_listen_irc (const struct cmd_options *opts,
- const struct list_head *workers)
+start_listen_irc (const struct irc_options *opts,
+ SENTRY *sentry)
{
irc_session_t *session = irc_create_session (&callbacks);
struct context context = {
- opts, workers
+ .sentry = sentry,
+ .opts = opts
};
if (!session)
#define IRC_H
#include <stdbool.h>
#include "cmd.h"
-#include "list.h"
+#include "mysql_sentry.h"
struct irc_user {
char *nickname;
char *realname;
};
-bool start_listen_irc(const struct cmd_options *opts,
- const struct list_head *workers);
+bool start_listen_irc(const struct irc_options *opts, SENTRY *sentry);
+
#endif
#include <stdlib.h>
#include <string.h>
#include "cmd.h"
-#include "conf.h"
-#include "list.h"
#include "error.h"
-#include "workers_registration.h"
#include "irc.h"
int
main (int argc, char **argv)
{
- struct cmd_options cmd_opts = { 0, };
- struct mysql_options mysql_opts = { 0, };
+ struct irc_options cmd_opts = { 0 };
+ struct mysql_options mysql_opts = { 0 };
+ struct config_options config_opts = { 0 };
FILE *config_file;
- parse_cmdopts (&cmd_opts, argc, argv);
- config_file = cmd_opts.conf_filename ? fopen (opts.conf_filename, "r")
+ parse_cmd_options (&cmd_opts, &config_opts, argc, argv);
+ config_file = config_opts.conf_filename ? fopen (config_opts.conf_filename, "r")
: default_config_file ();
if (!config_file)
fatal ("failed to open config file");
- parse_mysql_options(&mysql_opts);
- start_listen_irc (&cmd_opts, &workers);
+ parse_mysql_options(&mysql_opts, config_file);
return 0;
}
#include "mysql_sentry.h"
#include <mysql/mysql.h>
#include <xalloc.h>
+#include <stdbool.h>
#include "cmd.h"
+#include "init_queries.sql.h"
-typedef struct sentry
+struct sentry
{
MYSQL db_handle;
-} SENTRY;
+};
+
+static inline bool
+initialize_tables(MYSQL *db_handle)
+{
+ for (const char *query = *init_queries(); *query; ++query)
+ if (!mysql_query(db_handle, query))
+ return false;
+ return true;
+}
SENTRY *
sentry_initialize (const struct mysql_options *opts)
{
MYSQL db_handle;
mysql_init (&db_handle);
- if (!mysql_real_connect (&db_handle, opts->host, opts->user,
+ if (!mysql_real_connect (&db_handle, opts->host, opts->username,
opts->password, opts->database, opts->port,
NULL, 0))
{
fprintf(stderr, "Failed to connect to database: Error: %s\n",
- mysql_error(&mysql));
- return NULL;
- }
+ mysql_error(&db_handle));
+ goto connect;
+ }
+ if (!initialize_tables(&db_handle)) {
+ fprintf(stderr, "Failed to connect to database: Error: %s\n",
+ mysql_error(&db_handle));
+ goto tables;
+ }
+
struct sentry *new = xmalloc (sizeof *new);
+
new->db_handle = db_handle;
return new;
+
+ tables:
+ mysql_close(&db_handle);
+ connect:
+ return NULL;
}
static inline char*
xstrdup_safe(const char *ptr)
{
- return ptr ? xstrndup(ptr) : NULL;
+ return ptr ? xstrdup(ptr) : NULL;
}
#endif
+++ /dev/null
-#include "workers_registration.h"
-#include "log_worker.h"
-#include "list.h"
-#include "xalloc.h"
-static LIST_HEAD (worker_creators_list);
-
-#define add_worker_creator(TYPE, CREATOR) \
- do { \
- struct worker_creator *_node = xmalloc(sizeof *_node); \
- _node->type = (TYPE); \
- _node->creator = (CREATOR); \
- list_add(&_node->list, &worker_creators_list); \
- } while (0)
-
-
-void
-init_worker_creators (void)
-{
- add_worker_creator ("log", create_log_worker);
-}
-
-const struct worker_creator *
-worker_creator_by_type (const char *type)
-{
- struct list_head *ptr;
- list_for_each (ptr, &worker_creators_list)
- {
- struct worker_creator *node =
- list_entry (ptr, struct worker_creator, list);
- if (!strcmp (type, node->type))
- return node;
- }
- return NULL;
-}
+++ /dev/null
-#ifndef WORKERS_REGISTRATION_H
-#define WORKERS_REGISTRATION_H
-#include "worker.h"
-#include "list.h"
-#include "conf.h"
-struct worker_creator {
- struct worker* (*creator)(const struct envz *);
- const char *type;
- struct list_head list;
-};
-void init_worker_creators(void);
-const struct worker_creator* worker_creator_by_type(const char *type);
-#endif