+ CLIENT *cl, *from;
+ CHANNEL *chan;
+ char *currentTarget = Req->argv[0];
+ char *lastCurrentTarget = NULL;
+
+ assert(Client != NULL);
+ assert(Req != NULL);
+
+ if (Req->argc == 0) {
+ if (!SendErrors)
+ return CONNECTED;
+ return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
+ Client_ID(Client), Req->command);
+ }
+ if (Req->argc == 1) {
+ if (!SendErrors)
+ return CONNECTED;
+ return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
+ Client_ID(Client));
+ }
+ if (Req->argc > 2) {
+ if (!SendErrors)
+ return CONNECTED;
+ return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
+ Client_ID(Client), Req->command);
+ }
+
+ if (Client_Type(Client) == CLIENT_SERVER)
+ from = Client_Search(Req->prefix);
+ else
+ from = Client;
+ if (!from)
+ return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
+ Client_ID(Client), Req->prefix);
+
+ /* handle msgtarget = msgto *("," msgto) */
+ currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
+ ngt_UpperStr(Req->command);
+
+ while (currentTarget) {
+ /* Check for and handle valid <msgto> of form:
+ * RFC 2812 2.3.1:
+ * msgto = channel / ( user [ "%" host ] "@" servername )
+ * msgto =/ ( user "%" host ) / targetmask
+ * msgto =/ nickname / ( nickname "!" user "@" host )
+ */
+ if (strchr(currentTarget, '!') == NULL)
+ /* nickname */
+ cl = Client_Search(currentTarget);
+ else
+ cl = NULL;
+
+ if (cl == NULL) {
+ /* If currentTarget isn't a nickname check for:
+ * user ["%" host] "@" servername
+ * user "%" host
+ * nickname "!" user "@" host
+ */
+ char target[COMMAND_LEN];
+ char * nick = NULL;
+ char * user = NULL;
+ char * host = NULL;
+ char * server = NULL;
+
+ strlcpy(target, currentTarget, COMMAND_LEN);
+ server = strchr(target, '@');
+ if (server) {
+ *server = '\0';
+ server++;
+ }
+ host = strchr(target, '%');
+ if (host) {
+ *host = '\0';
+ host++;
+ }
+ user = strchr(target, '!');
+ if (user) {
+ /* msgto form: nick!user@host */
+ *user = '\0';
+ user++;
+ nick = target;
+ host = server; /* not "@server" but "@host" */
+ } else {
+ user = target;
+ }