summaryrefslogtreecommitdiff
path: root/agni.c
blob: 9d3b37d2a5d5a0faa6633579b0be48baf2b7fe6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
#define _GNU_SOURCE

#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/syscall.h>
#include <sched.h>
#include <stdatomic.h>

#include <sys/stat.h>
#include <sched.h>
#include <mqueue.h>

#include "config_servers.h"
#include "config_cmds.h"

#include "darray.h"
#include "buf.h"
#include "mq_cmd.h"
#include "mq_ntf.h"
#include "tbl_qcmd.h"
#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
nothing else
*/

#define MQ_MSG_SIZE 8192

//HACK
extern int __irc_buf_drain_io(irc_buf *ib);

/*
Wrap call back, to manage how going be executed callback.
Manage input and output params for callback.
INPUT(string)->OUTPUT(string)
*/
tble_cmd_resp* cllbk_wrapper( void *(*call)(void *), tble_cmd_param *param)
{
	void *data_out = NULL;
	tble_cmd_resp *resp = NULL;
	char *data=NULL;

	if (call == NULL)
	{
		PERM();
		return NULL;
	}

	if (param == NULL)
	{
		PERM();
		return NULL;
	}

	//prepare response 
	resp = tbl_cmd_resp_c(param);
	if (resp == NULL)
	{
		PERM();
	}
	tbl_cmd_resp_print(resp);
	data = alloc_new_str(param->param);

	//call callback
	data_out = call(data);

	//set result to returned response
	if (data_out == NULL)
	{
		//response doesnt have any output
		if (resp)
		{
			//response dont have result
			resp->ret_code = TBL_RSP_NORESP;
			resp->resp = NULL;
		}
	} else
	{
		if (resp)
		{
			//set succesfull repsonse
			resp->ret_code = TBL_RSP_OK;
			resp->resp = alloc_new_str(data_out);
		}
	}

	//dangerouse place =P
	free(data_out);
	free(data);

	return resp;
}

/*
return unique ID, with atomic counter should work in all cases
*/
_Atomic int _glbl_id=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) \
	send_mq_cmd(MQ,MQ_OUT,ID,CMD,strlen(CMD),PARAM,strlen(PARAM));
#define RECV_CMD_IN(MQ,CMD) \
	recv_mq_cmd(MQ,MQ_IN,CMD);
#define RECV_CMD_OUT(MQ,CMD) \
	recv_mq_cmd(MQ,MQ_OUT,CMD);

/*

|--------|<--IN---|---------|
| SERVER |        | MANAGER |
|--------|--OUT-->|---------|

*/
#define STACK_SIZE (1024*128)
/* not supposed to be changed. */
typedef struct server_cfg
{
	/* thread params */
	int tid;
	void *stack;
	//atomic_int running;
	_Atomic int running;
	mq_ntf_mdt *mq;
	/* irc server config */
	char *user;
	char *password;
	char *server; //should be changed to hostname?
	char **channels;
	char *port;
	int ssl;
} server_cfg;

/*******************************************************************************
server thread
*******************************************************************************/
/* server_cfg struct as input */
#define TH_STATE_INIT        0
#define TH_STATE_START       1
#define TH_STATE_LISTEN_IN   2
#define TH_STATE_LISTEN_OUT  3
#define TH_STATE_SEND_IN     4
#define TH_STATE_SEND_OUT    5
#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;
	int err;
	//mq_cmd *qcmd=NULL; //queue command
	int run;
	int mq_event;
	int byte_num;

	server_cfg *cfg = data;
	mq_ntf_mdt *mq = cfg->mq;
	mq_cmd *recv_cmd = NULL; // for recieved cmd from mq
	struct mq_attr out_attr, *ptr_out_attr=&out_attr;
	struct mq_attr in_attr, *ptr_in_attr=&in_attr;
	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;

	//table to match response/request
	tble_qcmd *qcmd = NULL;
	tbl_qcmd *qtbl = NULL;

	//create response table
	qtbl = tbl_qcmd_c(100);
	if (qtbl == NULL)
	{
		PERM();
	}

	atomic_fetch_add(&cfg->running,1);
	printf("Start client\n");
	printf("Server %d\n",cfg->tid);
	sleep(1);

	//PRINT("SERVER:%s PORT:%s USER:%s\n", cfg->server, cfg->port, cfg->user);

	//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", "6667");
	//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));

	//prepare message queue
	mq = cfg->mq;
	if (mq_ntf_getattr(mq, MQ_OUT, &ptr_out_attr) == -1)
	{
		printf("Cant get attribute\n");
		ENL();
	}
	//PRINT("SERVER:%s PORT:%s USER:%s\n", cfg->server, cfg->port, cfg->user);
	out_buf = malloc(out_attr.mq_msgsize);
	memset(out_buf, 0, out_attr.mq_msgsize);
	if (out_buf == NULL)
	{
		ENL();
	}

	//PRINT("SERVER:%s PORT:%s USER:%s\n", cfg->server, cfg->port, cfg->user);
	if (mq_ntf_getattr(mq, MQ_IN, &ptr_in_attr) == -1)
	{
		printf("Cant get attribute\n");
		ENL();
	}
	in_buf = malloc(in_attr.mq_msgsize);
	memset(in_buf, 0, in_attr.mq_msgsize);
	if (in_buf == NULL)
	{
		ENL();
	}

	//PRINT("SERVER:%s PORT:%s USER:%s\n", cfg->server, cfg->port, cfg->user);
	//create irc parsing structures
	memset(conn_buf, 0, TH_CONN_BUF_SZ);
	memset(cmd_buf, 0, TH_CONN_BUF_SZ);
	ib = irc_buf_create();
	if (ib == NULL)
	{
		PERM();
		//return 0;
	}
	
	//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:
			//PRINT("EVENT 0\n");
			break;
		case 1:
			PRINT("MQ_EVENT IN\n");
			if ((byte_num = mq_ntf_read(mq, MQ_IN, in_buf, in_attr.mq_msgsize)) == -1)
			{
				printf("Cant read input message \n");
			} else
			{
				in_buf[byte_num] = 0x0;
				PRINT("Recieve %s\n", in_buf);
			}
			break;
		default:
			printf("Unknown event type\n");
		}

		//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)
			{
				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("ID:%d Recieved response [%s]%zu\n",mq_cmd_id(recv_cmd), paramPtr, paramSz);
				
					//wrong 
					tble_cmd_resp *match_resp = malloc(sizeof(tble_cmd_resp));
					memset(match_resp, 0, sizeof(tble_cmd_resp));
					if (match_resp != NULL)
					{
						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
						match_resp->resp = alloc_new_str_s(paramPtr, paramSz);
						
						//check if command match
						int cmd_id = tbl_qcmd_resp(qtbl, match_resp);
						if (cmd_id > 0)
						{
							tble_qcmd *cmd = tbl_qcmd_match_id(qtbl, cmd_id);
							if (cmd != NULL)
							{

								PRINT("Found ID in qcmd table %d\n", cmd_id);
								char *resp_user = token_new_str(cmd->tok,0);
								char *dest_user = token_new_str(cmd->tok,2);
								char *sep = strchr(resp_user,'!');
								resp_user[sep-resp_user] = 0x0;

								memset(cmd_buf, 0, TH_CONN_BUF_SZ);

								if (dest_user[0] == '#')
								{
									//response for chan PRIVMSG
									//prepare response
									int fret = snprintf(cmd_buf, TH_CONN_BUF_SZ,":%s PRIVMSG %s :%s \r\n", cfg->user, dest_user, match_resp->resp);
									PRINT("%s\n",cmd_buf);
									write(conn, cmd_buf, fret);
								}
								else
								{
									//response for user PRIVMSG

									//prepare response
									int fret = snprintf(cmd_buf, TH_CONN_BUF_SZ,":%s PRIVMSG %s :%s \r\n", dest_user, resp_user, match_resp->resp);
									PRINT("%s\n",cmd_buf);
									write(conn, cmd_buf, fret);
								}

								free(dest_user); dest_user = NULL;
								free(resp_user); resp_user = NULL;

								//remove command by id
								//for now just remove command without checkings its state
								tbl_qcmd_del_by_id(qtbl, cmd_id); //no error check

								//NOT BEST PLACE WHERE TO CALL THIS FUNCTION
								//should be added proper command state check and so on
								tbl_qcmd_mng_states(qtbl); // This should be runed regulary
							}

						}

						tbl_cmd_resp_free(match_resp);
					}

					//lets memleak here

				} else
				{
					PRINT("Unknown command\n");
				}
			}
			mq_cmd_free(recv_cmd);
			recv_cmd = NULL;
			
		}
		

		//recieve irc commands and pass to MQ, save command to table
		//fix this by allowing drain just chunks, not whole buffer
		//memset(conn_buf, 0, TH_CONN_BUF_SZ);		
		if((cret = read(conn,conn_buf, 256))>0)
		{
			irc_buf_puts(ib, conn_buf, cret);
			while (irc_buf_ready(ib) == 1)
			{
				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");
					}
					
					itok = irc_parse(irc_line,strlen(irc_line));
					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)
						{

						}
						if (token_cmp(itok,1,"PING")==1)
						{
							int fret;
							fret = write(conn,"PONG",strlen("PONG"));
							printf("OUT<<Send %d\n", fret);
						}
						//something is sended to cbot
						if (token_cmp(itok,1,"PRIVMSG") == 1)
						{
							int fret,fret2;
							//char send_back[128];

							PNL();
							
							memset(cmd_buf,0,128);
							char *uname = token_new_str(itok,0);
							char *dest_name = token_new_str(itok,2);
							char *msg = token_new_str(itok,3); //could send without first symbol pal
							
							char *sep = strchr(uname,'!');
							uname[sep-uname] = 0x0;

							PRINT()
							//if (token_cmp(itok,3,":join") == 1)
							if (strncmp(":join",msg,5)==0)
							{
								PNL();
								//lets check if one more argument is there
								
								{
									PNL();
									char *sep_chan = strchr(msg,' ');

									//lets join channel
									memset(cmd_buf, 0, TH_CONN_BUF_SZ);
									int fret = snprintf(cmd_buf, TH_CONN_BUF_SZ, ":%s JOIN %s\r\n", dest_name, sep_chan);
									PRINT("cmd_buf %s\n", cmd_buf);
									write(conn, cmd_buf, fret);
									
								}
							} else
							{
								//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 cmd table command and add to table
								qcmd = tbl_qcmd_cmd_c();
								if (qcmd != NULL)
								{
									qcmd->cid = uniq_id();
									qcmd->state = QCMD_NONE;
									qcmd->cmd = alloc_new_str("PRIVMSG");
									qcmd->param = alloc_new_str(msg);
									qcmd->timestamp = time(NULL);
									tbl_qcmd_add_tok(qcmd, itok);
									//HOW? add commands that are in execution list, dont process all messages
									tbl_qcmd_add(qtbl, qcmd);
									tbl_qcmd_print_tbl(qtbl,TBL_PF_QCMD_ID
										|TBL_PF_QCMD_CID
										|TBL_PF_QCMD_STATE
										|TBL_PF_QCMD_CMD
										|TBL_PF_QCMD_PARAM
										|TBL_PF_QCMD_TID
										|TBL_PF_QCMD_TIDX);
								}

								//create command and send to mq
								mq_cmd *privcmd = mq_cmd_create(qcmd->cid,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg));
								char *bufPtr;
								bufPtr = mq_cmd_buf(privcmd);
								mq_ntf_write(mq, MQ_OUT, bufPtr, strlen(bufPtr));
								mq_cmd_free(privcmd);

								//for safety make null, as no one should use it anymore
								qcmd = NULL;
							}


							free(msg);
							free(uname);
							//printf("OUT<<Send %d\n", fret);
							
						}
						token_destroy(itok);
						itok = NULL;
					}
					FREE(irc_line);
					__irc_buf_drain_io(ib);
				}
				
			}	
		}

		//send command over queue
	}



	printf("End client\n");
	PNL();
	atomic_fetch_sub( &cfg->running,1);

	return 0;
}


#define EVENT_HND_STACK_SIZE (1024*1024)
typedef struct event_handler_cfg {
	/* thread params*/
	void *stack;
	mq_ntf_mdt *mq_listen;
	int mq_num;
	_Atomic int running;

} event_handler_cfg;

/* server_cfg struct as input */
#define EH_STATE_INIT        0
#define EH_STATE_START       1
#define EH_STATE_LISTEN_IN   2
#define EH_STATE_LISTEN_OUT  3
#define EH_STATE_SEND_IN     4
#define EH_STATE_SEND_OUT    5
#define EH_STATE_TERMINATION 6
#define EH_STATE_EXIT        7


/*******************************************************************************
Event thead. Recieve all events from server thread and pass them for execution.
Return results from execution to server thread.
*******************************************************************************/
/* Thread to reacieve messages and return them back */
int th_event_manager(void *data)
{
	event_handler_cfg *cfg = data;
	atomic_fetch_add(&cfg->running,1);
	mq_ntf_mdt *mq=NULL;
	char *out_buf = NULL;
	int run;
	int mq_event;

	//read any command
	mq_cmd *recv_cmd = NULL; // for recieved cmd from mq
	struct mq_attr out_attr, *ptr_out_attr=&out_attr;

	//execution and command table generation
	//tble_exec *ecmd = 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();
	}

	//load predefined/precompiled command list
	{
		single_cmd_def *single_cmd = confgi_cmd_list;

		while ((single_cmd!=NULL)&&(single_cmd->name!=NULL))
		{
			tble_exec *ecmd = NULL;

			ecmd = tbl_exec_c();
			if (ecmd == NULL)
			{
				PERM();
			}

			ecmd->id = uniq_id();
			ecmd->name = alloc_new_str("local-executor");
			ecmd->cmd = alloc_new_str(single_cmd->name);
			ecmd->callback = single_cmd->callback;

			if (-1 == tbl_exec_add(etbl, ecmd))
			{
				PERM();
			}

			single_cmd++;
		}
	}

	tbl_exec_print_tbl(etbl, TBL_PF_EXEC_ALL);

	//create command table
	qtbl = tbl_qcmd_c(10);//well 10 should be ok right?

	//config mq
	printf("Start event thread\n");
	mq = cfg->mq_listen;

	//get mq attributes
	if (mq_ntf_getattr(mq, MQ_OUT, &ptr_out_attr) == -1)
	{
		printf("Cant get attribute\n");
	}

	out_buf = malloc(out_attr.mq_msgsize);
	//maybe its not null

	printf("Start event loop\n");
	run = 1;
	while(run)
	{
		//check if there is some message and save it to buffer
		run += 1;
		mq_event = mq_ntf_select(mq, MQ_OUT);
		switch(mq_event)
		{
		case 0:
			//PRINT("EVENT 0\n");
			break;
		case 1:
			PRINT("MQ_EVENT OUT\n");
			if (mq_ntf_read(mq, MQ_OUT, out_buf, out_attr.mq_msgsize) == -1)
			{
				printf("Cant read output message\n");
			} else
			{
				out_buf[out_attr.mq_msgsize-1] = 0x0;
				printf("Recieve %s\n", out_buf);
			}
			break;
		default:
			printf("Unknown event type\n");
		}

		//if QUIT then quit the thread
		if (mq_event == 1)
		{
			PNL();

			recv_cmd = mq_cmd_creates(out_buf, out_attr.mq_msgsize, 1);
			//PNL();
			if (recv_cmd != NULL)
			{

				char *recv_cmd_param=NULL;
				size_t recv_cmd_param_sz=-1;

				PNL();
				PRINT("MQ:CMD %s\n", mq_cmd_buf(recv_cmd));


				//get command fields
				mq_cmd_param(recv_cmd, &recv_cmd_param, &recv_cmd_param_sz);
				
				

				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,"PING") == 0)
				{
					printf("Its fine im working\n");

					//create command and send to mq
					char msg[] = "PONG";
					mq_cmd *privcmd = mq_cmd_create(mq_cmd_id(recv_cmd),"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg));
						
					mq_cmd_free(privcmd);
				} else if (mq_cmd_o_cmp_cmd(recv_cmd,"PRIVMSG") == 0)
				{
					//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");
						}
					}

				} 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
		
		//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


	}
	free(out_buf);
	PNL();
	printf("End event thread\n");

	atomic_fetch_sub( &cfg->running,1);
	return 0;
}

/*******************************************************************************
Main code entry code. Init threads. Give basic params to them. And then become
as a watch dog.
*******************************************************************************/
int main(int argc, char **argv)
{
	int i;

	int cnt_servers,cnt_running;
	server_cfg *cfg_list;
	event_handler_cfg *evhnd_cfg;
	mq_ntf_mdt *mq_array;

	/*set atomic variables to init value*/
	atomic_store(&_glbl_id, 0);

	/* Load configuration */
	cnt_servers = SIZEOF_SERVER_LIST;
	for (i=0;i<SIZEOF_SERVER_LIST;i++)
	{
		printf("Load config for server %s\n",server_list[i].server);
	}

	cfg_list = malloc(sizeof(server_cfg)*cnt_servers);
	if (cfg_list == NULL)
	{
		ENL();
	}
	mq_array = malloc(sizeof(mq_ntf_mdt)*cnt_servers);
	if (mq_array == NULL)
	{
		ENL();
	}

	/* For each configuration create listener */

	for (i=0;i<SIZEOF_SERVER_LIST;i++)
	{
		/*initialise server config*/
		server_cfg *srvc = cfg_list+i;
		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;
		srvc->channels = isrvc->channels;
		srvc->port = isrvc->port;
		srvc->ssl = isrvc->ssl;

		//atomic_init( &srvc->running, 1);
		atomic_store(&srvc->running, 0);

		/* initalise posix mq */
		if (0 != mq_ntf_open(&mq_array[i], i))
		{
			printf("Couldnt open mq_ntf_open\n");
		}
		srvc->mq = &mq_array[i];
		//try to drain mq
		mq_ntf_drain(&mq_array[i]);

		PRINT("SERVER:%s PORT:%s USER:%s\n", srvc->server, srvc->port, srvc->user);

		/* clone new proc */
		clone(th_start_client, srvc->stack+STACK_SIZE, CLONE_VM|CLONE_FILES, (void *)srvc);
	}

	/* event handler thread */
	evhnd_cfg = malloc(sizeof(event_handler_cfg));
	memset(evhnd_cfg, 0, sizeof(event_handler_cfg));
	evhnd_cfg->stack = malloc(EVENT_HND_STACK_SIZE);
	atomic_store(&evhnd_cfg->running, 0);
	evhnd_cfg->mq_num = cnt_servers;
	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();
	/* wait a sec while all threads will start running */
	sleep(1);
	/* run until all threads are up */
	cnt_running = 1;
	while(cnt_running != 0)
	{
		//printf("Count\n");
		/*count how many proceses is running there*/
		int val;
		cnt_running = 0;
		for (i=0;i<cnt_servers;i++)
		{
			val = atomic_load(&cfg_list[i].running);
			if (val != 0)
				cnt_running += 1;
		}
		//PRINT("cnt_running %d\n",cnt_running);
		sleep(1);
	}

	free(cfg_list);

	ERROR("Exit\n");

	return 0;
}