Example #1
0
static bool Irc_Proto_Enqueue(const char *msg, size_t msg_len) {
	// create message node
	const double messageBucketSize = Cvar_GetFloatValue(irc_messageBucketSize);
	const double characterBucketSize = Cvar_GetFloatValue(irc_characterBucketSize);
	irc_bucket_message_t * const m = (irc_bucket_message_t*) Irc_MemAlloc(sizeof(irc_bucket_message_t));
	irc_bucket_message_t * n = irc_bucket.first_msg;
	if (irc_bucket.message_size + 1 <= messageBucketSize && irc_bucket.character_size + msg_len <= characterBucketSize) {
		m->msg = (char*) Irc_MemAlloc(msg_len);
		memcpy(m->msg, msg, msg_len);
		m->msg_len = msg_len;
		m->next = NULL;
		// append message node
		if (n) {
			while (n->next)
				n = n->next;
			n->next = m;
		} else
			irc_bucket.first_msg = m;
		// update bucket sizes
		++irc_bucket.message_size;
		irc_bucket.character_size += msg_len;
		return false;
	} else {
		strcpy(IRC_ERROR_MSG, "Bucket(s) full. Could not enqueue message.");
		return true;
	}
}
Example #2
0
static void Irc_Rcon_Flush_f(int redirected, const char *msg, const void *extra) {
	if (redirected == 1) {
		// cut into lines
		size_t len = strlen(msg);
		char * const outputbuf = (char*) Irc_MemAlloc(len + 1);
		char *line;
		memcpy(outputbuf, msg, len);
		outputbuf[len] = '\0';
		for (line = strtok(outputbuf, "\n"); line; line = strtok(NULL, "\n")) {
			// perform color code translation
			char * const colored_line = (char*) Irc_MemAlloc(strlen(line) * 2);
			char *c = colored_line;
			char chunk[101];
			Irc_ColorFilter(line, IRC_COLOR_WSW_TO_IRC, colored_line);
			// cut line into neat little chunks so the IRC server accepts them
			len = strlen(c);
			while (len) {
				size_t to_copy = min(sizeof(chunk) - 1, len);
				memcpy(chunk, c, to_copy);
				chunk[to_copy] = '\0';
				Irc_Proto_Msg(rcon_flush_to, chunk);
				c += to_copy;
				len -= to_copy;
			}
			Irc_MemFree(colored_line);
		}
		Irc_MemFree(outputbuf);
	}
}
Example #3
0
void Irc_Proto_AddListener(irc_command_t cmd, irc_listener_f listener) {
	irc_listener_node_t *n = (irc_listener_node_t*) Irc_MemAlloc(sizeof(irc_listener_node_t));
	n->listener = listener;
	n->next = NULL;
	switch (cmd.type) {
		irc_listener_node_t *prev;
		case IRC_COMMAND_NUMERIC:
			// numeric command, add to numeric_listeners
			prev = numeric_listeners[cmd.numeric];
			if (prev) {
				// not the first listener of this command, append to list
				while (prev->next)
					prev = prev->next;
				prev->next = n;
			} else {
				// first listener of this command, make list head
				numeric_listeners[cmd.numeric] = n;
			}
			break;
		case IRC_COMMAND_STRING:
			// string command, add to string_listeners
			if (IRC_IMPORT.Trie_Find(string_listeners, cmd.string, TRIE_EXACT_MATCH, (void**) &prev) == TRIE_OK) {
				// not the first listener of this command, append to list
				assert(prev);
				while (prev->next)
					prev = prev->next;
				prev->next = n;
			} else {
				// first listener of this command, make list head
				IRC_IMPORT.Trie_Insert(string_listeners, cmd.string, n);
			}
			break;
	}
}
Example #4
0
void Irc_Proto_RemoveListener(irc_command_t cmd, irc_listener_f listener) {
	if (!immutable_listeners) {
		// remove now
		irc_listener_node_t *n, *prev = NULL;
		switch (cmd.type) {
			case IRC_COMMAND_NUMERIC:
				// numeric command, remove from numeric_listeners
				for (n = numeric_listeners[cmd.numeric]; n; n = n->next) {
					if (n->listener == listener) {
						// match, remove
						if (prev)
							// not list head, cut
							prev->next = n->next;
						else
							// list head
							numeric_listeners[cmd.numeric] = n->next;
						Irc_MemFree(n);
						break;
					}
					prev = n;
				}
				break;
			case IRC_COMMAND_STRING:
				// string command, remove from string_listeners
				IRC_IMPORT.Trie_Find(string_listeners, cmd.string, TRIE_EXACT_MATCH, (void**) &n);
				for (; n; n = n->next) {
					if (n->listener == listener) {
						// match, remove
						if (prev) {
							// not list head, cut
							prev->next = n->next;
						} else {
							// list head
							if (n->next) {
								// has linked nodes, replace head with next node
								IRC_IMPORT.Trie_Replace(string_listeners, cmd.string, n->next, (void**) &prev);
							} else {
								// empty list, remove cmd.string from trie
								IRC_IMPORT.Trie_Remove(string_listeners, cmd.string, (void**) &prev);
							}
						}
						Irc_MemFree(n);
						break;
					}
					prev = n;
				}
				break;
		}
	} else {
		// prepend to removed_listeners for later removal
		irc_removed_listener_node_t * const n = (irc_removed_listener_node_t*) Irc_MemAlloc(sizeof(irc_removed_listener_node_t));
		n->cmd = cmd;
		n->listener = listener;
		n->next = removed_listeners;
		removed_listeners = n;	
	}
}
Example #5
0
void Irc_Proto_AddGenericListener(irc_listener_f listener) {
	irc_listener_node_t *n = (irc_listener_node_t*) Irc_MemAlloc(sizeof(irc_listener_node_t));
	n->listener = listener;
	n->next = NULL;
	if (generic_listeners) {
		irc_listener_node_t *prev = generic_listeners;
		while (prev->next)
			prev = prev->next;
		prev->next = n;
	} else
		generic_listeners = n;
}
Example #6
0
static void Irc_Rcon_ProcessMsg(const char *user, const char *msg) {

	static char nick[IRC_SEND_BUF_SIZE];
	irc_nick_prefix_t prefix;
	char *buf = (char*) Irc_MemAlloc((int) strlen(msg) + 1);
	const char *word;

	Irc_ParseName(user, nick, &prefix);
	strcpy(buf, msg);
	word = strtok(buf, " ");
	if (word && !strcasecmp(word, IRC_RCON_PREFIX)) {
		// it really is an RCON message, not a normal PRIVMSG
		unsigned int millis = IRC_IMPORT.Milliseconds();
		irc_rcon_user_t *rcon_user;
		if (IRC_IMPORT.Trie_Find(irc_rcon_users, user, TRIE_EXACT_MATCH, (void**) &rcon_user) == TRIE_OK) {
			// user is already authorized
			const unsigned int timeout = Cvar_GetIntegerValue(irc_rconTimeout);
			if (!timeout || ((millis - rcon_user->millis) / 1000) < timeout) {
				// no timeout, reset user timestamp
				irc_rcon_user_t *rcon_user_old;
				rcon_user->millis = millis;
				IRC_IMPORT.Trie_Replace(irc_rcon_users, user, (void*) rcon_user, (void**) &rcon_user_old);
				assert(rcon_user == rcon_user_old);
				word = strtok(NULL, " ");
				if (word) {
					if (!strcasecmp(word, IRC_RCON_LOGOUT)) {
						// user wants to log off
						Irc_Proto_Msg(nick, "Logged out. You may login again via " IRC_RCON_PREFIX " " IRC_RCON_LOGIN " <rcon_password>.");
						IRC_IMPORT.Trie_Remove(irc_rcon_users, user, (void**) &rcon_user);
						Irc_MemFree(rcon_user);
					} else {
						// redirect console and execute
						char cmd_buf[IRC_SEND_BUF_SIZE + 2];
						char rcon_buf[16384];	// make it big, we don't trust console redirect
						char *c = cmd_buf;
						size_t word_len = strlen(word);
						memset(rcon_buf, 0, sizeof(rcon_buf));
						memcpy(c, word, word_len);
						c += word_len;
						for (word = strtok(NULL, " "); word; word = strtok(NULL, " ")) {
							*c++ = ' ';
							word_len = strlen(word);
							memcpy(c, word, word_len);
							c += word_len;
						}
						*c = '\0';
						rcon_flush_to = nick;
						IRC_IMPORT.Com_BeginRedirect(1, rcon_buf, sizeof(rcon_buf) - 1, Irc_Rcon_Flush_f, NULL);
						IRC_IMPORT.Cmd_ExecuteString(cmd_buf);
						IRC_IMPORT.Com_EndRedirect();
					}
				}
			} else {
				// timeout, inform user
				Irc_Proto_Msg(nick, "Timed out. Please login via " IRC_RCON_PREFIX " " IRC_RCON_LOGIN " <rcon_password>.");
				IRC_IMPORT.Trie_Remove(irc_rcon_users, user, (void**) &rcon_user);
				Irc_MemFree(rcon_user);
			}
		} else {
			// user not authorized, check for IRC_RCON_LOGIN command
			word = strtok(NULL, " ");
			if (word && !strcasecmp(word, IRC_RCON_LOGIN)) {
				const cvar_t * const rcon_password = IRC_IMPORT.Cvar_Get("rcon_password", "", CVAR_ARCHIVE);
				word = strtok(NULL, " ");
				if (word && !strcmp(word, Cvar_GetStringValue(rcon_password))) {
					// password correct, authorize
					Irc_Proto_Msg(nick, "Logged in. You may now issue commands via " IRC_RCON_PREFIX " <command> {<arg>}. Log out via " IRC_RCON_PREFIX " " IRC_RCON_LOGOUT ".");
					rcon_user = (irc_rcon_user_t*) Irc_MemAlloc(sizeof(irc_rcon_user_t));
					rcon_user->millis = millis;
					IRC_IMPORT.Trie_Insert(irc_rcon_users, user, (void*) rcon_user);
				}
			}
		}
	}
	Irc_MemFree(buf);
}