Encapsulate obscure detail that lower numeric priority is higher queue priority
[clinton/bobotpp.git] / source / ServerQueue.C
CommitLineData
cb21075d 1// ServerQueue.C -*- C++ -*-
2// Copyright (c) 1997, 1998 Etienne BERNARD
46af1667 3// Copyright (C) 2002,2005,2008 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
c99c411a 17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18// 02110-1301, USA.
cb21075d 19
20#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif
23
4ce3a244 24#include <limits>
cb21075d 25#include "ServerQueue.H"
fd7440f1 26#include "Utils.H"
cb21075d 27
28ServerQueue::ServerQueue(Socket * s, bool d)
29 : Queue(s,d), penalty(0)
30{
31#ifdef HAVE_STL_CLEAR
32 serverQueue.clear();
33#endif
34}
35
36ServerQueue::~ServerQueue()
37{
4ce3a244 38 penalty = std::numeric_limits<int>::min ();
cb21075d 39 flush();
40}
41
42void
43ServerQueue::addItem(ServerQueueItem *sqi)
44{
45 std::list<ServerQueueItem *>::iterator it, it2;
46
c99c411a 47 for (it = serverQueue.begin(); it != serverQueue.end(); ++it)
48 {
94a03421 49 if (**it < *sqi)
c99c411a 50 {
51 break;
52 }
53 }
54
55 it2 = it;
56 --it2;
57 if (it2 != serverQueue.end() && *it2)
58 {
59 // All right, we try to merge this item to the previous
60 if ((*it2)->merge(sqi))
61 {
62 delete sqi;
63 return;
64 }
cb21075d 65 }
c99c411a 66
cb21075d 67 serverQueue.insert(it, sqi);
68}
69
70void
71ServerQueue::addLine(String l, int pr, int pen, int t)
72{
73 ServerQueueOtherItem * sqoi =
74 new ServerQueueOtherItem(l, pr, pen, t);
75 addItem(sqoi);
76}
77
78bool
79ServerQueue::flush()
80{
81 // Called every second, we decrement the penalty
82 if (penalty > 0)
c99c411a 83 {
84 penalty--;
85 }
86
87 while (!serverQueue.empty() && (penalty < 5))
88 {
89 ServerQueueItem * sqi = (*serverQueue.begin());
90 penalty += sqi->penalty + sqi->getLine().length()/100 + 1;
91
92 bool res = sendLine(sqi->getLine());
93 serverQueue.erase(serverQueue.begin());
94 delete sqi;
95
96 if (!res)
97 {
98 return false;
99 }
100 }
101
cb21075d 102 return true;
103}
104
fd7440f1 105#define MNICK (Interp::bot->nickName + "!" + Interp::bot->userHost)
cb21075d 106void
107ServerQueue::sendCTCP(String to, String command,
108 String message)
109{
110 sendPrivmsg(to, String("\001") + command + " " + message + "\001");
fd7440f1 111
fd7440f1 112#ifdef USESCRIPTS
113 if (command == "ACTION")
114 {
fed59248 115 Interp::bot->botInterp->RunHooks (Hook::SEND_ACTION,
116 MNICK + " " + to +
fd7440f1 117 " " + message,
46af1667 118 scm_list_n (Utils::
a6339323 119 str2scm (MNICK),
fd7440f1 120 Utils::
a6339323 121 str2scm (to),
fd7440f1 122 Utils::
a6339323 123 str2scm (message),
fd7440f1 124 SCM_UNDEFINED));
125 }
fed59248 126 else
127 Interp::bot->botInterp->RunHooks (Hook::SEND_CTCP,
c99c411a 128 MNICK + " " + to + " " +
129 command + " " + message,
46af1667 130 scm_list_n (Utils::
c99c411a 131 str2scm (MNICK),
132 Utils::
133 str2scm (to),
134 Utils::
135 str2scm
136 (command),
137 Utils::
138 str2scm (message),
139 SCM_UNDEFINED));
fd7440f1 140#endif
141
cb21075d 142}
fd7440f1 143#undef MNICK
cb21075d 144
145void
146ServerQueue::sendCTCPReply(String to, String command,
147 String message)
148{
149 sendNotice(to, String("\001") + command + " " +
150 message + "\001");
151}
152
153void
154ServerQueue::sendChannelMode(String mode)
155{
156 addLine(mode, CHANNELMODE_PRIORITY, CHANNELMODE_PENALTY,
157 ServerQueueItem::CHANNELMODE);
158}
159
160void
161ServerQueue::sendChannelMode(String channel, String mode, String parameters)
162{
163 ServerQueueChannelModeItem * sqcmi =
164 new ServerQueueChannelModeItem(channel, mode, parameters);
165 addItem(sqcmi);
166}
167
168void
169ServerQueue::sendInvite(String channel, String nick)
170{
171 addLine(String("INVITE ") + nick + " " + channel,
172 INVITE_PRIORITY, INVITE_PENALTY, ServerQueueItem::INVITE);
173}
174
175void
176ServerQueue::sendJoin(String channel, String key)
177{
178 addLine(String("JOIN ") + channel + " " + key,
179 JOIN_PRIORITY, JOIN_PENALTY, ServerQueueItem::JOIN);
180}
181
182void
183ServerQueue::sendKick(String channel, String nick, String reason)
184{
185 ServerQueueKickItem * sqki =
186 new ServerQueueKickItem(channel, nick, reason);
187 addItem(sqki);
188}
189
190void
191ServerQueue::sendNick(String nick)
192{
193 addLine(String("NICK ") + nick,
194 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::NICK);
195}
196
197void
198ServerQueue::sendNotice(String to, String message)
199{
200 ServerQueueNoticeItem *sqni =
201 new ServerQueueNoticeItem(to, message);
202 addItem(sqni);
203}
204
205void
206ServerQueue::sendPart(String channel)
207{
208 addLine(String("PART ") + channel,
209 PART_PRIORITY, PART_PENALTY, ServerQueueItem::PART);
210}
211
212void
213ServerQueue::sendPass(String pass)
214{
215 addLine(String("PASS ") + pass,
216 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::PASS);
217}
218
219void
220ServerQueue::sendPing(String server)
221{
222 addLine(String("PING :") + server,
223 PING_PRIORITY, PING_PENALTY, ServerQueueItem::PING);
224}
225
226void
227ServerQueue::sendPong(String crap)
228{
229 addLine(String("PONG ") + crap,
230 PONG_PRIORITY, PONG_PENALTY, ServerQueueItem::PONG);
231}
232
233void
234ServerQueue::sendPrivmsg(String dest, String message)
235{
236 addLine(String("PRIVMSG ") + dest + " :" + message,
237 PRIVMSG_PRIORITY, PRIVMSG_PENALTY, ServerQueueItem::PRIVMSG);
fd7440f1 238 // hook stuff
239#ifdef USESCRIPTS
240 if (message[0] != '\001')
a6339323 241 if (Utils::channel_p (dest))
fed59248 242 Interp::bot->botInterp->RunHooks (Hook::SEND_PUBLIC,
fd7440f1 243 Interp::bot->nickName + " " + dest +
244 " " + message,
46af1667 245 scm_list_n
c99c411a 246 (Utils::str2scm
247 (Interp::bot->nickName),
248 Utils::str2scm (dest),
249 Utils::str2scm (message),
250 SCM_UNDEFINED));
fd7440f1 251 else
ae97d6ec 252 Interp::bot->botInterp->RunHooks
253 (Hook::SEND_MESSAGE,
254 Interp::bot->nickName + " " + dest +
255 message,
46af1667 256 scm_list_n (Utils::str2scm (Interp::bot->nickName),
ae97d6ec 257 Utils::str2scm (dest),
258 Utils::str2scm
259 (message), SCM_UNDEFINED));
fd7440f1 260#endif
cb21075d 261}
262
263void
264ServerQueue::sendQuit(String reason)
265{
266 addLine(String("QUIT :") + reason,
267 QUIT_PRIORITY, QUIT_PENALTY, ServerQueueItem::QUIT);
268}
269
270void
271ServerQueue::sendTopic(String channel, String topic)
272{
273 addLine(String("TOPIC ") + channel + " :" + topic,
274 TOPIC_PRIORITY, TOPIC_PENALTY, ServerQueueItem::TOPIC);
275}
276
277void
278ServerQueue::sendUser(String username, String ircname)
279{
439869bf 280 addLine(String("USER ") + username + " 0 * :" + ircname,
cb21075d 281 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::USER);
282}
283
284void
285ServerQueue::sendUserMode(String nick, String mode)
286{
287 addLine(String("MODE ") + nick + " " + mode,
288 USERMODE_PRIORITY, USERMODE_PENALTY,
289 ServerQueueItem::USERMODE);
290}
291
292void
293ServerQueue::sendUserhost(String nick)
294{
295 addLine(String("USERHOST ") + nick,
296 USERHOST_PRIORITY, USERHOST_PENALTY, ServerQueueItem::USERHOST);
297}
298
299void
300ServerQueue::sendWho(String who)
301{
302 addLine(String("WHO ") + who,
303 WHO_PRIORITY, WHO_PENALTY, ServerQueueItem::WHO);
528799bd 304
305#ifdef USESCRIPTS
306 Interp::bot->botInterp->RunHooks (Hook::SEND_WHO,
307 who,
308 scm_list_n (Utils::
309 str2scm (who),
310 SCM_UNDEFINED));
311#endif
cb21075d 312}
313
314void
315ServerQueue::sendWhois(String nick)
316{
317 addLine(String("WHOIS ") + nick,
318 NICK_PRIORITY, WHOIS_PENALTY, ServerQueueItem::WHOIS);
528799bd 319
320#ifdef USESCRIPTS
321 Interp::bot->botInterp->RunHooks (Hook::SEND_WHOIS,
322 nick,
323 scm_list_n (Utils::
324 str2scm (nick),
325 SCM_UNDEFINED));
326#endif
cb21075d 327}