1 // ServerQueue.C -*- C++ -*-
2 // Copyright (c) 1997, 1998 Etienne BERNARD
3 // Copyright (C) 2002,2005 Clinton Ebadi
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
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.
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
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "ServerQueue.H"
27 ServerQueue::ServerQueue(Socket * s, bool d)
28 : Queue(s,d), penalty(0)
35 ServerQueue::~ServerQueue()
42 ServerQueue::addItem(ServerQueueItem *sqi)
44 std::list<ServerQueueItem *>::iterator it, it2;
46 for (it = serverQueue.begin(); it != serverQueue.end(); ++it) {
47 if ((*it)->priority > sqi->priority)
51 if (it2 != serverQueue.end() && *it2) {
52 // All right, we try to merge this item to the previous
53 if ((*it2)->merge(sqi)) {
58 serverQueue.insert(it, sqi);
62 ServerQueue::addLine(String l, int pr, int pen, int t)
64 ServerQueueOtherItem * sqoi =
65 new ServerQueueOtherItem(l, pr, pen, t);
72 // Called every second, we decrement the penalty
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());
88 #define MNICK (Interp::bot->nickName + "!" + Interp::bot->userHost)
90 ServerQueue::sendCTCP(String to, String command,
93 sendPrivmsg(to, String("\001") + command + " " + message + "\001");
96 if (command == "ACTION")
98 Interp::bot->botInterp->RunHooks (Hook::SEND_ACTION,
110 Interp::bot->botInterp->RunHooks (Hook::SEND_CTCP,
111 MNICK + " " + to + " " +
112 command + " " + message,
129 ServerQueue::sendCTCPReply(String to, String command,
132 sendNotice(to, String("\001") + command + " " +
137 ServerQueue::sendChannelMode(String mode)
139 addLine(mode, CHANNELMODE_PRIORITY, CHANNELMODE_PENALTY,
140 ServerQueueItem::CHANNELMODE);
144 ServerQueue::sendChannelMode(String channel, String mode, String parameters)
146 ServerQueueChannelModeItem * sqcmi =
147 new ServerQueueChannelModeItem(channel, mode, parameters);
152 ServerQueue::sendInvite(String channel, String nick)
154 addLine(String("INVITE ") + nick + " " + channel,
155 INVITE_PRIORITY, INVITE_PENALTY, ServerQueueItem::INVITE);
159 ServerQueue::sendJoin(String channel, String key)
161 addLine(String("JOIN ") + channel + " " + key,
162 JOIN_PRIORITY, JOIN_PENALTY, ServerQueueItem::JOIN);
166 ServerQueue::sendKick(String channel, String nick, String reason)
168 ServerQueueKickItem * sqki =
169 new ServerQueueKickItem(channel, nick, reason);
174 ServerQueue::sendNick(String nick)
176 addLine(String("NICK ") + nick,
177 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::NICK);
181 ServerQueue::sendNotice(String to, String message)
183 ServerQueueNoticeItem *sqni =
184 new ServerQueueNoticeItem(to, message);
189 ServerQueue::sendPart(String channel)
191 addLine(String("PART ") + channel,
192 PART_PRIORITY, PART_PENALTY, ServerQueueItem::PART);
196 ServerQueue::sendPass(String pass)
198 addLine(String("PASS ") + pass,
199 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::PASS);
203 ServerQueue::sendPing(String server)
205 addLine(String("PING :") + server,
206 PING_PRIORITY, PING_PENALTY, ServerQueueItem::PING);
210 ServerQueue::sendPong(String crap)
212 addLine(String("PONG ") + crap,
213 PONG_PRIORITY, PONG_PENALTY, ServerQueueItem::PONG);
217 ServerQueue::sendPrivmsg(String dest, String message)
219 addLine(String("PRIVMSG ") + dest + " :" + message,
220 PRIVMSG_PRIORITY, PRIVMSG_PENALTY, ServerQueueItem::PRIVMSG);
223 if (message[0] != '\001')
224 if (Utils::channel_p (dest))
225 Interp::bot->botInterp->RunHooks (Hook::SEND_PUBLIC,
226 Interp::bot->nickName + " " + dest +
229 str2scm (Interp::bot->nickName),
234 (message), SCM_UNDEFINED));
236 Interp::bot->botInterp->RunHooks
238 Interp::bot->nickName + " " + dest +
240 scm_listify (Utils::str2scm (Interp::bot->nickName),
241 Utils::str2scm (dest),
243 (message), SCM_UNDEFINED));
248 ServerQueue::sendQuit(String reason)
250 addLine(String("QUIT :") + reason,
251 QUIT_PRIORITY, QUIT_PENALTY, ServerQueueItem::QUIT);
255 ServerQueue::sendTopic(String channel, String topic)
257 addLine(String("TOPIC ") + channel + " :" + topic,
258 TOPIC_PRIORITY, TOPIC_PENALTY, ServerQueueItem::TOPIC);
262 ServerQueue::sendUser(String username, String ircname)
264 addLine(String("USER ") + username + " 0 * :" + ircname,
265 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::USER);
269 ServerQueue::sendUserMode(String nick, String mode)
271 addLine(String("MODE ") + nick + " " + mode,
272 USERMODE_PRIORITY, USERMODE_PENALTY,
273 ServerQueueItem::USERMODE);
277 ServerQueue::sendUserhost(String nick)
279 addLine(String("USERHOST ") + nick,
280 USERHOST_PRIORITY, USERHOST_PENALTY, ServerQueueItem::USERHOST);
284 ServerQueue::sendWho(String who)
286 addLine(String("WHO ") + who,
287 WHO_PRIORITY, WHO_PENALTY, ServerQueueItem::WHO);
291 ServerQueue::sendWhois(String nick)
293 addLine(String("WHOIS ") + nick,
294 NICK_PRIORITY, WHOIS_PENALTY, ServerQueueItem::WHOIS);