summaryrefslogtreecommitdiff
path: root/web/cgi/alpine/2.0/newlist.tcl
blob: 725a418b15d01b3a1202afcca29bad390513d5a2 (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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!./tclsh
# $Id: newlist.tcl 1266 2009-07-14 18:39:12Z hubert@u.washington.edu $
# ========================================================================
# Copyright 2008 University of Washington
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# ========================================================================

#  newlist.tcl
#
#  Purpose:  CGI script that generates a page displaying a message
#            list of the indicated folder. 
#
#  Input:    PATH_INFO: [/<col_number>]/<folder_name>[/<uid_of_first_msg>
#            along with possible search parameters:
set newlist_args {
  {op		{}	""}
  {df		{}	""}
  {uid		{}	""}
  {type		{}	""}
  {zoom		{}	""}
  {page		{}	""}
  {criteria	{}	""}
  {scope	{}	"new"}
}

# inherit global config
source ./alpine.tcl
source ./common.tcl
source ./foldercache.tcl

# default newlist state
set c 0
set f "INBOX"

# TEST
proc cgi_suffix {args} {
  return ""
}

set dmsgs ""
proc dm {s} {
  global dmsgs

  lappend dmsgs $s
}

proc focusOnResult {_focused} {
  upvar 1 $_focused focused

  if {[catch {WPCmd PEMailbox focus 1} focused]} {
    WPCmd PEInfo statmsg "Cannot focus: $focused"
    set focused 0
  } else {
    WPCmd PEInfo statmsg "Displaying $focused search results"
  }
}

# grok PATH_INFO for collection, 'c', and folder 'f'
if {[info exists env(PATH_INFO)] && [string length $env(PATH_INFO)]} {
  if {[regexp {^/([0-9]+)/(.*)$} $env(PATH_INFO) dummy c f]} {
    # Import data validate it and get session id
    if {[catch {WPGetInputAndID sessid} result]} {
      set harderr "Session Invalid: $result"
      set deadsession 1
    } else {
      # grok parameters
      foreach item $newlist_args {
	if {[catch {eval WPImport $item} result]} {
	  set harderr "Cannot Read Input: $result"
	  break;
	}
      }

      if {[catch {WPCmd PEMailbox messagecount} mc]} {
	set harderr $mc
	if {[regexp {[Ii]nactive [Ss]ession$} $mc]} {
	  set deadsession 1
	}
      }

    }
  } else {
    set harderr "Invalid Folder: $env(PATH_INFO)"
  }
} else {
  set harderr "No folder specified"
}

cgi_puts "Content-type: text/html; charset=\"UTF-8\"\n"

if {[info exists harderr]} {
  if {[info exists deadsession]} {
    cgi_division class=contentDeadSession {
      cgi_puts "This Web Alpine session is no longer active.<p>Click [cgi_url "here to restart your session." "$_wp(serverpath)/"] </div>"
    }
    
    exit
  } else {
    catch { WPCmd PEInfo statmsg "$harderr" }
    set c 0
    set f INBOX
  }
}

set defc [WPCmd PEFolder defaultcollection]
set focused [WPCmd PEMailbox focus]

# set uid to "current" message?
if {0 == [catch {WPCmd PEMailbox current} cm]} {
  set n [lindex $cm 0]
  set u [lindex $cm 1]
} else {
  if {$mc > 0} {
    WPCmd PEInfo statmsg "BOTCH: No current message"
  }

  # non given, set to first message in folder
  set n 1
  if {[catch {
    set n [WPCmd PEMailbox first]
    set u [WPCmd PEMailbox uid $n]
  } result]} {
    if {$mc > 0} {
      WPCmd PEInfo statmsg "No first message: $result"
    }

    set n 0
    set u 0
  }
}

# lines per page
if {[catch {WPCmd PEInfo indexlines} ppg] || $ppg <= 0} {
  set ppg $_wp(indexlines)
} elseif {$ppg > $_wp(indexlinesmax)} {
  set ppg $_wp(indexlinesmax)
}

# deal with page change
if {$mc > 0} {
  switch -regexp -- $op {
    ^next$ {
      set n [WPCmd PEMailbox next $n $ppg]

      if {[catch {WPCmd PEMailbox uid $n} u]} {
	set n [WPCmd PEMailbox first]
	set u [WPCmd PEMailbox uid $n]
      }
    }
    ^prev$ {
      if {$ppg >= $n} {
	set n [WPCmd PEMailbox first]
      } else {
	set n [WPCmd PEMailbox next $n -$ppg]
      }

      if {[catch {WPCmd PEMailbox uid $n} u]} {
	set n [WPCmd PEMailbox first]
	set u 0
      }
    }
    ^first$ {
      set n [WPCmd PEMailbox first]
      if {[catch {WPCmd PEMailbox uid $n} u]} {
	set n 1
	set u 0
      }
    }
    ^last$ {
      set n [WPCmd PEMailbox last]
      if {[catch {WPCmd PEMailbox uid $n} u]} {
	set n 1
	set u 0
      }
    }
    ^delete$ {
      if {[catch {WPCmd PEMailbox apply count new} trashed_new]} {
	set trashed_new 0
      }

      if {[catch {WPCmd PEMailbox apply flag ton del} result]} {
	WPCmd PEInfo statmsg "Delete failed: $result"
      } else {
	WPCmd PEInfo statmsg "$result message[WPplural $result] moved to Trash"
	if {$trashed_new > 0} {
	  set trashed $trashed_new
	}
      }
    }
    ^trash$ {
      if {[catch {WPCmd PEFolder empty $c [wpLiteralFolder $c $f] selected} result]} {
	WPCmd PEInfo statmsg "Cannot Remove: $result"
      } else {
	WPCmd PEInfo statmsg "$result message[WPplural $result] deleted forever"
	if {0 == [WPCmd PEMailbox messagecount]} {
	  set n 0
	  set u 0
	}
	set trashed [expr {$result * -1}]
      }
    }
    ^trashall$ {
      if {[catch {WPCmd PEFolder empty $c [wpLiteralFolder $c $f] all} result]} {
	WPCmd PEInfo statmsg "Cannot Remove: $result"
      } else {
	WPCmd PEInfo statmsg "$result message[WPplural $result] deleted forever"
	set n 0
	set u 0
	set trashed [expr {$result * -1}]
      }
    }
    ^spam$ {
      set numspam [WPCmd PEMailbox selected]
      if {$numspam > 0} {
	# aggregate save 
	if {[info exists _wp(spamsubj)] && [string length $_wp(spamsubj)]} {
	  set spamsubj $_wp(spamsubj)
	} else {
	  set spamsubj "Spam Report"
	}

	# aggregate delete
	if {[info exists _wp(spamfolder)] && [string length $_wp(spamfolder)]
	    && [catch {
	      if {[WPCmd PEFolder exists $defc $_wp(spamfolder)] == 0} {
		WPCmd PEFolder create $defc $_wp(spamfolder)
	      }

	      WPCmd PEMailbox apply save $defc $_wp(spamfolder)
	    } result]} {
	  WPCmd PEInfo statmsg "Error Reporting Spam: $result"
	} elseif {[info exists _wp(spamaddr)] && [string length $_wp(spamaddr)]
		  && [catch {WPCmd PEMailbox apply spam $_wp(spamaddr) $spamsubj} reason]} {
	  WPCmd PEInfo statmsg "Error Sending Spam Notice: $reason"
	} elseif {[catch {WPCmd PEMailbox apply delete} reason]} {
	  WPCmd PEInfo statmsg "Error marking Spam Deleted: $reason"
	} else {
	  WPCmd PEInfo statmsg "$numspam spam message[WPplural $numspam] reported"
	}
      }
    }
    ^copy$ {
      if {[string length $df] && [regexp {^([0-9]+)/(.*)$} $df dummy dfc dfn] && [string length $dfn]} {
	if {[catch {WPCmd PEMailbox apply count new} cpmv_new]} {
	  set cpmv_new 0
	}

	if {[catch {WPCmd PEMailbox apply copy $dfc $dfn} result]} {
	  WPCmd PEInfo statmsg "Cannot copy messages: $result"
	} else {
	  if {$dfc == $defc
	      && !(([info exists _wp(spamfolder)] && 0 == [string compare $f $_wp(spamfolder)])
		   || 0 == [string compare $f Trash])} {
	    addSaveCache $dfn
	    set savecachechange $dfn
	    set cpmvdest [cgi_quote_html $dfn]
	    if {$result > 0 && $cpmv_new > 0} {
	      set cpmv $cpmv_new
	    }
	  }

	  WPCmd PEInfo statmsg "Copied $result message[WPplural $result] to $dfn"
	}
      } else {
	WPCmd PEInfo statmsg "Cannot copy to $df"
      }
    }
    ^move$ {
      if {[string length $df] && [regexp {^([0-9]+)/(.*)$} $df dummy dfc dfn] && [string length $dfn]} {
	if {[catch {WPCmd PEMailbox apply count new} cpmv_new]} {
	  set cpmv_new 0
	}

	if {[catch {WPCmd PEMailbox apply move $dfc $dfn} result]} {
	  WPCmd PEInfo statmsg "Move Failure: $result"
	} else {
	  # if needed, empty what was moved
	  if {$c == [WPCmd PEFolder defaultcollection]
	      && (([info exists _wp(spamfolder)] && 0 == [string compare $f $_wp(spamfolder)])
		  || 0 == [string compare $f Trash])
	      && [catch {WPCmd PEFolder empty $c $f selected} result]} {
	    WPCmd PEInfo statmsg "Move Failure: $result"
	  } else {
	    if {$dfc == $defc
		&& !(([info exists _wp(spamfolder)] && 0 == [string compare $f $_wp(spamfolder)])
		     || 0 == [string compare $f Trash])} {
	      addSaveCache $dfn
	      set savecachechange $dfn
	      set cpmvdest [cgi_quote_html $dfn]
	      if {$result > 0 && $cpmv_new > 0} {
		set cpmv $cpmv_new
	      }
	    }

	    # clean up moved messages so they don't get tossed in Trash as well
	    if {[catch {WPCmd PEMailbox expunge} blasted] || [string length $blasted]} {
	      WPCmd PEInfo statmsg "Move Failure: $blasted"
	    } else {
	      WPCmd PEInfo statmsg "Moved $result message[WPplural $result] to $dfn"
	    }
	  }
	}
      } else {
	WPCmd PEInfo statmsg "Cannot move: to $df"
      }
    }
    ^movemsg$ {
      if {[regexp {^[0-9]+$} $uid] && $uid > 0 && [string length $df] && [regexp {^([0-9]+)/(.*)$} $df dummy dfc dfn] && [string length $dfn]} {
	if {[catch {
	  # destination Trash? just delete and let regular delete process move it
	  if {$dfc == $defc && 0 == [string compare Trash $dfn]} {
	    WPCmd PEMessage $uid flag deleted 1
	  } else {
	    WPCmd PEMessage $uid move $dfc $dfn
	  }

	  # source is trash/junk, remove explicitly
	  if {$c == $defc
	      && (([info exists _wp(spamfolder)] && 0 == [string compare $f $_wp(spamfolder)])
		  || 0 == [string compare $f Trash])} {
	    WPCmd PEFolder empty $c $f $uid
	  }

	  WPCmd PEInfo statmsg "Moved message to $dfn"
	  set cpmvdest [cgi_quote_html $dfn]
	  if {0 != [WPCmd PEMessage $uid flag new]} {
	    set cpmv 1
	  }
	} result]} {
	  WPCmd PEInfo statmsg "Move Failure: $result"
	}
      } else {
	WPCmd PEInfo statmsg "Cannot move: to $df"
      }
    }
    ^sort[A-Za-z]+$ {
      if {[regexp {^sort([[Rr]ev|)(.*)$} $op dummy rev sname]} {
	set sort [string tolower $sname]
	set rval [expr {[string length $rev] > 0}]
	if {[catch {WPCmd PEMailbox sort $sort $rval} cursort]} {
	  WPCmd PEInfo statmsg "Cannot set sort: $cursor"
	  set cursort [list nonsense 0]
	} else {
	  # store result 
	  WPCmd set sort [list $sort $rval]
	}
      } else {
	WPCmd PEInfo statmsg "Unrecognized Sort: $op"
      }
    }
    ^search$ {
      if {![regexp {broad|narrow} $scope]} {
	WPCmd PEMailbox focus 0
	WPCmd PEMailbox search none
	set scope broad
      }

      switch -- $type {
	none {
	  WPCmd PEMailbox focus 0
	  WPCmd PEMailbox search none
	}
	any {
	  if {![string length $criteria]} {
	    WPCmd PEInfo statmsg "No search criteria provided"
	  } elseif {[catch {WPCmd PEMailbox search $scope text ton any $criteria} result]} {
	      WPCmd PEInfo statmsg "Search failed: $result"
	  } else {
	    if {$result == 0} {
	      WPCmd PEInfo statmsg "No messages matched your search"
	      cgi_html_comment "SCOPE: $scope"
	      if {0 == [string compare new $scope]} {
		WPCmd PEMailbox focus 0
		WPCmd PEMailbox search none
	      }
	    } else {
	      set n [WPCmd PEMailbox first]
	      if {[catch {WPCmd PEMailbox uid $n} u]} {
		set n 1
		set u 0
	      }

	      focusOnResult focused
	    }
	  }
	}
	compound {
	  if {![string length $criteria]} {
	    WPCmd PEInfo statmsg "No search criteria provided"
	  } elseif {[catch {WPCmd PEMailbox search $scope compound $criteria} result]} {
	    WPCmd PEInfo statmsg "Search failed: $result"
	  } else {
	    if {$result == 0} {
	      WPCmd PEInfo statmsg "No messages matched your search"
	      if {0 == [string compare new $scope]} {
		WPCmd PEMailbox focus 0
		WPCmd PEMailbox search none
	      }
	    } else {
	      set n [WPCmd PEMailbox first]
	      if {[catch {WPCmd PEMailbox uid $n} u]} {
		set n 1
		set u 0
	      }

	      focusOnResult focused
	    }
	  }
	}
	default {
	  WPCmd PEInfo statmsg "Unrecognized search: $type"
	}
      }
    }
    ^focus$ {
      focusOnResult focused
    }
    ^unfocus$ {
      if {[catch {WPCmd PEMailbox focus 0} result]} {
	WPCmd PEInfo statmsg "Cannot unfocus: $result"
      } elseif {$focused > 0} {
	WPCmd PEInfo statmsg "All messages displayed"
	set focused 0
      }
    }
    noop -
    ^$ {
    }
    default {
    }
  }
}

if {$focused} {
  set mc $focused
}

# page framing (note maybe changed by actions above)
wpInitPageFraming u n mc ppg pn pt

cgi_puts [WPCmd cgi_buffer "drawMessageList $c {$f} $n $ppg"]

cgi_puts "<script>"
cgi_put "updateBrowseLinksAndSuch(\{"
cgi_put "u:$u,selected:[WPCmd PEMailbox selected],"
cgi_put "unread:[WPCmd PEMailbox flagcount [list unseen undeleted]],"
cgi_put "page:$pn,pages:$pt,count:$mc,"
cgi_put "searched:[WPCmd PEMailbox searched],focused:$focused,"
cgi_put "sort:'[lindex [WPCmd PEMailbox sort] 0]'"
if {[info exists trashed] && $trashed != 0} {
  cgi_put ",trashed:$trashed"
}
if {[info exists cpmv] && $cpmv != 0} {
  cgi_put ",cpmv:{f:'$cpmvdest',n:$cpmv}"
}
cgi_puts "\});"
if {0 == [string compare $page new]} {
  cgi_puts "showBrowseMenus();"
  cgi_puts "initMenus();"
  cgi_puts "initMorcButton('listMorcButton');"
  cgi_puts "initSelection();"
  wpSaveMenuJavascript "browse" $c $f [WPCmd PEFolder defaultcollection] morcInBrowseDone
  wpSetMessageListNewMailCheck
  cgi_puts "if(self.loadDDElements) loadDDElements();"
}
if {[info exists savecachechange]} {
  wpSaveMenuJavascript browse $c $f $defc morcInBrowseDone $savecachechange
}
wpStatusAndNewmailJavascript
cgi_puts "if(self.loadDDElements) loadDDElements();"
cgi_puts "</script>"