void transport_driver(int in,int cn) { int x,y,a,oldx,oldy,nr,mirror,oldmirror; unsigned long long bit; struct transport_ppd *dat; unsigned char buf[16]; if (!cn) return; // always make sure its not an automatic call if you don't handle it dat=set_data(cn,DRD_TRANSPORT_PPD,sizeof(struct transport_ppd)); if (!dat) return; // oops... // mark new transports as seen nr=it[in].drdata[0]; if (nr!=255) { // not clan exit if (nr<0 || nr>=ARRAYSIZE(trans)) { elog("illegal transport nr %d from item %d (%s) #1",nr,in,it[in].name); log_char(cn,LOG_SYSTEM,0,"Nothing happens - BUG (%d,#1).",nr); return; } bit=1<<nr; if (!(dat->seen&bit)) { log_char(cn,LOG_SYSTEM,0,"You have reached a new transportation point."); dat->seen|=bit; } } if (ch[cn].act2==0) { buf[0]=SV_TELEPORT; *(unsigned long long*)(buf+1)=dat->seen; psend(ch[cn].player,buf,9); return; } nr=(ch[cn].act2&255)-1; mirror=(ch[cn].act2/256); if (mirror<1 || mirror>26) mirror=RANDOM(26)+1; // clan hall is target if (nr>63 && nr<64+60) { if (may_enter_clan(cn,nr-63)) { x=17+((nr-64)/12)*49; y=16+((nr-64)%12)*20; a=13; } else { x=43+((nr-64)/12)*49; y=15+((nr-64)%12)*20; a=13; } } else if (nr<64) { bit=1<<nr; if (!(dat->seen&bit)) { log_char(cn,LOG_SYSTEM,0,"You've never been to %s before. You cannot go there.",trans[nr].name); return; } if (nr==22 && !(ch[cn].flags&CF_ARCH)) { log_char(cn,LOG_SYSTEM,0,"Sorry, Arches only!"); return; } if (nr<0 || nr>=ARRAYSIZE(trans)) { //elog("illegal transport nr %d #2",nr); log_char(cn,LOG_SYSTEM,0,"Nothing happens - BUG (%d,#2).",nr); return; } x=trans[nr].x; y=trans[nr].y; a=trans[nr].a; } else { log_char(cn,LOG_SYSTEM,0,"You've confused me. (BUG #1123)"); return; } if (x<1 || x>MAXMAP-2 || y<1 || y>MAXMAP-2) { log_char(cn,LOG_SYSTEM,0,"Nothing happens - BUG (%d,%d,%d).",x,y,a); return; } oldmirror=ch[cn].mirror; ch[cn].mirror=mirror; if ((ch[cn].flags&CF_PLAYER) && a && (a!=areaID || get_mirror(a,ch[cn].mirror)!=areaM)) { if (!change_area(cn,a,x,y)) { log_char(cn,LOG_SYSTEM,0,"Nothing happens - target area server is down."); ch[cn].mirror=oldmirror; } return; } buf[0]=SV_MIRROR; *(unsigned int*)(buf+1)=ch[cn].mirror; psend(ch[cn].player,buf,5); oldx=ch[cn].x; oldy=ch[cn].y; remove_char(cn); if (!drop_char(cn,x,y,0)) { log_char(cn,LOG_SYSTEM,0,"Please try again soon. Target is busy"); drop_char(cn,oldx,oldy,0); } }
GUI_status GUI_TextInput::KeyDown(SDL_Keysym key) { char ascii = get_ascii_char_from_keysym(key); if(!focused) return GUI_PASS; if(!isprint(ascii) && key.sym != SDLK_BACKSPACE) { KeyBinder *keybinder = Game::get_game()->get_keybinder(); ActionType a = keybinder->get_ActionType(key); switch(keybinder->GetActionKeyType(a)) { case NORTH_KEY: key.sym = SDLK_UP; break; case SOUTH_KEY: key.sym = SDLK_DOWN; break; case WEST_KEY: key.sym = SDLK_LEFT; break; case EAST_KEY: key.sym = SDLK_RIGHT; break; case TOGGLE_CURSOR_KEY: release_focus(); return GUI_PASS; // can tab through to SaveDialog case DO_ACTION_KEY: key.sym = SDLK_RETURN; break; case CANCEL_ACTION_KEY: key.sym = SDLK_ESCAPE; break; case HOME_KEY: key.sym = SDLK_HOME; case END_KEY: key.sym = SDLK_END; default : if(keybinder->handle_always_available_keys(a)) return GUI_YUM; break; } } switch(key.sym) { case SDLK_LSHIFT : case SDLK_RSHIFT : case SDLK_LCTRL : case SDLK_RCTRL : case SDLK_CAPSLOCK : break; case SDLK_KP_ENTER: case SDLK_RETURN : if(callback_object) callback_object->callback(TEXTINPUT_CB_TEXT_READY, this, text); case SDLK_ESCAPE : release_focus(); break; case SDLK_HOME : pos = 0; break; case SDLK_END : pos = length; break; case SDLK_KP_4 : case SDLK_LEFT : if(pos > 0) pos--; break; case SDLK_KP_6 : case SDLK_RIGHT : if(pos < length) pos++; break; case SDLK_DELETE : if(pos < length) //delete the character to the right of the cursor { pos++; remove_char(); break; } break; case SDLK_BACKSPACE : remove_char(); break; //delete the character to the left of the cursor case SDLK_UP : case SDLK_KP_8 : if(pos == length) { if(length+1 > max_width * max_height) break; length++; if(pos == 0 || text[pos-1] == ' ') text[pos] = 'A'; else text[pos] = 'a'; break; } text[pos]++; // We want alphanumeric characters or space if(text[pos] < ' ' || text[pos] > 'z') { text[pos] = ' '; break; } while(!isalnum(text[pos])) text[pos]++; break; case SDLK_KP_2 : case SDLK_DOWN : if(pos == length) { if(length+1 > max_width * max_height) break; length++; if(pos == 0 || text[pos-1] == ' ') text[pos] = 'Z'; else text[pos] = 'z'; break; } text[pos]--; // We want alphanumeric characters or space if(text[pos] < ' ' || text[pos] > 'z') { text[pos] = 'z'; break; } else if(text[pos] < '0') { text[pos] = ' '; break; } while(!isalnum(text[pos])) text[pos]--; break; default : if(isprint(ascii)) add_char(ascii); break; } return(GUI_YUM); }
int do_send(void){ struct stat fileStat; int fd, sockd = -1, is_set = 0; uint32_t fsize = 0, size_to_send = 0, num_of_file = 0; off_t offset = 0; ssize_t rc, tx = 0; static char buffer[2], tmp_buf[BUFSIZ]; char *ipv4_server = NULL, *tmp_input = NULL, *input_file = NULL, *dup_input = NULL; char *token = NULL, *tmp_token = NULL, *file = NULL, *is_gnome = "'"; static struct sockaddr_in remote_server_addr; static struct hostent *hp; printf("Write the IPv4 server address: "); /* SE SCRIVE TIPO CUSTOM=HOME ALLORA CERCARE SUL FILE */ if(fgets(tmp_buf, BUFSIZ, stdin) == NULL){ printf("fgets error\n"); return -1; } /* HO DOPPIO INSERIMENTO DI IP CON FGETS E SCANF...VEDERE COME SISTEMARE */ token = strtok(tmp_buf, "="); if(strcmp(token, "CUSTOM") == 0){ token = strtok(NULL, "\n"); ipv4_server = get_ip_from_file(token); if(strcmp(ipv4_server, "ERR") == 0){ printf("Cannot get the ip from the file, exiting...\n"); return -1; } //goto new_start; } else{ token = strtok(tmp_buf, "\n"); ipv4_server = (char *)malloc(strlen(token)+1); strcpy(ipv4_server, token); } /*if(scanf("%m[^\n]%*c", &ipv4_server) == EOF){ printf("\nscanf ERROR, exiting...\n"); return -1; } new_start:*/ /* Info sul server remoto */ hp = gethostbyname(ipv4_server); remote_server_addr.sin_family = AF_INET; remote_server_addr.sin_port = 15000; remote_server_addr.sin_addr.s_addr = ((struct in_addr*)(hp->h_addr)) -> s_addr; if((sockd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ printf("Error during socket creation\n"); free(ipv4_server); return -1; } if(connect(sockd, (struct sockaddr *) &remote_server_addr, sizeof(remote_server_addr)) < 0){ printf("Connection error\n"); free(ipv4_server); close(sockd); return -1; } printf("Drag (or write) the file(s) to send: "); if(scanf("%m[^\n]%*c", &tmp_input) == EOF){ printf("\nscanf ERROR, exiting...\n"); free(ipv4_server); close(sockd); return -1; } if(*tmp_input == *is_gnome) remove_char(tmp_input, '\''); /* se il primo carattere è ' allora lo tolgo */ is_set = 1; input_file = strdup(tmp_input); replace_space_with_vbar(input_file); token = NULL; dup_input = strdup(input_file); token = strtok(dup_input, "|"); while(token != NULL){ token = strtok(NULL, "|"); num_of_file++; } free(dup_input); token = NULL; if(send(sockd, &num_of_file, sizeof(num_of_file), 0) < 0){ printf("Failed to send the number of files\n"); if(is_set == 1) free(input_file); free(tmp_input); free(ipv4_server); free(input_file); close(sockd); return -1; } tmp_token = strtok(input_file, "|"); printf("%s\n", tmp_token); while(tmp_token != NULL){ rc = offset = 0; if(*tmp_token == '@') remove_char(tmp_token, '@'); if(stat(tmp_token, &fileStat) < 0){ printf("stat ERROR, exiting...\n"); if(is_set == 1) free(input_file); free(tmp_input); free(ipv4_server); free(input_file); close(sockd); return -1; } if(S_ISDIR(fileStat.st_mode)){ printf("ERROR: This is a directory not file, exiting...\n"); free(tmp_input); if(is_set == 1) free(input_file); free(ipv4_server); close(sockd); return -1; } file = basename(tmp_token); /* basename potrebbe modificare input_file così ne passo una copia e sono al sicuro */ fsize = strlen(file)+1; if(send(sockd, &fsize, sizeof(fsize), 0) < 0){ printf("Error on sending the file name length\n"); free(ipv4_server); if(is_set == 1) free(input_file); free(tmp_input); close(sockd); return -1; } if(send(sockd, file, fsize, 0) < 0){ printf("Error on sending the file name\n"); free(ipv4_server); if(is_set == 1) free(input_file); free(tmp_input); close(sockd); return -1; } fsize = fileStat.st_size; if(send(sockd, &fsize, sizeof(fsize), 0) < 0){ printf("Error on sending the file size\n"); free(ipv4_server); if(is_set == 1) free(input_file); free(tmp_input); close(sockd); return -1; } printf("Waiting for server confirmation...\n"); if(recv(sockd, buffer, 2, 0) < 0){ printf("Error on receiving Y or N\n"); free(ipv4_server); if(is_set == 1) free(input_file); free(tmp_input); close(sockd); return -1; } if(strcmp(buffer, "N") == 0){ printf("The server has aborted the file transfer\n"); free(ipv4_server); if(is_set == 1) free(input_file); free(tmp_input); close(sockd); return -2; } fd = open(tmp_token, O_RDONLY); if(fd < 0){ printf("Error on opening file\n"); free(ipv4_server); if(is_set == 1) free(input_file); free(tmp_input); close(sockd); return -1; } tx = 0; for(size_to_send = fsize; size_to_send > 0; ){ rc = sendfile(sockd, fd, &offset, size_to_send); if(rc <= 0){ printf("Error on sendfile"); close(fd); if(is_set == 1) free(input_file); free(ipv4_server); free(tmp_input); close(sockd); return -1; } tx += rc; printf("\r%d%%", (tx * 100 / fsize)); fflush(NULL); size_to_send -= rc; } close(fd); printf("\n"); tmp_token = strtok(NULL, "|"); } free(tmp_input); if(is_set == 1) free(input_file); free(ipv4_server); printf("--> File(s) successfully sent\n"); close(sockd); return 0; }