diff options
Diffstat (limited to 'agni.c')
-rw-r--r-- | agni.c | 153 |
1 files changed, 147 insertions, 6 deletions
@@ -44,6 +44,7 @@ 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; @@ -546,7 +547,7 @@ int recv_mq_cmd(mq_ntf_mdt *mq, |--------|--OUT-->|---------| */ -#define STACK_SIZE (1024*16) +#define STACK_SIZE (1024*1024) /* not supposed to be changed. */ typedef struct server_cfg { @@ -559,9 +560,9 @@ typedef struct server_cfg /* irc server config */ char *user; char *password; - char *server; + char *server; //should be changed to hostname? char **channels; - int port; + char *port; int ssl; } server_cfg; @@ -576,6 +577,7 @@ typedef struct server_cfg #define TH_STATE_TERMINATION 6 #define TH_STATE_EXIT 7 +#define TH_CONN_BUF_SZ 1024 int th_start_client(void *data) { int cmd_id = 1; @@ -593,6 +595,18 @@ int th_start_client(void *data) char *out_buf = NULL; char *in_buf = NULL; + //network creation var + int cret = -1; + int conn=-1; + char conn_buf[TH_CONN_BUF_SZ]; + char cmd_buf[TH_CONN_BUF_SZ]; + + //irc parsting + irc_buf *ib = NULL; + irc_token *itok = NULL; + char *irc_line = NULL; + + atomic_fetch_add(&cfg->running,1); printf("Start client\n"); printf("Server %d\n",cfg->tid); @@ -619,35 +633,79 @@ int th_start_client(void *data) } in_buf = malloc(in_attr.mq_msgsize); + PNL(); + //create irc parsing structures + memset(conn_buf, 0, TH_CONN_BUF_SZ); + memset(cmd_buf, 0, TH_CONN_BUF_SZ); + PNL(); + ib = irc_buf_create(); + PNL(); + if (ib == NULL) + { + PNL(); + PERM(); + } + PNL(); + PRINT("SERVER:%s PORT:%s USER:%s\n", cfg->server, cfg->port, cfg->user); + //conn = irc_connect(cfg->server, cfg->port); + conn = irc_connect("irc.freenode.net", cfg->port); + PNL(); + if (conn < 0) + { + PNL(); + printf("cant connect to server\n"); + //well we dont whant to just exit from thread + //lets put inside main thread CONNection checker + //if no connection just send some commmand and create + //some logic how to deal with it + atomic_fetch_sub(&cfg->running,1); + return 0; + } + PNL(); + //send some commands to irc to register nick + snprintf(cmd_buf, TH_CONN_BUF_SZ, "USER %s 0 0 :%s\r\n", cfg->user, cfg->user); + write(conn, cmd_buf, strlen(cmd_buf)); + snprintf(cmd_buf, TH_CONN_BUF_SZ,"NICK %s \r\n", cfg->user); + write(conn, cmd_buf, strlen(cmd_buf)); + //send command wait for response - + printf("Start loop\n"); run = 1; while (run) { + //Collect events from MQ printf("Client loop tick\n"); mq_event = mq_ntf_select(mq, MQ_IN); switch(mq_event) { - case 0: + case 1: PRINT("EVENT 0\n"); break; - case 1: + case 0: PRINT("EVENT 1\n"); + PNL(); if (mq_ntf_read(mq, MQ_IN, in_buf, in_attr.mq_msgsize) == -1) { + PNL(); printf("Cant read input message \n"); } else { + PNL(); in_buf[in_attr.mq_msgsize-1] = 0x0; printf("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 if (mq_event == 1) { PNL(); @@ -661,6 +719,83 @@ int th_start_client(void *data) } } } + PNL(); + //recieve commands from queue + + //recieve irc commands and pass to MQ + //fix this by allowing drain just chunks, not whole buffer + PNL(); + if((cret = read(conn,conn_buf, 128))>0) + { + printf("IN>>%s",conn_buf); + irc_buf_puts(ib, conn_buf, cret); + if (irc_buf_ready(ib) == 1) + { + PNL(); + irc_line = irc_buf_line(ib); + if (irc_line) + { + printf("PARSE>>%s\n",irc_line); + if (memcmp(irc_line,"PING",4)==0) + { + write(conn,"PONG",strlen("PONG")); + printf("OUT>>PONG\n"); + } + + + PNL(); + itok = irc_parse(irc_line,strlen(irc_line)); + PNL(); + if (itok!=NULL) + { + int j; + for (j=0;j<token_len(itok);j++) + { + printf("TK:[%s]\n",itok->tk_list[j]->token); + } + + if (token_cmp(itok,1,"NOTICE")==1) + { + int fret; + fret = write(conn,"NOTICE",strlen("NOTICE")); + printf("OUT<<Send %d\n",fret); + } + if (token_cmp(itok,1,"PING")==1) + { + int fret; + fret = write(conn,"PONG",strlen("PONG")); + printf("OUT<<Send %d\n", fret); + } + if (token_cmp(itok,1,"PRIVMSG") == 1) + { + int fret,fret2; + char send_back[1024]; + + memset(send_back,0,1024); + char *uname = token_new_str(itok,0); + char *msg = token_new_str(itok,3); + + char *sep = strchr(uname,'!'); + uname[sep-uname] = 0x0; + + fret2=snprintf(send_back,1024,":%s PRIVMSG %s %s\r\n",cfg->user,uname,msg); + printf("FORMATED [%s]",send_back); + fret = write(conn,send_back,fret2); + + free(msg); + free(uname); + printf("OUT<<Send %d\n", fret); + } + PNL(); + token_destroy(itok); + } + free(irc_line); + irc_line = NULL; + } + } + } + + //send command over queue } @@ -782,7 +917,9 @@ int th_event_manager(void *data) //applay to recieved command executor //other command pass to cmd/execution matching table + //pass to exec handler + //get response if command is immidieate otherwise wait for response in next execution @@ -825,6 +962,10 @@ int main(int argc, char **argv) irc_server_conf *isrvc = &server_list[i]; srvc->tid = i; srvc->stack = malloc(STACK_SIZE); //NULL will brake everything ;) + if (srvc->stack == NULL) + { + ERROR("BLow"); + } srvc->user = isrvc->user; srvc->password = isrvc->password; srvc->server = isrvc->server; |