Coverage for src/botadi/mokadi/mokadi_action_mail.py: 71%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

75 statements  

1# -*- coding: utf-8 -*- 

2""" 

3@file 

4@brief Defines an action for Mokadi. 

5""" 

6from .mokadi_action import MokadiAction 

7from .mokadi_info import MokadiInfo 

8from .mokadi_exceptions import MokadiException, MokadiAuthentification 

9from .mokadi_mails import enumerate_last_mails 

10from .mokadi_helper import parse_string_int 

11 

12 

13class MokadiActionMail(MokadiAction): 

14 """ 

15 Action. Mail. 

16 """ 

17 

18 def __init__(self, user, pwd, server, fLOG=None): 

19 """ 

20 Constructor. 

21 

22 @param user login 

23 @param pwd password 

24 @param server server 

25 @param fLOG logging function 

26 """ 

27 MokadiAction.__init__(self, fLOG=fLOG) 

28 self._user = user 

29 self._pwd = pwd 

30 self._server = server 

31 

32 def can_do(self, interpreted, message): 

33 """ 

34 Tells if the class can process the message. 

35 

36 @param interpreted interpreted message 

37 @param message message 

38 @return true if the class can process the message 

39 """ 

40 if len(interpreted) < 2: 

41 return False 

42 word = interpreted[1] 

43 for word in interpreted[1:]: 

44 if word[1] == ":mails:": 

45 return True 

46 return False 

47 

48 def process_interpreted_message(self, interpretation, message): 

49 """ 

50 Process the interpreted message. 

51 

52 @param interpretation interpretation 

53 @param message original message 

54 @return iterator on Info 

55 """ 

56 done = False 

57 stop = -1 

58 keep = -1 

59 good = False 

60 body = False 

61 interpretation0 = interpretation 

62 interpretation = [_ for _ in interpretation if _[1] != ":numero:"] 

63 interpretation_clean = [ 

64 _ for _ in interpretation if _[1] != ":stopword:"] 

65 if len(interpretation_clean) == 6: 

66 if interpretation_clean[1][1] == ":verb_voir:" and interpretation_clean[2][1] == ":mails:" and \ 

67 interpretation_clean[3][1] == ":int:" and interpretation_clean[4][1] == ":entier:": 

68 keep = parse_string_int(interpretation_clean[3][0]) 

69 good = True 

70 body = True 

71 elif len(interpretation) == 5: 

72 if interpretation[1][1] == ":verb_voir:" and interpretation[2][1] == ":int:" and \ 

73 interpretation[3][1] == ":mails:": 

74 stop = parse_string_int(interpretation[2][0]) 

75 good = True 

76 elif interpretation[1][1] == ":verb_voir:" and interpretation[2][1] == ":mails:" and \ 

77 interpretation[3][1] == ":int:": 

78 keep = parse_string_int(interpretation[3][0]) 

79 good = True 

80 if not good and len(interpretation_clean) == 4: 

81 if interpretation_clean[1][1] == ":verb_voir:" and interpretation_clean[2][1] == ":mails:": 

82 good = True 

83 if good: 

84 fetch = max(keep + 1, 5) 

85 try: 

86 mails = enumerate_last_mails( 

87 self._user, self._pwd, self._server, fLOG=self.fLOG, nb=fetch) 

88 except MokadiAuthentification: 

89 yield MokadiInfo("error", "Il m'est impossible de me connecter à la boîte mail de {0}.".format(self._user)) 

90 

91 try: 

92 for i, mail in enumerate(mails): 

93 if i == stop: 

94 break 

95 if keep not in (-1, i): 

96 continue 

97 self.fLOG(mail.get_name(), "**", mail.get_nb_attachements(), 

98 "**", mail.get_date_str()) 

99 h = mail.get_date().hour 

100 yield MokadiInfo("ok", "Mail reçu vers {0} heures de {1}.".format(h, mail.get_name())) 

101 subj = mail.get_field("subject") 

102 if subj is None: 

103 subj = "" 

104 else: 

105 subj = subj.split("\n")[0] 

106 yield MokadiInfo("ok", subj) 

107 nb = mail.get_nb_attachements() 

108 if nb > 0: 

109 yield MokadiInfo("ok", "Ce mail a {0} pièces jointes.".format(nb)) 

110 if body: 

111 # The content of the mail includes past answers. 

112 # This should be removed as well as html tags. 

113 for line in mail.body.split("\n"): 

114 yield MokadiInfo("ok", line) 

115 except MokadiAuthentification: 

116 yield MokadiInfo("error", "Il m'est impossible de me connecter à la boîte mail de {0}.".format(self._user)) 

117 

118 done = True 

119 if not done: 

120 raise MokadiException( 

121 "Unable to interpret '{0}'\n{1} - {2} - {3}\n.".format(message, len(interpretation0), interpretation, interpretation_clean))