Exemplo n.º 1
0
		static int list_commands(t_connection * c)
		{
			char * line;
			int    i;

			message_send_text(c, message_type_info, c, "Chat commands:");
			std::rewind(hfd);
			while ((line = file_get_line(hfd)) != NULL)
			{
				for (i = 0; line[i] == ' ' && line[i] != '\0'; i++); /* skip spaces in front of %command */
				if (line[i] == '%')  /* is this a command ? */
				{
					char *p, *buffer;
					int al;
					int skip;
					unsigned int length, position;

					/* ok. now we must see if there are any aliases */
					length = MAX_COMMAND_LEN + 1; position = 0;
					buffer = (char*)xmalloc(length + 1); /* initial memory allocation = pretty fair */
					p = line + i;
					do
					{
						al = 0;
						skip = 0;
						for (i = 1; p[i] != ' ' && p[i] != '\0' && p[i] != '#'; i++); /* skip command */
						if (p[i] == ' ') al = 1; /* we have something after the command.. must remember that */
						p[i] = '\0'; /* end the string at the end of the command */
						p[0] = '/'; /* change the leading character (% or space) read from the help file to / */
						if (!(command_get_group(p) & account_get_command_groups(conn_get_account(c)))) skip = 1;
						if (length < std::strlen(p) + position + 1)
							/* if we don't have enough space in the buffer then get some */
							length = std::strlen(p) + position + 1; /* the new length */
						buffer = (char*)xrealloc(buffer, length + 1);
						buffer[position++] = ' '; /* put a space before each alias */
						/* add the alias to the output string */
						std::strcpy(buffer + position, p); position += std::strlen(p);
						if (al)
						{
							for (; p[i + 1] == ' ' && p[i + 1] != '\0' && p[i + 1] != '#'; i++); /* skip spaces */
							if (p[i + 1] == '\0' || p[i + 1] == '#')
							{
								al = 0; continue;
							}
							p += i; /* jump to the next command */
						}
					} while (al);
					if (!skip) message_send_text(c, message_type_info, c, buffer); /* print out the buffer */
					xfree(buffer);
				}
			}
			file_get_line(NULL); // clear file_get_line buffer
			return 0;
		}
Exemplo n.º 2
0
		// add new line at the end of log file
		extern void userlog_append(t_account * account, const char * text)
		{
			// is logging enabled?
			if (!prefs_get_log_commands())
				return;
			
			unsigned int groups = 0;
			const char * cglist = prefs_get_log_command_groups();

			// convert string groups from config to integer
			for (int i = 0; i < strlen(cglist); i++)
			{
				if (cglist[i] == '1') groups |= 1;
				else if (cglist[i] == '2') groups |= 2;
				else if (cglist[i] == '3') groups |= 4;
				else if (cglist[i] == '4') groups |= 8;
				else if (cglist[i] == '5') groups |= 16;
				else if (cglist[i] == '6') groups |= 32;
				else if (cglist[i] == '7') groups |= 64;
				else if (cglist[i] == '8') groups |= 128;
			}

			// log only commands for admins/operators and users in "groups" defined in config
			if (!account_is_operator_or_admin(account, NULL) && !(account_get_command_groups(account) & groups))
				return;

			bool is_cmd_found = false;

			// if command list empty then log all commands
			if (userlog_commands.size() == 0)
				is_cmd_found = true;
			else
			{
				// get command name
				std::vector<std::string> args = split_command(text, 0);
				std::string cmd = args[0];

				// find command in defined command list
				for (std::vector<std::string>::iterator it = userlog_commands.begin(); it != userlog_commands.end(); ++it) {
					if (*it == cmd)
					{
						is_cmd_found = true;
						break;
					}
				}
			}
			if (!is_cmd_found)
				return;

			// get time string
			char        time_string[USEREVENT_TIME_MAXLEN];
			struct std::tm * tmnow;
			std::time_t      now;

			std::time(&now);
			if (!(tmnow = std::localtime(&now)))
				std::strcpy(time_string, "?");
			else
				std::strftime(time_string, USEREVENT_TIME_MAXLEN, USEREVENT_TIME_FORMAT, tmnow);


			char * filename = userlog_filename(account_get_name(account), true);

			if (FILE *fp = fopen(filename, "a"))
			{
				// append date and text
				std::fprintf(fp, "[%s] %s\n", time_string, text);
				std::fclose(fp);
			}
			else
			{
				ERROR1("could not write into user log file \"%s\"", filename);
			}
		}
Exemplo n.º 3
0
Arquivo: irc.c Projeto: 91D2/pvpgn
extern int irc_message_postformat(t_packet * packet, t_connection const * dest)
{
    int len;
    /* the four elements */
    char * e1;
    char * e1_2;
    char * e2;
    char * e3;
    char * e4;
    char const * tname = NULL;
    char const * toname = "AUTH"; /* fallback name */

    if (!packet) {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL packet");
	return -1;
    }
    if (!dest) {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL dest");
	return -1;
    }

    e1 = packet_get_raw_data(packet,0);
    e2 = strchr(e1,'\n');
    if (!e2) {
	eventlog(eventlog_level_warn,__FUNCTION__,"malformed message (e2 missing)");
	return -1;
    }
    *e2++ = '\0';
    e3 = strchr(e2,'\n');
    if (!e3) {
	eventlog(eventlog_level_warn,__FUNCTION__,"malformed message (e3 missing)");
	return -1;
    }
    *e3++ = '\0';
    e4 = strchr(e3,'\n');
    if (!e4) {
	eventlog(eventlog_level_warn,__FUNCTION__,"malformed message (e4 missing)");
	return -1;
    }
    *e4++ = '\0';

    if (prefs_get_hide_addr() && !(account_get_command_groups(conn_get_account(dest)) & command_get_group("/admin-addr")))
    {
      e1_2 = strchr(e1,'@');
      if (e1_2)
      {
	  *e1_2++ = '\0';
      }
    }
    else
    e1_2 = NULL;

    if (e3[0]=='\0') { /* fill in recipient */
    	if ((tname = conn_get_chatname(dest)))
    	    toname = tname;
    } else
    	toname = e3;

    if (strcmp(toname,"\r")==0) {
	toname = ""; /* HACK: the target field is really empty */
    }
    	
    len = (strlen(e1)+1+strlen(e2)+1+strlen(toname)+1+strlen(e4)+2+1);
    if (len<=MAX_IRC_MESSAGE_LEN) {
	char msg[MAX_IRC_MESSAGE_LEN+1];

	if (e1_2)
	    sprintf(msg,"%s@hidden %s %s %s\r\n",e1,e2,toname,e4);
	else
	    sprintf(msg,"%s %s %s %s\r\n",e1,e2,toname,e4);
	eventlog(eventlog_level_debug,__FUNCTION__,"sent \"%s\"",msg);
	packet_set_size(packet,0);
	packet_append_data(packet,msg,strlen(msg));
	if (tname)
	    conn_unget_chatname(dest,tname);
	return 0;
    } else {
	/* FIXME: split up message? */
    	eventlog(eventlog_level_warn,__FUNCTION__,"maximum IRC message length exceeded");
	if (tname)
	    conn_unget_chatname(dest,tname);
	return -1;
    }
}