-/* ttt-curses-client.c - curses based tic-tac-toe game client
+/*ttt-curses-client.c - curses based tic-tac-toe game client
*
* Copyright © 2005 Bryan Worth
*
/* TODO: see /usr/share/doc/ncurses-devel-5.3/test/view.c for example code
* to handle window resizing
- *
- * add code to read username from config file or prompt user if config
- * file does not exist. (username is currently hardcoded and needs
- * changed before compiling)
*
*/
while (xx < l) {
outc = string[xx];
- if (string[xx] != '\r')
+ if (string[xx] != '\r')
waddch (window, outc);
xx++;
}
}
-char *message2;
-int _socket;
-size_t maxread;
+FILE *sockin, *sockout;
int
main (int argc, char **argv)
{
- char *message;
- char buffer[1000];
- char *host;
- char *port;
+ char *host="localhost";
+ char *port="5334";
chtype c;
- ssize_t numread;
+ char *username;
+ char *envuser;
+ char *confpath;
+ char *conffile;
+ char *command_string;
+ int _socket;
+ char buffer[BUFSIZ];
+ char inplin[1024];
+ ttt_status_t status;
static WINDOW *mainwnd;
static WINDOW *dispwin;
+ static WINDOW *statwin;
static WINDOW *inpwin;
+ FILE *conf_file;
mainwnd = initscr();
int dlines = LINES - 2, cols = COLS;
(void) nonl ();
+ (void) nl ();
noecho ();
cbreak ();
nodelay (mainwnd, TRUE);
curs_set (0);
refresh ();
- dispwin = newwin (dlines - 1, cols - 2, 0, 0);
- inpwin = newwin (1, cols - 2, dlines, 0);
+ dispwin = newwin (dlines - 5, cols - 2, 0, 0);
+ statwin = newwin (4, cols - 2, dlines -3, 0);
+ inpwin = newwin (2, cols - 2, dlines, 0);
keypad (mainwnd, TRUE); // enable keyboard mapping
keypad (inpwin, TRUE); // enable keyboard mapping
nodelay (inpwin, TRUE);
wrefresh (mainwnd);
wrefresh (dispwin);
+ wrefresh (statwin);
wrefresh (inpwin);
(void) scrollok (mainwnd, TRUE);
(void) scrollok (dispwin, TRUE);
+ (void) scrollok (statwin, TRUE);
(void) scrollok (inpwin, TRUE);
(void) idlok (mainwnd, TRUE);
(void) idlok (dispwin, TRUE);
+ (void) idlok (statwin, TRUE);
(void) idlok (inpwin, TRUE);
wsetscrreg (dispwin, 0, dlines - 2);
+ wsetscrreg (statwin, 0, 4);
(void) signal (SIGINT, finish); /* arrange interrupts to terminate */
if (has_colors ()) {
init_pair (COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
init_pair (COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
}
- wbkgd (dispwin, COLOR_PAIR (COLOR_WHITE));
- wbkgd (inpwin, COLOR_PAIR (COLOR_CYAN));
+ //wbkgd (dispwin, COLOR_PAIR (COLOR_WHITE));
+ //wbkgd (inpwin, COLOR_PAIR (COLOR_CYAN));
wrefresh (dispwin);
wrefresh (inpwin);
- xasprintf (&host, "localhost");
- xasprintf (&port, "5334");
- xasprintf (&message, "HELO bryan\r\n");
+ xasprintf(&confpath,"%s/.ttt/",getenv("HOME"));
+ xasprintf(&conffile,"%s/.ttt/client.conf",getenv("HOME"));
+ username="user";
+ if (access (conffile, F_OK) != 0 ) {
+ envuser=getenv("USER");
+ if (envuser != NULL) username=strdup(envuser);
+ if (access (confpath, F_OK) != 0 ) {
+ xasprintf(&command_string,"mkdir %s",confpath);
+ system (command_string);
+ }
+ if ((conf_file = fopen(conffile,"w")) != NULL) {
+ fprintf(conf_file,"username=%s",username);
+ fclose(conf_file);
+ }
+ }
+ if ((conf_file = fopen(conffile,"r")) != NULL) {
+ while (fgets(buffer,BUFSIZ,conf_file)) {
+ if (buffer[strlen(buffer)-1] == 10) buffer[strlen(buffer)-1] = '\0';
+ if (strncmp(buffer,"username=",9) == 0) {
+ xasprintf (&username, "%s", &buffer[9]);
+ }
+ if (strncmp(buffer,"port=",5) == 0) {
+ xasprintf (&port, "%s",&buffer[5]);
+ }
+ }
+ fclose(conf_file);
+ }
+ status=ttt_socket_create_client (host, port, &_socket);
+ if (status) {
+ endwin();
+ printf("Unable to connect to server! Connection status: %d\n",status);
+ exit(1);
+ }
+ sockin=fdopen(_socket,"r");
+ sockout=fdopen(_socket,"w");
+
- ttt_socket_create_client (host, port, &_socket);
- ttt_socket_write (_socket, message, strlen (message));
- maxread = 100;
- numread = read (_socket, buffer, maxread);
- if (numread > 0) {
- buffer[numread] = '\0';
- xasprintf (&message, "%s", buffer);
- wprint (dispwin, message);
+ fprintf(sockout, "HELO %s\r\n",username);
+ fflush(sockout);
+
+ if (fgets(buffer,BUFSIZ,sockin)) {
+ if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
+ wprint (dispwin, buffer);
+ }
+ else {
+ if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
+ }
wrefresh (dispwin);
+ wrefresh (statwin);
}
- xasprintf (&message2, "MESSAGE \"");
+
+ fprintf(sockout, "WHO \r\n");
+ fflush(sockout);
+
+ inplin[0]='\0';
while (1) {
curs_set (1);
c = wgetch (inpwin);
- if (c != ERR) {
- xasprintf (&message2, "%s%c", message2, (int) c);
- waddch (inpwin, c);
- wrefresh (inpwin);
+ if ((c != ERR) && (strlen(inplin)< 1000)) {
+ switch ((int) c) {
+ case 13:
+ break;
+ case 10:
+ break;
+ case 8:
+ inplin[strlen(inplin)-1]='\0';
+ wmove (inpwin,0,strlen(inplin));
+ wclrtoeol( inpwin);
+ wrefresh (inpwin);
+ break;
+ case 127:
+ inplin[strlen(inplin)-1]='\0';
+ wmove (inpwin,0,strlen(inplin));
+ wclrtoeol (inpwin);
+ wrefresh (inpwin);
+ break;
+ case 263:
+ inplin[strlen(inplin)-1]='\0';
+ wmove (inpwin,0,strlen(inplin));
+ wclrtoeol (inpwin);
+ wrefresh (inpwin);
+ break;
+ default:
+ sprintf (inplin,"%s%c",inplin,(int) c);
+ waddch (inpwin, c);
+ wrefresh (inpwin);
+ }
}
- numread = 0;
- maxread = 100;
- numread = read (_socket, buffer, maxread);
- if (numread > 0) {
+ if (fgets(buffer,BUFSIZ,sockin)) {
curs_set (0);
- buffer[numread] = '\0';
- wprint (dispwin, buffer);
+ if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
+ wprint (dispwin, &buffer[15]);
+ }
+ else {
+ if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
+ }
wrefresh (dispwin);
+ wrefresh (statwin);
wrefresh (inpwin);
}
- if ((int) c == 13) {
+ if (((int) c == 13) || ((int) c == 10)) {
curs_set (0);
- xasprintf (&message2, "%s\"\r\n", message2);
- ttt_socket_write (_socket, message2, strlen (message2));
- xasprintf (&message2, "MESSAGE \"");
- maxread = 100;
- numread = read (_socket, buffer, maxread);
- if (numread > 0) {
- buffer[numread] = '\0';
- wprint (dispwin, buffer);
+ if (inplin[0] == '/') {
+ fprintf(sockout,"%s\r\n",&inplin[1]);
+ }
+ else {
+ fprintf(sockout,"MESSAGE \"%s\"\r\n",inplin);
+ }
+ fflush(sockout);
+ inplin[0] = '\0';
+ if (fgets(buffer,BUFSIZ,sockin)) {
+ if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
+ wprint (dispwin, buffer);
+ }
+ else {
+ if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
+ }
}
wrefresh (dispwin);
+ wrefresh (statwin);
werase (inpwin);
wrefresh (inpwin);
}
static void
finish (int sig)
{
- char buffer[1024];
- ssize_t numread;
+ char buffer[BUFSIZ];
+
endwin();
/* do your non-curses wrapup here */
- xasprintf (&message2, "QUIT\r\n");
- ttt_socket_write (_socket, message2, strlen (message2));
-
- maxread = 100;
- numread = read (_socket, buffer, maxread);
- buffer[numread] = '\0';
- xasprintf (&message2, "%s", buffer);
+ fprintf(sockout,"QUIT\r\n");
+ fflush(sockout);
+ while (fgets(buffer,BUFSIZ,sockin));
exit (0);
}