Example #1
0
File: irc.c Project: 91D2/pvpgn
extern int irc_send_ping(t_connection * conn)
{
    t_packet * p;
    char data[MAX_IRC_MESSAGE_LEN];
    
    if (!conn) {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection");
	return -1;
    }
    if (!(p = packet_create(packet_class_raw))) {
	eventlog(eventlog_level_error,__FUNCTION__,"could not create packet");
	return -1;
    }

    if((conn_get_wol(conn) == 1))
        return 0;

    conn_set_ircping(conn,get_ticks());
    if (conn_get_state(conn)==conn_state_bot_username)
    	sprintf(data,"PING :%u\r\n",conn_get_ircping(conn)); /* Undernet doesn't reveal the servername yet ... neither do we */
    else if ((6+strlen(server_get_hostname())+2+1)<=MAX_IRC_MESSAGE_LEN)
    	sprintf(data,"PING :%s\r\n",server_get_hostname());
    else
    	eventlog(eventlog_level_error,__FUNCTION__,"maximum message length exceeded");
    eventlog(eventlog_level_debug,__FUNCTION__,"[%d] sent \"%s\"",conn_get_socket(conn),data);
    packet_set_size(p,0);
    packet_append_data(p,data,strlen(data));
    conn_push_outqueue(conn,p);
    packet_del_ref(p);
    return 0;
}
Example #2
0
File: irc.c Project: 91D2/pvpgn
static int irc_who_connection(t_connection * dest, t_connection * c)
{
    t_account * a;
    char const * tempuser;
    char const * tempowner;
    char const * tempname;
    char const * tempip;
    char const * tempflags = "@"; /* FIXME: that's dumb */
    char temp[MAX_IRC_MESSAGE_LEN];
    char const * tempchannel;
    
    if (!dest) {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL destination");
	return -1;
    }
    if (!c) {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection");
	return -1;
    }
    a = conn_get_account(c);
    if (!(tempuser = clienttag_uint_to_str(conn_get_clienttag(c))))
    {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL clienttag (tempuser)");
	return -1;
    }
    if (!(tempowner = account_get_ll_owner(a)))
    {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL ll_owner (tempowner)");
	return -1;
    }
    if (!(tempname = conn_get_username(c)))
    {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL username (tempname)");
	return -1;
    }
    if (!(tempip = addr_num_to_ip_str(conn_get_addr(c))))
    {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL addr (tempip)");
	return -1;
    }
    if (!(tempchannel = irc_convert_channel(conn_get_channel(c))))
    {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL channel (tempchannel)");
	return -1;
    }
    if ((strlen(tempchannel)+1+strlen(tempuser)+1+strlen(tempip)+1+strlen(server_get_hostname())+1+strlen(tempname)+1+1+strlen(tempflags)+4+strlen(tempowner)+1)>MAX_IRC_MESSAGE_LEN) {
	eventlog(eventlog_level_info,__FUNCTION__,"WHO reply too long - skip");
	return -1;
    } else
        sprintf(temp,"%s %s %s %s %s %c%s :0 %s",tempchannel,tempuser,tempip,server_get_hostname(),tempname,'H',tempflags,tempowner);
    irc_send(dest,RPL_WHOREPLY,temp);
    return 0;
}
Example #3
0
File: irc.c Project: 91D2/pvpgn
extern int irc_send_pong(t_connection * conn, char const * params)
{
    t_packet * p;
    char data[MAX_IRC_MESSAGE_LEN];
    
    if (!conn) {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection");
	return -1;
    }
    if ((1+strlen(server_get_hostname())+1+4+1+strlen(server_get_hostname())+((params)?(2+strlen(params)):(0))+2+1) > MAX_IRC_MESSAGE_LEN) {
	eventlog(eventlog_level_error,__FUNCTION__,"max message length exceeded");
	return -1;
    }
    if (!(p = packet_create(packet_class_raw))) {
	eventlog(eventlog_level_error,__FUNCTION__,"could not create packet");
	return -1;
    }
    
    if (params)
    	sprintf(data,":%s PONG %s :%s\r\n",server_get_hostname(),server_get_hostname(),params);
    else
    	sprintf(data,":%s PONG %s\r\n",server_get_hostname(),server_get_hostname());
    eventlog(eventlog_level_debug,__FUNCTION__,"[%d] sent \"%s\"",conn_get_socket(conn),data);
    packet_set_size(p,0);
    packet_append_data(p,data,strlen(data));
    conn_push_outqueue(conn,p);
    packet_del_ref(p);
    return 0;
}
Example #4
0
File: irc.c Project: 91D2/pvpgn
extern int irc_send_cmd(t_connection * conn, char const * command, char const * params)
{
    t_packet * p;
    char data[MAX_IRC_MESSAGE_LEN+1];
    int len;
    char const * ircname = server_get_hostname(); 
    char const * nick;
    
    if (!conn) {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection");
	return -1;
    }
    if (!command) {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL command");
	return -1;
    }
    if (!(p = packet_create(packet_class_raw))) {
	eventlog(eventlog_level_error,__FUNCTION__,"could not create packet");
	return -1;
    }

    nick = conn_get_loggeduser(conn);
    if (!nick)
    	nick = "";
    	
    /* snprintf isn't portable -> check message length first */
    if (params) {
        len = 1+strlen(ircname)+1+strlen(command)+1+strlen(nick)+1+strlen(params)+2;
	if (len > MAX_IRC_MESSAGE_LEN) {
	    eventlog(eventlog_level_error,__FUNCTION__,"message to send is too large (%d bytes)",len);
	    return -1;
	}
	else
	    sprintf(data,":%s %s %s %s\r\n",ircname,command,nick,params);
    } else {
        len = 1+strlen(ircname)+1+strlen(command)+1+strlen(nick)+1+2;
    	if (len > MAX_IRC_MESSAGE_LEN) {
	    eventlog(eventlog_level_error,__FUNCTION__,"message to send is too large (%d bytes)",len);
	    return -1;
	}
	else
	sprintf(data,":%s %s %s\r\n",ircname,command,nick);
    }
    packet_set_size(p,0);
    packet_append_data(p,data,len);
    // eventlog(eventlog_level_debug,__FUNCTION__,"[%d] sent \"%s\"",conn_get_socket(conn),data);
    conn_push_outqueue(conn,p);
    packet_del_ref(p);
    return 0;
}
Example #5
0
File: irc.c Project: 91D2/pvpgn
static char * irc_message_preformat(t_irc_message_from const * from, char const * command, char const * dest, char const * text)
{
    char * myfrom;
    char const * mydest = "";
    char const * mytext = "";
    int len;
    char * msg;

    if (!command) {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL command");
	return NULL;
    }
    if (from) {
	if ((!from->nick)||(!from->user)||(!from->host)) {
	    eventlog(eventlog_level_error,__FUNCTION__,"got malformed from");
	    return NULL;
	}
	myfrom = xmalloc(strlen(from->nick)+1+strlen(from->user)+1+strlen(from->host)+1); /* nick + "!" + user + "@" + host + "\0" */
	sprintf(myfrom,"%s!%s@%s",from->nick,from->user,from->host);
    } else
    	myfrom = xstrdup(server_get_hostname());
    if (dest)
    	mydest = dest;
    if (text)
    	mytext = text;

    len = 1+strlen(myfrom)+1+
    	  strlen(command)+1+
    	  strlen(mydest)+1+
    	  1+strlen(mytext)+1;


    msg = xmalloc(len);
    sprintf(msg,":%s\n%s\n%s\n%s",myfrom,command,mydest,mytext);
    xfree(myfrom);
    return msg;
}
extern int handle_irc_welcome(t_connection * conn)
{
    char temp[MAX_IRC_MESSAGE_LEN];
    std::time_t temptime;
    char const * tempname;
    char const * temptimestr;

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

    tempname = conn_get_loggeduser(conn);

    if ((34+std::strlen(tempname)+1)<=MAX_IRC_MESSAGE_LEN)
        std::sprintf(temp,":Welcome to the %s IRC Network %s",prefs_get_irc_network_name(), tempname);
    else
        std::sprintf(temp,":Maximum length exceeded");
    irc_send(conn,RPL_WELCOME,temp);

    if ((14+std::strlen(server_get_hostname())+10+std::strlen(PVPGN_SOFTWARE" "PVPGN_VERSION)+1)<=MAX_IRC_MESSAGE_LEN)
        std::sprintf(temp,":Your host is %s, running "PVPGN_SOFTWARE" "PVPGN_VERSION,server_get_hostname());
    else
        std::sprintf(temp,":Maximum length exceeded");
    irc_send(conn,RPL_YOURHOST,temp);

    temptime = server_get_starttime(); /* FIXME: This should be build time */
    temptimestr = std::ctime(&temptime);
    if ((25+std::strlen(temptimestr)+1)<=MAX_IRC_MESSAGE_LEN)
        std::sprintf(temp,":This server was created %s",temptimestr); /* FIXME: is ctime() portable? */
    else
        std::sprintf(temp,":Maximum length exceeded");
    irc_send(conn,RPL_CREATED,temp);

    /* we don't give mode information on MYINFO we give it on ISUPPORT */
    if ((std::strlen(server_get_hostname())+7+std::strlen(PVPGN_SOFTWARE" "PVPGN_VERSION)+9+1)<=MAX_IRC_MESSAGE_LEN)
        std::sprintf(temp,"%s "PVPGN_SOFTWARE" "PVPGN_VERSION" - -",server_get_hostname());
    else
        std::sprintf(temp,":Maximum length exceeded");
    irc_send(conn,RPL_MYINFO,temp);

    std::sprintf(temp,"NICKLEN=%d TOPICLEN=%d CHANNELLEN=%d PREFIX=%s CHANTYPES="CHANNEL_TYPE" NETWORK=%s IRCD="PVPGN_SOFTWARE,
                 MAX_CHARNAME_LEN, MAX_TOPIC_LEN, MAX_CHANNELNAME_LEN, CHANNEL_PREFIX, prefs_get_irc_network_name());

    if((std::strlen(temp))<=MAX_IRC_MESSAGE_LEN)
        irc_send(conn,RPL_ISUPPORT,temp);
    else {
        std::sprintf(temp,":Maximum length exceeded");
        irc_send(conn,RPL_ISUPPORT,temp);
    }

    irc_send_motd(conn);

    message_send_text(conn,message_type_notice,NULL,"This is an experimental service");

    conn_set_state(conn,conn_state_bot_password);

    if (conn_get_ircpass(conn)) {
        message_send_text(conn,message_type_notice,NULL,"Trying to authenticate with PASS ...");
        irc_authenticate(conn,conn_get_ircpass(conn));
    } else {
        message_send_text(conn,message_type_notice,NULL,"No PASS command received. Please identify yourself by /msg NICKSERV identify <password>.");
    }
    return 0;
}
Example #7
0
File: irc.c Project: 91D2/pvpgn
extern int irc_message_format(t_packet * packet, t_message_type type, t_connection * me, t_connection * dst, char const * text, unsigned int dstflags)
{
    char * msg;
    char const * ctag;
    t_irc_message_from from;
    
    if (!packet)
    {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL packet");
	return -1;
    }

    msg = NULL;
    if (me)
        ctag = clienttag_uint_to_str(conn_get_clienttag(me));
    else
	    ctag = clienttag_uint_to_str(CLIENTTAG_IIRC_UINT);
        
    switch (type)
    {
    /* case message_type_adduser: this is sent manually in handle_irc */
	case message_type_adduser:
		/* when we do it somewhere else, then we can also make sure to not get our logs spammed */
		break;
    case message_type_join:
    	from.nick = conn_get_chatname(me);
    	from.user = ctag;
    	from.host = addr_num_to_ip_str(conn_get_addr(me));

	    if((conn_get_wol(me) == 1))
	    {
        	char temp[MAX_IRC_MESSAGE_LEN];
    		memset(temp,0,sizeof(temp));
    		
    		/**
            *  For WOL the channel JOIN output must be like the following:
    		*   user!WWOL@hostname JOIN :clanID,longIP channelName
    		*/
    		sprintf(temp,":0,%u",conn_get_addr(me));
    		msg = irc_message_preformat(&from,"JOIN",temp,irc_convert_channel(conn_get_channel(me)));
	    }
	    else
    	msg = irc_message_preformat(&from,"JOIN","\r",irc_convert_channel(conn_get_channel(me)));
    	conn_unget_chatname(me,from.nick);
    	break;
    case message_type_part:
    	from.nick = conn_get_chatname(me);
    	from.user = ctag;
    	from.host = addr_num_to_ip_str(conn_get_addr(me));
    	msg = irc_message_preformat(&from,"PART","\r",irc_convert_channel(conn_get_channel(me)));
    	conn_unget_chatname(me,from.nick);
    	break;
    case message_type_talk:
    case message_type_whisper:
    	{
    	    char const * dest;
	    char temp[MAX_IRC_MESSAGE_LEN];

	    if (me)
	    {
    	        from.nick = conn_get_chatname(me);
    	        from.host = addr_num_to_ip_str(conn_get_addr(me));
	    }
	    else
	    {
		from.nick = server_get_hostname();
		from.host = server_get_hostname();
	    }
	    
            from.user = ctag;
	    
    	    if (type==message_type_talk)
    	    	dest = irc_convert_channel(conn_get_channel(me)); /* FIXME: support more channels and choose right one! */
	    else
	        dest = ""; /* will be replaced with username in postformat */
	    sprintf(temp,":%s",text);
    	    msg = irc_message_preformat(&from,"PRIVMSG",dest,temp);
	    if (me)
    	        conn_unget_chatname(me,from.nick);
    	}
        break;
    case message_type_emote:
    	{
    	    char const * dest;
	    char temp[MAX_IRC_MESSAGE_LEN];

    	    /* "\001ACTION " + text + "\001" + \0 */
	    if ((8+strlen(text)+1+1)<=MAX_IRC_MESSAGE_LEN) {
		sprintf(temp,":\001ACTION %s\001",text);
	    } else {
		sprintf(temp,":\001ACTION (maximum message length exceeded)\001");
	    }
    	    from.nick = conn_get_chatname(me);
            from.user = ctag;
    	    from.host = addr_num_to_ip_str(conn_get_addr(me));
    	    /* FIXME: also supports whisper emotes? */
    	    dest = irc_convert_channel(conn_get_channel(me)); /* FIXME: support more channels and choose right one! */
	    msg = irc_message_preformat(&from,"PRIVMSG",dest,temp);
    	    conn_unget_chatname(me,from.nick);
    	}
        break;
    case message_type_broadcast:
    case message_type_info:
    case message_type_error:
	{
	    char temp[MAX_IRC_MESSAGE_LEN];
	    sprintf(temp,":%s",text);
	    msg = irc_message_preformat(NULL,"NOTICE",NULL,temp);
	}
	break;
    case message_type_channel:
    	/* ignore it */
	break;
    case message_type_mode:
	from.nick = conn_get_chatname(me);
	from.user = ctag;
	from.host = addr_num_to_ip_str(conn_get_addr(me));
	msg = irc_message_preformat(&from,"MODE","\r",text);
	conn_unget_chatname(me,from.nick);
	break;
   	/**
   	*  Westwood Online Extensions
   	*/
    case message_wol_joingame:
    	from.nick = conn_get_chatname(me);
    	from.user = ctag;
    	from.host = addr_num_to_ip_str(conn_get_addr(me));
    	msg = irc_message_preformat(&from,"JOINGAME",text,"\r");
    	conn_unget_chatname(me,from.nick);
    	break;
    case message_wol_gameopt_owner:
    	from.nick = conn_get_chatname(me);
    	from.user = ctag;
    	from.host = addr_num_to_ip_str(conn_get_addr(me));
    	msg = irc_message_preformat(&from,"GAMEOPT",irc_convert_channel(conn_get_channel(me)),text);
    	conn_unget_chatname(me,from.nick);
    	break;
    case message_wol_gameopt_join:
    	from.nick = conn_get_chatname(me);
    	from.user = ctag;
    	from.host = addr_num_to_ip_str(conn_get_addr(me));
    	msg = irc_message_preformat(&from,"GAMEOPT",channel_wol_get_game_owner(conn_get_channel(me)),text);
    	conn_unget_chatname(me,from.nick);
    	break;
    case message_wol_start_game:
    	from.nick = conn_get_chatname(me);
    	from.user = ctag;
    	from.host = addr_num_to_ip_str(conn_get_addr(me));
    	msg = irc_message_preformat(&from,"STARTG","u",text);
    	conn_unget_chatname(me,from.nick);
    	break;
    case message_wol_page:
    	from.nick = conn_get_chatname(me);
    	from.user = ctag;
    	from.host = addr_num_to_ip_str(conn_get_addr(me));
    	msg = irc_message_preformat(&from,"PAGE","u",text);
    	conn_unget_chatname(me,from.nick);
    	break;
    default:
    	eventlog(eventlog_level_warn,__FUNCTION__,"%d not yet implemented",type);
	return -1;
    }

    if (msg) {
	packet_append_string(packet,msg);
	xfree(msg);
        return 0;
    }
    return -1;
}
Example #8
0
File: irc.c Project: 91D2/pvpgn
extern int irc_welcome(t_connection * conn)
{
    char temp[MAX_IRC_MESSAGE_LEN];
    time_t temptime;
    char const * tempname;
    char const * temptimestr;
    char const * filename;
    FILE *fp;
    char * line, * formatted_line;
    char send_line[MAX_IRC_MESSAGE_LEN];
    char motd_failed = 0;
    
    if (!conn) {
	eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection");
	return -1;
    }

    tempname = conn_get_loggeduser(conn);

    if ((34+strlen(tempname)+1)<=MAX_IRC_MESSAGE_LEN)
        sprintf(temp,":Welcome to the %s IRC Network %s",prefs_get_irc_network_name(), tempname);
    else
        sprintf(temp,":Maximum length exceeded");
    irc_send(conn,RPL_WELCOME,temp);
    
    if ((14+strlen(server_get_hostname())+10+strlen(PVPGN_SOFTWARE" "PVPGN_VERSION)+1)<=MAX_IRC_MESSAGE_LEN)
        sprintf(temp,":Your host is %s, running "PVPGN_SOFTWARE" "PVPGN_VERSION,server_get_hostname());
    else
        sprintf(temp,":Maximum length exceeded");
    irc_send(conn,RPL_YOURHOST,temp);

    temptime = server_get_starttime(); /* FIXME: This should be build time */
    temptimestr = ctime(&temptime);
    if ((25+strlen(temptimestr)+1)<=MAX_IRC_MESSAGE_LEN)
        sprintf(temp,":This server was created %s",temptimestr); /* FIXME: is ctime() portable? */
    else
        sprintf(temp,":Maximum length exceeded");
    irc_send(conn,RPL_CREATED,temp);

    /* we don't give mode information on MYINFO we give it on ISUPPORT */
    if ((strlen(server_get_hostname())+7+strlen(PVPGN_SOFTWARE" "PVPGN_VERSION)+9+1)<=MAX_IRC_MESSAGE_LEN)
        sprintf(temp,"%s "PVPGN_SOFTWARE" "PVPGN_VERSION" - -",server_get_hostname());
    else
        sprintf(temp,":Maximum length exceeded");
    irc_send(conn,RPL_MYINFO,temp);
    
    if((conn_get_wol(conn) == 1))
        sprintf(temp,"NICKLEN=%d TOPICLEN=%d CHANNELLEN=%d PREFIX="CHANNEL_PREFIX" CHANTYPES="CHANNEL_TYPE" NETWORK=%s IRCD="PVPGN_SOFTWARE,
        WOL_NICKNAME_LEN, MAX_TOPIC_LEN, CHANNEL_NAME_LEN, prefs_get_irc_network_name());
    else
        sprintf(temp,"NICKLEN=%d TOPICLEN=%d CHANNELLEN=%d PREFIX="CHANNEL_PREFIX" CHANTYPES="CHANNEL_TYPE" NETWORK=%s IRCD="PVPGN_SOFTWARE,
        CHAR_NAME_LEN, MAX_TOPIC_LEN, CHANNEL_NAME_LEN, prefs_get_irc_network_name());
    
    if((strlen(temp))<=MAX_IRC_MESSAGE_LEN)
        irc_send(conn,RPL_ISUPPORT,temp);
    else {
        sprintf(temp,":Maximum length exceeded");
        irc_send(conn,RPL_ISUPPORT,temp);
    }    

    if ((3+strlen(server_get_hostname())+22+1)<=MAX_IRC_MESSAGE_LEN)
    	sprintf(temp,":- %s, "PVPGN_SOFTWARE" "PVPGN_VERSION", built on %s",server_get_hostname(),temptimestr);
    else
        sprintf(temp,":Maximum length exceeded"); 
    irc_send(conn,RPL_MOTDSTART,temp);

    if ((filename = prefs_get_motdfile())) {
	 if ((fp = fopen(filename,"r"))) {
	  while ((line=file_get_line(fp))) {
		if ((formatted_line = message_format_line(conn,line))) {
		  formatted_line[0]=' ';
		  sprintf(send_line,":-%s",formatted_line);
		  irc_send(conn,RPL_MOTD,send_line);
		  xfree(formatted_line);
		}
	  }

	  file_get_line(NULL); // clear file_get_line buffer
	  fclose(fp);
	}
	 else 
	 	motd_failed = 1;
   }
   else
     motd_failed = 1;
   
    if (motd_failed) {
      irc_send(conn,RPL_MOTD,":- Failed to load motd, sending default motd              ");
      irc_send(conn,RPL_MOTD,":- ====================================================== ");
      irc_send(conn,RPL_MOTD,":-                 http://www.pvpgn.org                   ");
      irc_send(conn,RPL_MOTD,":- ====================================================== ");
    }
    irc_send(conn,RPL_ENDOFMOTD,":End of /MOTD command");
    irc_send_cmd(conn,"NOTICE",":This is an experimental service.");
    
    conn_set_state(conn,conn_state_bot_password);
    if (connlist_find_connection_by_accountname(conn_get_loggeduser(conn))) {
    	irc_send_cmd(conn,"NOTICE","This account is already logged in, use another account.");
	return -1;
    }
    
    if (conn_get_ircpass(conn)) {
	irc_send_cmd(conn,"NOTICE",":Trying to authenticate with PASS ...");
	irc_authenticate(conn,conn_get_ircpass(conn));
    } else {
    	irc_send_cmd(conn,"NOTICE",":No PASS command received. Please identify yourself by /msg NICKSERV identify <password>.");
    }
    return 0;
}