|
|

楼主 |
发表于 2004-9-21 16:34:35
|
显示全部楼层
这里是主要的过滤代码,,
大家帮我查一下。。
[php]const gchar *QQ_SPT_IMG_EXT[] = {".BMP", ".GIF", ".JPEG", ".JPG", NULL};
#define QQ_USERDEFINED_SMILEY_FLAGS "\025"
#define MSG_FILTER_PROM "[MSG FILTERED]"
#define MSG_FILTER_PROM_SIZE (sizeof(MSG_FILTER_PROM) - 1) //donnot include the terminal char
#define QQ_SMILEY_SHOTCUT_MAX 6
gchar *qq_im_filter_userdefined_smiley(const gchar *text, gboolean maintain_shotcut) {
GString *converted;
gchar *begin, *end, *cnt, **smiley_ex_flags, *result;
static gchar shotcuts[26][QQ_SMILEY_SHOTCUT_MAX + 1]; //define as static to speedup
gint last_shotcut = -1;
if (!g_strstr_len(text, -1, QQ_USERDEFINED_SMILEY_FLAGS))
return NULL;
converted = g_string_new(text);
if (!converted)
return NULL;
begin = cnt = converted->str;
while (cnt && *cnt) {
cnt = g_strstr_len(cnt, -1, QQ_USERDEFINED_SMILEY_FLAGS);
if (!cnt || !(*(cnt +1))) {
break;
}
if (!isdigit(cnt[1])) {
cnt ++;
continue;
}
end = NULL;
if ( cnt[1] == '3') {
gchar *end1;
//find the best match
for (smiley_ex_flags = (gchar **)QQ_SPT_IMG_EXT; smiley_ex_flags != NULL, *smiley_ex_flags != '\0'; smiley_ex_flags++) {
end1 = g_strstr_len(cnt, -1, *smiley_ex_flags);
if ( !end && end1)
end = end1 + strlen(*smiley_ex_flags);
else if (end1 && end1 + strlen(*smiley_ex_flags) < end) {
end = end1 + strlen(*smiley_ex_flags);
}
}
if (end) {
if (*end ) {
// extension name of smiley --> ____
//\x1532FE05FF7FDBE0CF68CB2A108F7B8D5B48.GIFDabc
// shotcut length('D'-'A' = 3)-> ^--- <-shotcut of smiley
if (maintain_shotcut == FALSE && (*end >= 'A') && (end[0] - 'A' <= QQ_SMILEY_SHOTCUT_MAX)
&& (end + (end[0] - 'A') <= begin + converted->len)) {
if((!end[end[0] - 'A'] && end + end[0] - 'A' - cnt - 1 >= MSG_FILTER_PROM_SIZE) ||
( end[end[0] - 'A'] && end + end[0] - 'A' - cnt >= MSG_FILTER_PROM_SIZE)) {
strncpy(cnt, MSG_FILTER_PROM, MSG_FILTER_PROM_SIZE);//tell user we filter some msg.
cnt += MSG_FILTER_PROM_SIZE;
}
//store the shotcut
if ( last_shotcut < 25) {
// strncpy(shotcuts[++last_shotcut], end + 1 , end[0] - 'A');
// shotcuts[last_shotcut][end[0] - 'A'] = '\0';
++last_shotcut;
}
end += (end[0] - 'A') + 1;
}
else { //maintain the shotcut
//store the shotcut
if ( last_shotcut < 25 && (*end >= 'A') && end[0] - 'A' <= QQ_SMILEY_SHOTCUT_MAX) {
strncpy(shotcuts[++last_shotcut], end + 1 , end[0] - 'A');
shotcuts[last_shotcut][end[0] - 'A'] = '\0';
}
end ++;
}
}
if (end > begin + converted->len) //the end must be in the bound of `converted' string
end = begin + converted->len;
g_string_erase(converted, (cnt - begin), (end - cnt));
if (maintain_shotcut == FALSE)
continue;
}
else {
//\x153...
// ^-> ^
//skip...
cnt += 2;
continue;
}
}
else if (cnt[1] == '4' ) {
if (maintain_shotcut == FALSE) {
if (cnt[2] >= 'A' && cnt[2] <= 'Z' && cnt[2] - 'A' <= last_shotcut) {//\x154[A-Z]
g_string_erase(converted, (cnt - begin), 3); //just erase 3 chars
continue;
}
}
else {
if (cnt[2] >= 'A' && cnt[2] <= 'Z' && last_shotcut >= 0) { //\x154[A-Z]
gint shotcut_len = strlen(shotcuts[cnt[2] - 'A']);
if (shotcut_len > 0 && shotcut_len <= 3 && cnt[2] - 'A' <= last_shotcut) {
strncpy(cnt, shotcuts[cnt[2] - 'A'], shotcut_len);
cnt += shotcut_len;
if (shotcut_len < 3) {
g_string_erase(converted, (cnt - begin), 3 - shotcut_len);
}
continue;
}
else {
g_string_erase(converted, (cnt - begin), 3);
}
}
}
}
cnt ++;
}
result = converted->str;
g_string_free(converted, FALSE);
if (last_shotcut >= 0 && maintain_shotcut == TRUE) //clear the buffer
memset(&shotcuts[0][0], sizeof(shotcuts), 0);
return result;
}//qq_im_filter_userdefined_smiley
[/php] |
|