1 // ServerQueue.C -*- C++ -*-
2 // Copyright (c) 1997, 1998 Etienne BERNARD
3 // Copyright (C) 2002,2005,2008 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
25 #include "ServerQueue.H"
28 ServerQueue::ServerQueue(Socket * s, bool d)
29 : Queue(s,d), penalty(0)
36 ServerQueue::~ServerQueue()
38 penalty = std::numeric_limits<int>::min ();
43 ServerQueue::addItem(ServerQueueItem *sqi)
45 std::list<ServerQueueItem *>::iterator it, it2;
47 for (it = serverQueue.begin(); it != serverQueue.end(); ++it)
57 if (it2 != serverQueue.end() && *it2)
59 // All right, we try to merge this item to the previous
60 if ((*it2)->merge(sqi))
67 serverQueue.insert(it, sqi);
71 ServerQueue::addLine(String l, int pr, int pen, int t)
73 ServerQueueOtherItem * sqoi =
74 new ServerQueueOtherItem(l, pr, pen, t);
81 // Called every second, we decrement the penalty
87 while (!serverQueue.empty() && (penalty < 5))
89 ServerQueueItem * sqi = (*serverQueue.begin());
90 penalty += sqi->penalty + sqi->getLine().length()/100 + 1;
92 bool res = sendLine(sqi->getLine());
93 serverQueue.erase(serverQueue.begin());
105 #define MNICK (Interp::bot->nickName + "!" + Interp::bot->userHost)
107 ServerQueue::sendCTCP(String to, String command,
110 sendPrivmsg(to, String("\001") + command + " " + message + "\001");
113 if (command == "ACTION")
115 Interp::bot->botInterp->RunHooks (Hook::SEND_ACTION,
127 Interp::bot->botInterp->RunHooks (Hook::SEND_CTCP,
128 MNICK + " " + to + " " +
129 command + " " + message,
146 ServerQueue::sendCTCPReply(String to, String command,
149 sendNotice(to, String("\001") + command + " " +
154 ServerQueue::sendChannelMode(String mode)
156 addLine(mode, CHANNELMODE_PRIORITY, CHANNELMODE_PENALTY,
157 ServerQueueItem::CHANNELMODE);
161 ServerQueue::sendChannelMode(String channel, String mode, String parameters)
163 ServerQueueChannelModeItem * sqcmi =
164 new ServerQueueChannelModeItem(channel, mode, parameters);
169 ServerQueue::sendInvite(String channel, String nick)
171 addLine(String("INVITE ") + nick + " " + channel,
172 INVITE_PRIORITY, INVITE_PENALTY, ServerQueueItem::INVITE);
176 ServerQueue::sendJoin(String channel, String key)
178 addLine(String("JOIN ") + channel + " " + key,
179 JOIN_PRIORITY, JOIN_PENALTY, ServerQueueItem::JOIN);
183 ServerQueue::sendKick(String channel, String nick, String reason)
185 ServerQueueKickItem * sqki =
186 new ServerQueueKickItem(channel, nick, reason);
191 ServerQueue::sendNick(String nick)
193 addLine(String("NICK ") + nick,
194 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::NICK);
198 ServerQueue::sendNotice(String to, String message)
200 ServerQueueNoticeItem *sqni =
201 new ServerQueueNoticeItem(to, message);
206 ServerQueue::sendPart(String channel)
208 addLine(String("PART ") + channel,
209 PART_PRIORITY, PART_PENALTY, ServerQueueItem::PART);
213 ServerQueue::sendPass(String pass)
215 addLine(String("PASS ") + pass,
216 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::PASS);
220 ServerQueue::sendPing(String server)
222 addLine(String("PING :") + server,
223 PING_PRIORITY, PING_PENALTY, ServerQueueItem::PING);
227 ServerQueue::sendPong(String crap)
229 addLine(String("PONG ") + crap,
230 PONG_PRIORITY, PONG_PENALTY, ServerQueueItem::PONG);
234 ServerQueue::sendPrivmsg(String dest, String message)
236 addLine(String("PRIVMSG ") + dest + " :" + message,
237 PRIVMSG_PRIORITY, PRIVMSG_PENALTY, ServerQueueItem::PRIVMSG);
240 if (message[0] != '\001')
241 if (Utils::channel_p (dest))
242 Interp::bot->botInterp->RunHooks (Hook::SEND_PUBLIC,
243 Interp::bot->nickName + " " + dest +
247 (Interp::bot->nickName),
248 Utils::str2scm (dest),
249 Utils::str2scm (message),
252 Interp::bot->botInterp->RunHooks
254 Interp::bot->nickName + " " + dest +
256 scm_list_n (Utils::str2scm (Interp::bot->nickName),
257 Utils::str2scm (dest),
259 (message), SCM_UNDEFINED));
264 ServerQueue::sendQuit(String reason)
266 addLine(String("QUIT :") + reason,
267 QUIT_PRIORITY, QUIT_PENALTY, ServerQueueItem::QUIT);
271 ServerQueue::sendTopic(String channel, String topic)
273 addLine(String("TOPIC ") + channel + " :" + topic,
274 TOPIC_PRIORITY, TOPIC_PENALTY, ServerQueueItem::TOPIC);
278 ServerQueue::sendUser(String username, String ircname)
280 addLine(String("USER ") + username + " 0 * :" + ircname,
281 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::USER);
285 ServerQueue::sendUserMode(String nick, String mode)
287 addLine(String("MODE ") + nick + " " + mode,
288 USERMODE_PRIORITY, USERMODE_PENALTY,
289 ServerQueueItem::USERMODE);
293 ServerQueue::sendUserhost(String nick)
295 addLine(String("USERHOST ") + nick,
296 USERHOST_PRIORITY, USERHOST_PENALTY, ServerQueueItem::USERHOST);
300 ServerQueue::sendWho(String who)
302 addLine(String("WHO ") + who,
303 WHO_PRIORITY, WHO_PENALTY, ServerQueueItem::WHO);
306 Interp::bot->botInterp->RunHooks (Hook::SEND_WHO,
315 ServerQueue::sendWhois(String nick)
317 addLine(String("WHOIS ") + nick,
318 NICK_PRIORITY, WHOIS_PENALTY, ServerQueueItem::WHOIS);
321 Interp::bot->botInterp->RunHooks (Hook::SEND_WHOIS,