1 /*
2   main.h
3 
4   Argh, this is sooo messy. I have to clean it someday ;)
5 */
6 
7 #define TIME_WITH_SYS_TIME 1
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <limits.h>
11 #include <string.h>
12 #include <netdb.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #if HAVE_SYS_SELECT_H
16 #include <sys/select.h>
17 #endif
18 
19 #include <netinet/in.h>
20 #ifndef LINUX    /* linux gets wacko if you include both */
21 #ifndef __CYGWIN32__  /* Cygnus Win32 isn't all that happy with it either
22 */
23 #include <netinet/tcp.h>
24 #endif
25 #endif           /* but virtually every other OS NEEDS both */
26 #include <arpa/inet.h>      /* is this really necessary? */
27 #include <stdarg.h>
28 #include <errno.h>
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #include <fcntl.h>
33 
34 /* almost every module needs some sort of time thingy, so... */
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
37 # include <time.h>
38 #else
39 # if HAVE_SYS_TIME_H
40 #  include <sys/time.h>
41 # else
42 #  include <time.h>
43 # endif
44 #endif
45 
46 /* Defines */
47 #define TETVERSION "1.13"		/* What Tetrinet version we are for */
48 #define SERVERBUILD "16"		/* What build we are at */
49 #define NICKLEN 30			/* Maximum length of Nickname */
50 #define VERLEN 10			/* Maximum length of Tetrinet version */
51 #define UHOSTLEN 30			/* Maximum length of Hostname */
52 #define TEAMLEN NICKLEN			/* Maximum length of teamname */
53 /*#define MAXNET 80*/			/* Maximum network sockets */
54 #define MAXWINLIST 100			/* Maximum entries on Winlist */
55 #define TELNET_PORT 31457		/* Telnet port to listen on */
56 #define QUERY_PORT 31456		/* Query port to listen on */
57 #define FIELD_MAXX 12			/* Maximum horizontal size of playing field */
58 #define FIELD_MAXY 22			/* Maximum vertical size of playing field */
59 #define PIDFILELEN 80			/* Length of name of PID file */
60 #define SDMSGLEN 80			/* Length of Sudden Death Message */
61 #define PASSLEN 12			/* Length of Password */
62 #define MAXPLAYERS 6			/* Maximum number of players allowed */
63 #define CHANLEN 16			/* Length of channel names */
64 #define IPLEN 16			/* Size of IP */
65 #define DEFAULTCHANNEL "tetrinet"	/* Default channel name */
66 #define DEFAULTMAXPLAYERS 6		/* Default max players in channel */
67 #define DEFAULTPRIORITY 50		/* Default priority */
68 #define DESCRIPTIONLEN 31		/* Description */
69 
70 #define STATE_OFFLINE 0 	/* Offline */
71 #define STATE_ONLINE  1		/* Not in game */
72 #define STATE_INGAME  2		/* In game */
73 #define STATE_PAUSED  3		/* In game but paused */
74 
75 #define NET_FREE 0		/* Unused at the momen */
76 #define NET_TELNET 1		/* Person has just connected */
77 #define NET_TELNET_INIT 2	/* Person is sending init sequence */
78 #define NET_WAITINGFORTEAM 3	/* Waiting for initial team */
79 #define NET_CONNECTED 4		/* Everything works out, they're connected */
80 #define NET_QUERY 5		/* Person has connected to Query Port */
81 #define NET_QUERY_INIT 6	/* Person is sending query request */
82 #define NET_LOST 9		/* Lost connection */
83 
84 #define STAT_NOTPLAYING 0	/* Not playing */
85 #define STAT_PLAYING 1		/* Currently playing */
86 #define STAT_LOST 2		/* Playing, but lost */
87 
88 #define SD_NONE 0		/* No mode. Ignore timeout */
89 #define SD_INIT 1		/* Wait for timeout, it's first suddendeath */
90 #define SD_WAIT 2		/* Waiting between lines */
91 
92 #define LEVEL_UNUSED 0		/* Unused level */
93 #define LEVEL_NORMAL 1		/* Unauthenticated Person */
94 #define LEVEL_OP     2		/* Op by position player */
95 #define LEVEL_AUTHOP 3		/* OP'ed person (/op) */
96 
97 #define CYCLE 1				/* How many (s) is 1 cycle */
98 
99 typedef unsigned long IP;
100 
101 /* public structure of all the net connections */
102 
103 struct channel_t {
104   char name[CHANLEN+1];			/* Name of the channel */
105   int priority;				/* Priority of channel */
106   int maxplayers;			/* Maximum players allowed */
107   unsigned char status;			/* STATE_XXXXXX */
108   struct channel_t *next;		/* Next in the queue */
109   char description[DESCRIPTIONLEN];	/* Description */
110   char persistant;			/* 1=can't delete */
111   struct net_t *net;			/* Net structure */
112 
113   int sd_timeleft;			/* Sudden Death Timeout */
114   int sd_mode;				/* What SD mode we're in - SD_XXXX */
115 
116   /* Channel Settings */
117   int starting_level;
118   int lines_per_level;
119   int level_increase;
120   int lines_per_special;
121   int special_added;
122   int special_capacity;
123   int classic_rules;
124   int average_levels;
125   int sd_timeout;
126   int sd_lines_per_add;
127   int sd_secs_between_lines;
128   char sd_message[SDMSGLEN];
129 
130   int block_leftl;
131   int block_leftz;
132   int block_square;
133   int block_rightl;
134   int block_rightz;
135   int block_halfcross;
136   int block_line;
137   int special_addline;
138   int special_clearline;
139   int special_nukefield;
140   int special_randomclear;
141   int special_switchfield;
142   int special_clearspecial;
143   int special_gravity;
144   int special_quakefield;
145   int special_blockbomb;
146 
147   int stripcolour;		/* Strip colour from gmsg's */
148   int serverannounce;		/* Server announces winner */
149   int pingintercept;		/* Intercept ping's */
150 
151 
152 };
153 
154 /* Server Config */
155 struct game_t {
156   char bindip[IPLEN+1];
157   int maxchannels;
158   int starting_level;
159   int lines_per_level;
160   int level_increase;
161   int lines_per_special;
162   int special_added;
163   int special_capacity;
164   int classic_rules;
165   int average_levels;
166   int sd_timeout;
167   int sd_lines_per_add;
168   int sd_secs_between_lines;
169   char sd_message[SDMSGLEN+1];
170   int timeout_ingame;
171   int timeout_outgame;
172 
173   int block_leftl;
174   int block_leftz;
175   int block_square;
176   int block_rightl;
177   int block_rightz;
178   int block_halfcross;
179   int block_line;
180   int special_addline;
181   int special_clearline;
182   int special_nukefield;
183   int special_randomclear;
184   int special_switchfield;
185   int special_clearspecial;
186   int special_gravity;
187   int special_quakefield;
188   int special_blockbomb;
189 
190   int stripcolour;		/* Strip colour from gmsg's */
191   int serverannounce;		/* Server announces winner */
192   int pingintercept;		/* Intercept ping's */
193 
194   int command_clear;		/* Allow clearing of winlist? */
195   int command_kick;		/* Allow Kicks? */
196   int command_msg;		/* Allow msg's? */
197   int command_op;		/* Allow command OP */
198   int command_winlist;		/* Allow winlist */
199   int command_help;		/* Allow help */
200   int command_list;		/* Allow list */
201   int command_join;		/* Allow join */
202   int command_who;		/* Allow who */
203   int command_topic;		/* Allow topic */
204   int command_priority;		/* Allow priority */
205   int command_move;		/* Allow move */
206   int command_set;		/* Allow set */
207   int command_persistant;
208   int command_save;
209   int command_reset;
210 
211   int verbose;			/* Verbosity */
212   char pidfile[PIDFILELEN+1];
213 };
214 
215 
216 
217 struct net_t {
218   int sock; 				/* Socket this player is on */
219   IP addr; 				/* IP address of player */
220   unsigned int port; 			/* Port number they connected to */
221   char nick[NICKLEN+1];			/* Nickname of player */
222   char team[TEAMLEN+1]; 		/* Teamname of player */
223   char host[UHOSTLEN+1];		/* Resolved host */
224   char version[VERLEN+1];		/* TetriNET version */
225   int gameslot;				/* What slot (1-6) they occupy */
226   int level;				/* What playing level they've reached */
227   unsigned char field[FIELD_MAXX][FIELD_MAXY];   /* Playing Field of player */
228   unsigned char status;			/* Current Status - STAT_XXXXX */
229   int timeout;				/* Timeout on socket */
230   int securitylevel;			/* What security LEVEL - LEVEL_XXX */
231 
232   struct channel_t *channel;		/* What channel we're on */
233   unsigned char type; 			/* What this record type is - NET_XXXXXX */
234 
235   struct net_t *next;			/* Next in list */
236 };
237 
238 /* Winlist Structure */
239 struct winlist_t {
240   char status;				/* Type. p=player, t=team */
241   char name[NICKLEN+1];			/* Name of player/team */
242   unsigned long int score;		/* What they scored */
243   char inuse;				/* 1=inuse 0=available */
244 };
245 
246 
247 
248 /* Security structure */
249 struct security_t {
250   char op_password[PASSLEN+1];		/* Password to take ops */
251 };
252 
253 
254 
255 
256 
257 
258 
259 
260 
261 
262 /* Colours defined here */
263 /* I found these defined in "TetriNET Color Addon for mIRC" by TNL */
264 #define BOLD		2
265 #define ITALIC		22
266 #define UNDERLINE	31
267 #define BLACK		4
268 #define DARKGRAY	6
269 #define SILVER		15
270 #define NAVY		17
271 #define BLUE		5
272 #define CYAN		3
273 #define GREEN		12
274 #define NEON		14
275 #define TEAL		23
276 #define BROWN		16
277 #define RED		20
278 #define MAGENTA		8
279 #define VIOLET		19
280 #define YELLOW		25
281 #define WHITE		24
282 
283 /* net list */
284 struct net_t *gnet;			/* Start of global "socket" information */
285 struct game_t game;			/* Game Configuration */
286 struct winlist_t winlist[MAXWINLIST];	/* Winlist */
287 struct security_t security;		/* Security structure */
288 struct channel_t *chanlist;		/* Channel structure */
289 
290 /* And the proto types */
291 void net_telnet_init(struct net_t *n, char *buf);
292 void net_telnet(struct net_t *n, char *buf);
293 void net_query_init(struct net_t *n, char *buf);
294 void net_query(struct net_t *n, char *buf);
295 void lostnet(struct net_t *n);
296 void net_connected(struct net_t *n, char *buf);
297 void net_waitingforteam(struct net_t *n, char *buf);
298 void init_main(void);
299 void lprintf(char *format, ...);
300 void lvprintf(int priority, char *format, ...);
301