[project @ 2005-07-04 01:48:38 by unknown_lamer]
[clinton/bobotpp.git] / source / ServerQueue.C
CommitLineData
cb21075d 1// ServerQueue.C -*- C++ -*-
2// Copyright (c) 1997, 1998 Etienne BERNARD
a6339323 3// Copyright (C) 2002,2005 Clinton Ebadi
cb21075d 4
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// any later version.
9
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
39b022cb 17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
cb21075d 18
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23//#include <limits>
24#include "ServerQueue.H"
fd7440f1 25#include "Utils.H"
cb21075d 26
27ServerQueue::ServerQueue(Socket * s, bool d)
28 : Queue(s,d), penalty(0)
29{
30#ifdef HAVE_STL_CLEAR
31 serverQueue.clear();
32#endif
33}
34
35ServerQueue::~ServerQueue()
36{
37 penalty = INT_MIN;
38 flush();
39}
40
41void
42ServerQueue::addItem(ServerQueueItem *sqi)
43{
44 std::list<ServerQueueItem *>::iterator it, it2;
45
46 for (it = serverQueue.begin(); it != serverQueue.end(); ++it) {
47 if ((*it)->priority > sqi->priority)
48 break;
49 }
50 it2 = it; --it2;
51 if (it2 != serverQueue.end() && *it2) {
52 // All right, we try to merge this item to the previous
53 if ((*it2)->merge(sqi)) {
54 delete sqi;
55 return;
56 }
57 }
58 serverQueue.insert(it, sqi);
59}
60
61void
62ServerQueue::addLine(String l, int pr, int pen, int t)
63{
64 ServerQueueOtherItem * sqoi =
65 new ServerQueueOtherItem(l, pr, pen, t);
66 addItem(sqoi);
67}
68
69bool
70ServerQueue::flush()
71{
72 // Called every second, we decrement the penalty
73 if (penalty > 0)
74 penalty--;
75
76 while (!serverQueue.empty() && (penalty < 5)) {
77 ServerQueueItem * sqi = (*serverQueue.begin());
78 penalty += sqi->penalty + sqi->getLine().length()/100 + 1;
79 bool res = sendLine(sqi->getLine());
80 serverQueue.erase(serverQueue.begin());
81 delete sqi;
82 if (!res)
83 return false;
84 }
85 return true;
86}
87
fd7440f1 88#define MNICK (Interp::bot->nickName + "!" + Interp::bot->userHost)
cb21075d 89void
90ServerQueue::sendCTCP(String to, String command,
91 String message)
92{
93 sendPrivmsg(to, String("\001") + command + " " + message + "\001");
fd7440f1 94
fd7440f1 95#ifdef USESCRIPTS
96 if (command == "ACTION")
97 {
fed59248 98 Interp::bot->botInterp->RunHooks (Hook::SEND_ACTION,
99 MNICK + " " + to +
fd7440f1 100 " " + message,
101 scm_listify (Utils::
a6339323 102 str2scm (MNICK),
fd7440f1 103 Utils::
a6339323 104 str2scm (to),
fd7440f1 105 Utils::
a6339323 106 str2scm (message),
fd7440f1 107 SCM_UNDEFINED));
108 }
fed59248 109 else
110 Interp::bot->botInterp->RunHooks (Hook::SEND_CTCP,
111 MNICK + " " + to + " " +
112 command + " " + message,
113 scm_listify (Utils::
a6339323 114 str2scm (MNICK),
fed59248 115 Utils::
a6339323 116 str2scm (to),
fed59248 117 Utils::
a6339323 118 str2scm
fed59248 119 (command),
120 Utils::
a6339323 121 str2scm (message),
fed59248 122 SCM_UNDEFINED));
fd7440f1 123#endif
124
cb21075d 125}
fd7440f1 126#undef MNICK
cb21075d 127
128void
129ServerQueue::sendCTCPReply(String to, String command,
130 String message)
131{
132 sendNotice(to, String("\001") + command + " " +
133 message + "\001");
134}
135
136void
137ServerQueue::sendChannelMode(String mode)
138{
139 addLine(mode, CHANNELMODE_PRIORITY, CHANNELMODE_PENALTY,
140 ServerQueueItem::CHANNELMODE);
141}
142
143void
144ServerQueue::sendChannelMode(String channel, String mode, String parameters)
145{
146 ServerQueueChannelModeItem * sqcmi =
147 new ServerQueueChannelModeItem(channel, mode, parameters);
148 addItem(sqcmi);
149}
150
151void
152ServerQueue::sendInvite(String channel, String nick)
153{
154 addLine(String("INVITE ") + nick + " " + channel,
155 INVITE_PRIORITY, INVITE_PENALTY, ServerQueueItem::INVITE);
156}
157
158void
159ServerQueue::sendJoin(String channel, String key)
160{
161 addLine(String("JOIN ") + channel + " " + key,
162 JOIN_PRIORITY, JOIN_PENALTY, ServerQueueItem::JOIN);
163}
164
165void
166ServerQueue::sendKick(String channel, String nick, String reason)
167{
168 ServerQueueKickItem * sqki =
169 new ServerQueueKickItem(channel, nick, reason);
170 addItem(sqki);
171}
172
173void
174ServerQueue::sendNick(String nick)
175{
176 addLine(String("NICK ") + nick,
177 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::NICK);
178}
179
180void
181ServerQueue::sendNotice(String to, String message)
182{
183 ServerQueueNoticeItem *sqni =
184 new ServerQueueNoticeItem(to, message);
185 addItem(sqni);
186}
187
188void
189ServerQueue::sendPart(String channel)
190{
191 addLine(String("PART ") + channel,
192 PART_PRIORITY, PART_PENALTY, ServerQueueItem::PART);
193}
194
195void
196ServerQueue::sendPass(String pass)
197{
198 addLine(String("PASS ") + pass,
199 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::PASS);
200}
201
202void
203ServerQueue::sendPing(String server)
204{
205 addLine(String("PING :") + server,
206 PING_PRIORITY, PING_PENALTY, ServerQueueItem::PING);
207}
208
209void
210ServerQueue::sendPong(String crap)
211{
212 addLine(String("PONG ") + crap,
213 PONG_PRIORITY, PONG_PENALTY, ServerQueueItem::PONG);
214}
215
216void
217ServerQueue::sendPrivmsg(String dest, String message)
218{
219 addLine(String("PRIVMSG ") + dest + " :" + message,
220 PRIVMSG_PRIORITY, PRIVMSG_PENALTY, ServerQueueItem::PRIVMSG);
fd7440f1 221 // hook stuff
222#ifdef USESCRIPTS
223 if (message[0] != '\001')
a6339323 224 if (Utils::channel_p (dest))
fed59248 225 Interp::bot->botInterp->RunHooks (Hook::SEND_PUBLIC,
fd7440f1 226 Interp::bot->nickName + " " + dest +
227 " " + message,
228 scm_listify (Utils::
a6339323 229 str2scm (Interp::bot->nickName),
fd7440f1 230 Utils::
a6339323 231 str2scm (dest),
fd7440f1 232 Utils::
a6339323 233 str2scm
fd7440f1 234 (message), SCM_UNDEFINED));
235 else
ae97d6ec 236 Interp::bot->botInterp->RunHooks
237 (Hook::SEND_MESSAGE,
238 Interp::bot->nickName + " " + dest +
239 message,
240 scm_listify (Utils::str2scm (Interp::bot->nickName),
241 Utils::str2scm (dest),
242 Utils::str2scm
243 (message), SCM_UNDEFINED));
fd7440f1 244#endif
cb21075d 245}
246
247void
248ServerQueue::sendQuit(String reason)
249{
250 addLine(String("QUIT :") + reason,
251 QUIT_PRIORITY, QUIT_PENALTY, ServerQueueItem::QUIT);
252}
253
254void
255ServerQueue::sendTopic(String channel, String topic)
256{
257 addLine(String("TOPIC ") + channel + " :" + topic,
258 TOPIC_PRIORITY, TOPIC_PENALTY, ServerQueueItem::TOPIC);
259}
260
261void
262ServerQueue::sendUser(String username, String ircname)
263{
439869bf 264 addLine(String("USER ") + username + " 0 * :" + ircname,
cb21075d 265 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::USER);
266}
267
268void
269ServerQueue::sendUserMode(String nick, String mode)
270{
271 addLine(String("MODE ") + nick + " " + mode,
272 USERMODE_PRIORITY, USERMODE_PENALTY,
273 ServerQueueItem::USERMODE);
274}
275
276void
277ServerQueue::sendUserhost(String nick)
278{
279 addLine(String("USERHOST ") + nick,
280 USERHOST_PRIORITY, USERHOST_PENALTY, ServerQueueItem::USERHOST);
281}
282
283void
284ServerQueue::sendWho(String who)
285{
286 addLine(String("WHO ") + who,
287 WHO_PRIORITY, WHO_PENALTY, ServerQueueItem::WHO);
288}
289
290void
291ServerQueue::sendWhois(String nick)
292{
293 addLine(String("WHOIS ") + nick,
294 NICK_PRIORITY, WHOIS_PENALTY, ServerQueueItem::WHOIS);
295}