summaryrefslogtreecommitdiff
path: root/lib/IRC.cc
blob: bb7cbb61a68374f022dedfb8b22c72fcb314a213 (plain)
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
#include "String.h"
#include "IRC.h"
#include "HttpServ.h"

ircmsg_t ircmsgs[MSG_COUNT] = 

{
    { "RPL_WELCOME", RPL_WELCOME },
    { "RPL_YOURHOST", RPL_YOURHOST },
    { "RPL_CREATED", RPL_CREATED },
    { "RPL_MYINFO", RPL_MYINFO },
    { "RPL_BOUNCE", RPL_BOUNCE },
    { "RPL_TRACELINK", RPL_TRACELINK },
    { "RPL_TRACECONNECTING", RPL_TRACECONNECTING },
    { "RPL_TRACEHANDSHAKE", RPL_TRACEHANDSHAKE },
    { "RPL_TRACEUNKNOWN", RPL_TRACEUNKNOWN },
    { "RPL_TRACEOPERATOR", RPL_TRACEOPERATOR },
    { "RPL_TRACEUSER", RPL_TRACEUSER },
    { "RPL_TRACESERVER", RPL_TRACESERVER },
    { "RPL_TRACESERVICE", RPL_TRACESERVICE },
    { "RPL_TRACENEWTYPE", RPL_TRACENEWTYPE },
    { "RPL_TRACECLASS", RPL_TRACECLASS },
    { "RPL_TRACERECONNECT", RPL_TRACERECONNECT },
    { "RPL_STATSLINKINFO", RPL_STATSLINKINFO },
    { "RPL_STATSCOMMANDS", RPL_STATSCOMMANDS },
    { "RPL_STATSCLINE", RPL_STATSCLINE },
    { "RPL_STATSILINE", RPL_STATSILINE },
    { "RPL_STATSQLINE", RPL_STATSQLINE },
    { "RPL_ENDOFSTATS", RPL_ENDOFSTATS },
    { "RPL_UMODEIS", RPL_UMODEIS },
    { "RPL_SERVICEINFO", RPL_SERVICEINFO },
    { "RPL_SERVICE", RPL_SERVICE },
    { "RPL_SERVLIST", RPL_SERVLIST },
    { "RPL_SERVLISTEND", RPL_SERVLISTEND },
    { "RPL_STATSVLINE", RPL_STATSVLINE },
    { "RPL_STATSUPTIME", RPL_STATSUPTIME },
    { "RPL_STATSOLINE", RPL_STATSOLINE },
    { "RPL_STATSHLINE", RPL_STATSHLINE },
    { "RPL_STATSPING", RPL_STATSPING },
    { "RPL_STATSDLINE", RPL_STATSDLINE },
    { "RPL_LUSERCLIENT", RPL_LUSERCLIENT },
    { "RPL_LUSEROP", RPL_LUSEROP },
    { "RPL_LUSERUNKNOWN", RPL_LUSERUNKNOWN },
    { "RPL_LUSERCHANNELS", RPL_LUSERCHANNELS },
    { "RPL_LUSERME", RPL_LUSERME },
    { "RPL_ADMINME", RPL_ADMINME },
    { "RPL_ADMINLOC1", RPL_ADMINLOC1 },
    { "RPL_ADMINLOC2", RPL_ADMINLOC2 },
    { "RPL_ADMINEMAIL", RPL_ADMINEMAIL },
    { "RPL_TRACELOG", RPL_TRACELOG },
    { "RPL_TRACEEND", RPL_TRACEEND },
    { "RPL_TRYAGAIN", RPL_TRYAGAIN },
    { "RPL_NONE", RPL_NONE },
    { "RPL_AWAY", RPL_AWAY },
    { "RPL_USERHOST", RPL_USERHOST },
    { "RPL_ISON", RPL_ISON },
    { "RPL_UNAWAY", RPL_UNAWAY },
    { "RPL_NOWAWAY", RPL_NOWAWAY },
    { "RPL_WHOISUSER", RPL_WHOISUSER },
    { "RPL_WHOISSERVER", RPL_WHOISSERVER },
    { "RPL_WHOISOPERATOR", RPL_WHOISOPERATOR },
    { "RPL_WHOWASUSER", RPL_WHOWASUSER },
    { "RPL_ENDOFWHO", RPL_ENDOFWHO },
    { "RPL_WHOISIDLE", RPL_WHOISIDLE },
    { "RPL_ENDOFWHOIS", RPL_ENDOFWHOIS },
    { "RPL_WHOISCHANNELS", RPL_WHOISCHANNELS },
    { "RPL_LISTSTART", RPL_LISTSTART },
    { "RPL_LIST", RPL_LIST },
    { "RPL_LISTEND", RPL_LISTEND },
    { "RPL_CHANNELMODEIS", RPL_CHANNELMODEIS },
    { "RPL_UNIQOPIS", RPL_UNIQOPIS },
    { "RPL_NOTOPIC", RPL_NOTOPIC },
    { "RPL_TOPIC", RPL_TOPIC },
    { "RPL_INVITING", RPL_INVITING },
    { "RPL_SUMMONING", RPL_SUMMONING },
    { "RPL_INVITELIST", RPL_INVITELIST },
    { "RPL_ENDOFINVITELIST", RPL_ENDOFINVITELIST },
    { "RPL_EXCEPTLIST", RPL_EXCEPTLIST },
    { "RPL_ENDOFEXCEPTLIST", RPL_ENDOFEXCEPTLIST },
    { "RPL_VERSION", RPL_VERSION },
    { "RPL_WHOREPLY", RPL_WHOREPLY },
    { "RPL_NAMREPLY", RPL_NAMREPLY },
    { "RPL_KILLDONE", RPL_KILLDONE },
    { "RPL_CLOSEEND", RPL_CLOSEEND },
    { "RPL_LINKS", RPL_LINKS },
    { "RPL_ENDOFLINKS", RPL_ENDOFLINKS },
    { "RPL_ENDOFNAMES", RPL_ENDOFNAMES },
    { "RPL_BANLIST", RPL_BANLIST },
    { "RPL_ENDOFBANLIST", RPL_ENDOFBANLIST },
    { "RPL_ENDOFWHOWAS", RPL_ENDOFWHOWAS },
    { "RPL_INFO", RPL_INFO },
    { "RPL_MOTD", RPL_MOTD },
    { "RPL_ENDOFINFO", RPL_ENDOFINFO },
    { "RPL_MOTDSTART", RPL_MOTDSTART },
    { "RPL_ENDOFMOTD", RPL_ENDOFMOTD },
    { "RPL_YOUREOPER", RPL_YOUREOPER },
    { "RPL_REHASHING", RPL_REHASHING },
    { "RPL_YOURESERVICE", RPL_YOURESERVICE },
    { "RPL_MYPORTIS", RPL_MYPORTIS },
    { "RPL_TIME", RPL_TIME },
    { "RPL_USERSSTART", RPL_USERSSTART },
    { "RPL_USERS", RPL_USERS },
    { "RPL_ENDOFUSERS", RPL_ENDOFUSERS },
    { "RPL_NOUSERS", RPL_NOUSERS },
    { "ERR_NOSUCHNICK", ERR_NOSUCHNICK },
    { "ERR_NOSUCHSERVER", ERR_NOSUCHSERVER },
    { "ERR_NOSUCHCHANNEL", ERR_NOSUCHCHANNEL },
    { "ERR_CANNOTSENDTOCHAN", ERR_CANNOTSENDTOCHAN },
    { "ERR_TOOMANYCHANNELS", ERR_TOOMANYCHANNELS },
    { "ERR_WASNOSUCHNICK", ERR_WASNOSUCHNICK },
    { "ERR_TOOMANYTARGETS", ERR_TOOMANYTARGETS },
    { "ERR_NOSUCHSERVICE", ERR_NOSUCHSERVICE },
    { "ERR_NOORIGIN", ERR_NOORIGIN },
    { "ERR_NORECIPIENT", ERR_NORECIPIENT },
    { "ERR_NOTEXTTOSEND", ERR_NOTEXTTOSEND },
    { "ERR_NOTOPLEVEL", ERR_NOTOPLEVEL },
    { "ERR_WILDTOPLEVEL", ERR_WILDTOPLEVEL },
    { "ERR_BADMASK", ERR_BADMASK },
    { "ERR_UNKNOWNCOMMAND", ERR_UNKNOWNCOMMAND },
    { "ERR_NOMOTD", ERR_NOMOTD },
    { "ERR_NOADMININFO", ERR_NOADMININFO },
    { "ERR_FILEERROR", ERR_FILEERROR },
    { "ERR_NONICKNAMEGIVEN", ERR_NONICKNAMEGIVEN },
    { "ERR_ERRONEUSNICKNAME", ERR_ERRONEUSNICKNAME },
    { "ERR_NICKNAMEINUSE", ERR_NICKNAMEINUSE },
    { "ERR_NICKCOLLISION", ERR_NICKCOLLISION },
    { "ERR_UNAVAILRESOURCE", ERR_UNAVAILRESOURCE },
    { "ERR_USERNOTINCHANNEL", ERR_USERNOTINCHANNEL },
    { "ERR_NOTONCHANNEL", ERR_NOTONCHANNEL },
    { "ERR_USERONCHANNEL", ERR_USERONCHANNEL },
    { "ERR_NOLOGIN", ERR_NOLOGIN },
    { "ERR_SUMMONDISABLED", ERR_SUMMONDISABLED },
    { "ERR_USERSDISABLED", ERR_USERSDISABLED },
    { "ERR_NOTREGISTERED", ERR_NOTREGISTERED },
    { "ERR_NEEDMOREPARAMS", ERR_NEEDMOREPARAMS },
    { "ERR_ALREADYREGISTRED", ERR_ALREADYREGISTRED },
    { "ERR_NOPERMFORHOST", ERR_NOPERMFORHOST },
    { "ERR_PASSWDMISMATCH", ERR_PASSWDMISMATCH },
    { "ERR_YOUREBANNEDCREEP", ERR_YOUREBANNEDCREEP },
    { "ERR_YOUWILLBEBANNED", ERR_YOUWILLBEBANNED },
    { "ERR_KEYSET", ERR_KEYSET },
    { "ERR_CHANNELISFULL", ERR_CHANNELISFULL },
    { "ERR_UNKNOWNMODE", ERR_UNKNOWNMODE },
    { "ERR_INVITEONLYCHAN", ERR_INVITEONLYCHAN },
    { "ERR_BANNEDFROMCHAN", ERR_BANNEDFROMCHAN },
    { "ERR_BADCHANNELKEY", ERR_BADCHANNELKEY },
    { "ERR_BADCHANMASK", ERR_BADCHANMASK },
    { "ERR_NOCHANMODES", ERR_NOCHANMODES },
    { "ERR_BANLISTFULL", ERR_BANLISTFULL },
    { "ERR_NOPRIVILEGES", ERR_NOPRIVILEGES },
    { "ERR_CHANOPRIVSNEEDED", ERR_CHANOPRIVSNEEDED },
    { "ERR_CANTKILLSERVER", ERR_CANTKILLSERVER },
    { "ERR_RESTRICTED", ERR_RESTRICTED },
    { "ERR_UNIQOPPRIVSNEEDED", ERR_UNIQOPPRIVSNEEDED },
    { "ERR_NOOPERHOST", ERR_NOOPERHOST },
    { "ERR_NOSERVICEHOST", ERR_NOSERVICEHOST },
    { "ERR_UMODEUNKNOWNFLAG", ERR_UMODEUNKNOWNFLAG },
    { "ERR_USERSDONTMATCH", ERR_USERSDONTMATCH },
};

Channel * Channel::start = 0;

Channel::Channel(const String & Name, const String & Key) : next(0), prev(0) {
    next = start->next;
    prev = start;
    start = this;
    if (next) next->prev = this;
}

Channel::~Channel() {
    if (next) next->prev = prev;
    if (prev) prev->next = next; else start = next;
}

ircmsg_t find_msg(int code) {
    int i;
    
    ircmsg_t ret = {"", code};
    
    for (i = 0; i < MSG_COUNT; i++) {
	if (code == ircmsgs[i].code) return ircmsgs[i];
    }
    
    return ret;
}

IRC::IRC(const String & nick_n, const String & server_n, const String & user_n, const String & name_n, int port_n) :
    nick(nick_n), server(server_n), user(user_n), name(name_n), port(port_n), loginsequence(1) {
}

IRC::~IRC() {
}

bool IRC::Connect() {
    return sock.Connect(server, port);
}

int IRC::Parse(const String & line) {
    if (line.extract(0, 3) == "PING") {
	sock << "PONG" << line.extract(4) << endhl;
	cerr << "PING/PONG" << line.extract(4) << endhl;
	return 0;
    }
    
    return 1;
}

void IRC::MainLoop() {
    String line;
    int code, count = 0;

    while (true) {
	switch (loginsequence) {
	    case 1:
		cerr << "Trying the login sequence..." << endl;
		sock << "NICK " << nick << endhl;
		sock << "USER " << user << " 8 * :" << name << endhl;
//		sock << "JOIN #linuxdjeunz" << endhl;
		loginsequence = 0;
	}
	sock >> line;
	if ((code = Parse(line))) {
//	    sock << "PRIVMSG #phear.org : Unparsable: " << line << endhl;
	    cerr << "Unparsable: " << line << endl;
	}
	
	if (count == 20) {
    	    sock << "JOIN #linuxdjeunz" << endhl;
	}

	count++;
    }
}