Exemple #1
1
void		cmd_expulse(t_server *serv, int actual, char *buff)
{
	int		*who;
	int		i;
	int		j;

	(void)buff;
	i = 0;
	who = (int *)ft_memalloc(sizeof(int) * serv->connected);
	j = find_client_to_expulse(serv, actual, who);
	while (i < j)
	{
		if (serv->clients[actual].dir % 2 == 0)
			cmd_expulse2(serv, actual, who[i]);
		else
			cmd_expulse3(serv, actual, who[i]);
		if (serv->gfx.sock)
			ppo(serv->gfx.sock, &serv->clients[who[i]]);
		cmd_expulse4(serv, actual, who[i]);
		i++;
	}
	if (j == 0)
		write_client(serv->clients[actual].sock, "ko");
	else
		write_client(serv->clients[actual].sock, "ok");
}
Exemple #2
0
void		connect_nbr(t_server *serv, int actual, char *buff)
{
	int		i;
	int		nb;
	char	*tmp;
	int		eggs;

	(void)buff;
	i = 0;
	nb = get_nbr(serv, actual);
	eggs = 0;
	while (i < MAX_CLIENTS && serv->teams[i].name &&
		ft_strcmp(serv->teams[i].name, serv->clients[actual].team))
		i++;
	if (i < MAX_CLIENTS && serv->teams[i].name)
		eggs = serv->teams[i].baby;
	if ((serv->max_clients - nb) + eggs < 0)
	{
		write_client(serv->clients[actual].sock, "ko");
		return ;
	}
	tmp = ft_itoa((serv->max_clients - nb) + eggs);
	write_client(serv->clients[actual].sock, tmp);
	free(tmp);
}
Exemple #3
0
void			list_msg(int sock, char **channels, int j)
{
	int		i;

	write_client(sock, "\x1B[33mList of channels: ");
	i = 0;
	while (i < j)
	{
		write_client(sock, "\n");
		write_client(sock, channels[i]);
		i++;
	}
	write_client(sock, "\033[0m\n");
}
Exemple #4
0
/*
 * Returns okay response and outputs file to client
 */
int ok(int clientsd, char *timec, FILE *requestfile, char * totalsizec)
{
	ssize_t totalwrite, buf;
	char buffer[BUFFER_SIZE] = {0};
	totalwrite = 0;
	buf = 0;

	write_client(clientsd, "HTTP/1.1 200 OK\n");
	write_client(clientsd, "Date: ");
	write_client(clientsd, timec);
	write_client(clientsd, "\nContent-Type: text/html\n");
	write_client(clientsd, "Content-Length: ");
	write_client(clientsd, totalsizec);
	write_client(clientsd, "\n\n");

	while (fgets(buffer, BUFFER_SIZE, requestfile) != NULL)
	{
		buf = write_client(clientsd, buffer);
		if (buf == -1)
		{
			return totalwrite;
		}
		totalwrite += buf;
	}
	return totalwrite;
}
void		client_talking(t_server *server, int *actual, char *buff)
{
	int			i;
	t_client	client;
	char		*tmp;

	i = 0;
	while (i < *actual)
	{
		if (FD_ISSET(server->clients[i].sock, &(server->rdfs)))
		{
			client = server->clients[i];
			if (read_client(server->clients[i].sock, buff) == 0)
			{
				if_read(server, actual, buff, i);
			}
			else if (buff[0] == '/')
			{
				if ((tmp = cmd(buff, &(server->clients[i]), server)))
					write_client(server->clients[i].sock, tmp);
			}
			else
				send_to_all(server->clients, client, *actual, buff);
			break ;
		}
		i++;
	}
}
Exemple #6
0
void		buffer_send(int sock, char *add)
{
  static char	*buffer;
  static int	size_buf;
  int		size_tmp;

  if (buffer == NULL)
    {
      buffer = malloc(BUF_SIZE);
      buffer[0] = 0;
    }
  if (add != NULL)
    size_tmp = strlen(add);
  if ((size_buf + size_tmp + 1 > BUF_SIZE || add == NULL) && buffer != NULL)
    {
      write_client(sock, buffer);
      free(buffer);
      buffer = NULL;
      size_buf = 0;
    }
  if (add != NULL)
    {
      strcat(buffer, add);
      size_buf += size_tmp;
      buffer[size_buf] = '\n';
      size_buf++;
    }
}
int			new_client(t_server *server, int *actual, char *buff)
{
	int				csock;
	t_sockaddr_in	csin;
	socklen_t		sinsize;

	sinsize = sizeof(csin);
	csock = accept(server->sock, (t_sockaddr *)&csin, &sinsize);
	if (csock == SOCKET_ERROR)
	{
		error("accept");
		return (-1);
	}
	if (read_client(csock, buff) == -1)
		return (-1);
	while (check_pseudo(buff, server) == -1)
	{
		write_client(csock, "Pseudo already used");
		if (read_client(csock, buff) == -1)
			return (-1);
	}
	FD_SET(csock, &(server->rdfs));
	server->max = csock > server->max ? csock : server->max;
	init_client(server, *actual, buff, csock);
	(*actual)++;
	return (csock);
}
Exemple #8
0
void		cmd_droite(t_server *serv, int actual, char *buff)
{
	(void)buff;
	serv->clients[actual].dir = (serv->clients[actual].dir + 1) % 4;
	if (serv->gfx.sock)
		ppo(serv->gfx.sock, &serv->clients[actual]);
	write_client(serv->clients[actual].sock, "ok");
}
Exemple #9
0
void		edi(int csock, int num_egg)
{
	char	*msg;

	msg = ft_trunc("edi #%d", num_egg);
	write_client(csock, msg);
	free(msg);
}
Exemple #10
0
void		sgt(int csock, time_t *tm)
{
	char	*msg;

	msg = ft_trunc("edi #%d", tm);
	write_client(csock, msg);
	free(msg);
}
Exemple #11
0
static void		invite_msg2(t_serveur *serveur, t_client *client, int sock)
{
	char	*msg;

	msg = ft_strdup("\x1B[36mYou invited ");
	msg = ft_strjoin(msg, serveur->clients[sock].name);
	msg = ft_strjoin(msg, " in channel.\033[0m\n");
	write_client(client->sock, msg);
	msg = ft_strdup("\x1B[36m");
	msg = ft_strjoin(msg, client->name);
	msg = ft_strjoin(msg, " invite you to join channel ");
	msg = ft_strjoin(msg, client->channel);
	msg = ft_strjoin(msg, " type \"/join ");
	msg = ft_strjoin(msg, client->channel);
	msg = ft_strjoin(msg, "\" to join.\033[0m\n");
	write_client(serveur->clients[sock].sock, msg);
}
Exemple #12
0
void		pfk(int csock, t_client *client)
{
	char	*msg;

	msg = ft_trunc("pfk #%d", client->sock);
	write_client(csock, msg);
	free(msg);
}
Exemple #13
0
static void	cmd_expulse4(t_server *serv, int actual, int who)
{
	int		ret;
	char	*str;

	ret = expulser_direction(serv, actual, who);
	str = ft_trunc("deplacement %d", ret);
	write_client(serv->clients[who].sock, str);
}
Exemple #14
0
void file_not_found(int clientsd, char *timec)
{
	write_client(clientsd, "HTTP/1.1 404 Not Found\n");
	write_client(clientsd, "Date: ");
	write_client(clientsd, timec);
	write_client(clientsd, "\nContent-Type: text/html\n");
	write_client(clientsd, "Content-Length: 117\n\n");
	write_client(clientsd, "<html><body>\n<h2>Document not found</h2>\n");
	write_client(clientsd, "You asked for a document that doesn't exist. That is so sad.\n");
	write_client(clientsd, "</body></html>");
}
Exemple #15
0
void internal_server_error(int clientsd, char *timec)
{
	write_client(clientsd, "HTTP/1.1 500 Internal Server Error\n");
	write_client(clientsd, "Date: ");
	write_client(clientsd, timec);
	write_client(clientsd, "\nContent-Type: text/html\n");
	write_client(clientsd, "Content-Length: 131\n\n");
	write_client(clientsd, "<html><body>\n<h2>That Didn't work</h2>\n");
	write_client(clientsd, "I had some sort of problem dealing with your request. Sorry, I'm lame.\n");
	write_client(clientsd, "</body></html>");
}
Exemple #16
0
void forbidden(int clientsd, char *timec)
{
	write_client(clientsd, "HTTP/1.1 403 Forbidden\n");
	write_client(clientsd, "Date: ");
	write_client(clientsd, timec);
	write_client(clientsd, "\nContent-Type: text/html\n");
	write_client(clientsd, "Content-Length: 130\n\n");
	write_client(clientsd, "<html><body>\n<h2>Permission Denied</h2>\n");
	write_client(clientsd, "You asked for a document you are not permitted to see. It sucks to be you.\n");
	write_client(clientsd, "</body></html>");
}
Exemple #17
0
void		msz(int csock, t_map *map)
{
	char	*msg;

	msg = ft_trunc("msz %d %d", \
					map->x, \
					map->y);
	write_client(csock, msg);
	free(msg);
}
Exemple #18
0
void bad_request(int clientsd, char *timec)
{
	write_client(clientsd, "HTTP/1.1 400 Bad Request\n");
	write_client(clientsd, "Date: ");
	write_client(clientsd, timec);
	write_client(clientsd, "\nContent-Type: text/html\n");
	write_client(clientsd, "Content-Length: 107\n\n");
	write_client(clientsd, "<html><body>\n<h2>Malformed Request</h2>\n");
	write_client(clientsd, "Your broswer sent a request I could not understand.\n");
	write_client(clientsd, "</body></html>");

}
Exemple #19
0
static void	cmd_avance2(t_server *serv, int actual)
{
	if (serv->gfx.sock)
		ppo(serv->gfx.sock, &serv->clients[actual]);
	write_client(serv->clients[actual].sock, "ok");
	printf("YOUR POSITION IS NOW: x: %d y: %d dir:%d\n",
		serv->clients[actual].x, serv->clients[actual].y,
		serv->clients[actual].dir);
	printf("player %d avance\n", actual);
}
Exemple #20
0
void		cmd_fork(t_server *serv, int actual, char *buff)
{
	int		i;

	(void)buff;
	i = cmd_fork_loop(serv, actual);
	if (serv->gfx.sock)
		enw(serv->gfx.sock, &serv->teams[i], serv->teams[i].eggs - 1,
	serv->clients[actual].sock);
	write_client(serv->clients[actual].sock, "ok");
}
Exemple #21
0
void		cmd_gauche(t_server *serv, int actual, char *buff)
{
	if ((serv->clients[actual].dir - 1) == -1)
		serv->clients[actual].dir = 3;
	else
		serv->clients[actual].dir--;
	if (serv->gfx.sock)
		ppo(serv->gfx.sock, &serv->clients[actual]);
	write_client(serv->clients[actual].sock, "ok");
	(void)buff;
}
Exemple #22
0
char		*ft_who(char *buff, t_client *client, t_server *server)
{
	int			i;
	int			count;

	i = 0;
	count = 0;
	(void)buff;
	while (i < MAX_CLIENTS)
	{
		if (server->clients[i].name)
		{
			if (check_channel(*client, server->clients[i]))
			{
				if (count)
					write_client(client->sock, "\n");
				write_client(client->sock, server->clients[i].name);
				count++;
			}
		}
		i++;
	}
	return (NULL);
}
Exemple #23
0
int			manage_client(t_server *server,
				      fd_set *rfds,
				      fd_set *wfds)
{
  t_client		*client;

  client = find_client(server->root_clients, rfds, wfds);
  if (client)
  {
    if (FD_ISSET(client->fd, rfds))
      read_client(server, client);
    else
      write_client(server, client);
  }
  return (0);
}
Exemple #24
0
void		ft_wait(t_server *serv, t_cmd *cmd, int actual, char *buff)
{
	double					time;
	double					delta;
	int						i;

	time = (double)((double)clock() / (double)CLOCKS_PER_SEC);
	delta = (double)0;
	if (serv->clients[actual].last_cmd_end > time)
		delta = serv->clients[actual].last_cmd_end - (double)time;
	delta = (double)((double)cmd->time / (double)serv->t) + (double)delta;
	serv->clients[actual].last_cmd_end = (double)time + delta;
	if (!ft_strncmp("incantation", buff, 11))
		cmd_pre_incantation(serv, actual, buff);
	i = ft_wait_loop(serv, cmd, actual, buff);
	if (i == 10)
		write_client(serv->clients[actual].sock, "ko");
}
Exemple #25
0
t_client	*manage_select(t_client *client, t_time *time, int const sfd)
{
  fd_set	fd_read;
  fd_set	fd_write;
  int		fd_max;

  fd_max = fd_set_client(&fd_read, &fd_write, client, sfd);
  if (pselect(fd_max + 1, &fd_read, &fd_write, NULL, time, NULL) == -1)
    {
      perror("select()");
      g_keep_running = false;
      return (client);
    }
  if (FD_ISSET(sfd, &fd_read) && (client = add_client(client, sfd)) == NULL)
    {
      g_keep_running = false;
      return (client);
    }
  return (write_client(&fd_write, read_client(&fd_read, client)));
}
Exemple #26
0
static void send_message_to_all_clients(Client *clients, Client sender, int actual, const char *buffer, char from_server)
{
    int i = 0;
    char message[BUF_SIZE];
    message[0] = 0;
    for(i = 0; i < actual; i++)
    {
	/* on n'envoie pas de message à l'envoyeur */
	if(sender.sock != clients[i].sock)
	{
	    if(from_server == 0)
	    {
		strncpy(message, sender.name, BUF_SIZE - 1);
		strncat(message, " : ", sizeof message - strlen(message) - 1);
	    }
	    strncat(message, buffer, sizeof message - strlen(message) - 1);
	    write_client(clients[i].sock, message, clients);
	}
    }
}
Exemple #27
0
/**
* Envoyer un message à tout les clients connectés
**/
static void send_message_to_all_clients(Client *clients, Client sender, int actual, const char *buffer, char from_server)
{
   int i = 0;
   char message[BUF_SIZE];
   message[0] = 0;
   for(i = 0; i < actual; i++)
   {
      /* On n'envoi pas le message à l'expéditeur*/
      if(sender.sock != clients[i].sock)
      {
         /* So c'est un message ne venant pas du serveur on affiche un pseudos*/
         if(from_server == 0)
         {
            strncpy(message, sender.name, BUF_SIZE - 1);
            strncat(message, " : ", sizeof message - strlen(message) - 1);
         }
         strncat(message, buffer, sizeof message - strlen(message) - 1);
         write_client(clients[i].sock, message);
      }
   }
}
Exemple #28
0
void		pic(int csock, int actual, t_server *serv)
{
	char	*msg;
	int		i;

	i = -1;
	msg = ft_trunc("pic %d %d %d #%d", \
					serv->clients[actual].x, \
					serv->clients[actual].y, \
					serv->clients[actual].lvl, \
					serv->clients[actual].sock);
	while (serv->clients[++i].sock)
	{
		if ((actual != i) &&
			(serv->clients[actual].x == serv->clients[i].x) &&
			(serv->clients[actual].y == serv->clients[i].y))
			msg = ft_trunc("%s #%d", msg, serv->clients[actual].sock);
	}
	write_client(csock, msg);
	free(msg);
}
Exemple #29
0
static void filter_server_data(char* data, ssize_t size)
{
  if(saved_label.len > 0) {
    /* Skip continuation data */
    if(data[0] != '+') {
      int resp;
      /* Check if the response is tagged with the saved label */
      str_copyb(&linebuf, data, size);
      resp = parse_label();
      if(resp > 0) {
	if(!str_diff(&label, &saved_label)) {
	  log_line(data, size);
	  /* Check if the response was an OK */
	  if(!strncasecmp(linebuf.s + resp, "OK ", 3))
	    accept_client(username.s);
	  else
	    deny_client(username.s);
	  str_truncate(&saved_label, 0);
	}
      }
    }
  }
  write_client(data, size);
}
Exemple #30
0
void		cmd_pre_incantation(t_server *serv, int actual, char *buff)
{
	int		i;
	int		level;
	int		x;
	int		y;

	(void)buff;
	x = serv->clients[actual].x;
	y = serv->clients[actual].y;
	i = 0;
	level = serv->clients[actual].lvl;
	if (!verif_lvl_tb(serv, actual))
		return ;
	while (i < serv->connected)
	{
		if (serv->gfx.sock)
			pic(serv->gfx.sock, actual, serv);
		if (serv->clients[i].x == x && serv->clients[i].y == y &&
				level == serv->clients[i].lvl)
			write_client(serv->clients[i].sock, "elevation en cours");
		i++;
	}
}