summaryrefslogtreecommitdiff
path: root/ipl/procs/getmail.icn
blob: f7431b9a699f1eb02b13587050a88fc32dfd8e6e (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
############################################################################
#
#	File:     getmail.icn
#
#	Subject:  Procedure to parse mail file
#
#	Author:   Charles Shartsis
#
#	Date:     August 19, 1996
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
# The getmail procedure reads a Unix/Internet type mail folder
# and generates a sequence of records, one per mail message.
# It fails when end-of-file is reached.  Each record contains the
# message header and message text components parsed into separate
# record fields.  The entire uninterpreted message (header and text)
# are also stored in the record.  See the description
# of message_record below.
# 
# The argument to getmail is either the name of a mail folder or
# the file handle for a mail folder which has already been opened
# for reading.  If getmail is resumed after the last message is
# generated, it closes the mail folder and returns failure.
# 
# If getmail generates an incomplete sequence (does not close the
# folder and return failure) and is then restarted (not resumed)
# on the same or a different mail folder, the previous folder file
# handle remains open and inaccessible.  This may be a problem if
# done repeatedly since there is usually an OS-imposed limit
# on number of open file handles.  Safest way to use getmail
# is using one of the below forms:
# 
#     message := message_record()
#     every message := !getmail("folder_name") do {
#     
#             process message ...
#             
#     }
# 
#     message := message_record()
#     coex := create getmail("folder_name")
#     while message := @coex do {
#     
#             process message ...
#             
#     }
# 
# Note that if message_record's are stored  in a list, the records
# may be sorted by individual components (like sender, _date, _subject)
# using sortf function in Icon Version 9.0.
#     
############################################################################
#
#  Requires:  Icon Version 9 or greater
#
############################################################################

record message_record(

    # components of "From " line
    sender,         # E-Mail address of sender
    dayofweek,
    month,
    day,
    time,
    year,
    
    # selected message header fields
    
    # The following record fields hold the contents of common
    # message header fields.  Each record field contains the
    # corresponding message field's body (as a string) or a null indicating
    # that no such field was present in the header.
    # Note that a list of message_record's
    # can be sorted on any of these fields using the sortff function.
    # The record field name is related to the message header field name
    # in the following way:
    # 
    # record_field_name := "_" || 
    #     map(message_header_field_name, &ucase || "-", &lcase || "_")
    # 
    # Thus the "Mime-Version" field body is stored in the _mime_version
    # record field.  Multiline message header fields are "unfolded"
    # into a single line according to RFC 822.  The message field
    # name, the following colon, and any immediately following
    # whitespace are stripped from the beginning of the
    # record field.  E.g., if a header contains
    # 
    # Mime-Version:           1.0
    # 
    # then
    # 
    # message._mime_version := "1.0"
    # 
    # The "Received:" field is handled differently from the other
    # fields since there are typically multiple occurrences of it
    # in the same header. The _received record field is either null or
    # contains a list of "Received:" fields.  The message field names
    # are NOT stripped off.  Thus
    # 
    # Received: from relay4.UU.NET by mail.netcom.com (8.6.12/Netcom)
    #     id PAA10801; Sun, 28 May 1995 15:24:17 -0700
    # Received: from alterdial.UU.NET by relay4.UU.NET with SMTP 
    #     id QQyrsr05731; Sun, 28 May 1995 18:17:45 -0400
    # 
    # get stored as:
    # message._received :=
    # ["Received: from relay4.UU.NET by mail.netcom.com (8.6.12/Netcom)    id etc...",
    # "Received: from alterdial.UU.NET by relay4.UU.NET with SMTP     id etc..."]
     
    _return_path,
    _received,
    _date,
    _message_id,
    _x_sender,
    _x_mailer,
    _mime_version,
    _content_type,
    _to,
    _from,
    _subject,
    _status,
    _x_status,
    _path,
    _xref,
    _references,
    _errors_to,
    _x_lines,
    _x_vm_attributes,
    _reply_to,
    _newsgroups,
    _content_length,
    
    # The "other" field gets all the message header fields for which we have not set up
    # a specific record field.  The "other" record field either contains null
    # or a list of header fields not stored in the previous fields.
    # Message field names are NOT stripped off field bodies before being stored.
    # If there are multiple occurrences of the previously selected fields
    # (except _received which is assumed to occur multiple times), then 
    # the first occurrence is stored in the appropriate record field from
    # the list above while subsequent occurences in the same header are
    # stored as separate list elements in the "other" record field.
    # E.g., the following header fields:
    # 
    # ...
    # Whatever: Hello
    # Status: RO
    # Status: XX
    # Status: YY
    # ...
    # 
    # would be stored as
    # 
    # message._status := "RO"
    # message.other :=
    #     [..., "Whatever: Hello", "Status: XX", "Status: YY", ...]

    other,
    
    # The message text
    # This field is either null or a list of lines comprising
    # the message text.
    message_text,
    
    # The entire message - header and text
    # This field contains a list of uninterpreted lines (no RFC 822 unfolding)
    # comprising the raw message.
    
    all
    
)

# getmail SEQ
procedure getmail(folder_name)

    local folder, line, message, ws, item_tag, first_item_value, tag_field
    local time, message_text, unfolded_line
    
    ws := ' \t'
    
    if type(folder_name) == "file" then
        folder := folder_name
    else
        folder := open(folder_name, "r") |
            stop("Could not open ", folder_name)
    line := read(folder) | &null

    # body ITR UNTIL EOF
    until /line do {
        # message SEQ
            message := message_record()
            every !message := &null
            # header SEQ
                # from-line SEQ
                    message.all := []
                    put(message.all, line)
                    line ? (
                        ="From" & tab(many(ws)) &
                        message.sender <- tab(many(~ws)) & tab(many(ws)) &
                        message.dayofweek <- tab(many(&letters)) & tab(many(ws)) &
                        message.month <- tab(many(&letters)) & tab(many(ws)) &
                        message.day <- tab(many(&digits)) & tab(many(ws)) &
                        message.time <- match_time() & tab(many(ws)) &
                        message.year <- match_year()
                    ) |
                    stop("Invalid first message header line:\n", line)
                    line := read(folder) | &null
                # from-line END
                # header-fields ITR UNTIL EOF or blank-line or From line
                until /line | line == "" | is_From_line(line) do {
                    # header-field SEQ
                        # first-line SEQ
                            put(message.all, line)
                            # process quoted EOL character
                            if line[-1] == "\\" then
                                line[-1] := "\n"
                            unfolded_line := line
                            line := read(folder) | &null
                        # first-line END
                        # after-lines ITR UNTIL EOF or line doesn't start with ws or 
                        #               blank-line or From line
                        until /line | not any(ws, line) | line == "" | is_From_line(line) do {
                            # after-line SEQ
                                put(message.all, line)
                                # process quoted EOL character
                                if line[-1] == "\\" then
                                    line[-1] := "\n"
                                if unfolded_line[-1]  == "\n" then
                                    line[1] := ""
                                unfolded_line ||:= line
                                line := read(folder) | &null
                            # after-line END
                        # after-lines END
                        }
                        process_header_field(message, unfolded_line)
                    # header-field END
                # header-fields END
                }
            # header END
            # post-header ALT if blank line
            if line == "" then {
                # optional-message-text SEQ
                    # blank-line SEQ
                        put(message.all, line)
                        line := read(folder) | &null
                    # blank-line END
                    # message-text ITR UNTIL EOF or From line
                    until /line | is_From_line(line) do {
                        # message-text-line SEQ
                            put(message.all, line)
                            /message.message_text := []
                            put(message.message_text, line)
                            line := read(folder) | &null
                        # message-text-line END
                    # message-text END
                    }
                # optional-message-text END
            # post-header ALT default
            } else {
            # post-header END
            }
           suspend message
        # message END
    # body END
    }

    if folder ~=== &input then
        close(folder)
# getmail END
end

#############################################################################
#                   procedure is_From_line
#############################################################################

procedure is_From_line(line)

    return line ? ="From "

end

#############################################################################
#                   procedure match_time
#############################################################################

procedure match_time()

    suspend tab(any(&digits)) || tab(any(&digits)) || =":" ||
            tab(any(&digits)) || tab(any(&digits)) || =":" ||
            tab(any(&digits)) || tab(any(&digits))

end

#############################################################################
#                   procedure match_year
#############################################################################

procedure match_year()

    suspend tab(any(&digits)) || tab(any(&digits)) ||
            tab(any(&digits)) || tab(any(&digits))

end

#############################################################################
#                   procedure mfield_to_rfield_name
#############################################################################

procedure mfield_to_rfield_name(mfield_name)

    static mapfrom, mapto
    
    initial {
        mapfrom := &ucase || "-"
        mapto := &lcase || "_"
    }

    return "_" || map(mfield_name, mapfrom, mapto)

end

#############################################################################
#                   procedure process_header_field
#############################################################################

procedure process_header_field(message, field)

    local record_field_name, header_field_name, field_body
    static field_chars, ws
    
    # header field name can have ASCII 33 through 126 except for colon
    initial {
        field_chars := cset(string(&ascii)[34:-1]) -- ':'
        ws := ' \t'
    }
    
    field ? (
        header_field_name <- tab(many(field_chars)) & =":" &
        (tab(many(ws)) | "") &
        field_body <- tab(0)
    ) |
    stop("Invalid header field:\n", field)
    record_field_name := mfield_to_rfield_name(header_field_name)
    
    # This is one of the selected fields
    if message[record_field_name] then {
    
        # Its a "Received" field
        if record_field_name == "_received" then {
            # Append whole field to received field list
            /message._received := []
            put(message._received, field)
        
        # Not a "Received" field
        } else {
        
            # First occurrence in header of selected field
            if /message[record_field_name] then {
                # Assign field body to selected record field
                message[record_field_name] := field_body
            
            # Subsequent occurrence in header of selected field
            } else {
                # Append whole field to other field list
                /message.other := []
                put(message.other, field)
            }
        }
    
    # Not a selected field
    } else {
                # Append whole field to other field list
                /message.other := []
                put(message.other, field)
    }
    
end

#############################################################################