summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2017-03-18 20:30:06 +0000
committerFreeArtMan <dos21h@gmail.com>2017-03-18 20:30:06 +0000
commit0ae9dc3dfe4b1bd24a0d0385480d9ad7d07d0976 (patch)
treef51cbf81033fd9f87373c27c0c81d6b6c59396e9
parent39c0045fbb2f511484d5effb69c987906d0b52d8 (diff)
downloadagni-0ae9dc3dfe4b1bd24a0d0385480d9ad7d07d0976.tar.gz
agni-0ae9dc3dfe4b1bd24a0d0385480d9ad7d07d0976.zip
Basic response from 2 command, works for privatemessages
-rw-r--r--agni.c48
-rw-r--r--mq_cmd.c15
-rw-r--r--tbl_qcmd.c30
-rw-r--r--tbl_qcmd.h35
-rw-r--r--util.c22
-rw-r--r--util.h2
6 files changed, 123 insertions, 29 deletions
diff --git a/agni.c b/agni.c
index be07089..08259ba 100644
--- a/agni.c
+++ b/agni.c
@@ -158,6 +158,7 @@ tble_cmd_resp* cllbk_wrapper( void *(*call)(void *), tble_cmd_param *param)
return unique ID, with atomic counter should work in all cases
*/
_Atomic int _glbl_id=0;
+/*
int uniq_id()
{
int ret=-1,id;
@@ -171,6 +172,7 @@ int uniq_id()
return ret;
}
+*/
@@ -350,7 +352,7 @@ int th_start_client(void *data)
tbl_qcmd *qtbl = NULL;
//create response table
- qtbl = tbl_qcmd_c(10);
+ qtbl = tbl_qcmd_c(100);
if (qtbl == NULL)
{
PERM();
@@ -483,25 +485,48 @@ int th_start_client(void *data)
mq_cmd_param(recv_cmd, &paramPtr, &paramSz);
printf("ID:%d Recieved response [%s]%d\n",mq_cmd_id(recv_cmd), paramPtr,paramSz);
- //wrong
+ //wrong
tble_cmd_resp *match_resp = malloc(sizeof(tble_cmd_resp));
memset(match_resp, 0, sizeof(tble_cmd_resp));
if (match_resp != NULL)
{
- PNL();
+ //PNL();
match_resp->qid = mq_cmd_id(recv_cmd);
//for now any type is ok, make time consistant, for this usage
//match_cmd->type = QCMD_NONE
- PNL();
+ //PNL();
match_resp->resp = alloc_new_str_s(paramPtr, paramSz);
- PNL();
+ //PNL();
//check if command match
int cmd_id = tbl_qcmd_resp(qtbl, match_resp);
if (cmd_id > 0)
{
- PNL();
+
PRINT("%d\n",cmd_id);
+ tble_qcmd *cmd = tbl_qcmd_match_id(qtbl, cmd_id);
+ PNL();
+ if (cmd != NULL)
+ {
+ int buf_sz = TH_CONN_BUF_SZ>128?128:TH_CONN_BUF_SZ;
+
+ PNL();
+ char *resp_user = token_new_str(cmd->tok,0);
+ char *sep = strchr(resp_user,'!');
+ resp_user[sep-resp_user] = 0x0;
+
+ PNL();
+ memset(cmd_buf, 0, buf_sz);
+ PNL();
+ //prepare response
+ int fret = snprintf(cmd_buf, buf_sz,":%s PRIVMSG %s :%s \r\n",cfg->user,resp_user,match_resp->resp);
+ PRINT("cmd_buf %s \n", cmd_buf);
+ write(conn, cmd_buf, fret);
+
+ free(resp_user);
+
+ }
+
}
tbl_cmd_resp_free(match_resp);
@@ -567,14 +592,16 @@ int th_start_client(void *data)
memset(cmd_buf,0,128);
char *uname = token_new_str(itok,0);
char *msg = token_new_str(itok,3);
+
+ PRINT("msg %s\n", msg);
char *sep = strchr(uname,'!');
uname[sep-uname] = 0x0;
//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);
+ //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 cmd table command and set into table
qcmd = tbl_qcmd_cmd_c();
@@ -595,7 +622,8 @@ int th_start_client(void *data)
|TBL_PF_QCMD_TIDX);
}
-
+ PRINT("msg %s\n", msg);
+
//create command and send to mq
mq_cmd *privcmd = mq_cmd_create(qcmd->cid,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg));
char *bufPtr;
diff --git a/mq_cmd.c b/mq_cmd.c
index 93e7ea8..610ee2e 100644
--- a/mq_cmd.c
+++ b/mq_cmd.c
@@ -23,7 +23,7 @@ mq_cmd* mq_cmd_create(int id, char *cmd, size_t cmd_sz, char *param, size_t para
ENL();
return NULL;
}
- //memset(ret, 0, sizeof(mq_cmd));
+ memset(ret, 0, sizeof(mq_cmd));
ret->id = id;
ret->sz = MQ_CMD_MSG_SIZE;
@@ -37,8 +37,17 @@ mq_cmd* mq_cmd_create(int id, char *cmd, size_t cmd_sz, char *param, size_t para
}
//if not null string that everything will blow up pal
//cmd[cmd_sz-1] = 0x0;
- //param[param_sz-1] = 0x0;
- snprintf(ret->buf,ret->sz-1,":%d:%s: %s", id, cmd, param);
+ //param[param_sz-1] = 0x0;
+ char *t_cmd = malloc(cmd_sz+1);
+ memcpy(t_cmd, cmd, cmd_sz);
+ t_cmd[cmd_sz]=0;
+ char *t_param = malloc(param_sz+1);
+ memcpy(t_param, param, param_sz);
+ t_param[param_sz]=0;
+ snprintf(ret->buf,ret->sz-1,":%d:%s: %s", id, t_cmd, t_param);
+
+ free(t_cmd);
+ free(t_param);
return ret;
}
diff --git a/tbl_qcmd.c b/tbl_qcmd.c
index b3b2a88..4af9de8 100644
--- a/tbl_qcmd.c
+++ b/tbl_qcmd.c
@@ -354,6 +354,35 @@ int tbl_qcmd_add_tok(tble_qcmd *tbl, irc_token *tk)
return 0;
}
+tble_qcmd* tbl_qcmd_match_id(tbl_qcmd *tbl, int idx)
+{
+ int i;
+
+ if (tbl == NULL)
+ {
+ PERM();
+ return NULL;
+ }
+
+ if (idx < 0)
+ {
+ PERM();
+ return NULL;
+ }
+
+ for (i=0;i<tbl->size;i++)
+ {
+ tble_qcmd *iter = tbl->cmd[i];
+
+ if (iter->id == idx)
+ {
+ return iter;
+ }
+ }
+
+ return NULL;
+}
+
int tbl_qcmd_resp(tbl_qcmd *tbl, tble_cmd_resp *resp)
{
int ret = -1;
@@ -492,6 +521,7 @@ int tbl_cmd_resp_print(tble_cmd_resp *resp)
return 0;
}
+//add to print token if its there
int tbl_qcmd_print_cmd(tble_qcmd *tbl, int flags)
{
if (tbl == NULL)
diff --git a/tbl_qcmd.h b/tbl_qcmd.h
index d28644e..acd3da7 100644
--- a/tbl_qcmd.h
+++ b/tbl_qcmd.h
@@ -173,19 +173,6 @@ Output:
*/
int tbl_exec_in_s(tbl_exec *tbl, char *cmd);
-/*
-add parsed token to command list
-Input:
- tbl - executed command
- tk - token, its going to be copies, so fill free to FREE
-Output:
- 0 - if everything whent ok
- -1 - if there was some kind of mistake
-*/
-int tbl_qcmd_add_tok(tble_qcmd *tbl, irc_token *tk);
-
-
-
/*
if command is found then return pointer to it
@@ -275,7 +262,27 @@ Output:
int tbl_qcmd_del(tbl_qcmd *tbl, int idx);
+/*
+add parsed token to command list
+Input:
+ tbl - executed command
+ tk - token, its going to be copies, so fill free to FREE
+Output:
+ 0 - if everything whent ok
+ -1 - if there was some kind of mistake
+*/
+int tbl_qcmd_add_tok(tble_qcmd *tbl, irc_token *tk);
+
+
+
+/*
+Return command if there is ont that matching this id
+Return
+ !NULL - pointer to table elemnt NONFREE
+ NULL - nothing found or error
+*/
+tble_qcmd* tbl_qcmd_match_id(tbl_qcmd *tbl, int idx);
/*
@@ -285,7 +292,7 @@ Input:
tbl - array of executed cmds
resp - response recieved, match it to table values
Output:
- >0 -
+ >0 - command
-1 - if there was some kind of mistake
*/
int tbl_qcmd_resp(tbl_qcmd *tbl, tble_cmd_resp *resp);
diff --git a/util.c b/util.c
index 20ad6d8..b0d8efe 100644
--- a/util.c
+++ b/util.c
@@ -1,5 +1,23 @@
#include "util.h"
+/*
+return unique ID, with atomic counter should work in all cases
+*/
+_Atomic static int _glbl_id=1;
+int uniq_id()
+{
+ int ret=-1,id;
+
+ //what possible could go wrong?
+ // sry emilsp this is not important for now
+ id = atomic_load(&_glbl_id);
+ ret = id;
+ id += 1;
+ atomic_store(&_glbl_id, id);
+ return ret;
+
+}
+
char *alloc_new_str_s(char *str, size_t size)
{
char *ret = NULL;
@@ -15,14 +33,14 @@ char *alloc_new_str_s(char *str, size_t size)
return NULL;
}
- ret = malloc(size+1);
+ ret = malloc(size+1); //extra for 1 zero at then end
if (ret == NULL)
{
return NULL;
}
memcpy(ret, str, size);
- ret[size] = 0;
+ ret[size] = 0; //add zero at the end
return ret;
}
diff --git a/util.h b/util.h
index fd6bbfd..ef50d2b 100644
--- a/util.h
+++ b/util.h
@@ -4,7 +4,9 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <stdatomic.h>
+int uniq_id();
char *alloc_new_str_s(char *str, size_t size);
char *alloc_new_str(char *str);