From fc91eba3845a9e9c741da41ba467c042cbb4ca85 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Thu, 20 Apr 2017 16:17:32 +0100 Subject: callback execution works --- agni.c | 100 ++++++++++++++----------------------------------------------- tbl_qcmd.c | 36 ++++++++++++++++++++++ tbl_qcmd.h | 11 +++++++ 3 files changed, 70 insertions(+), 77 deletions(-) diff --git a/agni.c b/agni.c index dc91a93..4f8b220 100644 --- a/agni.c +++ b/agni.c @@ -44,11 +44,6 @@ void *cmd_pong(void *data) char *param = (char *)data; char *ret = NULL; - if (param == NULL) - { - return NULL; - } - printf("PONG\n"); ret = alloc_new_str("PONG"); @@ -61,11 +56,6 @@ 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"); @@ -78,11 +68,6 @@ 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"); @@ -858,73 +843,34 @@ int th_event_manager(void *data) mq_cmd_free(privcmd); } else if (mq_cmd_o_cmp_cmd(recv_cmd,"PRIVMSG") == 0) { - /* - printf("Some private message\n"); - if (mq_cmd_o_cmp_param(recv_cmd, ":DATE") == 0) - { - char msg[] = "No date"; - mq_cmd *privcmd = mq_cmd_create(mq_cmd_id(recv_cmd),"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 if (mq_cmd_o_cmp_param(recv_cmd, ":UPTIME") == 0) - { - char msg[] = "Forever"; - mq_cmd *privcmd = mq_cmd_create(mq_cmd_id(recv_cmd),"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg)); - - PRINT("%s\n", privcmd->buf); - - int id = mq_cmd_id(recv_cmd); - PRINT("%d\n",id); - - 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, ":HELLO") == 0) - { - char msg[] = "PONG TO HELLO"; - mq_cmd *privcmd = mq_cmd_create(mq_cmd_id(recv_cmd),"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg)); - - PRINT("%s\n", privcmd->buf); - - int id = mq_cmd_id(recv_cmd); - PRINT("%d\n",id); - - 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(mq_cmd_id(recv_cmd),"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg)); - - char *cmdBuf = mq_cmd_buf(privcmd); - mq_ntf_write(mq, MQ_IN, cmdBuf, strlen(cmdBuf)); - - mq_cmd_free(privcmd); - - } - */ - - //check if command callback excists if (recv_cmd_param!=NULL) { if (tbl_exec_in_s(etbl, recv_cmd_param+1)>=0) //there is ':' in front of command, check if ptr not null otherwise die { - + void *ret_msg = NULL; + + if (tbl_exec_run(etbl, recv_cmd_param+1, &ret_msg)>=0) + { + if (ret_msg!=NULL) + { + mq_cmd *privcmd = mq_cmd_create( + mq_cmd_id(recv_cmd), + "PRIVMSG", + strlen("PRIVMSG"), + ret_msg, + strlen(ret_msg) + ); + + char *cmdBuf = mq_cmd_buf(privcmd); + mq_ntf_write(mq, MQ_IN, cmdBuf, strlen(cmdBuf)); + + mq_cmd_free(privcmd); + } + } else + { + PRINT("Command execution error\n"); + } } else { PRINT("Command not found\n"); diff --git a/tbl_qcmd.c b/tbl_qcmd.c index deef61f..3071999 100644 --- a/tbl_qcmd.c +++ b/tbl_qcmd.c @@ -70,6 +70,42 @@ int tbl_exec_in_s(tbl_exec *tbl, char *cmd) return -1; } +int tbl_exec_run(tbl_exec *tbl, char *cmd, void **resp) +{ + int ret=-1; + + int i; + tble_exec *el=NULL; + + if ((tbl == NULL) || (cmd == NULL) || (resp==NULL) || (*resp != NULL)) + { + PERM(); + return -1; + } + + for (i=0;isize;i++) + { + el = tbl->cmd[i]; + if (strncmp(el->cmd, cmd, strlen(el->cmd))==0) + { + void *ret_resp=NULL; + + //execute callback + PRINT("EXEC CLB ID %d\n",i); + ret_resp = el->callback(NULL); //no params for now + + //return + if (ret_resp != NULL) + { + *resp = ret_resp; + ret = i; + } + } + } + + return ret; +} + tble_exec *tbl_exec_search_cmd(tbl_exec *tbl, tble_qcmd *cmd) { tble_exec *ret = NULL; diff --git a/tbl_qcmd.h b/tbl_qcmd.h index 5f66386..43357e5 100644 --- a/tbl_qcmd.h +++ b/tbl_qcmd.h @@ -173,6 +173,17 @@ Output: */ int tbl_exec_in_s(tbl_exec *tbl, char *cmd); +/* +Input: + tbl - table of executed commands + cmd - command to execute + resp - response from command, just string +Output: + >=0 - index in list of commands + -1 - somethign whent wrong +*/ +int tbl_exec_run(tbl_exec *tbl, char *cmd, void **resp); + /* if command is found then return pointer to it -- cgit v1.2.3