⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mail.c

📁 石器时代8.0服务端完整源代码。可以直接编译
💻 C
📖 第 1 页 / 共 2 页
字号:
#define _MAIL_C_#include "main.h"#include "mail.h"#include "util.h"#include "saacproto_serv.h"#include <malloc.h>#include <stdio.h>#include <time.h>#include <sys/types.h>#include <dirent.h>#include <sys/stat.h>typedef enum{    MS_NOUSE = 0,    MS_NEWMESSAGE,    MS_WAIT_ACK,} MAILSTATE;#define TEXT_MAX 1024struct mail{    int use;    unsigned int id_charname_hash;    char id_to[USERID_MAX];    char charname_to[CHARNAME_MAX];    char id_from[USERID_MAX];    char charname_from[CHARNAME_MAX];    char text[TEXT_MAX];    int option;    unsigned int message_id;    MAILSTATE state;    time_t recv_time;};struct mail *mailbuf;int mailbufsize = 0;static unsigned intgetNextMessageID(void){    FILE *fp;    unsigned int i;    char filename[1024];    char line[1000];    snprintf( filename, sizeof( filename ),              "%s/mail_id" , maildir );    fp = fopen( filename, "r" );    if( fp == NULL ){        fp = fopen( filename ,"w" );        if( fp == NULL ){            log( "不能创建 %s ... 使用同样的邮件ID,"                 " saac 发送变得缓慢!(id:9999)\n", filename );            return 9999;        }        fprintf( fp, "10000\n" );        fclose(fp);        return 1000;    }    fgets( line, sizeof(line), fp);    i = strtoul( line, NULL, 10 );    fclose(fp);    fp = fopen( filename, "w" );    if( fp == NULL ){        log( "不能写入新的ID到 %s ... 使用同样的数字!\n", filename );        return i;    }    fprintf( fp, "%u", i+1 );    fclose(fp);    log( "新邮件ID:%u\n", i);    return i;}static int reallocMailBuf( void ){    struct mail *previous = mailbuf;    struct mail *newbuf;    int new_mailbufsize;    if( mailbufsize == 0 ){        new_mailbufsize = 1;    } else {        new_mailbufsize = mailbufsize * 2;    }    newbuf = ( struct mail * )calloc( 1, new_mailbufsize *                                      sizeof( struct mail ));    if( newbuf == NULL ){        log( "回复邮件缓冲: 内件不足!! 新邮件大小:%d\n",             new_mailbufsize );        return -1;    }    memset( newbuf, 0 , new_mailbufsize * sizeof( struct mail ));    if( previous ) memcpy( (char*)newbuf, (char*)previous,                           mailbufsize * sizeof( struct mail ));    free( previous );    mailbufsize = new_mailbufsize;    mailbuf = newbuf;    log( "重新分配邮件缓冲: "         "新邮件缓冲:%d 旧地址:%x 新地址:%x\n",         new_mailbufsize, (unsigned int)previous,(unsigned int)newbuf );    return 0;}static int mailbuf_finder = 0;static int allocMail( int use_msgid, unsigned int msgid  ){    int i;    for(i=0;i<mailbufsize;i++){        mailbuf_finder ++;        if( mailbuf_finder == mailbufsize ) mailbuf_finder = 0;        if( mailbuf[mailbuf_finder].use == 0 ){            mailbuf[mailbuf_finder].use = 1;            mailbuf[mailbuf_finder].text[0] = 0;            mailbuf[mailbuf_finder].id_to[0] = 0;            mailbuf[mailbuf_finder].charname_to[0] = 0;            mailbuf[mailbuf_finder].id_from[0] = 0;            mailbuf[mailbuf_finder].charname_from[0] = 0;            if( use_msgid ){                mailbuf[mailbuf_finder].message_id = msgid;            } else {                mailbuf[mailbuf_finder].message_id = getNextMessageID();            }            mailbuf[mailbuf_finder].state = MS_NEWMESSAGE;            mailbuf[mailbuf_finder].recv_time = time(NULL);            return mailbuf_finder;        }    }    log( "分配邮件: 邮件缓冲失败.正在进行分配...\n" );    if( reallocMailBuf() < 0 ){        log( "分配邮件: 分配失败\n" );    } else {        return allocMail(use_msgid, msgid );    }    return -1;}void receiveMail( char *id_from,             char *charname_from,             char *id_to,             char *charname_to,             char *message,             int option,             int use_msgid,             unsigned int msgid              ){    char id_charname[1024];    int h, mbindex;    snprintf( id_charname, sizeof( id_charname), "%s_%s", id_to, charname_to );    h = hashpjw( id_charname ) & 0xff ;    if( (mbindex = allocMail(use_msgid, msgid )) < 0 ){        log( "回复邮件: 获取新的邮件缓冲失败.\n" );        return;    }    mailbuf[mbindex].id_charname_hash = h;    snprintf( mailbuf[mbindex].text, sizeof( mailbuf[mbindex].text ), "%s", message );    snprintf( mailbuf[mbindex].id_to, sizeof( mailbuf[mbindex].id_to ), "%s", id_to );    snprintf( mailbuf[mbindex].charname_to, sizeof( mailbuf[mbindex].charname_to ), "%s", charname_to );    snprintf( mailbuf[mbindex].id_from, sizeof( mailbuf[mbindex].id_from ),"%s", id_from );    snprintf( mailbuf[mbindex].charname_from, sizeof( mailbuf[mbindex].charname_from ),"%s", charname_from );    mailbuf[mbindex].option = option;    {        char childname[1000];        char savefile[1000];        char charname2[CHARNAME_MAX*2+1];        char text2[TEXT_MAX*2+1];        FILE *fp;                snprintf( childname, sizeof( childname ),"%u" ,                  mailbuf[mbindex].message_id );        makeDirFilename( savefile , sizeof(savefile), maildir, h, childname );        fp = fopen( savefile, "w" );        if( fp == NULL ){            log( "回复邮件 : 不能保存邮件文件: %s %s\n",                 savefile, strerror( errno ));            return;        }#define TO_ID_HEAD "ToID: "#define TO_CHAR_HEAD "ToChar: "#define FROM_ID_HEAD "FromID: "#define FROM_CHAR_HEAD "FromChar: "#define OPTION_HEAD "Option: "#define TEXT_HEAD "Text: "                fprintf( fp, TO_ID_HEAD "%s\n", id_to );        snprintf( charname2, sizeof( charname2), "%s", charname_to );        fprintf( fp, TO_CHAR_HEAD "%s\n", makeEscapeString( charname_to,                                               charname2,sizeof(charname2)) );        fprintf( fp, FROM_ID_HEAD "%s\n", id_from );        snprintf( charname2, sizeof( charname2), "%s", charname_from );        fprintf( fp, FROM_CHAR_HEAD "%s\n", makeEscapeString( charname_from,                                               charname2,sizeof(charname2)) );        fprintf( fp, OPTION_HEAD "%d\n", option );        snprintf( text2, sizeof( text2 ) , "%s", message );        fprintf( fp, TEXT_HEAD "%s\n", makeEscapeString( message,                                               text2, sizeof(text2)) );        fclose(fp);		{			static int mailnum=0;			if( mailnum%10 == 0)				log(".");			mailnum = (++mailnum%1000);			if( mailnum == 0 )				log("\n");		}    }    {        extern gmsv gs[MAXCONNECTION];        int i;        for(i=0;i<MAXCONNECTION;i++){            if( gs[i].use && gs[i].name[0] ){                saacproto_Message_send( i,                                        id_from, charname_from,                                        id_to, charname_to,                                        message, option,                                        mailbuf[mbindex].message_id );                mailbuf[mbindex].state = MS_WAIT_ACK;            }        }    }}void receiveMailAck( char *id, char *charname, int a , int mesgid ){    int i;    unsigned int h;    char id_charname[1024];    snprintf( id_charname, sizeof( id_charname), "%s_%s", id, charname );        h = hashpjw( id_charname ) & 0xff ;    for(i=0;i<mailbufsize;i++){        if( mailbuf[i].message_id == mesgid ){            if( mailbuf[i].use                && mailbuf[i].id_charname_hash == h                && strcmp( mailbuf[i].id_to, id ) == 0                && strcmp( mailbuf[i].charname_to, charname ) == 0                && mailbuf[i].state == MS_WAIT_ACK ){                char savefile[1024];                char childname[1000];                snprintf( childname,sizeof(childname),"%u",                          mailbuf[i].message_id );                makeDirFilename( savefile, sizeof(savefile),maildir,                                 h, childname );                if( unlink( savefile ) != 0 ){                    log( "failed to unlink %s: %s\n",

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -