About neohope

一直在努力,还没想过要放弃...

修改Thunderbird Mbox文件,找回丢失邮件

今天周六,起了个早,吃了早饭,沏上茶,一边洗衣服,一遍happy的看邮件。
邮件积攒的太多,于是操作就太快,结果,不知道为什么,一片邮件就消失了。

重要提示:
以下所有操作的前提是:进行完全备份

于是,尝试了重建所在文件夹的.msf索引文件,有两种方式:
1、在文件夹上,右击-》属性-》修复文件夹
2、关闭thunderbird,到邮箱路径下,删掉文件夹的.msf文件,重新打开Thunderbird
然后发现,只找回一部分邮件来。

这可不行,好多邮件很重要,于是,进行了大还原:
关闭Thunderbird,将global-messages-db.sqlite命名为global-messages-db.sqlite.bak,重启Thunderbird。
还是不行~~
哭的心都有了

好吧,找工具~~
尝试了ZMail和Advanced Media Recovery,测试证明,根本不好用~~

那只好找资料,自己开搞了。
Thunderbird的一个最底层邮件文件夹,由两部分组成,
没有后缀的MBox文件,和有后缀的.msf索引文件。

MBox就是邮件和附件的全部了,经过摸索,发现有以下规律
1、MBox可以用靠谱的文本编辑工具进行编辑,用notepad的同学们,您就省省吧
2、每一封邮件都是一段MIME格式的文本,每封邮件以如下格式开始,直到下一个”From – “结束

From - Thu Dec 19 01:26:18 2013
X-Account-Key: account4
X-UIDL: ZC0418-TJAs6xVgg1PigBtJ_pc_a3c
X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
X-Mozilla-Keys:                                                                                 
X-QQ-SSF: 00410000000000F0
X-QQ-BUSINESS-ORIGIN: 2
X-Originating-IP: 112.65.5.74
X-QQ-STYLE: 
X-QQ-mid: bizmail35t1387361723t6111701
From: "=?utf-8?B?5pyx6I6J6Imz?=" <xxx@xxx.com>
To: "=?utf-8?B?6auY5pmX?=" <xxx@xxx.com>

3、邮件根据编码不同,主要有gb2312,utf-8和BASE64编码三种
4、附件以MIME方式存储在邮件下面,如:

MIME-Version: 1.0
Content-Type: multipart/related;
	boundary="----=_NextPart_000_0007_01CE78E0.0A87C340"
...

5、两份邮件之间,没有关联关系,可以随便修改顺序,可以随便剪切复制粘贴,但要保证每一封邮件的完整性
6、邮件的状态,由以下两个标志位指定

module Mbox
  #X-Mozilla-Status
  #Message has been read.
  MSG_FLAG_READ=0x0001
  #A reply has been successfully sent.
  MSG_FLAG_REPLIED=0x0002
  #The user has flagged this message.
  MSG_FLAG_MARKED=0x0004
  #Already gone (when folder not compacted). Since actually removing a message from a folder is a semi-expensive operation, we tend to delay it; messages with this bit set will be removed the next time folder compaction is done. Once this bit is set, it never gets un-set.
  MSG_FLAG_EXPUNGED=0x0008
  #Whether subject has “Re:” on the front. The folder summary uniquifies all of the strings in it, and to help this, any string which begins with “Re:” has that stripped first. This bit is then set, so that when presenting the message, we know to put it back (since the “Re:” is not itself stored in the file).
  MSG_FLAG_HAS_RE=0x0010
  #Whether the children of this sub-thread are folded in the display.
  MSG_FLAG_ELIDED=0x0020
  #DB has offline news or imap article.
  MSG_FLAG_OFFLINE=0x0080
  #If set, this thread is watched.
  MSG_FLAG_WATCHED=0x0100
  #If set, then this message's sender has been authenticated when sending this msg. This means the POP3 server gave a positive answer to the XSENDER command. Since this command is no standard and only known by few servers, this flag is unmeaning in most cases.
  MSG_FLAG_SENDER_AUTHED=0x0200
  #If set, then this message's body contains not the whole message, and a link is available in the message to download the rest of it from the POP server. This can be only a few lines of the message (in case of size restriction for the download of messages) or nothing at all (in case of “Fetch headers only”)
  MSG_FLAG_PARTIAL=0x0400
  #If set, this message is queued for delivery. This only ever gets set on messages in the queue folder, but is used to protect against the case of other messages having made their way in there somehow – if some other program put a message in the queue, it won't be delivered later!
  MSG_FLAG_QUEUED=0x0800
  #This message has been forwarded.
  MSG_FLAG_FORWARDED=0x1000
  #These are used to remember the message priority in interal status flags.
  MSG_FLAG_PRIORITIES=0xE000

  
  #X-Mozilla-Status2
  #This message is new since the last time the folder was closed.
  MSG_FLAG2_NEW=0x00010000
  #If set, this thread is ignored.
  MSG_FLAG2_IGNORED=0x00040000
  #If set, this message is marked as deleted on the server. This only applies to messages on IMAP servers.
  MSG_FLAG2_IMAP_DELETED=0x00200000
  #This message required to send a MDN (Message Disposition Notification) to the sender of the message. For information about MDN see Wikipedia:Return receipt.
  MSG_FLAG2_MDN_REPORT_NEEDED=0x00400000
  #An MDN report message has been sent for this message. No more MDN report should be sent to the sender.
  MSG_FLAG2_MDN_REPORT_SENT=0x00800000
  #If set, this message is a template.
  MSG_FLAG2_TEMPLATE=0x01000000
  #These are used to store the message label.
  #label value
  #1 0x02000000
  #2 0x04000000
  #3 0x06000000
  #4 0x08000000
  #5 0x0A000000
  #6 0x0C000000
  #7 0x0E000000
  MSG_FLAG2_LABELS=0x0E000000
  #If set, this message has files attached to it.
  MSG_FLAG2_ATTACHMENT=0x10000000 
end

这样就好办了,写了个程序,查找每个mbox文件内的每个EMail,查看状态,找出状态错误的邮件:
PS:Ruby加起来学了没几天,哈哈哈

#!/usr/bin/ruby

class EnumFiles
  def enumFiles(folderPath)
    #枚举文件
    files = Dir.glob(folderPath+"*")
    return files
  end
  
  def enumFilesAll(folderPath)
    #枚举文件
    files = Dir.glob(folderPath+"**/*")
    return files
  end
end
#!/usr/bin/ruby

class ReadMbox
  def readLine(filePath)
    if File.exist?(filePath)
      f = File.open(filePath,"r+")
      lines=f.readlines
      return lines
    else
      puts(filePath + " does not exist")
    end
  end
  
  def readMsgNum(filePath,rootPath)
    msgNum=0
    msgErr=0
    bError=false
    
    if File.exist?(filePath)
          f = File.open(filePath,"r+")
          f.each{|l|
            if(l[0..6]=="From - ")
              msgNum=msgNum+1
              bError=false
            end
            
            if(l[0..17]=="X-Mozilla-Status: ") && (!bError)
              flag = l[18..21].to_i(16)
              if(flag & 0x0008 > 0)
                puts("]>"+l)
                msgErr=msgErr+1
                bError=true
              end
            end
      
            if(l[0..18]=="X-Mozilla-Status2: ") && (!bError)
              flag2 = l[19..26].to_i(16)
              if(flag2 & 0x00040000 > 0)
                puts("]>"+l)
                msgErr=msgErr+1
                bError=true
              end
            end
          }
    end
          
    puts(filePath.gsub(rootPath,"")+" EmailNum=\t"+msgNum.to_s()+"\terrNum=\t"+msgErr.to_s())
  end
end
#!/usr/bin/ruby

require "./EnumFiles.rb"
require "./ReadMbox.rb"

rootPath="PAHT/TO/MAIL/

files = EnumFiles.new().enumFilesAll(rootPath)
#puts files

mbox = ReadMbox.new()

files.each do |f|
  if(!File.directory?(f)) && (!(File.extname(f).length>0))
    mbox.readMsgNum(f,rootPath)
  end
end

最后,找到文件,发现并不是很多只有5个,就懒得写程序了,手动剪切到回收站,将标志位置为正常,刷新回收站索引,然后一切就太平啦

X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000

后记:
其实Thunderbird在删除邮件时(从回收站删除),为提高效率,并不一定会真的将邮件删掉,而很可能只是打上一个标记。

这样,其实只要用一个靠谱的编辑器,比如VIM、PSPad等,就可以很方便的定位到你需要的邮件,然后就可以进行备份和还原啦。

参考:
全局还原索引文件
官方推荐的编辑工具列表
标志位说明
标志位源码

Tomcat强制使用HTTPS

配置好Tomcat的SSL后,强制使用HTTPS方法如下:

1、全局强制HTTPS
修改%tomcat_home%\conf\web.xml,在文件底部,后面加上这样一段:

    <login-config>  
        <!-- Authorization setting for SSL -->  
        <auth-method>CLIENT-CERT</auth-method>  
        <realm-name>Client Cert Users-only Area</realm-name>  
    </login-config>  
    <security-constraint>  
        <!-- Authorization setting for SSL -->  
        <web-resource-collection >  
            <web-resource-name >SSL</web-resource-name>  
            <url-pattern>/*</url-pattern>  
        </web-resource-collection>  
        <user-data-constraint>  
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>  
        </user-data-constraint>  
    </security-constraint>  

2、某个webapp强制使用HTTPS
编辑该webapp的web.xml文件,增加以下内容:

	<security-constraint>
	    <web-resource-collection>
	        <web-resource-name>securedapp</web-resource-name>
	        <url-pattern>/*</url-pattern>
	    </web-resource-collection>
	    <user-data-constraint>
	        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
	    </user-data-constraint>
	</security-constraint>

关闭 SSL,只需要将CONFIDENTIAL改为NONE即可。

VC fix .pch file missing

表现:

Error   1   fatal error C1083: Cannot open precompiled header file: 'Debug\????.pch': No such file or directory

解决方法:
1、保证stdafx.h及stdafx.cpp在项目的最顶层,stdafx.h用于保存需要的头文件,stdafx.cpp中只有对stdafx.h的引用;
2、在VS中,右击需要修改的工程,选择”Properties”;
3、在左上角,选择“ All Configurations”;
4、在左边,找到“C/C++”,并定位到“Precompiled Headers”;
5、将选项Precompiled Header修改为: “Use (/Yu)”;
6、将选项“Precompiled Header File”修改为:“stdafx.h”;
7、保存设置;
8、保证#include “stdafx.h”为所有需要预编译的cpp文件的第一行;
9、VS中,右击stdafx.cpp,选择”Properties”;
10、在左上角,选择“ All Configurations”;
11、将选项Precompiled Header修改为: “Create (/Yc).”;
12、保存设置,重新编译。

第一次拆iPhone4(后记)

过了两周,液晶的花屏慢慢好了,感觉相当不错。

但最近发下,液晶花屏的地方,有一道比较明显的折痕,锁屏界面看不出来,解锁界面十分明显。

估计花屏就是它的原因吧。

🙂

PS:
今天早上又摔了,直接关机了,开不开。
插上数据线后,又可以用了,我可怜的iphone4啊。

Prolog101(08)

列表:
放在用方括号中的一组项目的集合,各项目之间使用逗号分割。

空表:
没有项目的列表,用[]表示。

表头与表尾:
[X|Y]可以与任意的列表匹配,匹配成功后,X绑定为列表的第一个项目的值,我们称之为表头(head)。
而Y则绑定为剩下的列表,我们称之为表尾(tail)。
表尾(tail)一定是列表,而表头(head)则是一个项目,该项目可以是表,也可以是其他的任何数据结构。
看下这个例子就清楚了:

1 ?- [a|[b,c,d]] = [a,b,c,d]. 
true.

2 ?- [a|b,c,d] = [a,b,c,d].
ERROR: Syntax error: Unexpected comma or bar in rest of list
...

3 ?- [H|T] = [a]. 
H = a,
T = [].
 
4 ?- [H|T] = [a,b,c,d]. 
H = a,
T = [b, c, d].

5 ?- [H|T] = [a,[b,c,d]]. 
H = a,
T = [[b, c, d]].

6 ?- [H|T] = [].
false.

7 ?- [A,B|T] = [a,b,c,d].
A = a,
B = b,
T = [c, d].

8 ?- [a|[b|[c|[d|[]]]]] = [a,b,c,d].
true.

检查数据是否存在:

1 ?- member(a, [a,b,c]). 
true .

2 ?- member(d, [a,b,c]). 
false.

3 ?- member(d, [a,b,c,[d],e]). 
false.

4 ?- member([d], [a,b,c,[d],e]).
true .

5 ?- member(X, [a,b,c]). 
X = a ;
X = b ;
X = c.

追加

1 ?- append([a,b,c],[d,e,f],X).
X = [a, b, c, d, e, f].

2 ?- append([],[d,e,f],X).
X = [d, e, f].

3 ?- append([a,b],Y,[a,b,c,d]).
Y = [c, d].

4?- append(X,Y,[a,b,c]).
X = [],
Y = [a, b, c] ;
X = [a],
Y = [b, c] ;
X = [a, b],
Y = [c] ;
X = [a, b, c],
Y = [] ;

删除

1 ?- delete([a,b,c,d,e], c, X).
X = [a, b, d, e]

Prolog101(07)

联合(Unification)

变量与任何项目: 变量可以与任何项目绑定,其中也包括变量
原始项目与原始项目: 两个原始项目(原子或整数)只有当它们相同时才能联合。
结构与结构: 如果两个结构的每个相应的参数能联合,那么这两个结构可以联合。

1 ?- (1,2,3)=(X,Y,Z).
X = 1,
Y = 2,
Z = 3.

2 ?- (1,2,3)=(X,X,Z).
false.

3 ?- (1,1,3)=(X,X,Z).
X = 1,
Z = 3.

4 ?- (1,X,3)=(X,1,Z).
X = 1,
Z = 3.

5 ?- (1,X,3)=(X,2,Z).
false.
1 ?- (1,2,X)=(1,2,(3,4,5)).
X = (3, 4, 5).

2 ?- (1,2,X)=(1,2,(3,4,Y)),Y=5.
X = (3, 4, 5),
Y = 5.

%_表示不关心匹配内容
3 ?- (1,2,X,Y,7)=(1,2,(3,4,5),6,_).
X = (3, 4, 5),
Y = 6.

4 ?- X = Y, Y = hi, write(X).
hi
X = Y, Y = hi.
%swipl -s objs.pl
%Hansen

%room
room(kitchen).

%objects and location
%object(Name, Color, Size, Weight).
location(object(candle, red, small, 1), kitchen).
location(object(apple, red, small, 1), kitchen).
location(object(apple, green, small, 1), kitchen).
location(object(table, blue, big, 50), kitchen).

%current room
here(kitchen).

%can take something?
can_take(Thing) :-  
here(Room), 
location(object(Thing, _, small, _), Room). 
can_take(Thing) :- 
here(Room), 
location(object(Thing, _, big, _), Room), 
write('The '), write(Thing),  
write(' is too big to carry.'), nl, 
fail. 
can_take(Thing) :- 
here(Room), 
not(location(object(Thing, _, _, _), Room)), 
write('There is no '), write(Thing), write(' here.'), nl, 
fail.

%out put the weight
write_weight(1) :- write('1 pound').  
write_weight(W) :- W > 1, write(W), write(' pounds').

%list all things in a room
list_things(Place) :-  
location(object(Thing, Color, Size, Weight), Place), 
write('A '),write(Size),tab(1), 
write(Color),tab(1), 
write(Thing), write(', weighing '), 
write_weight(Weight), nl, 
fail. 

Prolog101(06)

动态修改全局数据

asserta(X) :把子句X当作此子句的谓词的第一个子句加入到动态数据库中,不可回溯。
assert(X)或assertz(X) :把子句X当作此子句的谓词的最后一个子句加入到动态数据库中,不可回溯。
retract(X) :把子句X从动态数据库中删除,不可回溯。

%swipl -s room.pl
%Hansen

:-dynamic here/1. 
:-dynamic location/2. 
:-dynamic bag/1.

%房间定义
room(kitchen).
room(office).
room(hall). 
room(diningroom).
room(cellar). 

%门定义
door(office, hall).
door(kitchen, office).
door(hall, diningroom).
door(kitchen, cellar).
door(diningroom, kitchen).

%规则:有门的两个房间是相通的
connect(X,Y) :- door(X,Y).
connect(X,Y) :- door(Y,X).

%物品在哪个房间
location(desk, office).
location(apple, kitchen). 
location(flashlight, desk). 
location(washingmachine, cellar).
location(nani, washingmachine).
location(broccoli, kitchen).
location(crackers, kitchen).
location(computer, office).

%背包
bag(tourch).

%哪些物品可以吃
edible(apple).
edible(crackers).
edible(broccoli).

%当前位置
here(hall).

%移动
move(Place):- can_go(Place),retract(here(X)), asserta(here(Place)).
can_go(Place):- here(X), connect(X, Place).  
can_go(Place):- write('You can''t go to '),write(Place),write(' from here.'), nl, fail.

%获取物品
take(X):- can_take(X), retract(location(X,_)), asserta(bag(X)), write(X), write(' taken.'), nl.
can_take(Thing):- here(Place), location(Thing, Place). 
can_take(Thing):- write('There is no '), write(Thing), write(' here.'), nl, fail. 

%放下物品
put(X):- can_put(X), here(Place), retract(bag(X)), asserta(location(X,Place)), write(X), write(' put.'), nl.
can_put(Thing):- bag(Thing). 
can_put(Thing):- write('There is no '), write(Thing), write(' in your bag.'), nl, fail. 

%房间物品列表
list_things(Place):- location(X, Place),tab(2),write(X),nl,fail.
list_things(_).

%相连的
list_connections(Place):- connect(Place, X),tab(2),write(X),nl,fail.
list_connections(_).

%持有物品列表
list_bag(Thing):- bag(X),tab(2),write(X),nl,fail.
list_bag(_).

%吃东西
eat(Thing):- can_eat(Thing), retract(bag(X)), write(Thing), write(' eaten. Yummy!').
can_eat(Thing):- bag(Thing), edible(Thing).
can_eat(Thing):- not(bag(Thing)), write('There is no '), write(Thing), write(' in your bag.'), nl, fail. 
can_eat(Thing):- bag(Thing), write('You can''t eat the '), write(Thing), write('.'), nl, fail. 

%查看房间情况
look :-
here(Place), write('You are in the '), write(Place), nl,
write('You can see:'),nl,list_things(Place),  
write('You can go to:'), nl, list_connections(Place),
write('You have:'),nl,list_bag(Thing).

%帮助
game :-
write('Look around: look/0'),nl,
write('Move around: move/1'),nl,
write('Take something: take/1'),nl,
write('Eat something: eat/1').
1 ?- game.
Look around: look/0
Move around: move/1
Take something: take/1
Eat something: eat/1
true.

2 ?- look.
You are in the hall
You can see:
You can go to:
  diningroom
  office
You have:
  tourch
true.

3 ?- move(diningroom).
true .

4 ?- look.
You are in the diningroom
You can see:
You can go to:
  kitchen
  hall
You have:
  tourch
true.

5 ?- move(kitchen).
true .

6 ?- look.
You are in the kitchen
You can see:
  apple
  broccoli
  crackers
You can go to:
  office
  cellar
  diningroom
You have:
  tourch
true.
7 ?- take(apple).
apple taken.
true .

8 ?- look.
You are in the kitchen
You can see:
  broccoli
  crackers
You can go to:
  office
  cellar
  diningroom
You have:
  apple
  tourch
true.

9 ?- eat(apple).
apple eaten. Yummy!
true .

Prolog101(05)

递归定义都包括两个部分:边界条件与递归部分。
边界条件定义最简单的情况。而递归部分,则首先解决一部分问题,然后再调用其自身来解决剩下的部分,
每一次都将进行边界检测,如果剩下的部分已经是边界条件中所定义的情况时,那么递归就圆满成功了。

递归解决阶乘问题

%swipl -s factorial.pl
%Hansen

fun(N):- N>0,fact(N,1).
fun(N):- write(N), write(' is smaller than 1. '),fail.

fact(1,M):- write(M),!.
fact(N,M):- N1 is N-1, M1 is M*N, fact(N1,M1).
 1 ?- fun(0).
0 is smaller than 1. 
false.

 2 ?- fun(1).
1
true .

 3 ?- fun(2).
2
true .

 4 ?- fun(3).
6
true .

 5 ?- fun(4).
24
true .

 6 ?- fun(5).
120
true .

 7 ?- fun(6).
720
true .

递归解决汉诺塔问题

%swipl -s hanoi.pl
%Hansen

%N个盘子从A到C的问题,用递归解决的思路:
%N-1个盘子,从A到B
%1个盘子,从A到C
%N-1个盘子,从B到C

hanoi(N):-move(N,a,b,c).
move(1,A,_,C):-fromto(A,C),!.
move(N,A,B,C):-N1 is N-1,move(N1,A,C,B),fromto(A,C),move(N1,B,A,C).
fromto(Loc1,Loc2):-nl,write('move one disk from '),write(Loc1),write(' to '),write(Loc2).

执行查询:

 1 ?- hanoi(1).

move one disk from a to c
true.

 2 ?- hanoi(2).

move one disk from a to b
move one disk from a to c
move one disk from b to c
true.

 3 ?- hanoi(3).

move one disk from a to c
move one disk from a to b
move one disk from c to b
move one disk from a to c
move one disk from b to a
move one disk from b to c
move one disk from a to c
true.

%这里就会出错啦
 4 ?- hanoi(0).
ERROR: Out of local stack

Prolog101(04)

数学计算:
X is <数学表达式> 

数学运算:
+-*/
()

比较运算:
X > Y 
X < Y 
X >= Y 
X =< Y  
1 ?- X is 1*2+3/4-(5-6)*7.
X = 9.75.

2 ?- 3=<4.
true.

3 ?- 3>=4.
false.

4 ?- 3>4.
false.

5 ?- 3<4.
true.

6 ?- X is 2+2, 3>=X.  
false.

看一下摄氏度与华氏度互转的程序:

%swipl -s calc.pl
%Hansen

c_to_f(C,F) :- F is C*9/5+32.
f_to_c(F,C) :- C is (F-32)*5/9.
1 ?- c_to_f(11,F).
F = 51.8.

2 ?- f_to_c(51.8,F).
F = 10.999999999999998.

Prolog101(03)

1、常用输出函数:

write:把它的参数作为字符串输出到屏幕上。
从Call端口调用时总是成功的,从Redo端口回溯时总是失败的。

nl:在屏幕上输出一个回车符。
从Call端口调用时总是成功的,从Redo端口回溯时总是失败的。

tab:输出n个空格,n为它的参数(整数)。
从Call端口调用时总是成功的,从Redo端口回溯时总是失败的。

2、启用调试

?- debug.

3、and,or,not
and:两个条件之间,用“,”分割
or:两个条件之间,用“;“分割
not:我使用的版本中,有not函数
比如,下面的例子中,like(X):-(fruit(X);vegetable(X)),not(meat(X)),not(hate(X)).:
panpan喜欢吃的食物逻辑为:
a、喜欢蔬菜和水果
b、不喜欢吃肉
c、有些蔬菜不喜欢
d、其余不喜欢

%swipl -s fruit.pl
%Hansen

%and(A,B):- A, B.
%or(A,B):- A; B.
%not(A):- call(A), !, fail.

like(X):-(fruit(X);vegetable(X)),not(meat(X)),not(hate(X)).

fruit(apple).
fruit(grape).
fruit(pear).

meat(pork).
meat(mutton).
meat(beef).

vegetable(tomato).
vegetable(cabbage).
vegetable(spinach).
vegetable(celery).

hate(celery).

所以,panpan不喜欢羊肉,喜欢大白菜,但不喜欢芹菜

1 ?- like(mutton).
false.

2 ?- like(cabbage).
true.

3 ?- like(celery).
false.