/* * Text_to_buffer() * * Stores outbound text in a buffer, where it will * stay untill it is flushed in the gameloop. * * Will also parse ANSI colors and other tags. */ void text_to_buffer(SOCKET_DATA *dsock, const char *txt) { // if we're at the head of the outbuf and haven't entered a command, // also copy a newline so we're not printing in front of the prompt if(bufferLength(dsock->outbuf) == 0 && !dsock->bust_prompt) bufferCat(dsock->outbuf, "\r\n"); /* add data to buffer */ bufferCat(dsock->outbuf, txt); }
// // Send a message with Python statements potentially embedded in it. For //evaluating PyObject *PySocket_send(PyObject *self, PyObject *args, PyObject *kwds) { static char *kwlist[ ] = { "mssg", "dict", "newline", NULL }; SOCKET_DATA *me = NULL; char *text = NULL; PyObject *dict = NULL; bool newline = TRUE; if(!PyArg_ParseTupleAndKeywords(args, kwds, "s|Ob", kwlist, &text, &dict, &newline)) { PyErr_Format(PyExc_TypeError, "Invalid arguments supplied to Mudsock.send"); return NULL; } // is dict None? set it to NULL for expand_to_char if(dict == Py_None) dict = NULL; // make sure the dictionary is a dictionary if(!(dict == NULL || PyDict_Check(dict))) { PyErr_Format(PyExc_TypeError, "Mudsock.send expects second argument to be a dict object."); return NULL; } // make sure we exist if( (me = PySocket_AsSocket(self)) == NULL) { PyErr_Format(PyExc_TypeError, "Tried to send nonexistent socket."); return NULL; } if(dict != NULL) PyDict_SetItemString(dict, "me", self); // build our script environment BUFFER *buf = newBuffer(1); bufferCat(buf, text); // expand out our dynamic descriptions if we have a dictionary supplied if(dict != NULL) { PyObject *env = restricted_script_dict(); PyDict_Update(env, dict); // do the expansion expand_dynamic_descs_dict(buf, env, get_script_locale()); Py_XDECREF(env); } if(newline == TRUE) bufferCat(buf, "\r\n"); text_to_buffer(me, bufferString(buf)); // garbage collection deleteBuffer(buf); return Py_BuildValue(""); }
HELP_OLC *newHelpOLC(const char *keywords, const char *info, const char *user_groups, const char *related) { HELP_OLC *data = malloc(sizeof(HELP_OLC)); data->old_keywords = strdupsafe(keywords); data->keywords = strdupsafe(keywords); data->user_groups = strdupsafe(user_groups); data->related = strdupsafe(related); data->info = newBuffer(1); bufferCat(data->info, info); return data; }
bool flush_output(SOCKET_DATA *dsock) { bool success = TRUE; BUFFER *buf = NULL; // run any hooks prior to flushing our text hookRun("flush", hookBuildInfo("sk", dsock)); // quit if we have no output and don't need/can't have a prompt if(bufferLength(dsock->outbuf) <= 0 && (!dsock->bust_prompt || !socketHasPrompt(dsock))) return success; buf = newBuffer(1); // send our outbound text if(bufferLength(dsock->outbuf) > 0) { hookRun("process_outbound_text", hookBuildInfo("sk", dsock)); hookRun("finalize_outbound_text", hookBuildInfo("sk", dsock)); //success = text_to_socket(dsock, bufferString(dsock->outbuf)); bufferCat(buf, bufferString(dsock->outbuf)); bufferClear(dsock->outbuf); } // send our prompt if(dsock->bust_prompt && success) { socketShowPrompt(dsock); hookRun("process_outbound_prompt", hookBuildInfo("sk", dsock)); hookRun("finalize_outbound_prompt", hookBuildInfo("sk", dsock)); //success = text_to_socket(dsock, bufferString(dsock->outbuf)); bufferCat(buf, bufferString(dsock->outbuf)); bufferClear(dsock->outbuf); dsock->bust_prompt = FALSE; } success = text_to_socket(dsock, bufferString(buf)); deleteBuffer(buf); // return our success return success; }
void next_cmd_from_buffer(SOCKET_DATA *dsock) { // do we have stuff in our input list? If so, use that instead of inbuf dsock->cmd_read = FALSE; if(listSize(dsock->input) > 0) { char *cmd = listPop(dsock->input); bufferClear(dsock->next_command); bufferCat(dsock->next_command, cmd); dsock->cmd_read = TRUE; dsock->bust_prompt = TRUE; free(cmd); } else { int i = 0, cmd_end = -1; // are we building an IAC command? Try to continue it if(bufferLength(dsock->iac_sequence) > 0) i += read_iac_sequence(dsock, 0); // copy over characters until we hit a newline, an IAC command, or \0 for(; dsock->inbuf[i] != '\0' && cmd_end < 0; i++) { switch(dsock->inbuf[i]) { default: // append us to the command bufferCatCh(dsock->next_command, dsock->inbuf[i]); break; case '\n': // command end found cmd_end = ++i; case '\r': // ignore \r ... only pay attention to \n break; case (signed char) IAC: i += read_iac_sequence(dsock, i) - 1; break; } if(cmd_end >= 0) break; } // move the context of inbuf down int begin = 0; while(dsock->inbuf[i] != '\0') dsock->inbuf[begin++] = dsock->inbuf[i++]; dsock->inbuf[begin] = '\0'; // did we find a command? if(cmd_end >= 0) { dsock->cmd_read = TRUE; dsock->bust_prompt = TRUE; } } }
RESET_DATA *resetRead (STORAGE_SET *set) { RESET_DATA *reset = malloc(sizeof(RESET_DATA)); reset->type = read_int(set, "type"); reset->times = read_int(set, "times"); reset->chance = read_int(set, "chance"); reset->max = read_int(set, "max"); reset->room_max = read_int(set, "room_max"); reset->on = gen_read_list(read_list(set, "on"), resetRead); reset->in = gen_read_list(read_list(set, "in"), resetRead); reset->then = gen_read_list(read_list(set, "then"), resetRead); reset->arg = newBuffer(1); bufferCat(reset->arg, read_string(set, "arg")); return reset; }
void expand_to_char(CHAR_DATA *ch, const char *mssg, PyObject *dict, const char *locale, bool newline) { BUFFER *buf = newBuffer(1); bufferCat(buf, mssg); if(dict != NULL) { // build the script dictionary PyObject *script_dict = restricted_script_dict(); PyDict_Update(script_dict, dict); // do the expansion expand_dynamic_descs_dict(buf, script_dict, locale); // garbage collection and end Py_XDECREF(script_dict); } if(newline == TRUE) bufferCat(buf, "\r\n"); text_to_char(ch, bufferString(buf)); // garbage collection deleteBuffer(buf); }
int PySocket_set_outbound_text(PySocket *self, PyObject *value, void *closure) { if(!PyString_Check(value)) { PyErr_Format(PyExc_TypeError, "Outbound text must be in string format."); return -1; } SOCKET_DATA *sock = PySocket_AsSocket((PyObject *)self); if(sock == NULL) return -1; else { bufferClear(socketGetOutbound(sock)); bufferCat(socketGetOutbound(sock), PyString_AsString(value)); return 0; } }
void resetSetArg (RESET_DATA *reset, const char *arg) { bufferClear(reset->arg); bufferCat(reset->arg, arg); }
void expand_dynamic_descs_dict(BUFFER *desc, PyObject *dict,const char *locale){ // make our new buffer BUFFER *new_desc = newBuffer(bufferLength(desc)*2); BUFFER *code = newBuffer(1); // for things we have to evaluate bool proc_cond = FALSE; // are we processing a conditional statement? // copy over all of our description that is not dynamic text int start, end, i, j, size = bufferLength(desc); for(i = 0; i < size; i++) { // figure out when our next dynamic desc is. start = next_letter_in(bufferString(desc) + i, '['); // no more if(start == -1) { // copy the rest and skip to the end of the buffer bprintf(new_desc, "%s", bufferString(desc) + i); i = size - 1; } // we have another desc else { // copy everything up to start while(start > 0) { bprintf(new_desc, "%c", *(bufferString(desc) + i)); start--; i++; } // skip the start marker i++; // find our end end = next_letter_in(bufferString(desc) + i, ']'); // make sure we have it if(end == -1) break; // copy everything between start and end, and format the code bufferClear(code); for(j = 0; j < end; j++) bprintf(code, "%c", *(bufferString(desc) + i + j)); bufferReplace(code, "\n", " ", TRUE); bufferReplace(code, "\r", "", TRUE); // skip i up to the end i = i + end; // are we trying to process a conditional statement? if(!strncasecmp(bufferString(code), "if ", 3)) { // strip out the leading if and whitespace char *code_copy = strdup(bufferString(code)); char *ptr = code_copy + 3; while(isspace(*ptr)) ptr++; // copy over the cleaned-up code, signal we're processing a conditional bufferClear(code); bufferCat(code, ptr); proc_cond = TRUE; // garbage collection free(code_copy); } // evaluate the code PyObject *retval = eval_script(dict, bufferString(code), locale); // did we encounter an error? if(retval == NULL) break; // are we evaluating a conditional or no? else if(proc_cond) { BUFFER *cond_retval = expand_dynamic_conditional(desc, dict, retval, locale, &i); // if we have something to print, expand its embedded python if(*bufferString(cond_retval)) expand_dynamic_descs_dict(cond_retval, dict, locale); bufferCat(new_desc, bufferString(cond_retval)); deleteBuffer(cond_retval); proc_cond = FALSE; } // append the output else if(PyString_Check(retval)) bprintf(new_desc, "%s", PyString_AsString(retval)); else if(PyInt_Check(retval)) bprintf(new_desc, "%ld", PyInt_AsLong(retval)); else if(PyFloat_Check(retval)) bprintf(new_desc, "%lf", PyFloat_AsDouble(retval)); // invalid return type... else if(retval != Py_None) log_string("dynamic desc had invalid evaluation: %s", bufferString(code)); // oddly, freeing retval here corrupt memory. // And not freeing it doesn't cause a memory leak. So bizarre... // Py_XDECREF(retval); } } // copy over our contents bufferCopyTo(new_desc, desc); // garbage collection deleteBuffer(code); deleteBuffer(new_desc); }