慢慢地喜欢了Linux下无声的Linux版QQ,其实,我也不要求他那么多,至少,我还得看到是不是有新邮件了,可是它没有。没办法,自己做个邮件监控吧!
#include "unistd.h"
#include
#include "sys/socket.h"
#include "netinet/in.h"
#include "netdb.h"
#include "arpa/inet.h"
#include "stdlib.h"
#include "stdio.h"
#include
char user[]="QQ号码\r\n";
char passwd[]="QQ密码\r\n";
char serv[]="pop.qq.com";
char id_file_name[]="/usr/local/bin/mail.temp";
void err_quit(const char *str)
{
printf("%s\n",str);
exit(1);
}
void sendcmd(int fd,char *cmd)
{
if(write(fd,cmd,strlen(cmd))<0)
{
perror("send");
exit(1);
}
}
int isok(char *res)
{
if(res[0]=='+' && res[1]=='O' && res[2]=='K') return 1;
return 0;
}
int getresp(int fd,char *result)
{
bzero(result,4098);
int num=0;
num=recv(fd,result,4098,0);
if(num<0)
{
perror("read");
exit(1);
}
return num;
}
int main()
{
int fd,id_file;
char cmd[255];
char result[4098];
char sn[10];
char *start=result;
int cur;
int nn;
int num;
sockaddr_in sa;
struct hostent *host;
bzero(&sa,sizeof(sockaddr));
if((host=gethostbyname(serv))!=NULL)
{
bcopy(host->h_addr,&sa.sin_addr,host->h_length);
}else
{
perror("gethostbyname");
exit(1);
}
sa.sin_family=AF_INET;
sa.sin_port=htons(110);
fd=socket(AF_INET,SOCK_STREAM,0);
if(fd<0)
{
perror("socket");
exit(1);
}
if(connect(fd,(sockaddr*)&sa,sizeof(sockaddr))<0)
{
perror("connect");
exit(1);
}
//while(1){
strcpy(cmd,"USER ");
strcat(cmd,user);
sendcmd(fd,cmd);
getresp(fd,result);
if(!isok(result))
err_quit("user name");
strcpy(cmd,"PASS ");
strcat(cmd,passwd);
sendcmd(fd,cmd);
getresp(fd,result);
if(!isok(result))
err_quit("PASS");
strcpy(cmd,"UIDL\r\n");
sendcmd(fd,cmd);
if((id_file=open(id_file_name,O_RDWR))<0)
{
perror("open file");
exit(1);
}
read(id_file,sn,10);
nn=atoi(sn);
cur=0;
int out=0;
while(!out)
{
getresp(fd,result);
for(int i=0;i {
if(result[i]=='\n')
cur++;
else if(result[i]=='.')
{
out=1;
break;
}
}
}
cur-=2;
if(nn {
printf("你有%d封未读邮件\n",cur-nn);
if(lseek(id_file,0,SEEK_SET)<0)
exit(1);
sprintf(sn,"%d",cur);
if(write(id_file,sn,strlen(sn))<0)
exit(1);
}else
printf("你没有新邮件\n");
close(id_file);
strcpy(cmd,"QUIT\r\n");
sendcmd(fd,cmd);
getresp(fd,result);
// sleep(60);
//}
close(fd);
return 0;
}
编译:
g++ mailalert.cpp -o mailalert
将mailalert拷贝到/usr/local/bin
并在/usr/local/bin下创建一个害一个0的文件mail.temp
修改.bashrc,在最后增加:
mailalert
这样每次打开虚拟终端的时候,都会提示你是不是QQ邮箱有新邮件了
阅读(1263) | 评论(0) | 转发(0) |