]> jfr.im git - irc/thales.git/commitdiff
First implementation of actual db usage. Not linking yet
authorDmitry Bogatov <redacted>
Sat, 5 Jan 2013 00:05:56 +0000 (04:05 +0400)
committerDmitry Bogatov <redacted>
Sat, 5 Jan 2013 00:05:56 +0000 (04:05 +0400)
src/cmd.c
src/init_queries.sql.h
src/irc.c
src/main.c
src/mysql_sentry.h

index 08682ce97bdca1b740ed2a2cc347a333888a958f..16acd6dbf4b54372ecee2c1bda8a0c8a1fb69789 100644 (file)
--- a/src/cmd.c
+++ b/src/cmd.c
@@ -111,7 +111,7 @@ default_config_file(void)
   FILE *config_file;
 
   for (int index = 0; index != countof(filenames); ++index)
-    if (filenames[index] && (config_file = fopen(filenames[index], "w")))
+    if (filenames[index] && (config_file = fopen(filenames[index], "r")))
       return config_file;
   return NULL;
 }
index 82024337804dfc121e8d482c2054fc1f9853e30c..9120fc5b36825af91bb74f6db53baa4e080d9f78 100644 (file)
@@ -47,7 +47,7 @@ static const char *presence_tbl =
   "KEY chanid (chanid))";
 
 /* Stupid restriction of C */
-static inline const char **const
+static inline const char **
 init_queries ()
 {
   enum { query_count = 5 };
@@ -59,10 +59,4 @@ init_queries ()
   queries[4] = NULL;
   return queries;
 }
-
-
-
-
-
-
 #endif
index fe4ec98302053409ce42e5a1c70912cbe357fcf6..6dd03cba9a72b584cad6f869215441d4e053cfa7 100644 (file)
--- a/src/irc.c
+++ b/src/irc.c
@@ -1,10 +1,12 @@
 #include "irc.h"
-#include <stdlib.h>
 #include <libircclient/libircclient.h>
+#include <libircclient/libirc_rfcnumeric.h>
+#include <stdlib.h>
 #include <string.h>
 #include "defaults.h"
 #include "error.h"
 #include "utility.h"
+#include "mysql_sentry.h"
 
 #define EVENT_CALLBACK(function_name) void                              \
   function_name (irc_session_t * session, const char *event,            \
@@ -15,29 +17,21 @@ struct context {
   const struct irc_options *opts;
 };
 
-static bool
-parse_message (const char *msg, char **name, char **payload)
+EVENT_CALLBACK(event_nick)
 {
-  const char *const shebang = "#!";
-  const char *payload_begin;
-
-  *name = *payload = NULL;
-  if (strcmp (msg, shebang))
-    return false;              /* Is not command */
-
-  /* Looks like command, but no terminating colon */
-  if (!(payload_begin = strchr (msg, ':')))
-    return false;
 
-  *name = xrangedup (msg + strlen (shebang), payload_begin);
-  *payload = xstrdup (payload_begin + 1);
-  return true;
 }
 
+
 static
 EVENT_CALLBACK (event_join)
 {
-  printf ("%s joined %s\n", origin, *params);
+  const char *nickname =  origin;
+  const char *channel = *params;
+
+  printf ("%s joined %s\n", nickname, channel);
+  fflush(stdout);
+  irc_cmd_names(session, channel);
 }
 
 static
@@ -46,21 +40,47 @@ EVENT_CALLBACK (event_channel)
 
 }
 
+static inline void
+event_numeric_namreply(struct context *context, const char *channel,
+                       const char *nicknames)
+{
+  char *nicknames_buf = strdup(nicknames);
+
+  for (const char *nickname = strtok(nicknames_buf, " ");
+       nickname;
+       nickname = strtok(NULL, " "))
+    sentry_channel_presence_add(context->sentry, channel, nickname);
+
+  free(nicknames_buf);
+}
+
 static void
 event_numeric (irc_session_t * session, unsigned int
               event, const char *origin, const char **params,
               unsigned int count)
 {
-  printf ("Event =%d", event);
-}
+  struct context *context = irc_get_ctx(session);
 
+  switch (event) {
+  case LIBIRC_RFC_RPL_NAMREPLY:
+    assert(count >= 4);
+    const char *channel = params[2];
+    const char *nicknames = params[3];
+    event_numeric_namreply(context, channel, nicknames);
+    break;
+  }
+}
 
 static
 EVENT_CALLBACK (event_connect)
 {
   const struct context *ctx =  irc_get_ctx (session);
-  for (char **chan = ctx->opts->channels; *chan; ++chan)
+  printf("Connected!");
+  for (char **chan = ctx->opts->channels; *chan; ++chan) {
+    printf("sending connect request for %s\n", *chan);
     irc_cmd_join (session, *chan, NULL);
+  }
+  fflush(stdout);
 }
 
 /* Question to libircclient. Why not const? */
@@ -68,7 +88,8 @@ static irc_callbacks_t callbacks = {
   .event_channel = event_channel,
   .event_join = event_join,
   .event_connect = event_connect,
-  .event_numeric = event_numeric
+  .event_numeric = event_numeric,
+  .event_nick =  event_nick
 };
 
 bool
@@ -90,7 +111,7 @@ start_listen_irc (const struct irc_options *opts,
                           opts->nick ? opts->nick : default_nick, NULL,
                           NULL);
   if (error)
-    fatal ("%s", irc_strerror (irc_errno (session)));
+    fatal ("error = %d, %s", error, irc_strerror (irc_errno (session)));
   error = irc_run (session);
   if (error)
     fatal ("error = %d, %s", error, irc_strerror (irc_errno (session)));
index 9fef8779b10d151f99e6e364d186884a6bf649ca..e5c685e0fca2034e46514a5ec370e30047777082 100644 (file)
@@ -21,22 +21,30 @@ this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "cmd.h"
 #include "error.h"
 #include "irc.h"
+#include "mysql_sentry.h"
 
 int
 main (int argc, char **argv)
 {
-  struct irc_options cmd_opts = { 0 };
+  struct irc_options irc_opts = { 0 };
   struct mysql_options mysql_opts = { 0 };
   struct config_options config_opts = { 0 };
-  FILE *config_file;
 
-  parse_cmd_options (&cmd_opts, &config_opts, argc, argv);
-  config_file = config_opts.conf_filename ? fopen (config_opts.conf_filename, "r")
+
+  parse_cmd_options (&irc_opts, &config_opts, argc, argv);
+  FILE *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, config_file);
+
+SENTRY *sentry = sentry_initialize(&mysql_opts);
+#warning "mysql is not used"
+/* if (!sentry) */
+/*   fatal("failed to connect to database"); */
+
+  start_listen_irc(&irc_opts, sentry);
   return 0;
 }
index 0a24526b47100b80b04657678cc545aac72f6f4b..8a77b719ede2973f83681c7ba326cd3be7e69e1b 100644 (file)
@@ -1,10 +1,17 @@
 #ifndef MYSQL_SENTRY_H
 #define MYSQL_SENTRY_H
-
+#include <stdbool.h>
 struct mysql_options;
 struct sentry;
 typedef struct sentry SENTRY;
 
 SENTRY *sentry_initialize(const struct mysql_options *opts);
+bool sentry_channel_presence_clear(SENTRY *sentry, const char *channel);
+bool sentry_channel_presence_add(SENTRY *sentry,  const char *channel,
+                                 const char *nickname);
+
+bool sentry_update_presence(SENTRY *sentry,
+                            const char *nickname, const char *channel, bool online);
+
 
 #endif