]> jfr.im git - irc/thales.git/commitdiff
Fixed compilation errors after big rebuild.
authorDmitry Bogatov <redacted>
Thu, 27 Dec 2012 22:59:48 +0000 (02:59 +0400)
committerDmitry Bogatov <redacted>
Thu, 27 Dec 2012 22:59:48 +0000 (02:59 +0400)
12 files changed:
configure.ac
src/Makefile.am
src/cmd.c
src/cmd.h
src/init_queries.sql.h [new file with mode: 0644]
src/irc.c
src/irc.h
src/main.c
src/mysql_sentry.c
src/utility.h
src/workers_registration.c [deleted file]
src/workers_registration.h [deleted file]

index 2b39fbc1180b7161799ac83f9eef864b06bdb671..88da4cd2742c22d59f8ec5604cb1035e6de8d428 100644 (file)
@@ -13,6 +13,10 @@ AC_CHECK_HEADER([libircclient/libircclient.h],[],
         [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.
 
index cbf98e0c52804e55fcdad7e0ff73ac0617f074e4..87ee48003cc79c991c7f50d038d14d2f7010524a 100644 (file)
@@ -1,9 +1,10 @@
 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}
index c0a23035cae4f77bf52f3c0c275718cb1d5bcf33..08682ce97bdca1b740ed2a2cc347a333888a958f 100644 (file)
--- a/src/cmd.c
+++ b/src/cmd.c
@@ -20,6 +20,7 @@ this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <envz.h>
 #include "utility.h"
 
 static void
@@ -53,13 +54,15 @@ print_version (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'},
@@ -75,17 +78,19 @@ parse_cmdopts (struct irc_options *opts, int argc, char **argv)
        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);
       }
@@ -111,7 +116,7 @@ default_config_file(void)
   return NULL;
 }
 
-static void
+static inline void
 fill_envz(char **envz, size_t *envz_len, FILE *stream)
 {
   char *line = NULL;
index d312b37a646366863d47343a43eef3bfdce5b563..275c0604661f41ae5e93c0f3fe9253c511eb6e62 100644 (file)
--- a/src/cmd.h
+++ b/src/cmd.h
@@ -16,6 +16,9 @@ this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #ifndef CMD_H
 #define CMD_H
+#include <stdbool.h>
+#include <stdio.h>
+
 struct irc_options {
   const char *server;
   const char *nick;
@@ -30,8 +33,14 @@ struct mysql_options {
   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
diff --git a/src/init_queries.sql.h b/src/init_queries.sql.h
new file mode 100644 (file)
index 0000000..8202433
--- /dev/null
@@ -0,0 +1,68 @@
+#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
index 2e14da4c4f9bd1e11e63e2a5169a70cac4a2f745..fe4ec98302053409ce42e5a1c70912cbe357fcf6 100644 (file)
--- a/src/irc.c
+++ b/src/irc.c
   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
@@ -60,7 +59,7 @@ static
 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);
 }
 
@@ -73,12 +72,13 @@ static irc_callbacks_t callbacks = {
 };
 
 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)
index a00aa7a32e8e2a692ad0e241ef00e63514bbdd52..f249626ae3ae0f6c09637b9581a0c95917193d75 100644 (file)
--- a/src/irc.h
+++ b/src/irc.h
@@ -2,14 +2,14 @@
 #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
index 286325fe1ea685a6f826882b42f0642151d0ee2a..9fef8779b10d151f99e6e364d186884a6bf649ca 100644 (file)
@@ -19,27 +19,24 @@ this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #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;
 }
index 1d4393f6a66d48b8db1ade1383bb0dd7d6b10be7..41cc655f66f65c9072fb6c4a189354dbe1d47913 100644 (file)
@@ -1,27 +1,50 @@
 #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;
 }
index 6111b4677ffd452a367ebd606e717843ab1db17f..9b5ae7eadff6c869b17430d7232b6509b9432e30 100644 (file)
@@ -12,7 +12,7 @@ xrangedup (const void *begin, const void *end)
 static inline char*
 xstrdup_safe(const char *ptr)
 {
-  return ptr ? xstrndup(ptr) : NULL;
+  return ptr ? xstrdup(ptr) : NULL;
 }
 
 #endif
diff --git a/src/workers_registration.c b/src/workers_registration.c
deleted file mode 100644 (file)
index da79cd5..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-#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;
-}
diff --git a/src/workers_registration.h b/src/workers_registration.h
deleted file mode 100644 (file)
index 1993ad6..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-#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