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 int ServerQueue::max_penalty = 20;
30 ServerQueue::ServerQueue(Socket * s, bool d)
31 : Queue(s,d), penalty(0)
38 ServerQueue::~ServerQueue()
40 penalty = std::numeric_limits<int>::min ();
45 ServerQueue::addItem(ServerQueueItem *sqi)
47 BotLock queue_lock (queue_mutex);
48 std::list<ServerQueueItem *>::iterator it, it2;
50 for (it = serverQueue.begin(); it != serverQueue.end(); ++it)
60 if (it2 != serverQueue.end() && *it2)
62 // All right, we try to merge this item to the previous
63 if ((*it2)->merge(sqi))
70 serverQueue.insert(it, sqi);
74 ServerQueue::addLine(String l, int pr, int pen, int t)
76 ServerQueueOtherItem * sqoi =
77 new ServerQueueOtherItem(l, pr, pen, t);
84 // Locking around the entire queue flush prevents another thread
85 // from spamming the queue and preventing lower priority messages
86 // from ever being sent
87 BotLock flush_lock (queue_mutex);
89 // Called every second, we decrement the penalty
95 while (!serverQueue.empty() && (penalty < max_penalty))
97 ServerQueueItem * sqi = serverQueue.front ();
98 penalty += sqi->penalty + sqi->getLine().length()/100;
100 bool res = sendLine(sqi->getLine());
102 serverQueue.pop_front ();
114 #define MNICK (Interp::bot->nickName + "!" + Interp::bot->userHost)
116 ServerQueue::sendCTCP(String to, String command,
119 sendPrivmsg(to, String("\001") + command + " " + message + "\001");
122 if (command == "ACTION")
124 Interp::bot->botInterp->RunHooks (Hook::SEND_ACTION,
136 Interp::bot->botInterp->RunHooks (Hook::SEND_CTCP,
137 MNICK + " " + to + " " +
138 command + " " + message,
155 ServerQueue::sendCTCPReply(String to, String command,
158 sendNotice(to, String("\001") + command + " " +
163 ServerQueue::sendChannelMode(String mode)
165 addLine(mode, CHANNELMODE_PRIORITY, CHANNELMODE_PENALTY,
166 ServerQueueItem::CHANNELMODE);
170 ServerQueue::sendChannelMode(String channel, String mode, String parameters)
172 ServerQueueChannelModeItem * sqcmi =
173 new ServerQueueChannelModeItem(channel, mode, parameters);
178 ServerQueue::sendInvite(String channel, String nick)
180 addLine(String("INVITE ") + nick + " " + channel,
181 INVITE_PRIORITY, INVITE_PENALTY, ServerQueueItem::INVITE);
185 ServerQueue::sendJoin(String channel, String key)
187 addLine(String("JOIN ") + channel + " " + key,
188 JOIN_PRIORITY, JOIN_PENALTY, ServerQueueItem::JOIN);
192 ServerQueue::sendKick(String channel, String nick, String reason)
194 ServerQueueKickItem * sqki =
195 new ServerQueueKickItem(channel, nick, reason);
200 ServerQueue::sendNick(String nick)
202 addLine(String("NICK ") + nick,
203 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::NICK);
207 ServerQueue::sendNotice(String to, String message)
209 ServerQueueNoticeItem *sqni =
210 new ServerQueueNoticeItem(to, message);
215 ServerQueue::sendPart(String channel)
217 addLine(String("PART ") + channel,
218 PART_PRIORITY, PART_PENALTY, ServerQueueItem::PART);
222 ServerQueue::sendPass(String pass)
224 addLine(String("PASS ") + pass,
225 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::PASS);
229 ServerQueue::sendPing(String server)
231 addLine(String("PING :") + server,
232 PING_PRIORITY, PING_PENALTY, ServerQueueItem::PING);
236 ServerQueue::sendPong(String crap)
238 addLine(String("PONG ") + crap,
239 PONG_PRIORITY, PONG_PENALTY, ServerQueueItem::PONG);
243 ServerQueue::sendPrivmsg(String dest, String message)
245 addLine(String("PRIVMSG ") + dest + " :" + message,
246 PRIVMSG_PRIORITY, PRIVMSG_PENALTY, ServerQueueItem::PRIVMSG);
249 if (message[0] != '\001')
250 if (Utils::channel_p (dest))
251 Interp::bot->botInterp->RunHooks (Hook::SEND_PUBLIC,
252 Interp::bot->nickName + " " + dest +
256 (Interp::bot->nickName),
257 Utils::str2scm (dest),
258 Utils::str2scm (message),
261 Interp::bot->botInterp->RunHooks
263 Interp::bot->nickName + " " + dest +
265 scm_list_n (Utils::str2scm (Interp::bot->nickName),
266 Utils::str2scm (dest),
268 (message), SCM_UNDEFINED));
273 ServerQueue::sendQuit(String reason)
275 addLine(String("QUIT :") + reason,
276 QUIT_PRIORITY, QUIT_PENALTY, ServerQueueItem::QUIT);
280 ServerQueue::sendTopic(String channel, String topic)
282 addLine(String("TOPIC ") + channel + " :" + topic,
283 TOPIC_PRIORITY, TOPIC_PENALTY, ServerQueueItem::TOPIC);
287 ServerQueue::sendUser(String username, String ircname)
289 addLine(String("USER ") + username + " 0 * :" + ircname,
290 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::USER);
294 ServerQueue::sendUserMode(String nick, String mode)
296 addLine(String("MODE ") + nick + " " + mode,
297 USERMODE_PRIORITY, USERMODE_PENALTY,
298 ServerQueueItem::USERMODE);
302 ServerQueue::sendUserhost(String nick)
304 addLine(String("USERHOST ") + nick,
305 USERHOST_PRIORITY, USERHOST_PENALTY, ServerQueueItem::USERHOST);
309 ServerQueue::sendWho(String who)
311 addLine(String("WHO ") + who,
312 WHO_PRIORITY, WHO_PENALTY, ServerQueueItem::WHO);
315 Interp::bot->botInterp->RunHooks (Hook::SEND_WHO,
324 ServerQueue::sendWhois(String nick)
326 addLine(String("WHOIS ") + nick,
327 NICK_PRIORITY, WHOIS_PENALTY, ServerQueueItem::WHOIS);
330 Interp::bot->botInterp->RunHooks (Hook::SEND_WHOIS,