Example #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;
		}
Example #2
0
File: irc.c Project: 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;
    }
}