Chinaunix首页 | 论坛 | 博客
  • 博客访问: 523037
  • 博文数量: 118
  • 博客积分: 10028
  • 博客等级: 上将
  • 技术积分: 1820
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-07 18:46
文章分类

全部博文(118)

文章存档

2009年(12)

2008年(106)

我的朋友

分类: C/C++

2008-10-11 18:13:01

 
代码没啥大的改变,但是文件结构改变了,以前是采用头文件包含的形式,这次采用多个C文件方式,其间遇到了一些问题,不过都解决了,另外把注释都改成了英文注释~

YOURID是你的邮箱登陆名,YOURPASSWORD当然就是密码咯~ 我用的163邮箱,126和yeah.net应该是一样的(都是网易邮箱),新浪的用这会有问题,因为我是在输入密码后pop服务器返回里提取的邮件数,而新浪邮箱在这步只返回个OK,我没改,yahoo邮箱的pop服务器直接就不能免费用~ 这算是一个局限吧~


 原版在这:http://blog.chinaunix.net/u1/53217/showart_460688.html

改了后的文件有:chkmail.c chkmail.h comp.c alert.c makefile

直接贴代码算了~(在文章最后可下载所有代码压缩包)

chkmail.c

/* ********************************************************************
 * chkmail.c *
 * Functions: *
 * Connect to the pop server and get the mail number *
 * Open the specified file FILEPATH and obtain the old number *
 * Call comp(),to compare the two numbers *
 * Author:zuii *
 **********************************************************************/


#include "chkmail.h"

int
main(int argc,char **argv)
{
    int sockfd;
    int i,n_size;
    time_t tt;
    struct hostent *host=NULL;
    struct sockaddr_in serv_addr;
    char n_num[MAXSIZE],rubbish[MAXSIZE];
    FILE *fp;
    int f_size;
    struct stat f_stat;
    char o_num[10];
    
    char *POPMsg[]={
        "USER YOURID\r\n",
        "PASS YOURPASSWORD\r\n",
        "QUIT\r\n",
        NULL
        };

    while((host=gethostbyname("pop.163.com"))==NULL)
    {
        time(&tt);
        printf("%s",ctime(&tt));
        perror("gethostbyname error");
        sleep(10);
    }
    while(1)
    {
        /* Connect to the pop server */
        while((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
            time(&tt);
            printf("%s",ctime(&tt));
            perror("socket error");
            sleep(10);
        }
        serv_addr.sin_family=AF_INET;
        serv_addr.sin_port=htons(POP3SERVPORT);
        serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
        bzero(&(serv_addr.sin_zero),8);
        while (connect(sockfd, (struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1)
        {
            time(&tt);
            printf("%s",ctime(&tt));
            perror("connect error");
            sleep(10);
        }

        bzero(rubbish,MAXSIZE);
        recv(sockfd,rubbish,sizeof(rubbish),0);
        if(rubbish[1]!='O')
            continue;

        /* send username to server */
        bzero(rubbish,MAXSIZE);
        send(sockfd,POPMsg[0],strlen(POPMsg[0]),0);
        recv(sockfd,rubbish,sizeof(rubbish),0);
        if(rubbish[1]!='O')
            continue;
        
        /* send password,and get the new number */
        bzero(n_num,MAXSIZE);
        send(sockfd,POPMsg[1],strlen(POPMsg[1]),0);
        recv(sockfd,n_num,sizeof(n_num),0);

        for(i=0;n_num[i+4]!=' ';i++)
        {
            n_num[i]=n_num[i+4];
        }
        n_num[i]='\0';
        send(sockfd,POPMsg[2],strlen(POPMsg[2]),0);
        close(sockfd);
        n_size=strlen(n_num);

        /* open the file which contains the old mail number */
        if(access(FILEPATH,F_OK)==-1)
        {
            perror("Access error");
            exit(1);
        }
        else
        {
            if(stat(FILEPATH,&f_stat)==-1)
            {
                perror("Stat Error");
                exit(1);
            }
            f_size=f_stat.st_size;
            if((fp=fopen(FILEPATH,"r"))==NULL)
            {
                perror("Read error !\n");
                exit(1);
            }
            fgets(o_num,f_size+1,fp); /* read the old mail number */
            fclose(fp);
        }
        
        /* call comp() to compare the two numbers */
        comp(o_num,n_num);
        sleep(30);
    }
    return 0;
}



chkmail.h


/***********************************
 * chkmail.h *
 * the headers of this program *
 * and the functions *
 ***********************************/


#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <time.h>
#include <wait.h>

#define FILEPATH "/home/zuii/.mailnumber.txt"
#define POP3SERVPORT 110
#define MAXSIZE 4096

int argc;
char **argv;

void writenum(char n_num[]);
void handle();
void alert();
void comp(char o_num[],char n_num[]);


comp.c

/************************************************************
 * Compare the new number with old number *
 * Call writenum() to write the new number to the file *
 * And call alert() to notify the user when new mail coming *
 ************************************************************/


#include "chkmail.h"

/* call alert() */
void handle()
{
    int pid;
    int status;
    /* create a new process */
    pid=fork();
    if(pid==-1)
        exit(1);
    else if(pid==0)
        alert();
    wait(&status);
}

/* compare */
void comp(char o_num[],char n_num[])
{

    if(strlen(o_num)==strlen(n_num))
    { /* the digit of two numbers are the same */
        if(strcmp(o_num,n_num)>=0)
        { /* no new mail */
            writenum(n_num);
        }
        else
        { /* a new mail */
            writenum(n_num);
            handle();
        }    
    }
    else if(strlen(o_num)>strlen(n_num))
    { /* the digit of oldnumber is more than the newnumber */
        writenum(n_num);
    }    
    else
    { /* the digit of oldnumber is less than the newnumber */
        writenum(n_num);
        handle();
    }
}

/* write the new number to the file */
void writenum(char n_num[])
{
    FILE *w_fp;
    
    if((w_fp=fopen(FILEPATH,"w"))==NULL)
    {
        perror("Open file error");
        exit(-1);
    }
    fprintf(w_fp,"%s",n_num);
    fclose(w_fp);
}


alert.c

/***************************************
 * alert.c *
 * To notify when be called by comp() !*
 ***************************************/

#include "chkmail.h"
#include <gtk/gtk.h>

/* Close the window,kill the subprocess */
void closewin(GtkWidget *window,gpointer data)
{
    exit(0);
}

/* Create a window ,show that there is a new mail */
void alert()
{
    GtkWidget *window;
    GtkWidget *label;
    GtkWidget *button;
    GtkWidget *vbox;
    
    gtk_init(&argc,&argv);
    window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    
    gtk_window_set_title(GTK_WINDOW(window),"New Mail Coming");
    gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window),120,50);
    
    g_signal_connect(GTK_OBJECT(window),"destroy",GTK_SIGNAL_FUNC(closewin),NULL);
    
    label=gtk_label_new("有新邮件!");
    
    button=gtk_button_new_with_label("确定");
    
    g_signal_connect(GTK_OBJECT(button),"clicked",GTK_SIGNAL_FUNC(closewin),NULL);
    
    vbox=gtk_vbox_new(TRUE,5);
    
    gtk_box_pack_start(GTK_BOX(vbox),label,TRUE,TRUE,5);
    gtk_box_pack_start(GTK_BOX(vbox),button,FALSE,FALSE,5);
    
    gtk_container_add(GTK_CONTAINER(window),vbox);
    gtk_widget_show_all(window);
    gtk_main();
}


makefile


OBJ=chkmail.o comp.o alert.o

chkmail:$(OBJ) chkmail.h
    gcc -Wall $(OBJ) -o chkmail `pkg-config gtk+-2.0 --libs --cflags`
chkmail.o:chkmail.c chkmail.h
    gcc -Wall -c $<
comp.o:comp.c chkmail.h
    gcc -Wall -c $<
alert.o:alert.c chkmail.h
    gcc -Wall -c $< `pkg-config gtk+-2.0 --cflags`

clean:
    rm *.o


文件:chkmail.rar
大小:3KB
下载:下载

阅读(1219) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~