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