Encapsulate obscure detail that lower numeric priority is higher queue priority
[clinton/bobotpp.git] / source / ServerQueueItem.C
1 // ServerQueueItem.C -*- C++ -*-
2 // Copyright (c) 1998 Etienne BERNARD
3 // Copyright (c) 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 #include "StringTokenizer.H"
21 #include "ServerQueue.H"
22 #include "ServerQueueItem.H"
23
24 ServerQueueItem::ServerQueueItem(int pr, int pen, int t)
25 : priority(pr), penalty(pen), type(t)
26 { }
27
28 ServerQueueOtherItem::ServerQueueOtherItem(String l, int pr,
29 int pen, int t)
30 : ServerQueueItem(pr, pen, t), line(l)
31 { }
32
33 bool ServerQueueItem::operator< (const ServerQueueItem & item)
34 {
35 // lower numeric priority is higher priority
36 return item.priority < priority;
37 }
38
39 String
40 ServerQueueOtherItem::getLine()
41 {
42 return line;
43 }
44
45 ServerQueueChannelModeItem::ServerQueueChannelModeItem(String c, String m, String p)
46 : ServerQueueItem(CHANNELMODE_PRIORITY,
47 CHANNELMODE_PENALTY,
48 CHANNELMODE),
49 channel(c), mode(m), parameters(p)
50 {
51 StringTokenizer st(p);
52 paramcount = st.count_tokens();
53 }
54
55 bool
56 ServerQueueChannelModeItem::merge(ServerQueueItem *sqi)
57 {
58 if (sqi->type != CHANNELMODE)
59 return false;
60
61 ServerQueueChannelModeItem * sqcmi = (ServerQueueChannelModeItem *)sqi;
62
63 if (sqcmi->channel != channel || (paramcount + sqcmi->paramcount > 3))
64 return false;
65
66 char sign = '+';
67 char c;
68
69 for (int i = 0; i < mode.length(); i++) {
70 c = mode[i];
71 switch (c) {
72 case '+':
73 case '-':
74 sign = c;
75 }
76 }
77
78 char othersign = '+';
79
80 for (int i = 0; i < sqcmi->mode.length(); i++) {
81 c = sqcmi->mode[i];
82 switch (c) {
83 case '+':
84 case '-':
85 othersign = c;
86 break;
87 default:
88 if (othersign == sign)
89 mode = mode + String(c);
90 else {
91 mode = mode + String(othersign) + String(c);
92 sign = othersign;
93 }
94 }
95 }
96
97 parameters = parameters + " " + sqcmi->parameters;
98 paramcount += sqcmi->paramcount;
99 penalty += sqcmi->penalty;
100
101 return true;
102 }
103
104 String
105 ServerQueueChannelModeItem::getLine()
106 {
107 return String("MODE ") + channel + " " + mode + " " + parameters;
108 }
109
110
111
112 ServerQueueKickItem::ServerQueueKickItem(String c, String w, String r)
113 : ServerQueueItem(KICK_PRIORITY,
114 KICK_PENALTY,
115 KICK),
116 channel(c), who(w), reason(r), count(1)
117 { }
118
119 bool
120 ServerQueueKickItem::merge(ServerQueueItem *sqi)
121 {
122 if (sqi->type != KICK)
123 return false;
124 ServerQueueKickItem * sqki = (ServerQueueKickItem *)sqi;
125 if (sqki->channel != channel)
126 return false;
127 if (count + sqki->count > 4)
128 return false;
129 who = who + "," + sqki->who;
130 if (reason != sqki->reason)
131 reason = "Mixed kick ! Top efficiency";
132 penalty += sqki->penalty;
133 count += sqki->count;
134
135 return true;
136 }
137
138 String
139 ServerQueueKickItem::getLine()
140 {
141 return String("KICK ") + channel + " " + who + " :" + reason;
142 }
143
144 ServerQueueNoticeItem::ServerQueueNoticeItem(String d, String m)
145 : ServerQueueItem(NOTICE_PRIORITY,
146 NOTICE_PENALTY,
147 NOTICE),
148 dest(d), message(m), count(1)
149 { }
150
151 bool
152 ServerQueueNoticeItem::merge(ServerQueueItem *sqi)
153 {
154 if (sqi->type != NOTICE)
155 return false;
156 ServerQueueNoticeItem * sqni = (ServerQueueNoticeItem *)sqi;
157 if (sqni->message != message)
158 return false;
159 if (count + sqni->count > 4)
160 return false;
161 dest = dest + "," + sqni->dest;
162 penalty += sqni->penalty;
163 count += sqni->count;
164
165 return true;
166 }
167
168 String
169 ServerQueueNoticeItem::getLine()
170 {
171 return String("NOTICE ") + dest + " :" + message;
172 }