Ejemplo n.º 1
0
void chat_process_command(chat_context_t *ctx, ffs_packet_t *pkt) {
    int err = 0;

    /* Parse the packet */
    enum ChatCommand cmd = CC_LOGOUT;
    uint32_t pos = 0;
    err = ChatCommand_deserialize(pkt->data, FFS_DATA_SIZE, &pos, &cmd);
    if (err) goto cleanup;

    switch (cmd) {
    case CC_LOGOUT:
        err = chat_process_logout(ctx, pkt, &pos);
        break;
    case CC_SETSEEK:
        err = chat_process_setseek(ctx, pkt, &pos);
        break;
    case CC_GETSEEK:
        err = chat_process_getseek(ctx, pkt, &pos);
        break;
    case CC_GET_CHANNEL:
        err = chat_process_getchannel(ctx, pkt, &pos);
        break;
    case CC_CLOSE_CHANNEL:
        err = chat_process_closechannel(ctx, pkt, &pos);
        break;
    case CC_GET_EXCHANGE:
        err = chat_process_getexchange(ctx, pkt, &pos);
        break;
    case CC_ENCRYPT:
        err = chat_process_encrypt(ctx, pkt, &pos);
        break;
    case CC_INCOMING:
        err = chat_process_incoming(ctx, pkt, &pos);
        break;
    }

cleanup:
    if (err) {
        uint32_t length = 0;
        msel_memset(pkt->data, 0, FFS_DATA_SIZE);
        if (!ChatResponse_serialize(pkt->data, FFS_DATA_SIZE, &length, CR_ERROR)) {
            while (msel_svc(MSEL_SVC_SESSION_SEND, pkt) != MSEL_OK)
                msel_svc(MSEL_SVC_YIELD, NULL);
        }
    }
}
Ejemplo n.º 2
0
Chat()
{
  CHATID chatid;
  char cbuf[CHATLINE_TEXT_MAX+1];
  int cfd;
  int rc;
  int done_chatting = 0;
  int margin, curspos, maxpos, endpos;
  int ch, i;
  int metakey = 0;
  int shift, shiftdelta;

  move(2, 0);
  clrtobot();

  do {  
    if (getdata(2,0,"Enter chatid: ",chatid,sizeof chatid,DOECHO,1)==-1)
      return FULLUPDATE;

    if (*chatid == '\0') {
      strncpy(chatid, myinfo.userid, CHATID_MAX);
      chatid[CHATID_MAX] = '\0';
    }
    rc = bbs_chat(chatid, (LONG *)&cfd);
    switch (rc) {
    case S_OK: break;
    case S_BADCHATID:
    case S_CHATIDINUSE:
      bbperror(rc, NULL);
      break;
    default:
      bbperror(rc, "Error entering chat");
      return PARTUPDATE;
    }
  } while (rc != S_OK);

  add_io(cfd, 0);
  chat_resetscreen(chatid);

  memset(cbuf, '\0', sizeof cbuf);
  margin = CHATID_MAX+2;
  maxpos = sizeof(cbuf) - margin - 1;
  curspos = 0;
  endpos = 0;
  shift = 0;
  shiftdelta = (t_columns - margin) / 2;
    
  while (!done_chatting) {
    move(g_echatwin+1, margin+(curspos-shift));

    ch = igetch();
    if (NewPagePending()) {
      chat_show_page_request();
    }      
    if (ch == -1) done_chatting = 1;
    else if (ch == I_OTHERDATA) {
      /* Incoming! */
      if (chat_process_incoming(cfd, chatid) == -1)
        done_chatting = 1;
    }
    else if (metakey) {
      switch (ch) {
      case 'b': case 'B':
	if (curspos == 0) bell();
	else {
	  while (curspos && isspace(cbuf[curspos-1])) curspos--;
	  while (curspos && !isspace(cbuf[curspos-1])) curspos--;
	}
	break;
      case 'f': case 'F':
	if (curspos == endpos) bell();
	else {
	  while (curspos < endpos && isspace(cbuf[curspos])) curspos++;
	  while (curspos < endpos && !isspace(cbuf[curspos])) curspos++;
	}
	break;
      default: 
	bell();
      }
      metakey = 0;
    }
    else switch (ch) {
    case '\r': 
    case '\n':
      if (endpos > 0) {
        cbuf[endpos] = '\n';
        done_chatting = chat_process_local(cbuf, chatid);
        if (done_chatting == -1) {
          done_chatting = (bbs_chat_send(cbuf) != S_OK);
        }
      }
      memset(cbuf, '\0', sizeof cbuf);
      endpos = curspos = 0;
      move(g_echatwin+1, margin);
      clrtoeol();
      break;
    case CTRL('A'):
      if (curspos == 0) bell();
      else curspos = 0;
      break;
    case CTRL('B'):
      if (curspos == 0) bell();
      else curspos--;
      break;
    case CTRL('C'):
      bbs_chat_send("/e\n");
      done_chatting = 1;
      break;
    case CTRL('D'):
      if (curspos == endpos) bell();
      else {
        for (i=curspos; i<endpos; i++) cbuf[i] = cbuf[i+1];        
        endpos--;
        move(g_echatwin+1, margin+(curspos-shift));
        prints("%s", cbuf+curspos);
        clrtoeol();
      }
      break;
    case CTRL('E'):
      if (curspos == endpos) bell();
      else curspos = endpos;
      break;
    case CTRL('F'):
      if (curspos == endpos) bell();
      else curspos++;
      break;
    case CTRL('H'):
    case 127:
      if (curspos == 0) bell();
      else {
        for (i=curspos; i<=endpos; i++) cbuf[i-1] = cbuf[i];        
        endpos--;
        curspos--;
        move(g_echatwin+1, margin+(curspos-shift));
        prints("%s", cbuf+curspos);
	clrtoeol();
      }
      break;
    case CTRL('U'):
      if (endpos == 0) bell();
      else {
        memset(cbuf, '\0', sizeof cbuf);
        curspos = endpos = 0;
        move(g_echatwin+1, margin);
        clrtoeol();
      }
      break;
    case CTRL('W'):
      if (curspos == 0) bell();
      else {
        while (curspos && isspace(cbuf[curspos-1])) {
          for (i=curspos; i<=endpos; i++) cbuf[i-1] = cbuf[i];
	  curspos--, endpos--;
	}
        while (curspos && !isspace(cbuf[curspos-1])) {
          for (i=curspos; i<=endpos; i++) cbuf[i-1] = cbuf[i];
	  curspos--, endpos--;
	}
        move(g_echatwin+1, margin+(curspos-shift));
        prints("%s", cbuf+curspos);
        clrtoeol();
      }
      break;
    case 27:   /* ESC */
      metakey = 1;
      break;
    default:
      if (isprint(ch) && endpos < maxpos) {
        for (i=endpos; i>curspos; i--) cbuf[i] = cbuf[i-1];
        cbuf[curspos] = ch;        
        move(g_echatwin+1, margin+(curspos-shift));
        prints("%s", cbuf+curspos);
        curspos++;
        endpos++;
      }
      else bell();      
    }                              
    if (curspos-shift >= (t_columns-margin)) {
      while (curspos-shift >= (t_columns-margin)) shift += shiftdelta;
      move(g_echatwin+1, margin);
      prints("%s", cbuf+shift);
      clrtoeol();
    }
    else if (curspos < shift) {
      while (curspos < shift) shift -= shiftdelta;
      move(g_echatwin+1, margin);
      prints("%s", cbuf+shift);
      clrtoeol();
    }
  }

  bbs_exit_chat();
  g_you_have_mail = 0;
  add_io(0, 0);
  return FULLUPDATE;
}