-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc.cpp
250 lines (204 loc) · 5.04 KB
/
irc.cpp
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
#include <cstdlib>
#include <string>
#include "irc.h"
#include "extras.h"
int IRC::connect(char nick[], char server[], int port)
{
socket.socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socket.connect(server, port) < 0)
return -1;
else
{
char data[4096];
sprintf(buffer, "NICK %s\r\n", nick);
if (raw_command(buffer) < 0)
return -1;
sprintf(myName, "%s", nick);
sprintf(buffer, "USER %s cainor * :eXeTeL Services\r\n", nick);
if (raw_command(buffer) < 0)
return -1;
while (IRC::check())
{
IRC::read(data);
IRC::process(data);
}
return 0;
}
}
int IRC::join(char channel[])
{
sprintf(buffer, "JOIN %s\r\n", channel);
if (raw_command(buffer) < 0)
return -1;
return 1;
}
int IRC::leave(char channel[])
{
sprintf(buffer, "PART :%s\r\n", channel);
if (raw_command(buffer) < 0)
return -1;
return 1;
}
int IRC::private_message(char to[], char message[])
{
sprintf(buffer, "PRIVMSG %s :%s\r\n", to, message);
if (raw_command(buffer) < 0)
return -1;
return 1;
}
int IRC::send_channel(char channel[], char message[])
{
sprintf(buffer, "PRIVMSG %s :%s\r\n", channel, message);
if (raw_command(buffer) < 0);
return -1;
return 1;
}
int IRC::send_notice(char nick[], char message[])
{
sprintf(buffer ,"NOTICE %s : %s\r\n", nick, message);
if (raw_command(buffer) < 0);
return -1;
return 1;
}
int IRC::set_topic(char channel[], char topic[])
{
sprintf(buffer, "TOPIC %s :%s\r\n", channel, topic);
if (raw_command(buffer) < 0)
return -1;
return 1;
}
int IRC::change_nick(char nick[])
{
strncpy(myName, nick, sizeof(myName)-1);
sprintf(buffer, ":%s NICK %s\r\n", circnick, nick);
if (raw_command(buffer) < 0)
return -1;
return 1;
}
int IRC::raw_command(char command[])
{
if (stristr(command, "www.")) // block url's, prevent glines
return 0;
sprintf(buffer, "%s\r\n", command);
if (socket.write(buffer) < 0)
return -1;
return 1;
}
int IRC::read(char *input)
{
int length = 0;
length = socket.read(input);
return length;
}
int IRC::quit(char message[])
{
sprintf(buffer, "QUIT :%s\n", message);
if (raw_command(buffer) < 0)
return -1;
if (socket.close() < 0)
return -1;
return 1;
}
bool IRC::check()
{
//select call here
return(socket.checkfordata());
}
int IRC::process(char message[])
{
char *ptr;
ptr = strtok(message, "\n");
while (ptr != NULL)
{
char *tmp;
tmp = strstr(ptr, "PING :");
if (tmp != NULL)
{
bool valid = true;
char buffer[256];
int i = 0;
while((tmp[i] != '\n') && (tmp[i] != '\r'))
{
buffer[i] = tmp[i];
i++;
if (i > sizeof(buffer)-2)
{
valid = false;
break;
}
}
buffer[i] = '\0';
if (valid)
{
buffer[1] = 'O';
raw_command(buffer);
}
}
int i = sscanf(ptr, ":%[^!\n\r ]!%s%s%[^\n\r]", szNICK, szUSER, szCOMMAND, szREST);
if (i == 4)
{
if (strcmpi(szCOMMAND,"JOIN") == 0)
{
strcpy(szCHAN, szREST+2);
} //command = join, user = ident, nick = nick, rest = chan
else if (strcmpi(szCOMMAND,"PART") == 0)
{
onpart(szNICK);
} //command = part, user = ident, nick = nick, rest = chan
else if (strcmpi(szCOMMAND,"PRIVMSG") == 0)
{
char *vhostptr = strstr(message,"@");
char temp;
int iPtr = 0;
if (vhostptr != 0)
{
vhostptr++;
while (vhostptr[iPtr] != ' ')
iPtr++;
temp = vhostptr[iPtr];
vhostptr[iPtr] = '\0';
strcpy(szVHOST, vhostptr);
vhostptr[iPtr] = temp;
// we have, vhost!
}
sscanf(szREST, "%s :", szCHAN);
strcpy(szTEXT, szREST + 3 + strlen(szCHAN));
ontext(szCHAN, szNICK, szVHOST, szTEXT);
} //command = PRIVMSG, user = ident, nick = nick, rest = chan: message
else if (!strcmpi(szCOMMAND,"KICK"))
{
char *kickptr = szREST + 2;
char kick[32];
int i = 0;
while (*kickptr != ' ')
*kickptr++;
*kickptr++;
while (kickptr[i] != ' ')
{
kick[i] = kickptr[i];
i++;
}
kick[i] = '\0';
if (!strcmp(kick, ircnick)) // bot has been kicked from channel
{
sprintf(buffer, "unbanme %s", ircchannel);
private_message("ChanServ", buffer);
Sleep(1000);
join(ircchannel); // rejoin the channel
join(ircchannel);
}
onpart(kick);
}//command = mode, user = ident, nick = nick, rest = chan modes //does not catch whole list of modes/parameters
else if (!strcmpi(szCOMMAND,"QUIT"))
{
onpart(szNICK);
}
else if (strcmpi(szCOMMAND,"NICK") == 0)
{
onnamechange(szNICK, szREST + 2);
} //command nick, user = ident, rest = new nick, nick = old nick
}
ptr = strtok(NULL, "\n");
}
return 0;
}