home *** CD-ROM | disk | FTP | other *** search
/ Amiga Times / AmigaTimes.iso / spiele / FreeCiv / src / freeciv-1.7.0 / server / handchat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-06  |  2.9 KB  |  92 lines

  1. /********************************************************************** 
  2.  Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
  3.    This program is free software; you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation; either version 2, or (at your option)
  6.    any later version.
  7.  
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.
  12. ***********************************************************************/
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16.  
  17. #include <game.h>
  18. #include <packets.h>
  19. #include <player.h>
  20. #include <shared.h>
  21.  
  22.  
  23. /**************************************************************************
  24. ...
  25. **************************************************************************/
  26. void handle_chat_msg(struct player *pplayer, 
  27.              struct packet_generic_message *packet)
  28. {
  29.   int i;
  30.   struct packet_generic_message genmsg;
  31.   char *cp;
  32.  
  33.   /* this loop to prevent players from sending multiple lines
  34.    * which can be abused */
  35.   genmsg.x = -1;
  36.   genmsg.y = -1;
  37.   genmsg.event =-1;
  38.   for(cp=packet->message; *cp; ++cp)
  39.     if(!isprint(*cp & 0x7f)) {
  40.       *cp='\0';
  41.       break;
  42.     }
  43.   
  44.   cp=strchr(packet->message, ':');
  45.   
  46.   if (cp && *(cp-1)!=' ') {
  47.     struct player *pdest=0;
  48.     int nlen, nmatches=0;
  49.     char name[MAX_LENGTH_NAME];
  50.  
  51.     nlen=MIN(MAX_LENGTH_NAME-1, cp-packet->message);
  52.     strncpy(name, packet->message, nlen);
  53.     name[nlen]='\0';
  54.      
  55.     if(!(pdest=find_player_by_name(name))) {
  56.       /* no full match,  now look for pre-match */
  57.       for(i=0; i<game.nplayers; ++i) {
  58.     int j;
  59.     for(j=0; j<nlen; ++j)
  60.       if(tolower(packet->message[j])!=tolower(game.players[i].name[j]))
  61.         break;
  62.     if(j==nlen) {
  63.       ++nmatches;
  64.       pdest=&game.players[i];
  65.     }
  66.       }
  67.     }
  68.                
  69.     if(pdest && nmatches<=1) {
  70.       sprintf(genmsg.message, "->*%s* %s", pdest->name, cp+1+(*(cp+1)==' '));
  71.       send_packet_generic_message(pplayer->conn, PACKET_CHAT_MSG, &genmsg);
  72.       sprintf(genmsg.message, "*%s* %s",
  73.           pplayer->name, cp+1+(*(cp+1)==' '));
  74.       send_packet_generic_message(pdest->conn, PACKET_CHAT_MSG, &genmsg);
  75.     }
  76.     else if(nmatches>=2) {
  77.       sprintf(genmsg.message, "Game: %s is an ambiguous name-prefix", name);
  78.       send_packet_generic_message(pplayer->conn, PACKET_CHAT_MSG, &genmsg);
  79.     }
  80.     else {
  81.       sprintf(genmsg.message, "Game: there's no player by the name %s", name);
  82.       send_packet_generic_message(pplayer->conn, PACKET_CHAT_MSG, &genmsg);
  83.     }
  84.   }
  85.   else {
  86.     sprintf(genmsg.message, "<%s> %s", pplayer->name, packet->message);
  87.     for(i=0; i<game.nplayers; i++)
  88.       send_packet_generic_message(game.players[i].conn, PACKET_CHAT_MSG, 
  89.                   &genmsg);
  90.   }
  91. }
  92.