Server Help

Bot Questions - free chat function

Cheese - Wed Sep 10, 2008 11:45 pm
Post subject: free chat function
free chat function!

wrote it a few nights ago.
questions/comments/suggestions?


Code: Show/Hide

void botInfo::UnpackChatMsg(char* in_msg, int& channel, char* name, char* msg, char* bigmsg)
{ //Channel Message Unpacker by Cheese
   char buf;
   char buf1;
   char incomingbuf[255]="";
   char chatbuf[2]="";
   char namebuf[20]="";
   char msgbuf[255]="";
   int flag=0;
   int inum=100;
   int c1=0;
   int c2=0;
   int c3=0;
   
   int len=strlen(in_msg);
   for(int i=0; i < len; i++)
   {
      buf=*(in_msg+i);
      buf1=*(in_msg+i+1);
     
      incomingbuf[i]=buf;
      if(i<=1 && buf!=':')
      {
         chatbuf[c1]=buf;
         c1++;
         continue;
      }
      if(buf=='>' && buf1==' ')
      {
         flag=1;
      }
      if(i>=2 && flag==0)
      {
         namebuf[c2]=buf;
         inum=i+2;
         c2++;
         continue;
      }
      if(i>inum)
      {
         namebuf[c2]='\0';
         msgbuf[c3]=buf;
         c3++;
         continue;
      }
   }
   
   
   for(int i4 = 0; i4 < 255; i4++)
   {
      bigmsg[i4] = incomingbuf[i4];
   }
   //sendPublic(incomingbuf);
   //sendPublic("Stuff: Chat#="+(String)chatbuf+" Name="+(String)namebuf+" Message="+(String)msgbuf);
   
   channel=int(chatbuf[0]-48);
   for(int i2 = 0; i2 < 20; i2++)
   {
      name[i2] = namebuf[i2];
   }
   for(int i3 = 0; i3 < 255; i3++)
   {
      msg[i3] = msgbuf[i3];
   }
   
   return;
}


just put the message in the call, and it will return the channel number, person who said it, their message, and the whole undigested part by reference!
Snrrrub - Thu Sep 11, 2008 11:57 am
Post subject:
There's an out-of-bounds array access when you do:

Code: Show/Hide

buf1=*(in_msg+i+1);


On the last iteration of the loop, you're accessing an array element one past the end of the supplied buffer which could result in a crash.

The "namebuf" should be at least 25 bytes long since client <--> game server protocol assumes names with a max length of 24.

The "incomingbuf" variable should be eliminated - it's not necessary since you're just copying "in_msg" into it.

I haven't looked at the entire thing to be completely honest but you don't need to set flags and have strange control flow (continue, break, loops, etc.) to make something like this work - instead you should probably use sscanf and you'll accomplish the same in just a few lines.

I'd post a solution here but then where's the fun in learning about sscanf? icon_smile.gif

-Snrrrub
Cheese - Thu Sep 11, 2008 4:10 pm
Post subject:
well, this was written in mervbot initially, so ill keep [20] over [25] for awhile =P
also gonna keep incomingbuf, because its exclusively for debugging(commented out part)

why use sscanf if -2ez-? icon_razz.gif
Samapico - Thu Sep 11, 2008 4:59 pm
Post subject:
Quote:
well, this was written in mervbot initially, so ill keep [20] over [25] for awhile =P
But incoming CHAT messages might not have the same format... you receive the whole name of the player, and its message. So if you send messages from the Event_Chat to there, it might go BOOM
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group