aboutsummaryrefslogtreecommitdiffstats
path: root/mini_irc/mini_irc.c
blob: 326c9e887ae0e10e935393f85042b1d8e248c90b (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
#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>

static char *nick = "cbot_git";
static char *hostname = "irc.freenode.net";
static char *port = "6667";
static char *channel = "#mainlv";

static int conn;

#define IO_STR str_cat_buf

#define SYSC_WRITE(FD,X) write(FD,X,strlen(X));
#define SYSC_WRITE_O(FD,STR) write(FD,(STR).str,(STR).sz);write(0,"<< ",4);write(0,(STR).str,(STR).sz);
#define SYSC_WRITE_I(FD,STR) write(FD,(STR).str,(STR).sz);write(0,">> ",4);write(0,(STR).str,(STR).sz);
#define SYSC_WRITE_IO0(FD) write(FD,(IO_STR).str,(IO_STR).sz);write(0,"<< ",4);write(0,(IO_STR).str,(IO_STR).sz);

#define BUF_0(BUF)          (BUF).sz=0;
#define BUF_ADD_STR(BUF,X)  str_cat(BUF,X);
#define BUF_ADD_CHAR(BUF,C) {(BUF).str[(BUF).sz]=C;(BUF).sz+=1;}
#define BUF_CHAR(BUF,IDX)   (BUF).str[IDX]
#define BUF_SZ(BUF)         (BUF).sz
#define BUF_MAX(BUF)        ((BUF).sz==BUF_MAX_SZ)
#define BUF_SET0(BUF)       (BUF).str[(BUF).sz]=0;
#define BUF_STRCMP(BUF,STR) (!strncmp((BUF).str,STR,strlen(STR)))

#define STR_0() (IO_STR).sz=0;
#define STR_ADD(X) str_cat((&IO_STR),X);



#define BUF_MAX_SZ 1024
typedef struct pos_str
{
	int sz;
	char str[BUF_MAX_SZ+1];
} pos_str;

pos_str str_cat_buf;
pos_str str_buf;
pos_str serv_read;
pos_str serv_write;

#define TOK_CMP(IDX,STR) ((strlen(STR)==strlen(tok[IDX].s)) && !(strncmp(tok[IDX].s,STR,strlen(STR))))
#define TOK_NUM 20
typedef struct token
{
	char *s, *e;
} token;
static int tok_cnt=0;
token tok[TOK_NUM];

char* skip(char *s, char c) 
{
	while(*s != c && *s != '\0')
	{
		s++;
	}
	if(*s != '\0')
	{
		*s++ = '\0';
	}
	return s;
}

int str_cat(pos_str *buf, char *str)
{
	int i;
	int pos=0;
	int len = strlen(str);

	pos = buf->sz;
	for (i=0;(i<len)&&(pos+i<BUF_MAX_SZ);i++)
	{
		buf->str[pos+i] = str[i];
	}
	buf->sz = pos+i;

	return pos;
}

int print_str(int fd, char *str)
{
	int ret = 0;
	int len = strlen(str);

	write(fd, str, len);

	return ret;
}

/*
return fd!=ifconnection there
*/
int irc_connect( char *hostname, char *port )
{
	int fd=0;

	struct addrinfo serv, *res;

	memset(&serv, 0, sizeof(serv));
	serv.ai_family = AF_INET;
	serv.ai_socktype = SOCK_STREAM;
	getaddrinfo(hostname, port, &serv, &res);
	fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
	connect(fd, res->ai_addr, res->ai_addrlen);

	return fd;
}



int main()
{
	int i,j,m,len;
	int ret;
	char ch;
	int wordcount;
	char *user, *command, *where, *message, *iter, *start, *sep;
	print_str(0,"Start programm\n");
	
	//connect to server
	conn = irc_connect( hostname, port );
	if (conn<1)
	{
		SYSC_WRITE(1,"Cannot connect\n");
		return -1;
	}

	//send USER command
	STR_0();
	STR_ADD("USER ");
	STR_ADD(nick);
	STR_ADD(" 0 0 :");
	STR_ADD(nick);
	STR_ADD("\r\n");
	SYSC_WRITE_IO0(conn);

	//send NICK command
	STR_0();
	STR_ADD("NICK ");
	STR_ADD(nick);
	STR_ADD("\r\n");
	SYSC_WRITE_IO0(conn);

	BUF_0(str_buf);
	//server communication loop
	while (ret=read(conn, serv_read.str, BUF_MAX_SZ))
	{
		serv_read.sz = ret;
		serv_read.str[ret];
		//SYSC_WRITE_I(0,serv_read);

		//parse to get commands
		for (i=0;i<BUF_SZ(serv_read);i++)
		{
			BUF_ADD_CHAR(str_buf,BUF_CHAR(serv_read,i));
			ch = BUF_CHAR(serv_read,i);
			if ((i>0 
				&& (BUF_CHAR(serv_read,i) == '\n') 
				&& (BUF_CHAR(serv_read,i-1) == '\r')) 
				|| 
				(BUF_MAX(str_buf)))
			{
				BUF_SET0(str_buf);
				len = BUF_SZ(str_buf);
				//printf("len = %d\n",len);
				SYSC_WRITE_I(0,str_buf);
				
				//BUF_0(str_buf);
				//check if ping then answer to ping
				//if (BUF_STRCMP(str_buf,"PING"))
				//{
				//	STR_0();
				//	STR_ADD("PONG\r\n")
				//	SYSC_WRITE_IO0(conn);
				//} else 
				if (BUF_CHAR(str_buf,0) == ':')
				{
					#define START 1
					#define START_WORD 2
					#define CONTINUE 3
					#define MESSAGE 4
					#define END 5
					int state = START;
					//printf("TOKENIZING\n");
					//tokenize string
					tok_cnt = 0;
					iter = str_buf.str;

					for (j=0;j<len;j++)
					{

						
						switch(state)
						{
						case START:
							//printf("START %d %02x\n",j,iter[j]);
							if (iter[j]==' ')
							{
								break;
							}
							if (iter[j]=='\0')
							{
								state = END;
								break;
							}
							state = START_WORD;
							break;
						case START_WORD:
							//printf("WORD\n");
							tok[tok_cnt].s = iter+j;
							if (iter[j]==' ')
							{
								tok[tok_cnt].e = iter+j-1;
								tok_cnt += 1;
								state = START;
								break;
							}
							if (iter[j]=='\0')
							{
								tok[tok_cnt].e = iter+j-1;
								tok_cnt += 1;
								state = END;
								break;
							}
							if (iter[j]==':')
							{
								tok[tok_cnt].s = iter+j;
								//tok_cnt += 1;
								state = MESSAGE;
								break;
							}
							state = CONTINUE;
							break;
						case CONTINUE:
							//printf("CONTINUE\n");
							if ((iter[j]!=' ')&&(iter[j]!='\0'))
							{
								state = CONTINUE;
								break;
							}
							if (iter[j]=='\0')
							{
								tok[tok_cnt].e = iter+j-1;
								tok_cnt += 1;
								state = END;
								break;
							}
							if (iter[j]==' ')
							{
								tok[tok_cnt].e = iter+j-1;
								tok_cnt += 1;
								state = START_WORD;
								break;
							}
							//printf("Unknown state\n");
							break;
						case MESSAGE:
							//printf("MESSAGE\n");
							if ((iter[j]=='\0')||(iter[j]=='\n')||(iter[j]=='\r'))
							{
								tok[tok_cnt].e = iter+j-1;
								tok_cnt += 1;
								state = END;
								break;
							}
							state = MESSAGE;
							break;
						case END:
							//printf("END\n");
							break;
						default:
							printf("ERR STATE %d\n",state);
							return 0;
						};
						
					}
					if ((state==CONTINUE)||(state==MESSAGE))
					{
						tok[tok_cnt].e = iter+j-1;
						tok_cnt += 1;
					}
					for (j=0;j<tok_cnt;j++)
					{
						write(0,tok[j].s,tok[j].e-tok[j].s+1);
						*(tok[j].e+1)=0x0;
						write(0,"]\n",2);
					}
					if (tok_cnt < 2 ) continue;

					//replay on commands
					printf("CMD: %s\n",tok[1].s);
					if (TOK_CMP(1,"NOTICE?"))
					{
						STR_0();
						STR_ADD(tok[1].s);//command
						STR_ADD(" ");
						STR_ADD(tok[2].s)
						STR_ADD(" ");
						STR_ADD(tok[3].s);//message
						STR_ADD("\r\n");
						SYSC_WRITE_IO0(conn);
					} else if (TOK_CMP(1,"PING"))
					{
						STR_0();
						STR_ADD("PONG :");
						STR_ADD(tok[0].s);
						STR_ADD("\r\n");
						SYSC_WRITE_IO0(conn);
					} else if (TOK_CMP(1,"PRIVMSG"))
					{
						//join command
						if (TOK_CMP(3,":JOIN"))
						{
							STR_0();
							STR_ADD("JOIN ");
							STR_ADD(channel)
							STR_ADD("\r\n");
							SYSC_WRITE_IO0(conn);
						//echo
						} else
						{
							//if there is chan name then ech to chan
							if (tok[2].s[0]=='#')
							{
								STR_0();
								STR_ADD(tok[1].s);//command
								STR_ADD(" ");
								STR_ADD(tok[2].s);
								STR_ADD(" :");
								STR_ADD(tok[3].s+1);
								STR_ADD("\r\n");
								SYSC_WRITE_IO0(conn);
							} else
							{
								//get user name
								sep = strchr(tok[0].s,'!');
								tok[0].s[sep-tok[0].s] = 0x0;
								STR_0();
								STR_ADD(tok[1].s);//command
								STR_ADD(" ");
								STR_ADD(tok[0].s)
								STR_ADD(" :");
								//STR_ADD(tok[2].s);
								//STR_ADD("-");
								STR_ADD(tok[3].s+1);//message
								STR_ADD("\r\n");
								SYSC_WRITE_IO0(conn);
							}
						}
					}

				}

				BUF_0(str_buf);
			}
		}
	}
	

	print_str(0,"End program\n");

	return 0;
}