Removed some dispensable log messages
[cascardo/rnetproxy.git] / improxy.c
index 987ecb0..e69df59 100644 (file)
--- a/improxy.c
+++ b/improxy.c
@@ -1,56 +1,57 @@
+/*
+** Copyright (C) 2006 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+**  
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License as published by
+** the Free Software Foundation; either version 2 of the License, or
+** (at your option) any later version.
+**  
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+** GNU General Public License for more details.
+**  
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+**  
+*/
+
 #include <glib.h>
 #include <gnet.h>
 #include <stdio.h>
 #include "log.h"
+#include "nethook.h"
+#include "proto_detect.h"
 
-void client_event (GConn* conn, GConnEvent* event, gpointer data)
-{
-  GConn* server;
-  server = (GConn*) data;
-  switch (event->type)
-    {
-    case GNET_CONN_CONNECT:
-      g_debug ("Connected to localhost:80.");
-      gnet_conn_read (conn);
-      break;
-    case GNET_CONN_READ:
-      if (server == NULL)
-       {
-         g_debug ("Establishing connection to localhost:80.");
-         server = gnet_conn_new ("127.0.0.1", 80, client_event, (gpointer) conn);
-         gnet_conn_connect (server);
-         gnet_conn_set_callback (conn, client_event, (gpointer) server);
-       }
-      gnet_conn_write (server, event->buffer, event->length);
-      gnet_conn_read (conn);
-      break;
-    case GNET_CONN_WRITE:
-      break;
-    case GNET_CONN_CLOSE:
-      gnet_conn_unref (server);
-      gnet_conn_unref (conn);
-      break;
-    default:
-      g_warning ("Received an unexpected client event.");
-      break;
-    }
-}
+#define CONFFILE SYSCONFDIR "/improxy.conf"
 
 void new_client (GServer* server, GConn* conn, gpointer data)
 {
+  net_hook_t* hook;
   if (conn == NULL)
     {
       g_critical ("Server has received an error event.");
       return;
     }
   g_message ("Received connection from %s.", conn->hostname);
-  gnet_conn_set_callback (conn, client_event, NULL);
+  hook = proto_detect_new (conn);
   gnet_conn_read (conn);
 }
 
-int main ()
+static gchar* configfile;
+
+static GOptionEntry opt_entries[] =
+  {
+    { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile,
+      "Configuration file location", "file" },
+    { NULL }
+  };
+
+int main (int argc, char** argv)
 {
 
+  GOptionContext* opt_ctx;
   GKeyFile *keyfile;
   GInetAddr* inetaddr;
   gchar* conf_address;
@@ -59,9 +60,19 @@ int main ()
   gnet_init ();
   im_log_init ();
 
+  configfile = CONFFILE;
+  opt_ctx = g_option_context_new ("");
+  g_option_context_add_main_entries (opt_ctx, opt_entries, NULL);
+  if (!g_option_context_parse (opt_ctx, &argc, &argv, NULL))
+    {
+      g_log (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL,
+            "Could not parse command line options.");
+    }
+  g_option_context_free (opt_ctx);
+  
   keyfile =  g_key_file_new ();
 
-  g_key_file_load_from_file (keyfile, "improxy.conf", G_KEY_FILE_NONE, NULL);
+  g_key_file_load_from_file (keyfile, configfile, G_KEY_FILE_NONE, NULL);
 
   conf_address = g_key_file_get_string (keyfile, "global", "address", NULL);
   port = g_key_file_get_integer (keyfile, "global", "port", NULL);