aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorbeber <beber>2005-07-24 22:26:09 +0000
committerbeber <beber>2005-07-24 22:26:09 +0000
commit18fc944ae33b811e15bf028a35977f2e9c7bd6b5 (patch)
tree820b3f95655a67a8ee76b3a76e720e1eb852d4e2 /src
parentOops wrong file name (diff)
downloadespik-18fc944ae33b811e15bf028a35977f2e9c7bd6b5.tar.xz
indent
Diffstat (limited to 'src')
-rw-r--r--src/espik_common_handler.c71
-rw-r--r--src/espik_common_handler.h12
-rw-r--r--src/espik_config.c103
-rw-r--r--src/espik_irc.c215
-rw-r--r--src/espik_net.h10
5 files changed, 411 insertions, 0 deletions
diff --git a/src/espik_common_handler.c b/src/espik_common_handler.c
new file mode 100644
index 0000000..2c29322
--- /dev/null
+++ b/src/espik_common_handler.c
@@ -0,0 +1,71 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <Ecore.h>
+#include <Ecore_Con.h>
+
+#include "global.h"
+#include "espik_common_handler.h"
+#include "espik_irc.h"
+#include "espik_net.h"
+#include "espik.h"
+#include "error.h"
+
+int server_data (void *data __UNUSED__, int ev_type __UNUSED__, Ecore_Con_Event_Server_Data * ev)
+{
+ printf ("%s\n", (char *)ev->data);
+
+ return (0);
+}
+
+int kb_get (void *data __UNUSED__, Ecore_Fd_Handler * fd_handler)
+{
+ char *kb;
+ char *tmp;
+ int count;
+
+ kb = malloc (sizeof (char) * 200);
+ chk_malloc (kb);
+
+ do
+ {
+ memset (kb, 0, 200);
+ count = read (ecore_main_fd_handler_fd_get (fd_handler), kb, 198);
+
+ if (count > 1)
+ {
+ del_backslash (kb);
+ tmp = irc_send (kb);
+
+ if (tmp)
+ {
+ espik_raw_send (tmp);
+ free (tmp);
+ }
+ }
+ }
+ while (count >= 198);
+
+ if (count == -1)
+ perror ("read");
+
+ if (count == 0)
+ {
+ tmp = irc_quit ("", "^D pressed");
+ printf ("kb_get:\ntmp: %s\n", tmp);
+ espik_raw_send (tmp);
+ printf ("AFTER espik_raw_send\n");
+
+ // irc_disconnect();
+ espik_con_shutdown ();
+
+ ecore_main_loop_quit ();
+
+ free (tmp);
+ return (0);
+ }
+
+ return (1);
+}
diff --git a/src/espik_common_handler.h b/src/espik_common_handler.h
new file mode 100644
index 0000000..a9447c1
--- /dev/null
+++ b/src/espik_common_handler.h
@@ -0,0 +1,12 @@
+#ifndef _HAVE_ESPIK_COMMON_HANDLER_H
+#define _HAVE_ESPIK_COMMON_HANDLER_H
+
+#include "global.h"
+#include <Ecore_Con.h>
+#include <Ecore.h>
+
+int server_data (void *__UNUSED__, int __UNUSED__,
+ Ecore_Con_Event_Server_Data * __UNUSED__);
+int kb_get (void *__UNUSED__, Ecore_Fd_Handler *);
+
+#endif /* _HAVE_ESPIK_COMMON_HANDLER_H */
diff --git a/src/espik_config.c b/src/espik_config.c
new file mode 100644
index 0000000..9120b21
--- /dev/null
+++ b/src/espik_config.c
@@ -0,0 +1,103 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <pwd.h>
+
+#include <Ecore_Config.h>
+
+#include "espik_config.h"
+#include "error.h"
+
+t_servinfo *espik_server_config_set (char *server, unsigned short port)
+{
+ t_servinfo *s_info;
+
+ s_info = malloc (sizeof (t_servinfo));
+ chk_malloc (s_info);
+
+ s_info->host = server;
+ s_info->port = port;
+
+ return (s_info);
+}
+
+t_userinfo *espik_user_sysinfo_get ()
+{
+ struct passwd *pw;
+ t_userinfo *u_info;
+
+ pw = getpwuid (getuid ());
+
+ u_info = malloc (sizeof (t_userinfo));
+ chk_malloc (u_info);
+
+ u_info->nickname = pw->pw_name;
+ u_info->username = pw->pw_name;
+ u_info->realname = pw->pw_gecos;
+
+ return (u_info);
+}
+
+void espik_config_default_set ()
+{
+ t_userinfo *user_sysinfo;
+
+ ecore_config_string_default ("/config/server/0/host", "beber.melee");
+ ecore_config_int_default ("/config/server/0/port", 6667);
+
+ user_sysinfo = espik_user_sysinfo_get ();
+
+ ecore_config_string_default ("/config/user/nickname", user_sysinfo->nickname);
+ ecore_config_string_default ("/config/user/username", user_sysinfo->username);
+ ecore_config_string_default ("/config/user/realname", user_sysinfo->realname);
+
+ ecore_config_string_default ("/espik/version", ESPIK_VERSION);
+
+ free (user_sysinfo);
+}
+
+void espik_config_init ()
+{
+ if (ecore_config_init (APPS_NAME) != ECORE_CONFIG_ERR_SUCC)
+ {
+ printf ("Cannot init Ecore_Config");
+ exit (-1);
+ }
+
+ ecore_config_load ();
+}
+
+void espik_config_get (t_info * s_info)
+{
+ s_info->server = malloc (sizeof (t_servinfo));
+ chk_malloc (s_info->server);
+ s_info->client = malloc (sizeof (t_userinfo));
+ chk_malloc (s_info->client);
+
+ espik_config_default_set ();
+
+ s_info->server->host = ecore_config_string_get ("/config/server/0/host");
+ s_info->server->port = (unsigned short)ecore_config_int_get ("/config/server/0/port");
+
+ s_info->client->nickname = ecore_config_string_get ("/config/user/nickname");
+ s_info->client->username = ecore_config_string_get ("/config/user/username");
+ s_info->client->realname = ecore_config_string_get ("/config/user/realname");
+
+ ecore_config_save ();
+}
+
+void espik_config_set (t_info config)
+{
+ ecore_config_int_set ("/config/server/len", 1);
+ ecore_config_string_set ("/config/server/0/host", config.server->host);
+ ecore_config_int_set ("/config/server/0/port", (int)config.server->port);
+ ecore_config_int_set ("/config/server/0/autoconnect", 1);
+
+ ecore_config_string_set ("/config/user/nickname", config.client->nickname);
+ ecore_config_string_set ("/config/user/username", config.client->username);
+ ecore_config_string_set ("/config/user/realname", config.client->realname);
+
+ ecore_config_save ();
+}
diff --git a/src/espik_irc.c b/src/espik_irc.c
new file mode 100644
index 0000000..8846ab1
--- /dev/null
+++ b/src/espik_irc.c
@@ -0,0 +1,215 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#include <Ecore.h>
+//#include <Ecore_Con.h>
+
+#include "global.h"
+#include "espik_irc.h"
+#include "error.h"
+//#include "espik_common_handler.h"
+#include "espik.h"
+#include "espik_net.h"
+
+char *current_chan;
+
+typedef struct
+{
+ char *cmd_name;
+ char *(*format) (char *dest, char *msg);
+} cmd_t;
+
+cmd_t cmd_tab[] = {
+ {"JOIN", irc_join},
+ {"PART", irc_part},
+ {"PRIVMSG", irc_privmsg},
+ {"QUIT", irc_quit},
+ {"RAW", irc_raw},
+ /*{"ACTION", irc_action}, */
+
+ {0, 0}
+};
+
+char **separate_commandline (char *msg)
+{
+ unsigned int len;
+ char **two_words;
+
+ for (len = 0; msg[len] != ' '; len++); /* Yes, do nothing, just get len ! */
+
+ two_words = malloc (sizeof (char *) * 2);
+ chk_malloc (two_words);
+
+ msg[len] = '\0';
+ two_words[0] = msg;
+ two_words[1] = msg + len + 1;
+
+ return (two_words);
+}
+
+void string_upper (char *str)
+{
+ unsigned int len, i;
+
+ len = strlen (str);
+
+ for (i = 0; i < len; i++)
+ {
+ if (str[i] >= 0x61 && str[i] <= 0x7A)
+ str[i] -= 0x20;
+ }
+}
+
+unsigned short sendmsg_len (char *msg)
+{
+ return (strlen (msg) + 2);
+}
+
+char *irc_privmsg (char *chan, char *msg)
+{
+ char *out;
+ unsigned int len;
+
+ len = strlen ("PRIVMSG") + 1 /* space */
+ + strlen (chan) + 2 /* space: */
+ + strlen (msg) + 1; /* \0 */
+
+ out = malloc (sizeof (char) * len);
+ chk_malloc (out);
+
+ snprintf (out, len, "PRIVMSG %s :%s", chan, msg);
+
+ return (out);
+}
+
+char *irc_join (char *chan __UNUSED__, char *msg)
+{
+ char *out;
+ int len;
+
+ if (msg[0] != '#') /* COuld also by '&' and so one ... have to look on RFC, dev will be done with # only */
+ {
+ printf ("%s is not a channel\n", msg);
+ printf ("Usage: /JOIN #channel\n");
+ return (0);
+ }
+
+ len = strlen ("JOIN") + 1 + strlen (msg) + 1;
+ out = malloc (sizeof (char) * len);
+ chk_malloc (out);
+
+ snprintf (out, len, "JOIN %s", msg);
+
+ return (out);
+}
+
+char *irc_part (char *chan, char *msg)
+{
+ char *out;
+ int len;
+
+ if (msg[0] != '#')
+ {
+ len = strlen ("PART") + 1 + strlen (chan) + 2 + strlen (msg) + 1;
+ printf ("irc_part: len: %d\n", len);
+ out = malloc (sizeof (char) * len);
+ chk_malloc (out);
+ snprintf (out, len, "PART %s :%s", chan, msg);
+ }
+ else
+ {
+ char **sep;
+
+ sep = separate_commandline (msg);
+ len = strlen ("PART") + 1 + strlen (sep[0]) + 2 + strlen (sep[1]) + 1;
+ out = malloc (sizeof (char) * len);
+ chk_malloc (out);
+ snprintf (out, len, "PART %s :%s", sep[0], sep[1]);
+ }
+
+ return (out);
+}
+
+char *irc_quit (char *chan __UNUSED__, char *msg)
+{
+ char *out;
+ int len;
+
+ len = strlen ("QUIT") + 2 + strlen (msg) + 1;
+
+ out = malloc (sizeof (char) * len);
+ chk_malloc (out);
+
+ snprintf (out, len, "QUIT :%s", msg);
+
+ printf ("msg : %s\nout: %s\n", msg, out);
+
+ //espik_raw_send (out);
+ //espik_con_shutdown ();
+
+// irc_disconnect(sock);
+
+ return (out);
+}
+
+char *irc_raw (char *chan __UNUSED__, char *msg)
+{
+ return (msg);
+}
+
+/*Ecore_Con_Server *irc_connect (char *host __UNUSED__, int port __UNUSED__)
+{
+ return (NULL);
+}*/
+
+/*void irc_disconnect (Ecore_Con_Server * sock)
+{
+ printf ("> irc_disconnect\n");
+ ecore_main_loop_quit ();
+ printf ("< irc_disconnect\n");
+}*/
+
+char *irc_send (char *msg)
+{
+ char *out;
+
+ out = NULL;
+
+ current_chan = "#test";
+
+ if (msg[0] == '/')
+ {
+ char **cmd_and_message;
+
+ cmd_t *cmd;
+
+ cmd_and_message = separate_commandline (msg + 1);
+ string_upper (cmd_and_message[0]);
+
+ for (cmd = cmd_tab; cmd->cmd_name != 0; cmd++)
+ {
+ if ((strcmp (cmd_and_message[0], cmd->cmd_name)) == 0)
+ {
+ out = cmd->format (current_chan, cmd_and_message[1]);
+ break; /* WHy do job after found the good to do ? */
+ }
+ }
+ if (!out)
+ {
+ printf ("%s is not a know command\n", cmd_and_message[0]);
+ return (0);
+ }
+ }
+ else
+ {
+ out = irc_privmsg (current_chan, msg);
+ }
+
+ if (!out)
+ return (0);
+
+ printf ("%s\n", out);
+
+ return (out);
+}
diff --git a/src/espik_net.h b/src/espik_net.h
new file mode 100644
index 0000000..64e9717
--- /dev/null
+++ b/src/espik_net.h
@@ -0,0 +1,10 @@
+#ifndef _HAVE_ESPIK_NET_H
+#define _HAVE_ESPIK_NET_H
+
+#include "struct.h"
+
+void espik_raw_send (char *);
+void espik_con_init (t_info);
+void espik_con_shutdown ();
+
+#endif /* _HAVE_ESPIK_NET_H */