+ /* NB:
+ * For + and - mode characters, we don't just stick the character into the output sequence.
+ * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
+ * appearing in the output sequence, we store a flag which says there was a state change,
+ * which is set on any + or -, however, the + or - that we finish on is only appended to
+ * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
+ */
+ case '+':
+ /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
+ * however, will allow the + if it is the first item in the sequence, regardless.
+ */
+ if ((!adding) || (!output_sequence.length()))
+ state_change = true;
+ adding = true;
+ continue;
+ break;
+ case '-':
+ if ((adding) || (!output_sequence.length()))
+ state_change = true;
+ adding = false;
+ continue;
+ break;
+ default:
+
+ /* 65 is the ascii value of 'A' */
+ handler_id = *modeletter - 65;
+
+ if (modehandlers[handler_id])
+ {
+ bool abort = false;
+ for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
+ {
+ if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
+ abort = true;
+ }
+ if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
+ {
+ if (modehandlers[handler_id]->GetNumParams(adding))
+ {
+ if (pcnt < parameter_counter)
+ {
+ parameter = parameters[parameter_counter++];
+ }
+ else
+ {
+ parameter = "";
+ }
+ }
+ ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
+ if (ma == MODEACTION_ALLOW)
+ {
+ /* We're about to output a valid mode letter - was there previously a pending state-change? */
+ if (state_change)
+ output_sequence.append(adding ? "+" : "-");
+
+ /* Add the mode letter */
+ output_sequence = output_sequence + *modeletter;
+
+ /* Is there a valid parameter for this mode? If so add it to the parameter list */
+ if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
+ {
+ parameter_list << " " << parameter;
+ }
+
+ /* Call all the AfterMode events in the mode watchers for this mode */
+ for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
+ {
+ (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
+ }
+
+ /* Reset the state change flag */
+ state_change = false;
+ }
+ }
+ }
+ break;
+ }
+ }
+ /* Was there at least one valid mode in the sequence? */
+ if (output_sequence != "")
+ {
+ if (servermode)
+ {
+ if (type == MODETYPE_CHANNEL)
+ {
+ WriteChannelWithServ(Config->ServerName,targetchannel,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
+ }
+ }
+ else
+ {
+ if (type == MODETYPE_CHANNEL)
+ {
+ WriteChannel(targetchannel,user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
+ FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
+ }