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