/*
       _             __
  ____(_)__ _______ / /____ ____
 / __/ (_-</ __(_-</ __/ -_) __/
/_/ /_/___/\__/___/\__/\__/_/

Napster client for RISC OS
Copyright (C) 2000 Robert Dimond

Portions are based on gnap by Ryan Dahl.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA

Bransley Cottage, Cleobury Mortimer, Kidderminster, WORCS. DY14 0BZ
England. robdimond@cwcom.net

*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "oslib/socket.h"
#include "oslib/osfile.h"



#define HOSTPORT 8876
#define HOSTIP "208.184.216.222" // no longer used, we resolve it now.
/*#define HOSTIP "64.224.114.33"*/
/*#define HOSTIP "204.244.63.65"*/

#define HOSTNAME "server.napster.com"
#define VERSION_STRING /*"v2.0 BETA 3"*/ "v2.0 BETA 10.3"

typedef enum connect_level {
notc, nap_res, alt_res, tc_redir, get_redir, tc_server, try_login, loggedin} connect_level;

typedef enum download_active {
freesl, window, getinfo, tc, get_one, get_header, download} download_active;

typedef enum message_status {get_head, get_data} message_status;

typedef enum protocolv {proto_old, proto_new} protocolv;



typedef struct transfer {
 char user[30];
 char filename[255];
 char header[50];
 char checksum[100];
 int headcount;
 FILE * filehandle;
 /*int socket;*/
 socket_s socket;
 int tag;
 int update_tag;
 unsigned long int size;
 unsigned long int totalsize;
 unsigned long int oldsize;
 download_active active;
 int whandle;
 struct search_result * searchres;
} transfer;

typedef struct {
 char header[300];
 int hpos;
 int active;
 socket_s socket;
 int command;
} incoming;

void napster_resolve_redir(void);
void napster_resolve_alt(void);
int napster_poll(void);
int ipconv(char * ip);
int portconv(int port);
void napster_get_best_host(unsigned int * ip, int * port, char * string);
int napster_connect(void);
int napster_serverconnect(unsigned int ip, int port);
void napster_send(char * data, int type, int length);
void napster_handle_input(void);
void napster_handle_message(int mc, char * buffer);
void napster_handle_download(int);
void napster_handle_header(int);
void napster_mess_error(char * message);
void napster_mess_loginerror(char * message);
void napster_mess_searchresponse(char * buffer);
void napster_mess_completeresponse(char * line);
void napster_mess_loginack(char * email);
void napster_mess_alreadyreg(void);
void napster_mess_regsuccess(void);
void napster_mess_dlack(char * buffer);
void napster_mess_motd(char * buffer);
void napster_mess_searchend(void);
int napster_mo_userlogin(char * username, char * password, int port, int connection, int new_user);
int napster_mo_dlrequest(search_result * toget);
void napster_download_end(int);
char * napster_sanefile(char * file);
int napster_init_server(void);
int napster_end_server(void);
void napster_incoming(socket_s newsocket);
void napster_handle_inconheader(int ino);
void napster_close(void);
int nerror(os_error * err);
void napster_handle_nfconnected(int tno);
void napster_handle_one(int tno);
void napster_mess_privatemessage(char * buffer);









