📄 message.c
字号:
bn_int_set(&packet->u.server_message.flags,0); else bn_int_set(&packet->u.server_message.flags,cflags_to_bncflags(channel_get_flags(channel))); } bn_int_set(&packet->u.server_message.latency,conn_get_latency(me)); { char const * tname; tname = conn_get_chatname(me); packet_append_string(packet,tname); conn_unget_chatname(me,tname); packet_append_string(packet,text); } break; case message_type_userflags: if (!me) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection for %s",message_type_get_str(type)); return -1; } bn_int_set(&packet->u.server_message.type,SERVER_MESSAGE_TYPE_USERFLAGS); bn_int_set(&packet->u.server_message.flags,conn_get_flags(me)|dstflags); bn_int_set(&packet->u.server_message.latency,conn_get_latency(me)); { char const * tname; char const * playerinfo; tname = conn_get_chatcharname(me, dst); packet_append_string(packet,tname); conn_unget_chatcharname(me,tname); if ((conn_get_clienttag(me) == CLIENTTAG_WARCRAFT3_UINT) || (conn_get_clienttag(me) == CLIENTTAG_WAR3XP_UINT)) playerinfo = conn_get_w3_playerinfo(me); else playerinfo = conn_get_playerinfo(me); if (playerinfo == NULL) { playerinfo = ""; } packet_append_string(packet, playerinfo); } break; case message_type_whisperack: if (!me) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection for %s",message_type_get_str(type)); return -1; } if (!text) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL text for %s",message_type_get_str(type)); return -1; } bn_int_set(&packet->u.server_message.type,SERVER_MESSAGE_TYPE_WHISPERACK); bn_int_set(&packet->u.server_message.flags,conn_get_flags(me)|dstflags); bn_int_set(&packet->u.server_message.latency,conn_get_latency(me)); { char const * tname; tname = conn_get_chatcharname(me, dst); packet_append_string(packet,tname); conn_unget_chatcharname(me,tname); packet_append_string(packet,text); } break; case message_type_friendwhisperack: // [zap-zero] 20020518 if (!me) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection for %s",message_type_get_str(type)); return -1; } if (!text) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL text for %s",message_type_get_str(type)); return -1; } bn_int_set(&packet->u.server_message.type,SERVER_MESSAGE_TYPE_WHISPERACK); bn_int_set(&packet->u.server_message.flags,conn_get_flags(me)|dstflags); bn_int_set(&packet->u.server_message.latency,conn_get_latency(me)); { packet_append_string(packet,"your friends"); packet_append_string(packet,text); } break; case message_type_channelfull: /* FIXME */ bn_int_set(&packet->u.server_message.type,SERVER_MESSAGE_TYPE_CHANNELFULL); bn_int_set(&packet->u.server_message.flags,0); bn_int_set(&packet->u.server_message.latency,0); packet_append_string(packet,""); packet_append_string(packet,""); break; case message_type_channeldoesnotexist: if (!me) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection for %s",message_type_get_str(type)); return -1; } if (!text) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL text for %s",message_type_get_str(type)); return -1; } bn_int_set(&packet->u.server_message.type,SERVER_MESSAGE_TYPE_CHANNELDOESNOTEXIST); bn_int_set(&packet->u.server_message.flags,conn_get_flags(me)|dstflags); bn_int_set(&packet->u.server_message.latency,conn_get_latency(me)); { char const * tname; tname = conn_get_chatname(me); packet_append_string(packet,tname); conn_unget_chatname(me,tname); packet_append_string(packet,text); } break; case message_type_channelrestricted: /* FIXME */ bn_int_set(&packet->u.server_message.type,SERVER_MESSAGE_TYPE_CHANNELRESTRICTED); bn_int_set(&packet->u.server_message.flags,0); bn_int_set(&packet->u.server_message.latency,0); packet_append_string(packet,""); packet_append_string(packet,""); break; case message_type_info: if (!text) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL text for %s",message_type_get_str(type)); return -1; } bn_int_set(&packet->u.server_message.type,SERVER_MESSAGE_TYPE_INFO); bn_int_set(&packet->u.server_message.flags,0); bn_int_set(&packet->u.server_message.latency,0); packet_append_string(packet,""); packet_append_string(packet,text); break; case message_type_error: if (!text) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL text for %s",message_type_get_str(type)); return -1; } bn_int_set(&packet->u.server_message.type,SERVER_MESSAGE_TYPE_ERROR); bn_int_set(&packet->u.server_message.flags,0); bn_int_set(&packet->u.server_message.latency,0); packet_append_string(packet,""); packet_append_string(packet,text); break; case message_type_emote: if (!me) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection for %s",message_type_get_str(type)); return -1; } if (!text) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL text for %s",message_type_get_str(type)); return -1; } if (dstflags&MF_X) return -1; /* player is ignored */ bn_int_set(&packet->u.server_message.type,SERVER_MESSAGE_TYPE_EMOTE); bn_int_set(&packet->u.server_message.flags,conn_get_flags(me)|dstflags); bn_int_set(&packet->u.server_message.latency,conn_get_latency(me)); { char const * tname; tname = conn_get_chatcharname(me, dst); packet_append_string(packet,tname); conn_unget_chatcharname(me,tname); packet_append_string(packet,text); } break; default: eventlog(eventlog_level_error,__FUNCTION__,"got bad message type %d",(int)type); return -1; } return 0;} extern t_message * message_create(t_message_type type, t_connection * src, t_connection * dst, char const * text){ t_message * message; message = xmalloc(sizeof(t_message)); message->num_cached = 0; message->packets = NULL; message->classes = NULL; message->dstflags = NULL; message->mclasses = NULL; message->type = type; message->src = src; message->dst = dst; message->text = text; return message;}extern int message_destroy(t_message * message){ unsigned int i; if (!message) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL message"); return -1; } for (i=0; i<message->num_cached; i++) if (message->packets[i]) packet_del_ref(message->packets[i]); if (message->packets) xfree(message->packets); if (message->classes) xfree(message->classes); if (message->dstflags) xfree(message->dstflags); if (message->mclasses) xfree(message->mclasses); xfree(message); return 0;}static t_packet * message_cache_lookup(t_message * message, t_connection *dst, unsigned int dstflags){ unsigned int i; t_packet * packet; t_message_class mclass; t_conn_class class; if (!message) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL message"); return NULL; } class = conn_get_class(dst); mclass = conn_get_message_class(message->src, dst); for (i=0; i<message->num_cached; i++) if (message->classes[i]==class && message->dstflags[i]==dstflags && message->mclasses[i]==mclass) return message->packets[i]; { t_packet * * temp_packets; t_conn_class * temp_classes; unsigned int * temp_dstflags; t_message_class *temp_mclasses; if (!message->packets) temp_packets = xmalloc(sizeof(t_packet *)*(message->num_cached+1)); else temp_packets = xrealloc(message->packets,sizeof(t_packet *)*(message->num_cached+1)); if (!message->classes) temp_classes = xmalloc(sizeof(t_conn_class)*(message->num_cached+1)); else temp_classes = xrealloc(message->classes,sizeof(t_conn_class)*(message->num_cached+1)); if (!message->dstflags) temp_dstflags = xmalloc(sizeof(unsigned int)*(message->num_cached+1)); else temp_dstflags = xrealloc(message->dstflags,sizeof(unsigned int)*(message->num_cached+1)); if (!message->mclasses) temp_mclasses = xmalloc(sizeof(t_message_class)*(message->num_cached+1)); else temp_mclasses = xrealloc(message->mclasses,sizeof(t_message_class)*(message->num_cached+1)); message->packets = temp_packets; message->classes = temp_classes; message->dstflags = temp_dstflags; message->mclasses = temp_mclasses; } switch (class) { case conn_class_telnet: if (!(packet = packet_create(packet_class_raw))) { eventlog(eventlog_level_error,__FUNCTION__,"could not create packet"); return NULL; } if (message_telnet_format(packet,message->type,message->src,message->dst,message->text,dstflags)<0) { packet_del_ref(packet); packet = NULL; /* we can cache the NULL too */ } break; case conn_class_bot: if (!(packet = packet_create(packet_class_raw))) { eventlog(eventlog_level_error,__FUNCTION__,"could not create packet"); return NULL; } if (message_bot_format(packet,message->type,message->src,message->dst,message->text,dstflags)<0) { packet_del_ref(packet); packet = NULL; /* we can cache the NULL too */ } break; case conn_class_bnet: if (!(packet = packet_create(packet_class_bnet))) { eventlog(eventlog_level_error,__FUNCTION__,"could not create packet"); return NULL; } if (message_bnet_format(packet,message->type,message->src,dst,message->text,dstflags)<0) { packet_del_ref(packet); packet = NULL; /* we can cache the NULL too */ } break; case conn_class_irc: case conn_class_wol: if (!(packet = packet_create(packet_class_raw))) { eventlog(eventlog_level_error,__FUNCTION__,"could not create packet"); return NULL; } /* irc_message_format() is in irc.c */ if (irc_message_format(packet,message->type,message->src,message->dst,message->text,dstflags)<0) { packet_del_ref(packet); packet = NULL; /* we can cache the NULL too */ } break; case conn_class_init: case conn_class_file: case conn_class_d2cs_bnetd: case conn_class_w3route: packet = NULL; break; /* cache the NULL but dont send any error, * this are normal connections */ default: eventlog(eventlog_level_error,__FUNCTION__,"unsupported connection class %d",(int)class); packet = NULL; /* we can cache the NULL too */ } message->num_cached++; message->packets[i] = packet; message->classes[i] = class; message->dstflags[i] = dstflags; message->mclasses[i] = mclass; return packet;}extern int message_send(t_message * message, t_connection * dst){ t_packet * packet; unsigned int dstflags; if (!message) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL message"); return -1; } if (!dst) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL dst connection"); return -1; } dstflags = 0; if (message->src) { char const * tname; if ((tname = conn_get_chatname(message->src)) && conn_check_ignoring(dst,tname)==1) { conn_unget_chatname(message->src,tname); dstflags |= MF_X; } if (tname) conn_unget_chatname(message->src,tname); } if (!(packet = message_cache_lookup(message,dst,dstflags))) return -1; /* FIXME: this is not needed now, message has dst */ if ((conn_get_class(dst)==conn_class_irc)||(conn_get_class(dst)==conn_class_wol)) { /* HACK: IRC message always need the recipient and are therefore bad to cache. */ /* So we only cache a pseudo packet and convert it to a real packet later ... */ packet = packet_duplicate(packet); /* we want to modify packet so we have to create a copy ... */ if (irc_message_postformat(packet,dst)<0) { packet_del_ref(packet); /* we don't need the previously created copy anymore ... */ return -1; } } conn_push_outqueue(dst,packet); if ((conn_get_class(dst)==conn_class_irc)||(conn_get_class(dst)==conn_class_wol)) packet_del_ref(packet); /* we don't need the previously created copy anymore ... */ return 0;}extern int message_send_all(t_message * message){ t_connection * c; t_elem const * curr; int rez; if (!message) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL message"); return -1; } rez = -1; LIST_TRAVERSE_CONST(connlist(),curr) { c = elem_get_data(curr); if (message_send(message,c)==0) rez = 0; } return rez;}extern int message_send_text(t_connection * dst, t_message_type type, t_connection * src, char const * text){ t_message * message; int rez; if (!dst) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection"); return -1; } if (!(message = message_create(type,src,dst,text))) return -1; rez = message_send(message,dst); message_destroy(message); return rez;}extern int message_send_admins(t_connection * src, t_message_type type, char const * text){ t_elem const * curr; t_connection * tc; int counter = 0; LIST_TRAVERSE_CONST(connlist(),curr) { tc = elem_get_data(curr); if (!tc) continue; if (account_get_auth_admin(conn_get_account(tc),NULL)==1 && tc != src) { message_send_text(tc,type,src,text); counter++; } } return counter;}extern int message_send_formatted(t_connection * dst, char const * text){ char * line; if (!dst) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection"); return -1; } if (!(line = message_format_line(dst,text))) { eventlog(eventlog_level_error,__FUNCTION__,"could not format input text \"%s\"",text); return -1; } /* caller beware: empty messages can crash Blizzard clients */ switch (line[0]) { case 'C': if (line[1]=='/') handle_command(dst,&line[1]); else if (conn_get_channel(dst) && !conn_quota_exceeded(dst,&line[1])) channel_message_send(conn_get_channel(dst),message_type_talk,dst,&line[1]); break; case 'B': message_send_text(dst,message_type_broadcast,dst,&line[1]); break; case 'E': message_send_text(dst,message_type_error,dst,&line[1]); break; case 'M': message_send_text(dst,message_type_talk,dst,&line[1]); break; case 'T': message_send_text(dst,message_type_emote,dst,&line[1]); break; case 'I': case 'W': message_send_text(dst,message_type_info,dst,&line[1]); break; default: eventlog(eventlog_level_error,__FUNCTION__,"unknown message type '%c'",line[0]); xfree(line); return -1; } xfree(line); return 0;}extern int message_send_file(t_connection * dst, FILE * fd){ char * buff; if (!dst) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection"); return -1; } if (!fd) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL fd"); return -1; } while ((buff = file_get_line(fd))) { message_send_formatted(dst,buff); } file_get_line(NULL); // clear file_get_line buffer return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -