summaryrefslogtreecommitdiff
path: root/agni.c
diff options
context:
space:
mode:
Diffstat (limited to 'agni.c')
-rw-r--r--agni.c257
1 files changed, 199 insertions, 58 deletions
diff --git a/agni.c b/agni.c
index 8dc5291..d405396 100644
--- a/agni.c
+++ b/agni.c
@@ -27,6 +27,7 @@
#include "util.h"
#include "sock_conn.h"
#include "irc_parse.h"
+#include "mmm.h"
/*
no defence programming, no error checking, no argument checking just PoC
@@ -55,6 +56,42 @@ void *cmd_pong(void *data)
return ret;
}
+void *cmd_uptime(void *data)
+{
+ char *param = (char *)data;
+ char *ret = NULL;
+
+ if (param == NULL)
+ {
+ return NULL;
+ }
+
+ printf("UPTIME\n");
+
+ ret = alloc_new_str("UpTime is infinite\n");
+
+ return ret;
+}
+
+void *cmd_date(void *data)
+{
+ char *param = (char *)data;
+ char *ret = NULL;
+
+ if (param == NULL)
+ {
+ return NULL;
+ }
+
+ printf("DATE\n");
+
+ ret = alloc_new_str("I dont whant to date with you\n");
+
+ return ret;
+}
+
+
+
/*
Wrap call back, to manage how going be executed callback.
Manage input and output params for callback.
@@ -137,7 +174,7 @@ int uniq_id()
-
+/*
int send_mq_cmd(mq_ntf_mdt *mq,
int io,
int cmd_id,
@@ -231,7 +268,7 @@ int recv_mq_cmd(mq_ntf_mdt *mq,
return 0;
}
-
+*/
#define SEND_CMD_IN(MQ,ID,CMD,PARAM) \
send_mq_cmd(MQ,MQ_IN,ID,CMD,strlen(CMD),PARAM,strlen(PARAM));
#define SEND_CMD_OUT(MQ,ID,CMD,PARAM) \
@@ -392,7 +429,7 @@ int th_start_client(void *data)
switch(mq_event)
{
case 0:
- PRINT("EVENT 0\n");
+ //PRINT("EVENT 0\n");
break;
case 1:
PRINT("MQ_EVENT IN\n");
@@ -402,48 +439,61 @@ int th_start_client(void *data)
printf("Cant read input message \n");
} else
{
- PNL();
+ //PNL();
in_buf[in_attr.mq_msgsize-1] = 0x0;
- printf("Recieve %s\n", in_buf);
+ PRINT("Recieve %s\n", in_buf);
}
PNL();
break;
default:
printf("Unknown event type\n");
}
- //PNL();
- //sleep(1);
-
- //PNL();
//fast code to exit if QUIT command is recieved
+ //for command in queue
+ //recieve commands from queue
if (mq_event == 1)
{
//PNL();
recv_cmd = mq_cmd_creates(in_buf, in_attr.mq_msgsize, -1);
if (recv_cmd != NULL)
{
+ //char *bufPtr = mq_cmd_buf(recv_cmd);
+ //printf("Recieved response [%s]\n",bufPtr);
+ //char *cmdPtr;
+ //size_t cmdSz;
+ //mq_cmd_cmd(recv_cmd, &cmdPtr, &cmdSz);
+ //printf("CMD %s %d\n", cmdPtr, cmdSz);
if (mq_cmd_o_cmp_cmd(recv_cmd,"QUIT") == 0)
{
printf("QUIT recieved lets quit main loop\n");
break;
+ } else if (mq_cmd_o_cmp_cmd(recv_cmd,"PRIVMSG:") == 0)
+ {
+ PRINT("Response\n")
+ char *paramPtr;
+ size_t paramSz;
+ mq_cmd_param(recv_cmd, &paramPtr, &paramSz);
+ printf("Recieved response [%s]%d\n",paramPtr,paramSz);
+ } else
+ {
+ PRINT("Unknown command\n");
}
}
+ mq_cmd_free(recv_cmd);
+ recv_cmd = NULL;
+
}
- //PNL();
- //recieve commands from queue
+
//recieve irc commands and pass to MQ
//fix this by allowing drain just chunks, not whole buffer
- //PNL();
+ //memset(conn_buf, 0, TH_CONN_BUF_SZ);
if((cret = read(conn,conn_buf, 256))>0)
{
- //printf("IN>>%s",conn_buf);
irc_buf_puts(ib, conn_buf, cret);
- //if (irc_buf_ready(ib) == 1)
while (irc_buf_ready(ib) == 1)
{
- //PNL();
irc_line = irc_buf_line(ib);
if (irc_line)
{
@@ -453,12 +503,9 @@ int th_start_client(void *data)
write(conn,"PONG",strlen("PONG"));
printf("OUT>>PONG\n");
}
-
- //PNL();
itok = irc_parse(irc_line,strlen(irc_line));
- //PNL();
- if (itok!=NULL)
+ if (itok != NULL)
{
int j;
for (j=0;j<token_len(itok);j++)
@@ -492,16 +539,18 @@ int th_start_client(void *data)
char *sep = strchr(uname,'!');
uname[sep-uname] = 0x0;
- fret2=snprintf(cmd_buf,128,":%s PRIVMSG %s PONG\r\n",cfg->user,uname);
+ //just send to test that thos commands works mate
+ fret2=snprintf(cmd_buf,128,":%s PRIVMSG %s PONGER\r\n",cfg->user,uname);
printf("FORMATED [%s]",cmd_buf);
fret = write(conn,cmd_buf,fret2);
//create command and send to mq
+ PRINT("msg %s\n");
mq_cmd *privcmd = mq_cmd_create(1,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg));
- char *str_mq = mq_cmd_buf(privcmd);
- printf("str_mq %s\n",str_mq);
- mq_ntf_write(mq, MQ_OUT, str_mq, strlen(str_mq));
- //free(str_mq);
+ char *bufPtr;
+ bufPtr = mq_cmd_buf(privcmd);
+ PRINT("BUF %s", bufPtr);
+ mq_ntf_write(mq, MQ_OUT, bufPtr, strlen(bufPtr));
mq_cmd_free(privcmd);
@@ -510,13 +559,13 @@ int th_start_client(void *data)
printf("OUT<<Send %d\n", fret);
}
- //PNL();
token_destroy(itok);
+ itok = NULL;
}
- free(irc_line);
- irc_line = NULL;
+ FREE(irc_line);
__irc_buf_drain_io(ib);
}
+
}
}
@@ -533,7 +582,7 @@ int th_start_client(void *data)
}
-#define EVENT_HND_STACK_SIZE (64*1024)
+#define EVENT_HND_STACK_SIZE (1024*1024)
typedef struct event_handler_cfg {
/* thread params*/
void *stack;
@@ -566,7 +615,6 @@ int th_event_manager(void *data)
mq_ntf_mdt *mq=NULL;
char *out_buf = NULL;
int run;
- int err;
int mq_event;
//read any command
@@ -575,23 +623,23 @@ int th_event_manager(void *data)
//execution and command table generation
tble_exec *ecmd = NULL;
- tbl_exec *etbl = NULL;
- tble_qcmd *qcmd=NULL;
- tbl_qcmd *qtbl = NULL;
+ tbl_exec *etbl = NULL;
+ tble_qcmd *qcmd = NULL;
+ tbl_qcmd *qtbl = NULL;
//create execution table
etbl = tbl_exec_list_c(10);
if (etbl == NULL)
{
PERM();
- return -1;
+ //return -1;
}
ecmd = tbl_exec_c();
if (ecmd == NULL)
{
PERM();
- return -1;
+ //return -1;
}
ecmd->id = uniq_id();
ecmd->name = alloc_new_str("local-executor");
@@ -601,18 +649,53 @@ int th_event_manager(void *data)
if (-1 == tbl_exec_add(etbl, ecmd))
{
PERM();
- return -1;
+ //return -1;
+ }
+
+ ecmd = tbl_exec_c();
+ if (ecmd == NULL)
+ {
+ PERM();
+ //return -1;
+ }
+ ecmd->id = uniq_id();
+ ecmd->name = alloc_new_str("local-executor");
+ ecmd->cmd = alloc_new_str("DATE");
+ ecmd->callback = cmd_date;
+
+ if (-1 == tbl_exec_add(etbl, ecmd))
+ {
+ PERM();
+ //return -1;
+ }
+
+ ecmd = tbl_exec_c();
+ if (ecmd == NULL)
+ {
+ PERM();
+ //return -1;
+ }
+ ecmd->id = uniq_id();
+ ecmd->name = alloc_new_str("local-executor");
+ ecmd->cmd = alloc_new_str("UPTIME");
+ ecmd->callback = cmd_uptime;
+
+ if (-1 == tbl_exec_add(etbl, ecmd))
+ {
+ PERM();
+ //return -1;
}
+
tbl_exec_print_tbl(etbl, TBL_PF_EXEC_ALL);
etbl = tbl_exec_list_c(10);
if (etbl == NULL)
{
PERM();
- return -1;
- }
+ //return -1;
+ }//PNL();
//create command table
- qtbl = tbl_qcmd_c(10);
+ qtbl = tbl_qcmd_c(10);//well 10 should be ok right?
//config mq
printf("Start event thread\n");
@@ -638,7 +721,7 @@ int th_event_manager(void *data)
switch(mq_event)
{
case 0:
- PRINT("EVENT 0\n");
+ //PRINT("EVENT 0\n");
break;
case 1:
PRINT("MQ_EVENT OUT\n");
@@ -662,47 +745,101 @@ int th_event_manager(void *data)
//if QUIT then quit the thread
if (mq_event == 1)
{
+ PNL();
+
+ recv_cmd = mq_cmd_creates(out_buf, out_attr.mq_msgsize, 1);
//PNL();
- recv_cmd = mq_cmd_creates(out_buf, out_attr.mq_msgsize, -1);
if (recv_cmd != NULL)
{
- //PNL();
+ PNL();
+ PRINT("MQ:CMD %s\n", mq_cmd_buf(recv_cmd));
if (mq_cmd_o_cmp_cmd(recv_cmd,"QUIT") == 0)
{
printf("QUIT recieved lets quit main loop\n");
break;
- }
-
- if (mq_cmd_o_cmp_cmd(recv_cmd,"PING") == 0)
+ } else if (mq_cmd_o_cmp_cmd(recv_cmd,"PING") == 0)
{
printf("Its fine im working\n");
//create command and send to mq
char msg[] = "PONG";
mq_cmd *privcmd = mq_cmd_create(1,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg));
- char *str_mq = mq_cmd_buf(privcmd);
- mq_ntf_write(mq, MQ_IN, str_mq, strlen(str_mq));
- free(str_mq);
+
+ //char *str_mq = mq_cmd_buf(privcmd);
+ //mq_ntf_write(mq, MQ_IN, str_mq, strlen(str_mq));
+
mq_cmd_free(privcmd);
- }
-
- if (mq_cmd_o_cmp_cmd(recv_cmd,"PRIVMSG") == 0)
+ } else if (mq_cmd_o_cmp_cmd(recv_cmd,"PRIVMSG") == 0)
{
printf("Some private message\n");
- char msg[] = "PONG";
+ /*char msg[] = "PONG EVENT THREAD";
+
mq_cmd *privcmd = mq_cmd_create(1,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg));
- char *str_mq = mq_cmd_buf(privcmd);
- mq_ntf_write(mq, MQ_IN, str_mq, strlen(str_mq));
- free(str_mq);
+
+ //recieve params and match command to them
+ char *paramPtr=NULL;
+ size_t paramSz=0;
+ mq_cmd_param(recv_cmd, &paramPtr, &paramSz);
+ PRINT("param = %s\n",paramPtr);
+ mq_ntf_write(mq, MQ_IN, paramPtr, paramSz);
+
mq_cmd_free(privcmd);
- }
+ */
+ if (mq_cmd_o_cmp_param(recv_cmd, ":DATE") == 0)
+ {
+ char msg[] = "No date";
+ mq_cmd *privcmd = mq_cmd_create(1,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg));
- if (mq_cmd_o_cmp_cmd(recv_cmd,"CMD2") == 0)
+ char *cmdBuf = mq_cmd_buf(privcmd);
+ mq_ntf_write(mq, MQ_IN, cmdBuf, strlen(cmdBuf));
+ cmdBuf = NULL;
+
+ mq_cmd_free(privcmd);
+ privcmd = NULL;
+ } else if (mq_cmd_o_cmp_param(recv_cmd, ":UPTIME") == 0)
+ {
+ char msg[] = "Forever";
+ mq_cmd *privcmd = mq_cmd_create(1,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg));
+
+ char *cmdBuf = mq_cmd_buf(privcmd);
+ mq_ntf_write(mq, MQ_IN, cmdBuf, strlen(cmdBuf));
+ cmdBuf = NULL;
+
+ mq_cmd_free(privcmd);
+ privcmd = NULL;
+ } else
+ {
+ char msg[] = "PONG EVENT THREAD";
+
+ mq_cmd *privcmd = mq_cmd_create(1,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg));
+
+ //recieve params and match command to them
+ //char *paramPtr=NULL;
+ //size_t paramSz=0;
+ //mq_cmd_param(recv_cmd, &paramPtr, &paramSz);
+ //PRINT("param = %s\n",paramPtr);
+ char *cmdBuf = mq_cmd_buf(privcmd);
+ mq_ntf_write(mq, MQ_IN, cmdBuf, strlen(cmdBuf));
+
+ mq_cmd_free(privcmd);
+ }
+
+ } else if (mq_cmd_o_cmp_cmd(recv_cmd,"CMD2") == 0)
{
printf("Hey dude it works second time\n");
+ } else
+ {
+ printf("Unknow command\n");
+ char *cmdPtr;
+ size_t cmdSz;
+ mq_cmd_cmd(recv_cmd, &cmdPtr, &cmdSz);
+ char *cmdNew = alloc_new_str_s(cmdPtr,cmdSz);
+ printf("Unknown cmd [%s]\n", cmdNew);
+ FREE(cmdNew);
}
}
+ mq_cmd_free(recv_cmd);
}
//applay to recieved command executor
@@ -804,7 +941,9 @@ int main(int argc, char **argv)
evhnd_cfg->mq_listen = mq_array;
clone(th_event_manager, evhnd_cfg->stack+EVENT_HND_STACK_SIZE, CLONE_VM|CLONE_FILES, (void *)evhnd_cfg);
- PNL();
+ //PNL();
+ /* wait a sec while all threads will start running */
+ sleep(1);
/* run until all threads are up */
cnt_running = 1;
while(cnt_running != 0)
@@ -819,11 +958,13 @@ int main(int argc, char **argv)
if (val != 0)
cnt_running += 1;
}
- PRINT("cnt_running %d\n",cnt_running);
+ //PRINT("cnt_running %d\n",cnt_running);
sleep(1);
}
free(cfg_list);
+ ERROR("Exit\n");
+
return 0;
} \ No newline at end of file