int aff_case(t_voir *voir, int i, int fd, int dir, int check) { if (i == 1) check = 2; while (i) { aff_player(voir, fd); aff_all_item(voir, fd); if (upgrade_value(voir, dir) == 0) upgrade_follow(voir, dir); if (i == 1 && check == 1) return (0); if (i == 1 && check == 2) { my_putstr(" ,"); fd_puts(fd, " ,"); return (0); } if (i > 1) { my_putstr(" ,"); fd_puts(fd, " ,"); } i--; } return (0); }
void voir_right(t_player *player, int nb_spot, int fd, int dir) { int i; int z; int tmp; t_voir voir; i = 1; tmp = 1; z = 0; voir.posx = player->posx; voir.posy = player->posy; voir.tmpx = player->posx; voir.tmpy = player->posy; my_putstr("{ "); fd_puts(fd, "{ "); while (i <= nb_spot) { verif_pos_voir(&voir, z); tmp = aff_case(&voir, i, fd, dir, tmp); i += 2; z++; } my_putstr(" }\n"); fd_puts(fd, " }\n"); }
void add_food(int fd, int x, int y) { struct timeval *end; struct timeval *res; float add; if (map[x][y].spot[FOOD] == 0) { printf("\033[0;31mSending message to %d :\033[0;0m ko\n", fd); fd_puts(fd, "ko\n"); return ; } if ((end = get_timeval_planning(fd)) == NULL) { printf("\033[0;31mSending message to %d :\033[0;0m ko\n", fd); fd_puts(fd, "ko\n"); return ; } res = xmalloc(sizeof(struct timeval)); add = ((float) NEW_FOOD / (float) parse.t) * 1000000.; res->tv_sec = end->tv_sec + (long) add / 1000000; res->tv_usec = end->tv_usec + (long) add % 1000000; remove_death(planning, fd); map[x][y].spot[FOOD]--; insert_tlist(planning, strdup("mort\n"), fd, res); printf("\033[0;31mSending message to %d :\033[0;0m ok\n", fd); fd_puts(fd, "ok\n"); }
void get_object(t_player *player, int type, int fd) { int i; int x; int y; x = player->posx; y = player->posy; i = 0; while (i <= 6) { if (i == type && map[player->posx][player->posy].spot[i] > 0) { player->inv[i]++; map[player->posx][player->posy].spot[i]--; printf("\033[0;31mSending message to %d :\033[0;0m ok\n", fd); fd_puts(fd, "ok\n"); return ; } else if (i == type) { printf("\033[0;31mSending message to %d :\033[0;0m ko\n", fd); fd_puts(fd, "ko\n"); return ; } i++; } }
/* smtp_write() -- A printf to an fd and append <CR/LF> */ ssize_t smtp_write(int fd, char *format, ...) { char buf[(BUF_SZ + 2)]; va_list ap; ssize_t outbytes = 0; va_start(ap, format); if(vsnprintf(buf, (BUF_SZ - 1), format, ap) == -1) { die("smtp_write() -- vsnprintf() failed"); } va_end(ap); if(log_level > 0) { log_event(LOG_INFO, "%s\n", buf); } if(minus_v) { (void)fprintf(stderr, "[->] %s\n", buf); } (void)strcat(buf, "\r\n"); outbytes = fd_puts(fd, buf, strlen(buf)); return (outbytes >= 0) ? outbytes : 0; }
void aff_player(t_voir *voir, int fd) { int i; i = map[voir->tmpx][voir->tmpy].nb_player; while (i) { my_putstr("joueur"); fd_puts(fd, "joueur"); i--; if (i > 0) { my_putstr(" "); fd_puts(fd, " "); } else if (i == 0) return ; } }
void aff_item(t_voir *voir, int fd, int type, char *item) { int i; i = map[voir->tmpx][voir->tmpy].spot[type]; if (i > 0) { my_putstr(" "); fd_puts(fd, " "); } while (i) { my_putstr(item); fd_puts(fd, item); i--; if (i > 0) { my_putstr(" "); fd_puts(fd, " "); } } }
/* ** Send a formatted string */ int fd_printf(FDBUF *fdp, const char *fmt, ...) { va_list ap; char buf[8192]; int ecode; va_start(ap, fmt); ecode = s_vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); if (ecode < 0 || ecode >= sizeof(buf)) return EINVAL; return fd_puts(fdp, buf); }
void set_object(t_player *player, int type, int fd) { int i; i = 0; while (i <= 6) { if (i == type) { if (player->inv[i] > 0) { player->inv[i]--; map[player->posx][player->posy].spot[i]++; printf("\033[0;31mSending message to %d :\033[0;0m ok\n", fd); fd_puts(fd, "ok\n"); } else send_ko(fd); return ; } i++; } }
static inline int _fd_getc(FDBUF *fdp) { int c; if (fdp->ungetc != -1) { c = fdp->ungetc; fdp->ungetc = -1; return c; } Again: c = _fd_rgetc(fdp); if (c == EOF) return EOF; /* Simple, basic TELNET protocol handling */ if ((fdp->flags & FDF_TELNET) && c == IAC) { c = _fd_rgetc(fdp); switch (c) { case EOF: return EOF; case AO: fd_purge(fdp); goto Again; case AYT: fd_puts(fdp, "\n[Yes]\n"); fd_flush(fdp); goto Again; case NOP: goto Again; case IP: /* Interrupt process */ /* XXX: What should we do? */ goto Again; case SYNCH: /* Synchronize */ return -2; case WILL: case WONT: c = _fd_rgetc(fdp); if (c == EOF) return EOF; pthread_mutex_lock(&fdp->out_lock); _fd_putc(fdp, IAC); _fd_putc(fdp, DONT); _fd_putc(fdp, c); _fd_flush(fdp); pthread_mutex_unlock(&fdp->out_lock); goto Again; case DO: case DONT: c = _fd_rgetc(fdp); if (c == EOF) return EOF; pthread_mutex_lock(&fdp->out_lock); _fd_putc(fdp, IAC); _fd_putc(fdp, WONT); _fd_putc(fdp, c); _fd_flush(fdp); pthread_mutex_unlock(&fdp->out_lock); goto Again; default: goto Again; } } if ((fdp->flags & FDF_CRLF) && fdp->lastc == '\r' && c == '\n') goto Again; fdp->lastc = c; if ((fdp->flags & FDF_CRLF) && c == '\r') c = '\n'; return c; }
/* ssmtp() -- send the message (exactly one) from stdin to the mailhub SMTP port */ int ssmtp(char *argv[]) { char b[(BUF_SZ + 2)], *buf = b+1, *p, *q; #ifdef MD5AUTH char challenge[(BUF_SZ + 1)]; #endif struct passwd *pw; int i, sock; uid_t uid; bool_t minus_v_save, leadingdot, linestart = True; int timeout = 0; int bufsize = sizeof(b)-1; b[0] = '.'; outbytes = 0; ht = &headers; uid = getuid(); if((pw = getpwuid(uid)) == (struct passwd *)NULL) { die("Could not find password entry for UID %d", uid); } get_arpadate(arpadate); if(read_config() == False) { log_event(LOG_INFO, "%s not found", config_file); } if((p = strtok(pw->pw_gecos, ";,"))) { if((gecos = strdup(p)) == (char *)NULL) { die("ssmtp() -- strdup() failed"); } } revaliases(pw); /* revaliases() may have defined this */ if(uad == (char *)NULL) { uad = append_domain(pw->pw_name); } rt = &rcpt_list; header_parse(stdin); #if 1 /* With FromLineOverride=YES set, try to recover sane MAIL FROM address */ uad = append_domain(uad); #endif from = from_format(uad, override_from); /* Now to the delivery of the message */ (void)signal(SIGALRM, (void(*)())handler); /* Catch SIGALRM */ (void)alarm((unsigned) MAXWAIT); /* Set initial timer */ if(setjmp(TimeoutJmpBuf) != 0) { /* Then the timer has gone off and we bail out */ die("Connection lost in middle of processing"); } if((sock = smtp_open(mailhost, port)) == -1) { die("Cannot open %s:%d", mailhost, port); } else if (use_starttls == False) /* no initial response after STARTTLS */ { if(smtp_okay(sock, buf) == False) die("Invalid response SMTP server"); } /* If user supplied username and password, then try ELHO */ if(auth_user) { outbytes += smtp_write(sock, "EHLO %s", hostname); } else { outbytes += smtp_write(sock, "HELO %s", hostname); } (void)alarm((unsigned) MEDWAIT); if(smtp_okay(sock, buf) == False) { die("%s (%s)", buf, hostname); } /* Try to log in if username was supplied */ if(auth_user) { #ifdef MD5AUTH if(auth_pass == (char *)NULL) { auth_pass = strdup(""); } if(auth_method && strcasecmp(auth_method, "cram-md5") == 0) { outbytes += smtp_write(sock, "AUTH CRAM-MD5"); (void)alarm((unsigned) MEDWAIT); if(smtp_read(sock, buf) != 3) { die("Server rejected AUTH CRAM-MD5 (%s)", buf); } strncpy(challenge, strchr(buf,' ') + 1, sizeof(challenge)); memset(buf, 0, bufsize); crammd5(challenge, auth_user, auth_pass, buf); } else { #endif memset(buf, 0, bufsize); to64frombits(buf, auth_user, strlen(auth_user)); if (use_oldauth) { outbytes += smtp_write(sock, "AUTH LOGIN %s", buf); } else { outbytes += smtp_write(sock, "AUTH LOGIN"); (void)alarm((unsigned) MEDWAIT); if(smtp_read(sock, buf) != 3) { die("Server didn't like our AUTH LOGIN (%s)", buf); } /* we assume server asked us for Username */ memset(buf, 0, bufsize); to64frombits(buf, auth_user, strlen(auth_user)); outbytes += smtp_write(sock, buf); } (void)alarm((unsigned) MEDWAIT); if(smtp_read(sock, buf) != 3) { die("Server didn't accept AUTH LOGIN (%s)", buf); } memset(buf, 0, bufsize); to64frombits(buf, auth_pass, strlen(auth_pass)); #ifdef MD5AUTH } #endif /* We do NOT want the password output to STDERR * even base64 encoded.*/ minus_v_save = minus_v; minus_v = False; outbytes += smtp_write(sock, "%s", buf); minus_v = minus_v_save; (void)alarm((unsigned) MEDWAIT); if(smtp_okay(sock, buf) == False) { die("Authorization failed (%s)", buf); } } /* Send "MAIL FROM:" line */ outbytes += smtp_write(sock, "MAIL FROM:<%s>", uad); (void)alarm((unsigned) MEDWAIT); if(smtp_okay(sock, buf) == 0) { die("%s", buf); } /* Send all the To: adresses */ /* Either we're using the -t option, or we're using the arguments */ if(minus_t) { if(rcpt_list.next == (rcpt_t *)NULL) { die("No recipients specified although -t option used"); } rt = &rcpt_list; while(rt->next) { p = rcpt_remap(rt->string); outbytes += smtp_write(sock, "RCPT TO:<%s>", p); (void)alarm((unsigned)MEDWAIT); if(smtp_okay(sock, buf) == 0) { die("RCPT TO:<%s> (%s)", p, buf); } rt = rt->next; } } else { for(i = 1; (argv[i] != NULL); i++) { p = strtok(argv[i], ","); while(p) { /* RFC822 Address -> "foo@bar" */ q = rcpt_remap(addr_parse(p)); outbytes += smtp_write(sock, "RCPT TO:<%s>", q); (void)alarm((unsigned) MEDWAIT); if(smtp_okay(sock, buf) == 0) { die("RCPT TO:<%s> (%s)", q, buf); } p = strtok(NULL, ","); } } } /* Send DATA */ outbytes += smtp_write(sock, "DATA"); (void)alarm((unsigned) MEDWAIT); if(smtp_read(sock, buf) != 3) { /* Oops, we were expecting "354 send your data" */ die("%s", buf); } outbytes += smtp_write(sock, "Received: by %s (sSMTP sendmail emulation); %s", hostname, arpadate); if(have_from == False) { outbytes += smtp_write(sock, "From: %s", from); } if(have_date == False) { outbytes += smtp_write(sock, "Date: %s", arpadate); } #ifdef HASTO_OPTION if(have_to == False) { outbytes += smtp_write(sock, "To: postmaster"); } #endif ht = &headers; while(ht->next) { outbytes += smtp_write(sock, "%s", ht->string); ht = ht->next; } (void)alarm((unsigned) MEDWAIT); /* End of headers, start body */ outbytes += smtp_write(sock, ""); /*prevent blocking on pipes, we really shouldnt be using stdio functions like fgets in the first place */ fcntl(STDIN_FILENO,F_SETFL,O_NONBLOCK); while(!feof(stdin)) { if (!fgets(buf, bufsize, stdin)) { /* if nothing was received, then no transmission * over smtp should be done */ sleep(1); /* don't hang forever when reading from stdin */ if (++timeout >= MEDWAIT) { log_event(LOG_ERR, "killed: timeout on stdin while reading body -- message saved to dead.letter."); die("Timeout on stdin while reading body"); } continue; } /* Trim off \n, double leading .'s */ leadingdot = standardise(buf, &linestart); if (linestart || feof(stdin)) { linestart = True; outbytes += smtp_write(sock, "%s", leadingdot ? b : buf); } else { if (log_level > 0) { log_event(LOG_INFO, "Sent a very long line in chunks"); } if (leadingdot) { outbytes += fd_puts(sock, b, sizeof(b)); } else { outbytes += fd_puts(sock, buf, bufsize); } } (void)alarm((unsigned) MEDWAIT); } if(!linestart) { smtp_write(sock, ""); } /* End of body */ outbytes += smtp_write(sock, "."); (void)alarm((unsigned) MAXWAIT); if(smtp_okay(sock, buf) == 0) { die("%s", buf); } /* Close connection */ (void)signal(SIGALRM, SIG_IGN); outbytes += smtp_write(sock, "QUIT"); (void)smtp_okay(sock, buf); (void)close(sock); log_event(LOG_INFO, "Sent mail for %s (%s) uid=%d username=%s outbytes=%d", from_strip(uad), buf, uid, pw->pw_name, outbytes); return(0); }