Esempio n. 1
0
/*
 * Chat, if the parameters are not NULL, a connection with the named
 * channel is made with the give username which will be forced to the
 * used nick name. This mode is used for forced sysop chat.
 * If the parameters are NULL, then it's up to the user what happens.
 */
void Chat(char *username, char *channel)
{
    int		    width, curpos = 0, stop = FALSE, data, rc;
    unsigned char   ch;
    char	    sbuf[81], resp[128], *name, *mname;
    static char	    buf[200];
    time_t	    c_start, c_end;


    WhosDoingWhat(SYSOPCHAT, NULL);
    clear();

    rsize = rows - 5;
    rpointer = 0;

    if (SYSOP == TRUE) {
	/*
	 * Forbid the sysop to chat, the sysop MUST use mbmon.
	 */
	Syslog('+', "The Sysop attempted to chat");
	pout(LIGHTRED, BLACK, (char *) Language(29));
	Enter(1);
	Pause();
	return;
    }
   
    if (username && channel) {
	colour(LIGHTGREEN, BLACK);
	PUTCHAR('\007');
	/* *** Sysop is starting chat *** */
	pout(LIGHTGREEN, BLACK, (char *) Language(59));
	Enter(1);
	sleep(1);
	PUTCHAR('\007');
	sleep(1);
	PUTCHAR('\007');
	Syslog('+', "Sysop chat started");
	chat_with_sysop = TRUE;
    } else {
	Syslog('+', "User started chatting");
    }
    
    /*
     * Setup the screen, this is only possible in ANSI mode.
     */
    clear();
    locate(1, 1);
    colour(WHITE, BLUE);
    snprintf(buf, 200, "%-*s", cols, " MBSE BBS Chat Server");
    mvprintw(1, 1, buf);

    mname = xstrcpy(clencode(exitinfo.sUserName));
    name  = xstrcpy(clencode(exitinfo.Name));
    width = cols - (strlen(name) + 3);
    snprintf(buf, 200, "CCON,4,%d,%s,%s,0;", mypid, mname, name);
    free(mname);
    free(name);
    if (socket_send(buf) == 0) {
	strncpy(buf, socket_receive(), sizeof(buf)-1);
	if (strncmp(buf, "200:1,", 6) == 0) {
	    Syslog('!', "Chat server is not available");
	    colour(LIGHTRED, BLACK);
	    mvprintw(4, 1, (char *) Language(30));
	    Enter(2);
	    Pause();
	    chat_with_sysop = FALSE;
	    return;
	}
    }

    locate(rows - 2, 1);
    colour(WHITE, BLUE);
    snprintf(buf, 200, "%-*s", cols, " Type \"/EXIT\" to exit or \"/HELP\" for help.");
    mvprintw(rows - 2, 1, buf);

    colour(WHITE, BLACK);
    mvprintw(rows - 1, 1, ">");
    mvprintw(rows - 1, width + 2, "<");
    memset(&sbuf, 0, sizeof(sbuf));
    memset(&rbuf, 0, sizeof(rbuf));
    colour(LIGHTGRAY, BLACK);

    /*
     * If username and channelname are given, send the /nick and /join
     * commands to the chatserver.
     */
    if (username && channel) {
	snprintf(buf, 200, "CPUT:2,%d,/nick %s;", mypid, clencode(username));
	if (socket_send(buf) == 0)
	    strcpy(buf, socket_receive());
	snprintf(buf, 200, "CPUT:2,%d,/join %s;", mypid, clencode(channel));
	if (socket_send(buf) == 0)
	    strcpy(buf, socket_receive());
    }

    chatting = TRUE;
    c_start = time(NULL);

    while (stop == FALSE) {

	/*
	 * Check for new message, loop fast until no more data available.
	 */
	data = TRUE;
	while (data) {
	    snprintf(buf, 200, "CGET:1,%d;", mypid);
	    if (socket_send(buf) == 0) {
		strncpy(buf, socket_receive(), sizeof(buf)-1);
		if (strncmp(buf, "100:2,", 6) == 0) {
		    strncpy(resp, strtok(buf, ":"), 10);    /* Should be 100        */
		    strncpy(resp, strtok(NULL, ","), 5);    /* Should be 2          */
		    strncpy(resp, strtok(NULL, ","), 5);    /* 1= fatal, chat ended */
		    rc = atoi(resp);
		    memset(&resp, 0, sizeof(resp));
		    strncpy(resp, cldecode(strtok(NULL, ";")), 80);  /* The message          */
		    DispMsg(resp);
		    if (rc == 1) {
			Syslog('+', "Chat server error: %s", resp);
			stop = TRUE;
			data = FALSE;
		    }
		} else {
		    data = FALSE;
		}
	    }
	}

	if (stop)
	    break;

        /*
	 * Check for a pressed key, if so then process it.
	 * Allow hi-ascii for multi-language.
	 */
	ch = testkey(rows -1, curpos + 2);
	if ((ch == KEY_BACKSPACE) || (ch == KEY_RUBOUT) || (ch == KEY_DEL)) {
	    alarm_on();
	    if (curpos) {
		curpos--;
		sbuf[curpos] = '\0';
		BackErase();
	    } else {
		PUTCHAR(7);
	    }
	/* if KEY_DEL isprint, do no output again */
	} else if (isprint(ch) || traduce((char *)&ch)) {
	    alarm_on();
	    if (curpos < width) {
		PUTCHAR(ch);
		sbuf[curpos] = ch;
		curpos++;
	    } else {
		PUTCHAR(7);
	    }
	} else if ((ch == '\r') && curpos) {
	    alarm_on();
	    snprintf(buf, 200, "CPUT:2,%d,%s;", mypid, clencode(sbuf));
	    if (socket_send(buf) == 0) {
		strcpy(buf, socket_receive());
		if (strncmp(buf, "100:2,", 6) == 0) {
		    strncpy(resp, strtok(buf, ":"), 10);    /* Should be 100            */
		    strncpy(resp, strtok(NULL, ","), 5);    /* Should be 2              */
		    strncpy(resp, strtok(NULL, ","), 5);    /* 1= fatal, chat ended	*/
		    rc = atoi(resp);
		    strncpy(resp, cldecode(strtok(NULL, ";")), 80);  /* The message              */
		    DispMsg(resp);
		    if (rc == 1) {
			Syslog('+', "Chat server error: %s", resp);
			stop = TRUE;
		    }
		}
	    }
	    curpos = 0;
	    memset(&sbuf, 0, sizeof(sbuf));
	    locate(rows - 1, 2);
	    clrtoeol();
	    colour(WHITE, BLACK);
	    mvprintw(rows - 1, 1, ">");
	    mvprintw(rows - 1, width + 2, "<");
	    colour(LIGHTGRAY, BLACK);
	}
    }
    chatting = FALSE;
    c_end = time(NULL);
    mib_chats++;
    mib_chatminutes += (unsigned int) ((c_end - c_start) / 60);


    /* 
     * Before sending the close command, purge all outstanding messages.
     */
    data = TRUE;
    while (data) {
	snprintf(buf, 200, "CGET:1,%d;", mypid);
	if (socket_send(buf) == 0) {
	    strncpy(buf, socket_receive(), sizeof(buf)-1);
	    if (strncmp(buf, "100:2,", 6) == 0) {
		strncpy(resp, strtok(buf, ":"), 10);    /* Should be 100        */
		strncpy(resp, strtok(NULL, ","), 5);    /* Should be 2          */
		strncpy(resp, strtok(NULL, ","), 5);	/* 1= fatal error	*/
		rc = atoi(resp);
		memset(&resp, 0, sizeof(resp));
		strncpy(resp, cldecode(strtok(NULL, ";")), 80);  /* The message          */
		DispMsg(resp);
		if (rc == 1) {
		    Syslog('+', "Chat server error: %s", resp);
		    data = FALSE;   /* Even if there is more, prevent a loop */
		}
	    } else {
		data = FALSE;
	    }
	}
    }

    if (username && channel) {
	/*
	 * Disjoin sysop channel
	 */

	/* *** Sysop has terminated chat *** */
	snprintf(buf, 200, "%s", (char *) Language(60));
	DispMsg(buf);
	Syslog('+', "Sysop chat ended");
	chat_with_sysop = FALSE;
    } else {
	Syslog('+', "User chat ended");
    }

    /*
     * Close server connection
     */
    snprintf(buf, 200, "CCLO,1,%d;", mypid);
    if (socket_send(buf) == 0) {
	strcpy(buf, socket_receive());
    }
    sleep(2);
    clear();
}
Esempio n. 2
0
int main()
{
  typedef viennagrid::triangular_2d_mesh                                  DomainType;
  typedef viennagrid::result_of::cell_tag<DomainType>::type               CellTag;
  typedef viennagrid::result_of::element<DomainType, CellTag>::type       CellType;

  typedef viennamath::function_symbol   FunctionSymbol;
  typedef viennamath::equation          Equation;

  //
  // Create a storage object
  //
  typedef viennadata::storage<> StorageType;
  StorageType   storage;

  //
  // Specify PDEs and derive weak form:
  //
  FunctionSymbol u(0, viennamath::unknown_tag<>());   //an unknown function used for PDE specification
  Equation equ_1 = viennamath::make_equation( viennamath::laplace(u), -1);
  std::cout << "Strong form: " << equ_1 << std::endl;
  std::cout << "Weak form: " << viennafem::make_weak_form(equ_1) << std::endl;
  std::cout << "-------------" << std::endl;

  Equation equ_2 = viennamath::make_equation( viennamath::laplace(u) + u, -1);
  std::cout << "Strong form: " << equ_2 << std::endl;
  std::cout << "Weak form: " << viennafem::make_weak_form(equ_2) << std::endl;
  std::cout << "-------------" << std::endl;

  Equation equ_3 = viennamath::make_equation( viennamath::div( 3.0 * viennamath::grad(u)), -1);
  std::cout << "Strong form: " << equ_3 << std::endl;
  std::cout << "Weak form: " << viennafem::make_weak_form(equ_3) << std::endl;
  std::cout << "-------------" << std::endl;

  Equation equ_4 = viennamath::make_equation( viennamath::div( 4.0 * viennamath::grad(u)) + u, -1);
  std::cout << "Strong form: " << equ_4 << std::endl;
  std::cout << "Weak form: " << viennafem::make_weak_form(equ_4) << std::endl;
  std::cout << "-------------" << std::endl;

  Equation equ_5 = viennamath::make_equation( u, -1);
  std::cout << "Strong form: " << equ_5 << std::endl;
  std::cout << "Weak form: " << viennafem::make_weak_form(equ_5) << std::endl;
  std::cout << "-------------" << std::endl;

  viennafem::cell_quan<CellType, viennamath::expr::interface_type>  permittivity; permittivity.wrap_constant( storage, testkey() );
  Equation equ_6 = viennamath::make_equation( viennamath::div( permittivity * viennamath::grad(u)), -1);
  std::cout << "Strong form: " << equ_6 << std::endl;
  std::cout << "Weak form: " << viennafem::make_weak_form(equ_6) << std::endl;
  std::cout << "-------------" << std::endl;

  std::cout << "*************************************" << std::endl;
  std::cout << "* Weak forms finished successfully! *" << std::endl;
  std::cout << "*************************************" << std::endl;
  return EXIT_SUCCESS;
}