Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check contents of string pointer

Status
Not open for further replies.

tomchesley

Technical User
Apr 16, 2006
17
US
I am a newbee in C / C++
I currently have the current IRC Hybrid server 7.2.3 working successfully.
I would like to modify it just a little to only allow names that are 12 characters in length and are all uppercase letters between A and P
I have the length test working great, that wasnt to hard.
But not sure how to test the contents of the name/NICK
Any assistance is appreciated
My current code that i am modifying is below

Code:
  // source_p->name = NICK
  // source_p->host = IP in the form of x.x.x.x where x = 0 - 255

  int nlen;
  nlen = strlen(source_p->name);

  //test if 12 char NICK if not disconnect them
  if (nlen !=12)
  {
	  exit_client(source_p, &me, "BAD NICK");
	  return;
  }

  sendto_one(source_p, form_str(RPL_WELCOME), me.name, source_p->name, 
             ServerInfo.network_name, source_p->name);
  sendto_one(source_p, form_str(RPL_YOURHOST), me.name, source_p->name,
             get_listener_name(source_p->localClient->listener), ircd_version);
  sendto_one(source_p, form_str(RPL_CREATED),
             me.name, source_p->name, built_date);
  sendto_one(source_p, form_str(RPL_MYINFO),
             me.name, source_p->name, me.name, ircd_version, umode_buffer);
  show_isupport(source_p);
 
I would do something like this:
Code:
/* Check if any letters aren't between A & P. */
for ( int i = 0; i < strlen( str ); ++i )
{
    if ( strcspn( str, "ABCDEFGHIJKLMNOP" ) > 0 )
    {
        /* String is bad, do something here. */
        break;
    }
}
 
using what you had, i had to set i as int before the for because gcc didnt like the int = 0 in the for loop
the code only checked first letter then, dont think the temp is going to next character so i added temp++ at end of for with no luck
i removed the break because it would crash after the disconnect
I then set the len to the len of the original name

this seemed to work.

Code:
char *temp = source_p->name;
int i;
for ( i = 0; i < strlen(source_p->name); ++i )
{
    if ( strcspn( temp, "ABCDEFGHIJKLMNOP" ) > 0 )
    {
        /* String is bad, do something here. */
        /* disconnect the user tell them bad nick */
        exit_client(source_p, &me, "Nick not A-P");
        return
    }
    temp++
}
 
Oops, I think I was combining two different thoughts I had...
I'm pretty sure this is all you need:
Code:
if ( strcspn( temp, "ABCDEFGHIJKLMNOP" ) > 0 )
{
    /* String is bad, do something here. */
    /* disconnect the user tell them bad nick */
    exit_client(source_p, &me, "Nick not A-P");
    return
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top