?? qmail-pop3d.c
字號:
/*
關鍵數據結構
隊列: --> prioq
這個數據結構在很多qmail很多程式中都有用到,最好記下來
代碼:
struct prioq_elt {
datetime_sec dt;//時間戳,優先級
unsigned long id;//郵件唯一id,你可以把它同qmail-queue分析中介紹中
// pid文件inode聯系起來
};
prioq在prioq.h中prioq是這樣定義的
GEN_ALLOC_typedef(prioq,struct prioq_elt,p,len,a)
展開后實際上定義為
typedef struct prioq
{
struct prioq_elt *p; // 指針
unsigned int len; //隊列的長度
unsigned int a;
}prioq;
//消息塊: --> message 我把它叫作消息塊是因為他并不包含消息內容,也許這樣
// 稱呼它并不確切
代碼:
struct message {
int flagdeleted; //刪除標記,在qmail-pop3d程式退出時進行實際刪除動作
unsigned long size; //消息文件大小
char *fn; //消息文件名
} *m;
主要功能:
qmail-pop3d是則vchkpw或checkpassword之類的程式啟動的。這些程式(vchkpw)
會更改環境變量USER,HOME,SHELL等等,并在啟動qmail-pop3d前將工作目錄
改變到$HOME下.qmail-pop3d在啟動時首先檢查./Maildir/tmp
(./Maildir是在argv中指定的)下最后訪問時間超過36小時的文件,如果存在就將其刪除。
也正是由于qmail-pop3d在啟動時就有chdir的動作,所以qmail-pop3d
不支持mailbox形式的pop.掃描Maildir/cur及Maildir/new目錄構造一個消息塊數組 m
(首先是構造一個臨時隊列pq,然后根據這個隊列來構造消息塊數組),輸出+OK,
進入命令循環,等待用戶輸入pop命令進行相應的處理.具體見代碼分析.
*/
#include <sys/types.h>#include <sys/stat.h>#include "commands.h"#include "sig.h"#include "getln.h"#include "stralloc.h"#include "substdio.h"#include "alloc.h"#include "open.h"#include "prioq.h"#include "scan.h"#include "fmt.h"#include "str.h"#include "exit.h"#include "maildir.h"#include "readwrite.h"#include "timeoutread.h"#include "timeoutwrite.h"void die() { _exit(0); }
//超時讀,超時時間為20分鐘,正常返回獨到的字節數,否則程序失敗die()int saferead(fd,buf,len) int fd; char *buf; int len;{ int r; r = timeoutread(1200,fd,buf,len); if (r <= 0) die(); return r;}
//超時寫,超時時間為20分鐘,正常返回寫的字節數,否則程序失敗die()int safewrite(fd,buf,len) int fd; char *buf; int len;{ int r; r = timeoutwrite(1200,fd,buf,len); if (r <= 0) die(); return r;}
//定義ssout為向fd1寫,超時時間為20分鐘,定義ssin為從fd0讀,超時時間為20分鐘
//由于tcpserver或inetd已經重定向了fd1,fd0到網絡,所以這就等同于向網絡寫char ssoutbuf[1024];substdio ssout = SUBSTDIO_FDBUF(safewrite,1,ssoutbuf,sizeof ssoutbuf);char ssinbuf[128];substdio ssin = SUBSTDIO_FDBUF(saferead,0,ssinbuf,sizeof ssinbuf);void put(buf,len) char *buf; int len;{ substdio_put(&ssout,buf,len); //將buf緩存中的內容向網絡寫}void puts(s) char *s;{ substdio_puts(&ssout,s); //將s的內容向網絡寫,這個函數實際上是調用的substdio_put}void flush() //確保輸出緩存中已經沒有內容{ substdio_flush(&ssout); }void err(s) char *s;{ puts("-ERR "); puts(s); puts("\r\n"); flush();}
//錯誤處理函數void die_nomem() { err("out of memory"); die(); }void die_nomaildir() { err("this user has no $HOME/Maildir"); die(); }void die_scan() { err("unable to scan $HOME/Maildir"); die(); }void err_syntax() { err("syntax error"); }void err_unimpl() { err("unimplemented"); }void err_deleted() { err("already deleted"); }void err_nozero() { err("messages are counted from 1"); }void err_nosuch() { err("unable to open that message"); }void err_nounlink() { err("unable to unlink all deleted messages"); }void okay() { puts("+OK \r\n"); flush(); }void printfn(fn) char *fn;{ fn += 4; put(fn,str_chr(fn,':'));}char strnum[FMT_ULONG];stralloc line = {0};void blast(ssfrom,limit) //從ssfrom讀數據輸出到fd1,一次一行(用全局緩存line)substdio *ssfrom;unsigned long limit; //除開消息頭部信息,最多讀limit行,limit為0將全部讀完{ int match; int inheaders = 1; for (;;) { if (getln(ssfrom,&line,&match,'\n') != 0) die(); if (!match && !line.len) break; if (match) --line.len; /* no way to pass this info over POP */ if (limit) if (!inheaders) if (!--limit) break; if (!line.len) inheaders = 0; else if (line.s[0] == '.') put(".",1); put(line.s,line.len); put("\r\n",2); } put("\r\n.\r\n",5); flush();}stralloc filenames = {0};prioq pq = {0};struct message { int flagdeleted; //刪除標記,在程序退出時進行實際刪除動作 unsigned long size; //文件大小 char *fn; //文件名} *m;int numm; //全局變量記錄隊列長度int last = 0;void getlist(){ struct prioq_elt pe; struct stat st; int i; maildir_clean(&line); //清除Maildir/tmp/目錄下最后訪問時間超過36小時的文件 if (maildir_scan(&pq,&filenames,1,1) == -1) die_scan(); numm = pq.p ? pq.len : 0; //記錄下隊列長度
//通過隊列pq構造消息塊數組,構建結束后隊列pq刪除 m = (struct message *) alloc(numm * sizeof(struct message)); //分配消息塊 if (!m) die_nomem(); for (i = 0;i < numm;++i) { if (!prioq_min(&pq,&pe)) { numm = i; break; } prioq_delmin(&pq); m[i].fn = filenames.s + pe.id; m[i].flagdeleted = 0; if (stat(m[i].fn,&st) == -1) m[i].size = 0; else m[i].size = st.st_size; }}void pop3_stat() //打印類似+OK<消息數量><刪除標記未設置的消息所占空間>{ //如+OK 3 3555表示總共有3條消息,占用空間3555(通過stat取得的) int i; unsigned long total; total = 0; for (i = 0;i < numm;++i) if (!m[i].flagdeleted) total += m[i].size; puts("+OK "); put(strnum,fmt_uint(strnum,numm)); puts(" "); put(strnum,fmt_ulong(strnum,total)); puts("\r\n"); flush();}void pop3_rset() //重置pop對話,清除所有刪除標記{ int i; for (i = 0;i < numm;++i) m[i].flagdeleted = 0; last = 0; okay();}void pop3_last() //顯示最后一個消息塊{ puts("+OK "); put(strnum,fmt_uint(strnum,last)); puts("\r\n"); flush();}
//結束一次pop對話,刪除所有刪除標記設置的消息,將new下的消息移到cur下void pop3_quit(){ int i; for (i = 0;i < numm;++i) if (m[i].flagdeleted) { if (unlink(m[i].fn) == -1) err_nounlink(); } else if (str_start(m[i].fn,"new/")) { if (!stralloc_copys(&line,"cur/")) die_nomem(); if (!stralloc_cats(&line,m[i].fn + 4)) die_nomem(); if (!stralloc_cats(&line,":2,")) die_nomem(); if (!stralloc_0(&line)) die_nomem(); rename(m[i].fn,line.s); /* if it fails, bummer */ } okay(); die();}
//檢查消息塊是否存在,或消息塊的刪除標記是否已經設置了
//成功返回消息塊的位置int型
//失敗返回-1int msgno(arg) char *arg;{ unsigned long u; if (!scan_ulong(arg,&u)) { err_syntax(); return -1; } if (!u) { err_nozero(); return -1; } --u; if (u >= numm) { err_toobig(); return -1; } if (m[u].flagdeleted) { err_deleted(); return -1; } return u;}
//將arg指定消息塊設置刪除標記,實際刪除動作將在pop3退出時進行void pop3_dele(arg) char *arg;{ int i; i = msgno(arg); if (i == -1) return; m[i].flagdeleted = 1; if (i + 1 > last) last = i + 1; okay();}void list(i,flaguidl)int i;int flaguidl;{
//顯示消息塊的內容,如果flaguidl設置,輸出消息文件名,否則消息大小 put(strnum,fmt_uint(strnum,i + 1)); puts(" "); if (flaguidl) printfn(m[i].fn); else put(strnum,fmt_ulong(strnum,m[i].size)); puts("\r\n");}
//如果指定了參數arg那么列出arg指定的消息塊的內容,否則列出全部消息void dolisting(arg,flaguidl) char *arg; int flaguidl;{ unsigned int i; if (*arg) { i = msgno(arg); if (i == -1) return; puts("+OK "); list(i,flaguidl); } else { okay(); for (i = 0;i < numm;++i) if (!m[i].flagdeleted) list(i,flaguidl); puts(".\r\n"); } flush();}void pop3_uidl(arg) char *arg; { dolisting(arg,1); }void pop3_list(arg) char *arg; { dolisting(arg,0); }substdio ssmsg; char ssmsgbuf[1024];void pop3_top(arg) char *arg; //顯示指定消息的內容{ int i; unsigned long limit; int fd; i = msgno(arg); //郵件號 if (i == -1) return; arg += scan_ulong(arg,&limit); //顯示幾行,如果未指定那么limit為0(balst函數打印全部內容) while (*arg == ' ') ++arg; if (scan_ulong(arg,&limit)) ++limit; else limit = 0; fd = open_read(m[i].fn); if (fd == -1) { err_nosuch(); return; } okay();
//關系ssmsg為從指定的消息文件中讀 substdio_fdbuf(&ssmsg,read,fd,ssmsgbuf,sizeof(ssmsgbuf));
//從ssmsg中讀到fd1,如果limit大于0將只讀取除消息頭外的limit行,如果等于0讀全部郵件 blast(&ssmsg,limit); close(fd);}struct commands pop3commands[] = { //pop3命令及處理函數表 { "quit", pop3_quit, 0 }, { "stat", pop3_stat, 0 }, { "list", pop3_list, 0 } //顯示消息大小, { "uidl", pop3_uidl, 0 } //顯示消息文件名, { "dele", pop3_dele, 0 }, { "retr", pop3_top, 0 } //取一條消息的內容,與top實現是一樣的, { "rset", pop3_rset, 0 } //重置pop對話,清除所有刪除標記, { "last", pop3_last, 0 }, { "top", pop3_top, 0 }, { "noop", okay, 0 }, { 0, err_unimpl, 0 }} ;
//qmail-pop3d有vchkpw或checkpassword之類的程序啟動,只有認證通過后才能
//執行本程序提供各種pop3命令void main(argc,argv)int argc;char **argv;{ sig_alarmcatch(die); sig_pipeignore(); if (!argv[1]) die_nomaildir();
//由于vchkpw或checkpassword之類的程序在啟動pop3之前已經將工作目錄改變到HOME下了
//所以這里直接進入arg指定的Maildir目錄,也是由于這個改變目錄原因,qmail-pop3d
//不支持Mailbox。 if (chdir(argv[1]) == -1) die_nomaildir(); getlist(); //這里構造了我們前面提到了消息塊數組*m okay();
//進入命令循環 commands(&ssin,pop3commands); die();}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -