reindent files, add some glue to allow mailing from within the ttyplay session

git-svn-id: svn://katsu.triplehelix.org/dgamelaunch/trunk@13 db0b04b0-f4d1-0310-9a6d-de3e77497b0e
This commit is contained in:
Joshua Kwan 2003-12-31 01:58:56 +00:00
parent 7971e5b99a
commit 363b57f1a2
4 changed files with 120 additions and 86 deletions

View File

@ -81,7 +81,7 @@ extern struct winsize win;
/* local functions */ /* local functions */
static void writefile (int); static void writefile (int);
static void write_canned_rcfile (char*); static void write_canned_rcfile (char *);
static int userexist (char *); static int userexist (char *);
static int passwordgood (char *, char *); static int passwordgood (char *, char *);
@ -92,12 +92,13 @@ int pid_game = 0;
int loggedin = 0; int loggedin = 0;
char rcfilename[80]; char rcfilename[80];
char ttyrec_filename[100]; char ttyrec_filename[100];
char *chosen_name;
/* preallocate this mem. bad, but ohwell. is only for pointers */ /* preallocate this mem. bad, but ohwell. is only for pointers */
/* makes a max number of users compiled in */ /* makes a max number of users compiled in */
int f_num = 0; int f_num = 0;
struct dg_user** users = NULL; struct dg_user **users = NULL;
struct dg_user* me = NULL; struct dg_user *me = NULL;
/* ************************************************************* */ /* ************************************************************* */
/* for ttyrec */ /* for ttyrec */
@ -227,9 +228,9 @@ inprogressmenu ()
strncpy (m_time, replacestr + 12, 8); strncpy (m_time, replacestr + 12, 8);
mvprintw (7 + i, 1, "%c) %-15s %s %s (%ldm %lds idle)", mvprintw (7 + i, 1, "%c) %-15s %s %s (%ldm %lds idle)",
i + 97, m_name, m_date, m_time, i + 97, m_name, m_date, m_time,
(time (&ctime) - pstat.st_mtime) / 60, (time (&ctime) - pstat.st_mtime) / 60,
(time (&ctime) - pstat.st_mtime) % 60); (time (&ctime) - pstat.st_mtime) % 60);
i++; i++;
} }
} }
@ -251,6 +252,11 @@ inprogressmenu ()
/* valid choice has been made */ /* valid choice has been made */
snprintf (ttyrecname, 130, "%s%s", LOC_TTYRECDIR, snprintf (ttyrecname, 130, "%s%s", LOC_TTYRECDIR,
games[menuchoice - 97]); games[menuchoice - 97]);
chosen_name = strdup (games[menuchoice - 97]);
if (!(replacestr = strstr (chosen_name, ":")))
exit (145);
else
*replacestr = '\0';
replacestr = strstr (ttyrecname, ":"); replacestr = strstr (ttyrecname, ":");
if (!replacestr) if (!replacestr)
exit (145); exit (145);
@ -306,8 +312,8 @@ changepw ()
if (buf && *buf == '\0') if (buf && *buf == '\0')
return; return;
if (strstr(buf, ":") != NULL) if (strstr (buf, ":") != NULL)
exit(112); exit (112);
mvaddstr (12, 1, "And again:"); mvaddstr (12, 1, "And again:");
mvaddstr (13, 1, "=> "); mvaddstr (13, 1, "=> ");
@ -320,7 +326,7 @@ changepw ()
error = 1; error = 1;
} }
me->password = strdup(crypt (buf, buf)); me->password = strdup (crypt (buf, buf));
writefile (0); writefile (0);
} }
@ -537,7 +543,7 @@ freefile ()
} }
if (users) if (users)
free(users); free (users);
users = NULL; users = NULL;
f_num = 0; f_num = 0;
@ -559,17 +565,17 @@ initncurses ()
/* ************************************************************* */ /* ************************************************************* */
struct dg_user * struct dg_user *
deep_copy (struct dg_user * src) deep_copy (struct dg_user *src)
{ {
struct dg_user *dest = malloc(sizeof (struct dg_user)); struct dg_user *dest = malloc (sizeof (struct dg_user));
dest->username = strdup(src->username); dest->username = strdup (src->username);
dest->email = strdup(src->email); dest->email = strdup (src->email);
dest->env = strdup(src->env); dest->env = strdup (src->env);
dest->password = strdup(src->password); dest->password = strdup (src->password);
dest->flags = src->flags; dest->flags = src->flags;
return dest; return dest;
} }
void void
@ -606,10 +612,10 @@ login ()
error = 1; error = 1;
if ((me_index = userexist (user_buf)) != -1) if ((me_index = userexist (user_buf)) != -1)
{ {
me = deep_copy(users[me_index]); me = deep_copy (users[me_index]);
error = 0; error = 0;
} }
} }
clear (); clear ();
@ -644,10 +650,10 @@ newuser ()
loggedin = 0; loggedin = 0;
if (me) if (me)
free(me); free (me);
me = malloc (sizeof (struct dg_user));
me = malloc(sizeof(struct dg_user));
while (error) while (error)
{ {
clear (); clear ();
@ -672,7 +678,7 @@ newuser ()
if (userexist (buf) == -1) if (userexist (buf) == -1)
error = 0; error = 0;
else else
error = 1; error = 1;
for (i = 0; i < strlen (buf); i++) for (i = 0; i < strlen (buf); i++)
{ {
@ -691,7 +697,7 @@ newuser ()
return; return;
} }
me->username = strdup(buf); me->username = strdup (buf);
/* password step */ /* password step */
@ -710,10 +716,10 @@ newuser ()
getnstr (buf, 20); getnstr (buf, 20);
/* we warned em */ /* we warned em */
if (strstr(buf, ":") != NULL) if (strstr (buf, ":") != NULL)
exit(112); exit (112);
me->password = strdup(crypt (buf, buf)); me->password = strdup (crypt (buf, buf));
/* email step */ /* email step */
@ -732,16 +738,16 @@ newuser ()
refresh (); refresh ();
getnstr (buf, 80); getnstr (buf, 80);
if (strstr(buf, ":") != NULL) if (strstr (buf, ":") != NULL)
exit(113); exit (113);
me->email = strdup(buf); me->email = strdup (buf);
me->env = calloc(1, 1); me->env = calloc (1, 1);
loggedin = 1; loggedin = 1;
snprintf (rcfilename, 80, "%s%s.nethackrc", LOC_DGLDIR, me->username); snprintf (rcfilename, 80, "%s%s.nethackrc", LOC_DGLDIR, me->username);
write_canned_rcfile(rcfilename); write_canned_rcfile (rcfilename);
writefile (1); writefile (1);
} }
@ -751,10 +757,10 @@ newuser ()
int int
passwordgood (char *cname, char *cpw) passwordgood (char *cname, char *cpw)
{ {
if (!strncmp(crypt(cpw, cpw), me->password, 13)) if (!strncmp (crypt (cpw, cpw), me->password, 13))
return 1; return 1;
if (!strncmp(cpw, me->password, 20)) if (!strncmp (cpw, me->password, 20))
return 1; return 1;
return 0; return 0;
} }
@ -789,8 +795,8 @@ readfile (int nolock)
{ {
char *b = buf, *n = buf; char *b = buf, *n = buf;
users = realloc(users, sizeof(struct dg_user*) * (f_num + 1)); users = realloc (users, sizeof (struct dg_user *) * (f_num + 1));
users[f_num] = malloc(sizeof(struct dg_user)); users[f_num] = malloc (sizeof (struct dg_user));
users[f_num]->username = (char *) calloc (22, sizeof (char)); users[f_num]->username = (char *) calloc (22, sizeof (char));
users[f_num]->email = (char *) calloc (82, sizeof (char)); users[f_num]->email = (char *) calloc (82, sizeof (char));
users[f_num]->password = (char *) calloc (22, sizeof (char)); users[f_num]->password = (char *) calloc (22, sizeof (char));
@ -881,42 +887,45 @@ userexist (char *cname)
/* ************************************************************* */ /* ************************************************************* */
void void
write_canned_rcfile (char* target) write_canned_rcfile (char *target)
{ {
FILE *canned, *newfile; FILE *canned, *newfile;
char buf[1024]; char buf[1024];
size_t bytes; size_t bytes;
if (!(newfile = fopen(target, "w"))) if (!(newfile = fopen (target, "w")))
{ {
bail: bail:
mvaddstr(13,1,"You don't know how to write that! You write \"%s\" was here and the scroll disappears."); mvaddstr (13, 1,
mvaddstr(14,1,"(Sorry, but I couldn't open one of the nethackrc files. This is a bug.)"); "You don't know how to write that! You write \"%s\" was here and the scroll disappears.");
return; mvaddstr (14, 1,
} "(Sorry, but I couldn't open one of the nethackrc files. This is a bug.)");
return;
if (!(canned = fopen(LOC_CANNED, "r"))) }
if (!(canned = fopen (LOC_CANNED, "r")))
goto bail; goto bail;
while ((bytes = fread(buf, 1, 1024, canned)) > 0) while ((bytes = fread (buf, 1, 1024, canned)) > 0)
{
if (fwrite(buf, 1, bytes, newfile) != bytes)
{ {
if (ferror(newfile)) if (fwrite (buf, 1, bytes, newfile) != bytes)
{ {
mvaddstr(13,1,"Your hand slips while engraving."); if (ferror (newfile))
mvaddstr(14,1,"(Encountered a problem writing the new file. This is a bug.)"); {
fclose(canned); mvaddstr (13, 1, "Your hand slips while engraving.");
fclose(newfile); mvaddstr (14, 1,
return; "(Encountered a problem writing the new file. This is a bug.)");
} fclose (canned);
fclose (newfile);
return;
}
}
} }
}
fclose (canned);
fclose(canned); fclose (newfile);
fclose(newfile);
} }
void void
editoptions () editoptions ()
@ -926,9 +935,9 @@ editoptions ()
rcfile = fopen (rcfilename, "r"); rcfile = fopen (rcfilename, "r");
printf (" read"); printf (" read");
if (!rcfile) /* should not really happen except for old users */ if (!rcfile) /* should not really happen except for old users */
write_canned_rcfile(rcfilename); write_canned_rcfile (rcfilename);
/* use virus to edit */ /* use virus to edit */
myargv[0] = ""; myargv[0] = "";
@ -972,18 +981,20 @@ writefile (int requirenew)
* as someone else. just die. */ * as someone else. just die. */
exit (111); exit (111);
} }
fprintf (fp, "%s:%s:%s:%s\n", me->username, me->email, me->password, me->env); fprintf (fp, "%s:%s:%s:%s\n", me->username, me->email, me->password,
me->env);
my_done = 1; my_done = 1;
} }
else else
{ {
fprintf (fp, "%s:%s:%s:%s\n", users[i]->username, users[i]->email, users[i]->password, fprintf (fp, "%s:%s:%s:%s\n", users[i]->username, users[i]->email,
users[i]->env); users[i]->password, users[i]->env);
} }
} }
if (loggedin && !my_done) if (loggedin && !my_done)
{ /* new entry */ { /* new entry */
fprintf (fp, "%s:%s:%s:%s\n", me->username, me->email, me->password, me->env); fprintf (fp, "%s:%s:%s:%s\n", me->username, me->email, me->password,
me->env);
} }
flock (fileno (fpl), LOCK_UN); flock (fileno (fpl), LOCK_UN);
@ -1076,17 +1087,19 @@ main (void)
} }
} }
assert(loggedin); assert (loggedin);
endwin (); endwin ();
/* environment */ /* environment */
snprintf (atrcfilename, 81, "@%s", rcfilename); snprintf (atrcfilename, 81, "@%s", rcfilename);
len = (sizeof (LOC_SPOOLDIR) / sizeof (LOC_SPOOLDIR[0])) + strlen (me->username) + 1; len =
(sizeof (LOC_SPOOLDIR) / sizeof (LOC_SPOOLDIR[0])) +
strlen (me->username) + 1;
spool = malloc (len + 1); spool = malloc (len + 1);
snprintf (spool, len, "%s/%s", LOC_SPOOLDIR, me->username); snprintf (spool, len, "%s/%s", LOC_SPOOLDIR, me->username);
setenv ("NETHACKOPTIONS", atrcfilename, 1); setenv ("NETHACKOPTIONS", atrcfilename, 1);
setenv ("MAIL", spool, 1); setenv ("MAIL", spool, 1);
setenv ("SIMPLEMAIL", "1", 1); setenv ("SIMPLEMAIL", "1", 1);
@ -1108,8 +1121,8 @@ main (void)
freefile (); freefile ();
if (me) if (me)
free(me); free (me);
exit (1); exit (1);
return 1; return 1;
} }

View File

@ -5,10 +5,10 @@
struct dg_user struct dg_user
{ {
char* username; char *username;
char* email; char *email;
char* env; char *env;
char* password; char *password;
int flags; int flags;
}; };

View File

@ -45,9 +45,17 @@
#include "io.h" #include "io.h"
#include "stripgfx.h" #include "stripgfx.h"
extern void domailuser (char *);
extern void initncurses (void);
extern char *chosen_name;
extern int loggedin;
int ttyplay_main (char *ttyfile, int mode, int rstripgfx);
extern int caught_sighup; extern int caught_sighup;
off_t seek_offset_clrscr; off_t seek_offset_clrscr;
int bstripgfx; int bstripgfx;
char *ttyfile_local;
typedef double (*WaitFunc) (struct timeval prev, typedef double (*WaitFunc) (struct timeval prev,
struct timeval cur, double speed); struct timeval cur, double speed);
@ -197,6 +205,16 @@ ttypread (FILE * fp, Header * h, char **buf, int pread)
case 'q': case 'q':
return 0; return 0;
break; break;
case 'm':
if (loggedin)
{
initncurses ();
domailuser (chosen_name);
endwin ();
ttyplay_main (ttyfile_local, 1, 0);
return 0;
}
break;
} }
} }
} }
@ -381,6 +399,8 @@ ttyplay_main (char *ttyfile, int mode, int rstripgfx)
FILE *input = stdin; FILE *input = stdin;
struct termios old, new; struct termios old, new;
ttyfile_local = ttyfile;
/* strip graphics mode flag */ /* strip graphics mode flag */
bstripgfx = rstripgfx; bstripgfx = rstripgfx;
if (bstripgfx) if (bstripgfx)

View File

@ -106,10 +106,11 @@ ttyrec_main (char *username)
snprintf (dirname, 100, "%s%s", LOC_TTYRECDIR, username); snprintf (dirname, 100, "%s%s", LOC_TTYRECDIR, username);
if (access(dirname, F_OK) != 0) if (access (dirname, F_OK) != 0)
mkdir (dirname, 0755); mkdir (dirname, 0755);
snprintf (dirname, 100, "%s%s/%s", LOC_TTYRECDIR, username, ttyrec_filename); snprintf (dirname, 100, "%s%s/%s", LOC_TTYRECDIR, username,
ttyrec_filename);
if ((fscript = fopen (dirname, "w")) == NULL) if ((fscript = fopen (dirname, "w")) == NULL)
{ {