1 /***************************************************************************
2  *   Copyright (C) 2004 by Giacomo Lozito                                  *
3  *   city_hunter@users.sf.net                                              *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 
21 
22 #include "quaqut.h"
23 #include "protocol.h"
24 
quaqut_display_instructions(void)25 void quaqut_display_instructions( void )
26 {
27   printf( "\nUSAGE:   quaqut [-param [argument]] host\n"
28 	  "\nParameters are listed below:\n\n"
29 	  "\t-a\t(default) display complete information\n"
30 	  "\t-s\tdisplay only server information\n"
31 	  "\t-g\tdisplay only game information\n"
32 	  "\t-y\tdisplay only players information\n"
33 	  "\t-p port\tspecify game port for server\n"
34 	  "\t-q port\tspecify query port for server\n"
35 	  "\t-t nnn\tset a custom timeout to wait server reply, nnn\n"
36 	    "\t\tis in tenths of second and can vary from 1 to 999\n"
37 	  "\t-d nnnn\tquaqut daemon mode (keeps querying), nnnn is the\n"
38 	    "\t\tinterval in seconds between queries (from 10 to 9999)\n"
39 	  "\t-f fln\twrite information to file fln instead of printing\n"
40 	    "\t\tthem to standard output (example -f /home/bar/foo.log)\n"
41 	  "\t-r\tdisplay output in raw format\n"
42 	  "\t-v\tdisplay quaqut version\n\n"
43 	  );
44 }
45 
quaqut_display_error(int error_code,char * message,char * extra)46 void quaqut_display_error( int error_code , char* message , char* extra )
47 {
48   if ( error_code == 0 ) /* it is a custom error message */
49   {
50     printf( message , extra );
51   }
52   else /* we have a preset message to display */
53   {
54     switch (error_code)
55     {
56       case QUAQUT_ERROR_INVALIDQUERYTIMEOUT:
57         printf( "Error: invalid timeout value after parameter -t\n" );
58 	break;
59       case QUAQUT_ERROR_INVALIDDAEMONINTERVAL:
60         printf( "Error: invalid interval value after parameter -d\n" );
61 	break;
62       case QUAQUT_ERROR_MISSINGFILE:
63         printf( "Error: missing log file after parameter -f\n" );
64 	break;
65       case QUAQUT_ERROR_MISSINGQUERYTIMEOUT:
66         printf( "Error: missing timeout value after parameter -t\n" );
67 	break;
68       case QUAQUT_ERROR_MISSINGDAEMONINTERVAL:
69         printf( "Error: missing interval value after parameter -d\n" );
70 	break;
71       case QUAQUT_ERROR_MISSINGHOSTNAME:
72         printf( "Error: missing hostname\n" );
73 	break;
74       case QUAQUT_ERROR_MISSINGPORT:
75         printf( "Error: missing port number\n" );
76 	break;
77     }
78   }
79 }
80 
quaqut_display_version(void)81 void quaqut_display_version( void )
82 {
83   printf( "quaqut %s , written by Giacomo Lozito\nquaqut is released under GNU GPL (version 2)\n" , VERSION );
84 }
85 
quaqut_display_process(int protocol,char command)86 int quaqut_display_process( int protocol , char command )
87 {
88   /* Determine where we are going to write ouput (stdout or file) */
89   FILE *destination;
90   if ( quaqut.options & QUAQUT_OPTION_LOGFILE )
91   {
92     destination = fopen( quaqut.output_filename , "w" );
93     if ( !destination )
94     {
95       quaqut_display_error( QUAQUT_ERROR_FILEPROBLEM , NULL , NULL );
96       return 0;
97     }
98   }
99   else destination = stdout;
100 
101   switch ( command )
102   {
103     case 'a':
104       quaqut_display_information( destination , QUAQUT_PROTOCOL_UT2004 , QUAQUT_SERVERINFO );
105       quaqut_display_information( destination , QUAQUT_PROTOCOL_UT2004 , QUAQUT_GAMEINFO );
106       if ( quaqut.buffer_reply_size[QUAQUT_PLAYERINFO] > 0 )
107         quaqut_display_information( destination , QUAQUT_PROTOCOL_UT2004 , QUAQUT_PLAYERINFO );
108       break;
109 
110     case 's':
111       quaqut_display_information( destination , QUAQUT_PROTOCOL_UT2004 , QUAQUT_SERVERINFO );
112       break;
113 
114     case 'g':
115       quaqut_display_information( destination , QUAQUT_PROTOCOL_UT2004 , QUAQUT_GAMEINFO );
116       break;
117 
118     case 'y':
119       quaqut_display_information( destination , QUAQUT_PROTOCOL_UT2004 , QUAQUT_PLAYERINFO );
120       break;
121   }
122 
123   if ( quaqut.options & QUAQUT_OPTION_LOGFILE )
124     fclose(destination);
125   else
126     fflush(stdout);
127 
128   return 1;
129 }
130 
quaqut_display_information(FILE * destination,int protocol,int request)131 void quaqut_display_information( FILE* destination , int protocol , int request )
132 {
133   if ( quaqut.options & QUAQUT_OPTION_RAWINFO )
134   {
135     /* Raw format can be useful if users want to
136        organize ouput within their frontend or whatever */
137     fprintf( destination , "%s\n%s" , quaqut_raw_label[protocol][request] , quaqut.buffer_reply[request] );
138   }
139   else
140   {
141     /* Nicely formatted output :)
142        (which depends on game protocol) */
143 
144     switch ( protocol )
145     {
146       case QUAQUT_PROTOCOL_UT2004:
147         quaqut_display_information_ut2004( destination , request );
148         break;
149     }
150   }
151 }
152 
quaqut_display_information_ut2004(FILE * destination,int request)153 void quaqut_display_information_ut2004( FILE* destination , int request )
154 {
155   switch ( request )
156   {
157     case QUAQUT_SERVERINFO:
158     {
159       if ( quaqut.buffer_reply_entriesnum[request] > 0 )
160       {
161         char* token[quaqut.buffer_reply_entriesnum[request]];
162         int i;
163 
164 	token[0] = (char*)quaqut_utils_strsep( quaqut.buffer_reply[request] , '\n' );
165         for ( i = 1 ; i < quaqut.buffer_reply_entriesnum[request] ; i++ )
166           token[i] = (char*)quaqut_utils_strsep( NULL , '\n' );
167 
168 	fprintf( destination , "\n***** Server Information *****\n" );
169         fprintf( destination , "** Server:  %s\n" , token[4] );
170 	fprintf( destination , "** Port:    %s\n" , token[2] );
171         fprintf( destination , "** Game:    %s - %s\n" , token[6] , token[5] );
172 	fprintf( destination , "** Players: %s/%s\n" , token[7] , token[8] );
173 	fprintf( destination , "** Skill:   %s\n" , token[11] );
174       }
175       else
176       {
177         fprintf( destination , "\n***** Server Information *****\n" );
178         fprintf( destination , "No information to display\n" );
179       }
180       break;
181     }
182 
183     case QUAQUT_GAMEINFO:
184     {
185       if ( quaqut.buffer_reply_entriesnum[request] > 0 )
186       {
187         char* token[quaqut.buffer_reply_entriesnum[request]];
188         int i;
189 
190 	token[0] = (char*)quaqut_utils_strsep( quaqut.buffer_reply[request] , '\n' );
191         for ( i = 1 ; i < quaqut.buffer_reply_entriesnum[request] ; i++ )
192           token[i] = (char*)quaqut_utils_strsep( NULL , '\n' );
193 
194 	fprintf( destination , "\n***** Game Information *****\n" );
195 	for ( i = 0 ; i < quaqut.buffer_reply_entriesnum[request] ; i = i + 2 )
196 	  fprintf( destination , "** %s: %s\n" , token[i] , token[i+1] );
197       }
198       else
199       {
200         fprintf( destination , "\n***** Game Information *****\n" );
201         fprintf( destination , "No information to display\n" );
202       }
203       break;
204     }
205 
206     case QUAQUT_PLAYERINFO:
207     {
208       if ( quaqut.buffer_reply_entriesnum[request] > 0 )
209       {
210         char* token[quaqut.buffer_reply_entriesnum[request]];
211 	int i;
212 
213 	token[0] = (char*)quaqut_utils_strsep( quaqut.buffer_reply[request] , '\n' );
214         for ( i = 1 ; i < quaqut.buffer_reply_entriesnum[request] ; i++ )
215           token[i] = (char*)quaqut_utils_strsep( NULL , '\n' );
216 
217 	fprintf( destination , "\n***** Players Information *****\n" );
218 	fprintf( destination , "** %-25s %-8s %-8s %s\n**\n" , "player name" , "score" , "ping" , "team" );
219 
220 	for ( i = 0 ; i < quaqut.buffer_reply_entriesnum[request] ; i = i + 5 )
221 	  fprintf( destination , "** %-25s %-8s %-8s %c\n" , token[i+1] , token[i+3] , token[i+2] , token[i+4][0] );
222       }
223       else
224       {
225         fprintf( destination , "\n***** Player Information *****\n" );
226         fprintf( destination , "No information to display\n" );
227       }
228       break;
229     }
230 
231   }
232 }
233