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
27 #include "BotInterp.H"
28 #include "ServerQueue.H"
31 int ServerQueue::max_penalty = 20;
33 ServerQueue::ServerQueue(Socket * s, bool d)
34 : Queue(s,d), penalty(0)
41 ServerQueue::~ServerQueue()
43 penalty = std::numeric_limits<int>::min ();
48 ServerQueue::addItem(ServerQueueItem *sqi)
50 BotLock queue_lock (queue_mutex);
51 std::list<ServerQueueItem *>::iterator it, it2;
53 for (it = serverQueue.begin(); it != serverQueue.end(); ++it)
63 if (it2 != serverQueue.end() && *it2)
65 // All right, we try to merge this item to the previous
66 if ((*it2)->merge(sqi))
73 serverQueue.insert(it, sqi);
77 ServerQueue::addLine(String l, int pr, int pen, int t)
79 ServerQueueOtherItem * sqoi =
80 new ServerQueueOtherItem(l, pr, pen, t);
87 // Locking around the entire queue flush prevents another thread
88 // from spamming the queue and preventing lower priority messages
89 // from ever being sent
90 BotLock flush_lock (queue_mutex);
92 // Called every second, we decrement the penalty
98 while (!serverQueue.empty() && (penalty < max_penalty))
100 ServerQueueItem * sqi = serverQueue.front ();
101 penalty += sqi->penalty + sqi->getLine().length()/100;
103 bool res = sendLine(sqi->getLine());
105 serverQueue.pop_front ();
117 #define MNICK (Interp::bot->nickName + "!" + Interp::bot->userHost)
119 ServerQueue::sendCTCP(String to, String command,
122 sendPrivmsg(to, String("\001") + command + " " + message + "\001");
125 if (command == "ACTION")
127 Interp::bot->botInterp->RunHooks (Hook::SEND_ACTION,
139 Interp::bot->botInterp->RunHooks (Hook::SEND_CTCP,
140 MNICK + " " + to + " " +
141 command + " " + message,
158 ServerQueue::sendCTCPReply(String to, String command,
161 sendNotice(to, String("\001") + command + " " +
166 ServerQueue::sendChannelMode(String mode)
168 addLine(mode, CHANNELMODE_PRIORITY, CHANNELMODE_PENALTY,
169 ServerQueueItem::CHANNELMODE);
173 ServerQueue::sendChannelMode(String channel, String mode, String parameters)
175 ServerQueueChannelModeItem * sqcmi =
176 new ServerQueueChannelModeItem(channel, mode, parameters);
181 ServerQueue::sendInvite(String channel, String nick)
183 addLine(String("INVITE ") + nick + " " + channel,
184 INVITE_PRIORITY, INVITE_PENALTY, ServerQueueItem::INVITE);
188 ServerQueue::sendJoin(String channel, String key)
190 addLine(String("JOIN ") + channel + " " + key,
191 JOIN_PRIORITY, JOIN_PENALTY, ServerQueueItem::JOIN);
195 ServerQueue::sendKick(String channel, String nick, String reason)
197 ServerQueueKickItem * sqki =
198 new ServerQueueKickItem(channel, nick, reason);
203 ServerQueue::sendNick(String nick)
205 addLine(String("NICK ") + nick,
206 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::NICK);
210 ServerQueue::sendNotice(String to, String message)
212 ServerQueueNoticeItem *sqni =
213 new ServerQueueNoticeItem(to, message);
218 ServerQueue::sendPart(String channel)
220 addLine(String("PART ") + channel,
221 PART_PRIORITY, PART_PENALTY, ServerQueueItem::PART);
225 ServerQueue::sendPass(String pass)
227 addLine(String("PASS ") + pass,
228 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::PASS);
232 ServerQueue::sendPing(String server)
234 addLine(String("PING :") + server,
235 PING_PRIORITY, PING_PENALTY, ServerQueueItem::PING);
239 ServerQueue::sendPong(String crap)
241 addLine(String("PONG ") + crap,
242 PONG_PRIORITY, PONG_PENALTY, ServerQueueItem::PONG);
246 ServerQueue::sendPrivmsg(String dest, String message)
248 addLine(String("PRIVMSG ") + dest + " :" + message,
249 PRIVMSG_PRIORITY, PRIVMSG_PENALTY, ServerQueueItem::PRIVMSG);
252 if (message[0] != '\001')
253 if (Utils::channel_p (dest))
254 Interp::bot->botInterp->RunHooks (Hook::SEND_PUBLIC,
255 Interp::bot->nickName + " " + dest +
259 (Interp::bot->nickName),
260 Utils::str2scm (dest),
261 Utils::str2scm (message),
264 Interp::bot->botInterp->RunHooks
266 Interp::bot->nickName + " " + dest +
268 scm_list_n (Utils::str2scm (Interp::bot->nickName),
269 Utils::str2scm (dest),
271 (message), SCM_UNDEFINED));
276 ServerQueue::sendQuit(String reason)
278 addLine(String("QUIT :") + reason,
279 QUIT_PRIORITY, QUIT_PENALTY, ServerQueueItem::QUIT);
283 ServerQueue::sendTopic(String channel, String topic)
285 addLine(String("TOPIC ") + channel + " :" + topic,
286 TOPIC_PRIORITY, TOPIC_PENALTY, ServerQueueItem::TOPIC);
290 ServerQueue::sendUser(String username, String ircname)
292 addLine(String("USER ") + username + " 0 * :" + ircname,
293 NICK_PRIORITY, NICK_PENALTY, ServerQueueItem::USER);
297 ServerQueue::sendUserMode(String nick, String mode)
299 addLine(String("MODE ") + nick + " " + mode,
300 USERMODE_PRIORITY, USERMODE_PENALTY,
301 ServerQueueItem::USERMODE);
305 ServerQueue::sendUserhost(String nick)
307 addLine(String("USERHOST ") + nick,
308 USERHOST_PRIORITY, USERHOST_PENALTY, ServerQueueItem::USERHOST);
312 ServerQueue::sendWho(String who)
314 addLine(String("WHO ") + who,
315 WHO_PRIORITY, WHO_PENALTY, ServerQueueItem::WHO);
318 Interp::bot->botInterp->RunHooks (Hook::SEND_WHO,
327 ServerQueue::sendWhois(String nick)
329 addLine(String("WHOIS ") + nick,
330 NICK_PRIORITY, WHOIS_PENALTY, ServerQueueItem::WHOIS);
333 Interp::bot->botInterp->RunHooks (Hook::SEND_WHOIS,