/*
* 原始版本来至南大zhch
*
* 增加功能:从Robot版取文章作为语言规则.:)
* by ZV 2002.4.21
*
* TODO: 正加正则表达式。 现已找到 rx-1.5.tar.gz,正在调试中....
**/
#include "/root/bbssrc/include/bbs.h" // bbs.h目录
#include "netinet/in.h"
#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#define DEBUG
#define MAXWORDS 1000
#define MAXWORDLEN 100
#define MAXREPLY 100
#define BBSROBOTBOARD "bbsRobot" // 机器人语言规则版
#define TOBBSHOST "202.115.16.2" // bbs ip
#define ROBOTNAME "ROBOT" // 机器人帐号
#define ROBOTPWD "faint123" // 机器人密码
char status[100]="login";
char words[MAXWORDS][2][MAXWORDLEN];
int words_num = 0;
char *noansi(char *s) {
static char buf[10000];
unsigned char c;
int len=0, ansiflag=0;
bzero(buf, 10000);
while(c=s[0]) {
s++;
if(ansiflag==0) {
if(c==27) {
ansiflag=1;
continue;
}
buf[len]=toupper(c);
len++;
} else
if(strchr("[0123456789;", c)==0) {
if(c=='H') {
buf[len]='\n';
len++;
}
ansiflag=0;
}
}
return buf;
}
int main() {
char buf[10000], *s;
int fd, num, i;
struct sockaddr_in blah;
FILE *fp;
bzero((char *)&blah, sizeof(blah));
blah.sin_family=AF_INET;
blah.sin_addr.s_addr=inet_addr(TOBBSHOST);
blah.sin_port=htons(23);
fd=socket(AF_INET, SOCK_STREAM, 0);
if(connect(fd, (struct sockaddr *)&blah, 16)<0) return -1;
close(0);
dup2(fd, 0);
while(1) {
bzero(buf, 10000);
if(read(0, buf, 9000)<=0) break;
s=strtok(buf, "\r\n");
while(s!=0) {
deal(noansi(s));
s=strtok(NULL, "\r\n");
}
}
}
int robot_read_reply_msg()
{
struct fileheader x;
FILE *fp;
bzero(&x, sizeof(x));
if ((fp = fopen(BBSHOME"/boards/"BBSROBOTBOARD"/.DIR", "r")) == NULL) {
printf(BBSHOME"/boards/"BBSROBOTBOARD"/.DIR 不存在!");
exit(-1);
}
words_num = 0;
while (fread(&x, sizeof(x), 1, fp) >0) {
int i;
if (!strstr(x.title, "|") || !(x.accessed[0] & FILE_MARKED)) continue;
for (i=0; x.title[i]!='|'; i++)
words[words_num][0][i] = toupper(x.title[i]);
words[words_num][0][i] = 0;
if ((i == 0) || (x.title[i+1] == 0)) continue;
strcpy(words[words_num][1], &x.title[i+1]);
#ifdef DEBUG
printf("[%2d] accessed[%d][%s] [%s]\n",words_num, x.accessed[0] & FILE_MAR
KED, words[words_num][0], words[words_num][1]);
#endif
words_num++;
}
fclose(fp);
}
int deal(char *buf) {
char iacreply[]={255,255,0};
usleep(20000);
// printf("%s\r\n", buf);
fflush(stdout);
if(strchr(buf, 255))
return write(0, iacreply, 2);
if(strstr(buf, " (^Z回)")) return domsg(buf);
if(!strcmp(status, "login"))
return dologin(buf);
if(!strcmp(status, "mainmenu"))
return domenu(buf);
if(strstr(buf, "请输入聊天代号:")) {
write(0, "\n", 1);
sleep(1);
return 1;
}
}
int domsg(char *frombuf) {
int rnd;
int i, reply_num;
char buf[100];
char reply[MAXREPLY][MAXWORDLEN];
for(i=0; (i<strlen(frombuf))&&(frombuf[i]!=':'); i++);
for(i++; (i<strlen(frombuf))&&(frombuf[i]!=':'); i++);
strcpy(&buf[0], &frombuf[i+1]);
i = strlen(buf)-6;
buf[i] = 0;
i--;
while ((i>=0) && (buf[i] == ' '))
buf[i--] = 0;
#ifdef DEBUG
printf("domsg[%s]\n", buf);
#endif
robot_read_reply_msg();
reply_num = 0;
for (i = 0; i<words_num; i++) {
if (strstr(buf, words[i][0])) {
strcpy(reply[reply_num], words[i][1]);
reply_num++;
if (reply_num > MAXREPLY)
break;
}
#ifdef DEBUG
if (strstr(buf, words[i][0])) {
printf("REPLY: [%d][%s][%s]\n", reply_num, buf, reply[reply_num-1]);
}
#endif
}
if (reply_num > 0) {
return rmsg(reply[rand() % reply_num]);
} else {
return rmsg(":)");
}
}
int dologin(char *buf) {
if(strstr(buf, "试用请输入")) return r(ROBOTNAME);
if(strstr(buf, "请输入密码")) return r(ROBOTPWD);
if(strstr(buf, "按任何键继续")) return r2("q");
if(strstr(buf, "[好朋友列表]")) return r2("q");
if(strstr(buf, "☆ 上次连线时间为")) return r2("q");
if(strstr(buf, "★ 主选单 ★")) return r("T");
if(strstr(buf, " (2)CHAT 小百合茶馆")) return r("2");
if(strstr(buf, "请输入聊天代号")) return r("");
if(strstr(buf, "您想删除重复的 LOGIN 吗")) return r("");
if(strstr(buf, "这个代号已经有人用了")) {
char buf2[80];
sprintf(buf2, "chzh%d", rand()%10);
return r(buf2);
}
}
int domenu(char *buf) {
return r("T");
strcpy(status, "ok");
}
int rmsg(char *s) {
char buf[256];
sleep(1);
sprintf(buf, "%c%s\n", 26, s);
write(0, buf, strlen(buf));
}
int r(char *s) {
char buf[256];
sleep(1);
sprintf(buf, "%s\n", s);
write(0, buf, strlen(buf));
}
int r2(char *buf) {
sleep(1);
write(0, buf, strlen(buf));
}
--
修改:turbozv FROM 202.115.22.145
FROM 202.115.22.145