Fix bug in setting up w3 and w4 games
[capablanca.git] / lasker-2.2.3 / src / fics_addplayer.c
1 /*
2    Copyright (c) 1993 Richard V. Nash.
3    Copyright (c) 2000 Dan Papasian
4    Copyright (C) Andrew Tridgell 2002
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 static void usage(char *progname)
24 {
25   d_printf( "Usage: %s [-l] [-n] UserName FullName EmailAddress\n", progname);
26   exit(1);
27 }
28
29 /* Parameters */
30 static int local = 1;
31 static char *funame = NULL, *fname = NULL, *email = NULL;
32
33 unsigned chessd_reload_flag;
34
35 #define PASSLEN 4
36 int main(int argc, char *argv[])
37 {
38   int admin, i, ch;
39   int p;
40   char password[PASSLEN + 1];
41   char salt[3];
42   char *text;
43   struct player *pp;
44
45   admin = 0;
46
47   while((ch = getopt(argc, argv, "lna:")) != -1) {
48         switch(ch) {
49         case 'l':
50                 local = 1;
51                 break;
52         case 'n':
53                 local = 0;
54                 break;
55         case 'a':
56                 admin = atoi(optarg);
57       }
58   }
59
60   funame = strdup(argv[argc-3]);
61   fname = strdup(argv[argc-2]);
62   email = strdup(argv[argc-1]);
63   if (!funame || !fname || !email)
64     usage(argv[0]);
65
66   /* Add the player here */
67   if (strlen(funame) >= MAX_LOGIN_NAME) {
68     d_printf( "Player name is too long\n");
69     exit(0);
70   }
71   if (strlen(funame) < 3) {
72     d_printf( "Player name is too short\n");
73     exit(0);
74   }
75   if (!alphastring(funame)) {
76     d_printf( "Illegal characters in player name. Only A-Za-z allowed.\n");
77     exit(0);
78   }
79
80   srandom(time(0));
81   p = player_new();
82   if (!player_read(p, funame)) {
83     d_printf( "%s already exists.\n", funame);
84     exit(0);
85   }
86
87   pp = &player_globals.parray[p];
88
89   pp->name = strdup(funame);
90   pp->login = strdup(funame);
91   pp->adminLevel = admin;
92
93   stolower(pp->login);
94   pp->fullName = strdup(fname);
95   pp->emailAddress = strdup(email);
96   for (i = 0; i < PASSLEN; i++) {
97     password[i] = 'a' + random() % 26;
98   }
99   password[i] = '\0';
100   salt[0] = 'a' + random() % 26;
101   salt[1] = 'a' + random() % 26;
102   salt[2] = '\0';
103   pp->passwd = strdup(chessd_crypt(password,salt));
104   PFlagON(p, PFLAG_REG);
105 /*  pp->network_player = !local; */
106   PFlagON(p, PFLAG_RATED);
107   player_save(p);
108 /*  changed by Sparky  12/15/95  Dropped reference to 'Network' players
109     and spiffed it up  */
110 /*
111   printf("Added %s player >%s< >%s< >%s< >%s<\n", local ? "local" : "network",
112          funame, fname, email, password);
113   rasprintf(&text, "\nYou have been added as a %s player.\nIf this is a network account it may take a while to show up on all of the\nclients.\n\nLogin Name: %s\nFull Name: %s\nEmail Address: %s\nInitial Password: %s\n\nIf any of this information is incorrect, please contact the administrator\nto get it corrected.\nPlease write down your password, as it will be your initial passoword\non all of the servers.\n", local ? "local" : "network", funame, fname, email, password);
114 */
115
116   printf("Added %s account: >%s< >%s< >%s< >%s<\n", 
117          admin?"ADMIN":"player",
118          funame, fname, email, password);
119
120   asprintf(&text, "\nYour player account has been created.\n\n"
121    "Login Name: %s\nFull Name: %s\nEmail Address: %s\nInitial Password: %s\n\n"
122    "If any of this information is incorrect, please contact the administrator\n"
123    "to get it corrected.\n\n"
124    "You may change your password with the password command on the the server.\n"
125    "\nPlease be advised that if this is an unauthorized duplicate account for\n"
126    "you, by using it you take the risk of being banned from accessing this\n"
127    "chess server.\n\n"
128    "Regards,\n\nThe FICS admins\n", 
129         funame, fname, email, password);
130
131   free(text);
132
133   mail_string_to_address(email, "FICS Account Created", text);
134   return 0;
135 }