File: bogo.tcl

package info (click to toggle)
exmh 1%3A2.9.0-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 4,216 kB
  • sloc: tcl: 38,046; perl: 1,647; makefile: 130; sh: 101; exp: 75; csh: 9; sed: 2
file content (169 lines) | stat: -rw-r--r-- 5,690 bytes parent folder | download | duplicates (6)
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
proc Bogo_Init {} {
    global bogo
    Preferences_Add "Bayesian Spam Filter" \
"Parameters for Bayesian spam filters that have to be told if they marked messages incorrectly. Your usage of this feature will depend on how you call it from procmail. See your program's man page for details." {
    {bogo(inUse) bogoinUse OFF {Use a Bayesian spam filter}
"Enables support for the 'learn' mode for training various
popular Bayesian spam filters"}
    {bogo(progname) bogoProgName {CHOICE bogofilter spamoracle spamassassin other} {Spam Program}
"Which spam program you use. If it is not in your $PATH, 
select other and give the full path name."}
    {bogo(mismarked) bogoMismarked ON {Is spam mismarked?}
"When you run the filter from procmail, does it add the tokens?
If it does, then spam that makes it through is mismarked, and you
want this flag on. This only matters for bogofilter: On means 
it uses -Sn and -Ns; for off it uses -s and -n."}
    {bogo(otherspam) bogoOtherSpam {} {Other--Spam}
"How the program is invoked, flags included, for spam."}
    {bogo(otherham) bogoOtherHam {} {Other--Non-Spam}
"How the other program is invoked, flags included, for non-spam."}
    {bogo(spammessage) bogoSpamMessage {CHOICE nothing rmm refile} {Spam Procedure}
"What to do with a spam message after your Bayesian filter has reclassified it."}
    {bogo(spamfolder) bogoSpamRefile bogus {Spam Folder}
"If you selected 'refile' for spam messages, 
this is the destination folder."}
    {bogo(hammessage) bogoHamMessage {CHOICE nothing refile} {Ham Procedure}
"What to do with a ham message after your Bayesian filter has reclassified it."}
    {bogo(hamfolder) bogoHamRefile inbox {Ham Folder}
"If you selected 'refile' for ham messages,
this is the destination folder."}
    {bogo(stdin) bogoStdin ON {Feed messages on STDIN?}
"If your filter expects mails to be passed via STDIN, leave this on.
Your program will be passed mails via STDIN, one per invocation.
Otherwise your filter program will get a list of files as arguments.
Spamassassin supports either method."}
    }
    trace variable bogo(progname) w BogoSetup
    BogoSetup
}

proc BogoSetup {args} {
    global bogo
    case $bogo(progname) {
	"bogofilter" {
	    if {$bogo(mismarked)} {
		set bogo(spamprog) {bogofilter -Ns}
		set bogo(hamprog) {bogofilter -Sn}
	    } else {
		set bogo(spamprog) {bogofilter -s}
		set bogo(hamprog) {bogofilter -n}
	    }
	}
	"spamoracle" {
	    set bogo(spamprog) {spamoracle add -good}
	    set bogo(hamprog) {spamoracle add -spam}
	}
	"spamassassin" {
	    set bogo(spamprog) {sa-learn --spam}
	    set bogo(hamprog) {sa-learn --ham}
	}
	"other" {
	    set bogo(spamprog) $bogo(otherspam)
	    set bogo(hamprog) $bogo(otherham)
	}
    }		
}

proc Bogo_Filter {{spam spam}} {
    global exmh msg bogo mhProfile
    Exmh_Debug Bogo $spam
    if {!$bogo(inUse)} {
        Exmh_Status "Bayesian filter not enabled in Preferences"
	return
    }
    if {$spam == "spam"} {
        set msgs [Ftoc_CurMsgs]
        Exmh_Status "Marking [llength $msgs] msg[expr {[llength $msgs] > 1 ? "s" : ""}] as SPAM"
        Exmh_Debug Bogo spamprog="$bogo(spamprog)", message="$msgs", action="$bogo(spammessage)"
	if {$bogo(stdin)} {
	    Ftoc_MsgIterate msgid {
		if [catch "exec $bogo(spamprog) <$mhProfile(path)/$exmh(folder)/$msgid" in] {
		    Exmh_Status $in
		    return
		}
	    }
	} else {
	    set paths {}
	    Ftoc_MsgIterate msgid {
		lappend paths "$mhProfile(path)/$exmh(folder)/$msgid"
	    }
	    if [catch "exec $bogo(spamprog) $paths" in] {
		Exmh_Status $in
		return
	    }
	}
	if {$bogo(spammessage) == "rmm"} {
	    Msg_Remove Ftoc_RemoveMark show
	}
	if {$bogo(spammessage) == "refile"} {
            set oldtarget $exmh(target)
	    set exmh(target) $bogo(spamfolder)
            Exmh_Debug Bogo refile spam to $exmh(target)
	    Msg_Move Ftoc_MoveMark 1 noshow
            set exmh(target) $oldtarget
	}
	return
    } elseif {$spam == "ham"} {
        set msgs [Ftoc_CurMsgs]
        Exmh_Status "Marking [llength $msgs] msg[expr {[llength $msgs] > 1 ? "s" : ""}] as HAM"
        Exmh_Debug Bogo hamprog="$bogo(hamprog)", message="$msgs", action="$bogo(hammessage)"

	if {$bogo(stdin)} {
	    Ftoc_MsgIterate msgid {
		if [catch "exec $bogo(hamprog) <$mhProfile(path)/$exmh(folder)/$msgid" in] {
		    Exmh_Status $in
		    return
		}
	    }
	} else {
	    set paths {}
	    Ftoc_MsgIterate msgid {
		lappend paths "$mhProfile(path)/$exmh(folder)/$msgid"
	    }
	    if [catch "exec $bogo(hamprog) $paths" in] {
		Exmh_Status $in
		return
	    }
	}

	if {$bogo(hammessage) == "refile"} {
            set oldtarget $exmh(target)
	    set exmh(target) $bogo(hamfolder)
            Exmh_Debug Bogo refile ham to $exmh(target)
	    Msg_Move Ftoc_MoveMark 1 noshow
            set exmh(target) $oldtarget
	}
	return
    } else {
	Exmh_Status "Spam button config error."
	return
    }
}
proc Bogo_FilterFolder {{spam spam}} {
  global exmh bogo
  global mhProfile
  set folder $exmh(folder)

  if {!$bogo(inUse)} {
    Exmh_Status "Bayesian filter not enabled in Preferences"
    return
  }
  Exmh_Status "Learning $exmh(folder) as $spam"
  if {$bogo(progname) != "spamassassin"} {
    Exmh_Status "Only sa-learn is currently supported for learning a folder"
    return
  }
  set pipe [open "|sa-learn --$spam $mhProfile(path)/$exmh(folder)"]
  fileevent $pipe readable [list BogoFilterReader $pipe $exmh(folder) $spam]
}
proc BogoFilterReader {pipe folder spam} {
  if {[eof $pipe]} {
    Exmh_Status "Learned $folder as $spam"
    if {[catch {close $pipe} err]} {
      Exmh_Debug "sa-learn $folder: $err"
    }
  } else {
    gets $pipe line
    Exmh_Debug "BogoFilterReader: $line"
  }
}