示例#1
0
文件: pysocket.c 项目: KaSt/nereamud
PyObject *PySocket_edit_text(PyObject *self, PyObject *args) {
  PyObject *on_complete = NULL;
  char            *text = NULL;
  char            *type = NULL;
  SOCKET_DATA     *sock = NULL;
  EDITOR        *editor = text_editor;

  if(!PyArg_ParseTuple(args, "sO|s", &text, &on_complete, &type)) {
    PyErr_Format(PyExc_TypeError, "invalid arguments to sock.edit_text");
    return NULL;
  }

  if(!PySocket_Check(self)) {
    PyErr_Format(PyExc_TypeError, "Owner of edit_text not a socket");
    return NULL;
  }
  else if( (sock = PySocket_AsSocket(self)) == NULL) {
    PyErr_Format(PyExc_TypeError, "sock.edit_text called on non-existent socket.");
    return NULL;
  }

  if(type != NULL && compares(type, "script"))
    editor = script_editor;
  
  // begin the editor
  socketStartPyEditorFunc(sock, editor, text, on_complete);
  return Py_BuildValue("O", Py_None);
}
示例#2
0
文件: save.c 项目: pfchrono/cmud
D_MOBILE *load_player(char *player)
{
  FILE *fp;
  D_MOBILE *dMob = NULL;
  char pfile[256];
  char pName[20];
  char *word;
  bool done = FALSE, found;
  int i, size;

  pName[0] = toupper(player[0]);
  size = strlen(player);
  for (i = 1; i < size; i++)
    pName[i] = tolower(player[i]);
  pName[i] = '\0';

  /* open the pfile so we can write to it */
  sprintf(pfile, "../players/%s.pfile", pName);     
  if ((fp = fopen(pfile, "r")) == NULL)
    return NULL;

  /* create new mobile data */
  if (StackSize(dmobile_free) <= 0)
  {
    if ((dMob = malloc(sizeof(*dMob))) == NULL)
    {
      bug("Load_player: Cannot allocate memory.");
      abort();
    }
  }
  else
  {
    dMob = (D_MOBILE *) PopStack(dmobile_free);
  }
  clear_mobile(dMob);

#ifdef IMC
    imc_initchar( dMob );
#endif

  /* load data */
  word = fread_word(fp);
  while (!done)
  {
    found = FALSE;
    switch (word[0])
    {
      case 'C':
        if (compares(word, "Coordx")) { IREAD( "Coordx", dMob->coordx ); }
	if (compares(word, "Coordy")) { IREAD( "Coordy", dMob->coordy ); }
	if (compares(word, "Coordz")) { IREAD( "Coordz", dMob->coordz ); }
	break;
      case 'E':
        if (!strcasecmp(word, "EOF")) {done = TRUE; found = TRUE; break;}
        break;
      case 'I':
#ifdef IMC
           if( ( found = imc_loadchar( dMob, fp, word ) ) )
                break;
#endif
        break;
      case 'L':
        IREAD( "Level",     dMob->level     );
        break;
      case 'N':
        SREAD( "Name",      dMob->name      );
        break;
      case 'P':
        SREAD( "Password",  dMob->password  );
        break;
      case 'T':
      SREAD( "Title",     dMob->title     );
        break;
    }
    if (!found)
    {
      bug("Load_player: unexpected '%s' in %s's pfile.", word, player);
      free_mobile(dMob);
      return NULL;
    }

    /* read one more */
    if (!done) word = fread_word(fp);
  }

  fclose(fp);
  return dMob;
}
示例#3
0
文件: socket.c 项目: KaSt/nereamud
/* 
 * New_socket()
 *
 * Initializes a new socket, get's the hostname
 * and puts it in the active socket_list.
 */
SOCKET_DATA *new_socket(int sock)
{
  struct sockaddr_in   sock_addr;
  pthread_attr_t       attr;
  pthread_t            thread_lookup;
  LOOKUP_DATA        * lData;
  SOCKET_DATA        * sock_new;
  int                  argp = 1;
  socklen_t            size;

  /* initialize threads */
  pthread_attr_init(&attr);   
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

  /* create and clear the socket */
  sock_new = calloc(1, sizeof(SOCKET_DATA));

  /* attach the new connection to the socket list */
  FD_SET(sock, &fSet);

  /* clear out the socket */
  clear_socket(sock_new, sock);
  sock_new->closed = FALSE;

  /* set the socket as non-blocking */
  ioctl(sock, FIONBIO, &argp);

  /* update the socket list and table */
  listPut(socket_list, sock_new);
  propertyTablePut(sock_table, sock_new);

  /* do a host lookup */
  size = sizeof(sock_addr);
  if (getpeername(sock, (struct sockaddr *) &sock_addr, &size) < 0)
  {
    perror("New_socket: getpeername");
    sock_new->hostname = strdup("unknown");
  }
  else
  {
    /* set the IP number as the temporary hostname */
    sock_new->hostname = strdup(inet_ntoa(sock_addr.sin_addr));

    if (!compares(sock_new->hostname, "127.0.0.1"))
    {
      /* allocate some memory for the lookup data */
      if ((lData = malloc(sizeof(*lData))) == NULL)
      {
        bug("New_socket: Cannot allocate memory for lookup data.");
        abort();
      }

      /* Set the lookup_data for use in lookup_address() */
      lData->buf    =  strdup((char *) &sock_addr.sin_addr);
      lData->dsock  =  sock_new;

      /* dispatch the lookup thread */
      pthread_create(&thread_lookup, &attr, &lookup_address, (void*) lData);
    }
    else sock_new->lookup_status++;
  }

  /* negotiate compression */
  // text_to_buffer(sock_new, (char *) compress_will2);
  // text_to_buffer(sock_new, (char *) compress_will);

  /* send the greeting */
  // text_to_buffer(sock_new, bufferString(greeting));

  /* everything went as it was supposed to */
  return sock_new;
}