static void cd_ConsoleLog (int priority, const char* format, ...) { /* Return on MASKed log priorities */ if (LOG_MASK(priority) & cd_mask) { return; } static const char* names[] = { "EMERG", "ALERT", "CRIT", "ERR", "WARNING", "NOTICE", "INFO", "DEBUG" }; va_list ap; va_start(ap, format); CDString* priorityBuffer; CDString* messageBuffer = CD_CreateStringFromFormatList(format, ap); if (priority >= (sizeof(names) / sizeof(char*)) || priority < 0) { priorityBuffer = CD_CreateStringFromCString("UNKNOWN"); } else { priorityBuffer = CD_CreateStringFromCString(names[priority]); } printf("%s: %s\n", CD_StringContent(priorityBuffer), CD_StringContent(messageBuffer)); fflush(stdout); CD_DestroyString(priorityBuffer); CD_DestroyString(messageBuffer); va_end(ap); }
bool MC_StringIsValid (MCString self) { assert(self); for (size_t i = 0, ie = CD_StringLength(self); i < ie; i++) { bool has = false; CDString* ch = CD_CharAt(self, i); for (size_t h = 0, he = cd_UTF8_strlen(MCCharset); h < he; h++) { const char* che = &MCCharset[cd_UTF8_offset(MCCharset, h)]; if (strncmp(CD_StringContent(ch), che, CD_StringSize(ch)) == 0) { has = true; break; } } if (!has && !(strncmp(CD_StringContent(ch), "§", 2) == 0 && i < ie - 2)) { CD_DestroyString(ch); return false; } CD_DestroyString(ch); } return true; }
inline bool CD_StringEndWith (CDString* self, const char* check) { size_t length = strlen(check); return strncmp(CD_StringContent(self) + CD_StringSize(self) - length, check, length) == 0; }
static void cd_UpdateLength (CDString* self) { assert(self); self->length = CD_UTF8_strnlen(CD_StringContent(self), self->raw->slen); }
CDString* CD_StringBasename (CDString* self) { char* path = strdup(CD_StringContent(self)); char* file = basename(path); CDString* result = CD_CreateStringFromCStringCopy(file); free(path); return result; }
CDString* CD_StringDirname (CDString* self) { char* path = strdup(CD_StringContent(self)); char* directory = dirname(path); CDString* result = CD_CreateStringFromCStringCopy(directory); free(path); return result; }
void SV_BufferAddString (CDBuffer* self, CDString* data) { CDString* sanitized = SV_StringSanitize(data); SVShort size = htons(CD_StringSize(sanitized)); evbuffer_add(self->raw, &size, SVShortSize); evbuffer_add(self->raw, CD_StringContent(sanitized), CD_StringSize(sanitized)); SV_DestroyString(sanitized); }
MCString MC_StringSanitize (MCString self) { CDString* result = CD_CreateString(); assert(self); for (size_t i = 0, ie = CD_StringLength(self); i < ie; i++) { bool has = false; CDString* ch = CD_CharAt(self, i); for (size_t h = 0, he = cd_UTF8_strlen(MCCharset); h < he; h++) { const char* che = &MCCharset[cd_UTF8_offset(MCCharset, h)]; if (strncmp(CD_StringContent(ch), che, CD_StringSize(ch)) == 0) { has = true; break; } } if (i == ie - 2 && strncmp(CD_StringContent(ch), "§", 2) == 0){ CD_DestroyString(ch); break; } if (has || strncmp(CD_StringContent(ch), "§", 2) == 0) { CD_AppendString(result, ch); } else { CD_AppendCString(result, "?"); } CD_DestroyString(ch); } cd_UpdateLength(self); return result; }
CDString* CD_CreateStringFromOffset (CDString* string, size_t offset, size_t limit) { const char* data; assert(string); if (offset >= CD_StringLength(string)) { return NULL; } data = CD_StringContent(string) + CD_UTF8_offset(CD_StringContent(string), offset); if (limit == 0) { limit = strlen(data); } else { limit = CD_UTF8_offset(data, limit); } return CD_CreateStringFromBufferCopy(data, limit); }
static bool cdadmin_HandleCommand (CDServer* server, CDPlayer* player, CDString* command) { CDRegexp* regexp = CD_CreateRegexp("^(\\w+)(?:\\s+(.*?))?$", CDRegexpNone); CDRegexpMatches* matches = CD_RegexpMatch(regexp, command); if (!matches) { goto error; } SDEBUG(server, "Command> %s: %s\n", CD_StringContent(matches->item[1]), CD_StringContent(matches->item[2])); #include "src/auth.c" #include "src/workers.c" // #include "src/player.c" // #include "src/ticket.c" error: { CD_DestroyRegexpKeepString(regexp); if (matches) { CD_DestroyRegexpMatches(matches); } return true; } done: { CD_DestroyRegexpKeepString(regexp); if (matches) { CD_DestroyRegexpMatches(matches); } return false; } }
static bool cdadmin_HandleChat (CDServer* server, CDPlayer* player, CDString* message) { if (!player->username) { return false; } CDString* name = cdadmin_ColoredNick(player); SLOG(server, LOG_NOTICE, "<%s> %s", CD_StringContent(player->username), CD_StringContent(message)); CD_ServerBroadcast(server, CD_CreateStringFromFormat("<%s> %s", CD_StringContent(name), CD_StringContent(message))); // TODO: testing/WIP? //CD_ServerBroadcast(server, CD_CreateStringFromCString(MCCharset)); CD_DestroyString(name); return false; }
CDString* CD_InsertString (CDString* self, CDString* insert, size_t position) { assert(self); assert(insert); cd_MakeStringInternal(self); if (binsert(self->raw, CD_UTF8_offset(CD_StringContent(self), position), insert->raw, '\0') == BSTR_OK) { cd_UpdateLength(self); } else { self = NULL; } return self; }
static void cd_StaticRequest (struct evhttp_request* request, CDServer* server) { int error = HTTP_OK; const char* message = "OK"; const char* uri = evhttp_request_get_uri(request); struct evhttp_uri* decoded = evhttp_uri_parse(uri); if (evhttp_request_get_command(request) != EVHTTP_REQ_GET) { error = HTTP_BADMETHOD; message = "Invalid request method"; goto end; } if (!decoded || strstr(evhttp_uri_get_path(decoded), "..")) { error = HTTP_BADREQUEST; message = "Bad request"; goto end; } DO { CDString* path = CD_CreateStringFromFormat("%s/%s", server->config->cache.httpd.root, evhttp_uri_get_path(decoded) ? evhttp_uri_get_path(decoded) : "index.html"); if (CD_IsDirectory(CD_StringContent(path))) { CD_AppendCString(path, "/index.html"); } if (!CD_IsReadable(CD_StringContent(path))) { error = HTTP_NOTFOUND; message = "File not found"; CD_DestroyString(path); goto end; } struct evbuffer* buffer = evbuffer_new(); int fd = open(CD_StringContent(path), O_RDONLY); evhttp_add_header(evhttp_request_get_output_headers(request), "Content-Type", cd_GuessContentType(CD_StringContent(path))); evbuffer_add_file(buffer, fd, 0, CD_FileSize(CD_StringContent(path))); evhttp_send_reply(request, HTTP_OK, "OK", buffer); evbuffer_free(buffer); CD_DestroyString(path); } end: { if (decoded) { evhttp_uri_free(decoded); } if (error != HTTP_OK) { evhttp_send_error(request, error, message); } } }
inline bool CD_StringIsEqual (CDString* a, const char* b) { return strcmp(CD_StringContent(a), b) == 0; }
inline bool CD_StringStartWith (CDString* self, const char* check) { return strncmp(CD_StringContent(self), check, strlen(check)) == 0; }