📄 irc.c
字号:
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;}/* Channel name conversion rules: *//* Not allowed in IRC (RFC2812): NUL, BELL, CR, LF, ' ', ':' and ','*//* ' ' -> '_' *//* '_' -> '%_' *//* '%' -> '%%' *//* '\b' -> '%b' *//* '\n' -> '%n' *//* '\r' -> '%r' *//* ':' -> '%=' *//* ',' -> '%-' *//* In IRC a channel can be specified by '#'+channelname or '!'+channelid */extern char const * irc_convert_channel(t_channel const * channel){ char const * bname; static char out[CHANNEL_NAME_LEN]; unsigned int outpos; int i; if (!channel) return "*"; memset(out,0,sizeof(out)); out[0] = '#'; outpos = 1; bname = channel_get_name(channel); for (i=0; bname[i]!='\0'; i++) { if (bname[i]==' ') { out[outpos++] = '_'; } else if (bname[i]=='_') { out[outpos++] = '%'; out[outpos++] = '_'; } else if (bname[i]=='%') { out[outpos++] = '%'; out[outpos++] = '%'; } else if (bname[i]=='\b') { out[outpos++] = '%'; out[outpos++] = 'b'; } else if (bname[i]=='\n') { out[outpos++] = '%'; out[outpos++] = 'n'; } else if (bname[i]=='\r') { out[outpos++] = '%'; out[outpos++] = 'r'; } else if (bname[i]==':') { out[outpos++] = '%'; out[outpos++] = '='; } else if (bname[i]==',') { out[outpos++] = '%'; out[outpos++] = '-'; } else { out[outpos++] = bname[i]; } if ((outpos+2)>=(sizeof(out))) { sprintf(out,"!%u",channel_get_channelid(channel)); return out; } } return out;}extern char const * irc_convert_ircname(char const * pircname){ static char out[CHANNEL_NAME_LEN]; unsigned int outpos; int special; int i; char const * ircname = pircname + 1; if (!ircname) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL ircname"); return NULL; } outpos = 0; memset(out,0,sizeof(out)); special = 0; if (pircname[0]=='!') { t_channel * channel; channel = channellist_find_channel_bychannelid(atoi(ircname)); if (channel) return channel_get_name(channel); else return NULL; } else if (pircname[0]!='#') { return NULL; } for (i=0; ircname[i]!='\0'; i++) { if (ircname[i]=='_') { out[outpos++] = ' '; } else if (ircname[i]=='%') { if (special) { out[outpos++] = '%'; special = 0; } else { special = 1; } } else if (special) { if (ircname[i]=='_') { out[outpos++] = '_'; } else if (ircname[i]=='b') { out[outpos++] = '\b'; } else if (ircname[i]=='n') { out[outpos++] = '\n'; } else if (ircname[i]=='r') { out[outpos++] = '\r'; } else if (ircname[i]=='=') { out[outpos++] = ':'; } else if (ircname[i]=='-') { out[outpos++] = ','; } else { /* maybe it's just a typo :) */ out[outpos++] = '%'; out[outpos++] = ircname[i]; } } else { out[outpos++] = ircname[i]; } if ((outpos+2)>=(sizeof(out))) { return NULL; } } return out;}/* splits an string list into its elements *//* (list will be modified) */static char ** irc_split_elems(char * list, int separator, int ignoreblank){ int i; int count; char ** out; if (!list) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL list"); return NULL; } for (count=0,i=0;list[i]!='\0';i++) { if (list[i]==separator) { count++; if (ignoreblank) { /* ignore more than one separators "in a row" */ while ((list[i+1]!='\0')&&(list[i]==separator)) i++; } } } count++; /* count separators -> we have one more element ... */ /* we also need a terminating element */ out = xmalloc((count+1)*sizeof(char *)); out[0] = list; if (count>1) { for (i=1;i<count;i++) { out[i] = strchr(out[i-1],separator); if (!out[i]) { eventlog(eventlog_level_error,__FUNCTION__,"BUG: wrong number of separators"); xfree(out); return NULL; } if (ignoreblank) while ((*out[i]+1)==separator) out[i]++; *out[i]++ = '\0'; } if ((ignoreblank)&&(out[count-1])&&(*out[count-1]=='\0')) { out[count-1] = NULL; /* last element is blank */ } } else if ((ignoreblank)&&(*out[0]=='\0')) { out[0] = NULL; /* now we have 2 terminators ... never mind */ } out[count] = NULL; /* terminating element */ return out;}static int irc_unget_elems(char ** elems){ if (!elems) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL elems"); return -1; } xfree(elems); return 0;}extern char ** irc_get_listelems(char * list){ return irc_split_elems(list,',',0);}extern int irc_unget_listelems(char ** elems){ return irc_unget_elems(elems);}extern char ** irc_get_paramelems(char * list){ return irc_split_elems(list,' ',1);}extern int irc_unget_paramelems(char ** elems){ return irc_unget_elems(elems);}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 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -