File: mailer.li

package info (click to toggle)
lisaac 1%3A0.13.1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 15,232 kB
  • ctags: 6,386
  • sloc: ansic: 242,479; xml: 635; lisp: 333; makefile: 119; sh: 73; asm: 38
file content (167 lines) | stat: -rw-r--r-- 4,724 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
///////////////////////////////////////////////////////////////////////////////
//                              Lisaac Example                               //
//                                                                           //
//                   LSIIT - ULP - CNRS - INRIA - FRANCE                     //
//                                                                           //
//   This program is free software: you can redistribute it and/or modify    //
//   it under the terms of the GNU General Public License as published by    //
//   the Free Software Foundation, either version 3 of the License, or       //
//   (at your option) any later version.                                     //
//                                                                           //
//   This program is distributed in the hope that it will be useful,         //
//   but WITHOUT ANY WARRANTY; without even the implied warranty of          //
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           //
//   GNU General Public License for more details.                            //
//                                                                           //
//   You should have received a copy of the GNU General Public License       //
//   along with this program.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                           //
//                     http://isaacproject.u-strasbg.fr/                     //
///////////////////////////////////////////////////////////////////////////////
Section Header
  
  + name    := MAILER;
     
  - author  := "Benoit Sonntag";
  
  - comment := "Extract mail adress for Matthieu.";
  
Section Inherit
  
  - parent_object:OBJECT := OBJECT;
      
Section Public
  
  - buffer:STRING := STRING.create 2048;
  
  - mail:HASHED_SET[STRING] := HASHED_SET[STRING].create;
  
  - extract_mail <-
  ( + idx,begin,end:INTEGER;
    + n:STRING;
    
    idx := buffer.lower;
    {idx <= buffer.upper}.while_do {
      idx := buffer.index_of '@' since idx;
      (idx <= buffer.upper).if {
	begin := idx - 1;
	{
	  (begin >= buffer.lower) && {
	    (buffer.item begin.is_letter_or_digit) || 
	    {buffer.item begin = '_'} ||
	    {buffer.item begin = '.'} 
	  }
	}.while_do {
	  begin := begin - 1;
	};
	begin := begin + 1;
	//
	end := idx + 1;
	{
	  (end <= buffer.upper) && {
	    (buffer.item end.is_letter_or_digit) || 
	    {buffer.item end = '_'} ||
	    {buffer.item end = '.'} 
	  }
	}.while_do {
	  end := end + 1;
	};
	end := end - 1;
	//
	((end - begin) > 3).if { 
	  n := buffer.substring begin to end;
	  mail.add n;
	};
	idx := end + 1;
      };
    };    
  );
   
  - main <-
  ( + e:ENTRY;
    + file:STD_FILE;
    + dir:DIRECTORY;
    
    //
    // Parse command line option.
    //
    (COMMAND_LINE.count != 2).if {      
      "Extract mail for Matthieu (by Sonntag Benoit)\n\
      \Usage  : mailer <directory>\n\
      \Output : mail.txt\n".print;
      die_with_code exit_failure_code;
    };
    
    //
    // Open directory.
    //
    dir := FILE_SYSTEM;    
    e := dir.get (COMMAND_LINE.item 1);
    ((e != NULL) && {e.is_directory}).if {      
      e := e.open;
      (e != NULL).if {
	dir.close;
	dir ?= e;
      } else {
	'`'.print;
	COMMAND_LINE.item 2.print;
	"\': Permission denied.\n".print;
	die_with_code exit_failure_code;
      };
    } else {
      '`'.print;
      COMMAND_LINE.item 2.print;
      "\': No such directory.\n".print;
      die_with_code exit_failure_code;
    };
    
    //
    // Select all text file 
    //
    (dir.lower).to (dir.upper) do { i:INTEGER;
      e := dir.item i;
      ((e.is_file) && {e.name.has_suffix ".txt"}).if {
	"Parse : ".print;
	e.path.print;	
	e := e.open;
	(e = NULL).if {
	  " : Not open.".print;          
	} else {
	  file ?= e;          
	  buffer.clear;
	  buffer.set_capacity (file.size.to_integer);
	  file.read buffer size (file.size);
	  extract_mail;
	  file.close;
	  " \t OK".print;
	};
	'\n'.print;
      };
    };
    
    "Result : ".print;
    mail.count.print;
    " found.\n".print;
    
    //
    // Save file.
    //    
    (mail.count != 0).if {
      e := dir.make_file "mail.txt";
      (e = NULL).if {      
	"`mail.txt' : File exists, cannot create file.\n".print;
	die_with_code exit_failure_code;
      };
      file ?= e.open;
      buffer.clear;
      (mail.lower).to (mail.upper - 1) do { j:INTEGER;
	buffer.append (mail.item j);
	buffer.add_last ';';
	buffer.add_last ' ';            
      };
      buffer.append (mail.last);
      file.write buffer from 1 size (buffer.count);
      file.close;          
      "Write `mail.txt' ok !\n".print;
    };
  );