Subversion Repositories Okuu

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 nishi 1
/* $Id: ircfw.c 2 2024-09-09 13:22:05Z nishi $ */
2
 
3
#define IRCFW_SRC
4
#include "ircfw.h"
5
 
6
#include <stdbool.h>
7
#include <stdlib.h>
8
#include <string.h>
9
 
10
#include <sys/socket.h>
11
#include <netinet/tcp.h>
12
#include <arpa/inet.h>
13
 
14
struct ircfw_message ircfw_message;
15
 
16
char* ircfw_strcat(const char* a, const char* b) {
17
	char* str = malloc(strlen(a) + strlen(b) + 1);
18
	memcpy(str, a, strlen(a));
19
	memcpy(str + strlen(a), b, strlen(b));
20
	str[strlen(a) + strlen(b)] = 0;
21
	return str;
22
}
23
 
24
char* ircfw_strcat3(const char* a, const char* b, const char* c) {
25
	char* tmp = ircfw_strcat(a, b);
26
	char* str = ircfw_strcat(tmp, c);
27
	free(tmp);
28
	return str;
29
}
30
 
31
char* ircfw_strdup(const char* str) { return ircfw_strcat(str, ""); }
32
 
33
void ircfw_init(void) {
34
	ircfw_message.prefix = NULL;
35
	ircfw_message.params = NULL;
36
	ircfw_message.command = NULL;
37
}
38
 
39
void ircfw_parse_params(const char* str) {
40
	ircfw_message.params = malloc(sizeof(*ircfw_message.params));
41
	ircfw_message.params[0] = NULL;
42
	int i;
43
	int incr = 0;
44
	bool until_end = false;
45
	char* dup = ircfw_strdup(str);
46
	for(i = 0;; i++) {
47
		if(dup[i] == 0 || (!until_end && dup[i] == ' ')) {
48
			char oldc = dup[i];
49
			dup[i] = 0;
50
 
51
			char* param = ircfw_strdup(dup + incr);
52
 
53
			char** old_params = ircfw_message.params;
54
			int j;
55
			for(j = 0; old_params[j] != NULL; j++)
56
				;
57
			ircfw_message.params = malloc(sizeof(*ircfw_message.params) * (2 + j));
58
			for(j = 0; old_params[j] != NULL; j++) {
59
				ircfw_message.params[j] = old_params[j];
60
			}
61
			ircfw_message.params[j] = param;
62
			ircfw_message.params[j + 1] = NULL;
63
			free(old_params);
64
 
65
			incr = i + 1;
66
			if(oldc == 0) break;
67
		} else if(dup[i] == ':' && !until_end) {
68
			until_end = true;
69
			incr = i + 1;
70
		}
71
	}
72
	free(dup);
73
}
74
 
75
int ircfw_socket_send_cmd(int sock, const char* name, const char* cmd) {
76
	char* str = ircfw_strcat(cmd, "\r\n");
77
	if(name != NULL) {
78
		char* old = str;
79
		char* tmp = ircfw_strcat3(":", name, " ");
80
		str = ircfw_strcat(tmp, old);
81
		free(old);
82
		free(tmp);
83
	}
84
	int st = send(sock, str, strlen(str), 0);
85
	free(str);
86
	return st < 0 ? 1 : 0;
87
}
88
 
89
int ircfw_socket_read_cmd(int sock) {
90
	char c[2];
91
	c[1] = 0;
92
	char* str = malloc(1);
93
	str[0] = 0;
94
	bool err = false;
95
	bool end = false;
96
	while(1) {
97
		int s = recv(sock, c, 1, 0);
98
		if(s <= 0) {
99
			err = true;
100
			break;
101
		}
102
		if(c[0] == '\n') {
103
			end = true;
104
			break;
105
		} else if(c[0] != '\r') {
106
			char* tmp = str;
107
			str = ircfw_strcat(tmp, c);
108
			free(tmp);
109
		}
110
	}
111
	if(ircfw_message.prefix != NULL) free(ircfw_message.prefix);
112
	if(ircfw_message.params != NULL) {
113
		int i;
114
		for(i = 0; ircfw_message.params[i] != NULL; i++) {
115
			free(ircfw_message.params[i]);
116
		}
117
		free(ircfw_message.params);
118
	}
119
	if(ircfw_message.command != NULL) free(ircfw_message.command);
120
	ircfw_message.prefix = NULL;
121
	ircfw_message.params = NULL;
122
	ircfw_message.command = NULL;
123
 
124
	if(str[0] == ':') {
125
		int i;
126
		for(i = 0; str[i] != 0; i++) {
127
			if(str[i] == ' ') {
128
				str[i] = 0;
129
				ircfw_message.prefix = ircfw_strdup(str + 1);
130
				i++;
131
				int start = i;
132
				for(;; i++) {
133
					if(str[i] == ' ' || str[i] == 0) {
134
						char oldc = str[i];
135
						str[i] = 0;
136
						ircfw_message.command = ircfw_strdup(str + start);
137
						if(oldc != 0) {
138
							i++;
139
							ircfw_parse_params(str + i);
140
						}
141
						break;
142
					}
143
				}
144
				break;
145
			}
146
		}
147
	} else {
148
		int i;
149
		for(i = 0; str[i] != 0; i++) {
150
			if(str[i] == ' ' || str[i] == 0) {
151
				char oldc = str[i];
152
				str[i] = 0;
153
				ircfw_message.command = ircfw_strdup(str);
154
				if(oldc != 0) {
155
					i++;
156
					ircfw_parse_params(str + i);
157
				}
158
				break;
159
			}
160
		}
161
	}
162
 
163
	free(str);
164
	return err ? 1 : 0;
165
}