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