milter 1.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/events.cc DELETED
@@ -1,426 +0,0 @@
1
- /**
2
- * contextual information for events
3
- * it is apparently fashionable in node.js-speak to call these "batons"
4
- *
5
- * do you like 1980s C style standards for variable names? no? haha!
6
- *
7
- * do you like encapsulation? :)
8
- *
9
- * hahaha!
10
- */
11
- #include <string.h>
12
- #include <arpa/inet.h>
13
- #include <pthread.h>
14
- #include <assert.h>
15
- #include <node.h>
16
- #include <node_buffer.h>
17
- #include <v8.h>
18
- #include "libmilter/mfapi.h"
19
- #include "milter.h"
20
- #include "events.h"
21
- #include "envelope.h"
22
-
23
- using namespace v8;
24
- using namespace node;
25
-
26
-
27
- MilterEvent::MilterEvent (envelope_t *env)
28
- : fi_envelope(env),
29
- ev_next(NULL),
30
- is_done(false),
31
- fi_retval(SMFIS_TEMPFAIL) // TODO: allow custom default
32
- {
33
- pthread_mutex_init(&this->pt_lock, NULL);
34
- pthread_cond_init(&this->pt_ready, NULL);
35
- }
36
-
37
-
38
- bool MilterEvent::Lock () { return 0 == pthread_mutex_lock(&this->pt_lock); }
39
-
40
- bool MilterEvent::Unlock () { return 0 == pthread_mutex_unlock(&this->pt_lock); }
41
-
42
- bool MilterEvent::Wait () { return 0 == pthread_cond_wait(&this->pt_ready, &this->pt_lock); }
43
-
44
-
45
- void MilterEvent::Append (MilterEvent *ev)
46
- {
47
- assert(this->ev_next == NULL);
48
- this->ev_next = ev;
49
- }
50
-
51
- void MilterEvent::Detatch ()
52
- {
53
- this->ev_next = NULL;
54
- }
55
-
56
- MilterEvent *MilterEvent::Next () const { return this->ev_next; }
57
-
58
- void MilterEvent::SetMilterContext (SMFICTX *context)
59
- {
60
- assert(NULL != context);
61
- this->smfi_context = context;
62
- }
63
-
64
- bool MilterEvent::IsDone () const { return this->is_done; }
65
-
66
- int MilterEvent::Result () const { return this->fi_retval; }
67
-
68
- /*virtual*/ MilterEvent::~MilterEvent ()
69
- {
70
- pthread_cond_destroy(&this->pt_ready);
71
- pthread_mutex_destroy(&this->pt_lock);
72
- }
73
-
74
-
75
- /**
76
- * the last call made by the script kiddie from a callback is:
77
- * envelope.done(retval)
78
- * that then returns here, via the envelope's pointer to the current event
79
- *
80
- * xxfi_close will always be a MilterClose which will cause Postfire() to
81
- * Reset() its persistent envelope handle
82
- */
83
- bool MilterEvent::Done (Isolate *isolate, int retval)
84
- {
85
- this->Postfire(isolate);
86
-
87
- this->fi_retval = retval;
88
- this->is_done = (0 == pthread_cond_signal(&this->pt_ready));
89
-
90
- // TODO: throw FatalException if the sleeping libmilter thread is not signaled
91
-
92
- this->Unlock();
93
-
94
- // TODO: throw FatalException if lock is not yielded?
95
-
96
- return this->is_done;
97
- }
98
-
99
-
100
- /**
101
- * caling envelope.whatever() outside of certain events is largely forbidden
102
- */
103
- bool MilterEvent::IsNegotiate () const
104
- {
105
- return false;
106
- }
107
- bool MilterEvent::IsConnect () const
108
- {
109
- return false;
110
- }
111
- bool MilterEvent::IsEndMessage () const
112
- {
113
- return false;
114
- }
115
-
116
-
117
-
118
- /**
119
- * the default prefire event
120
- *
121
- * must be called during trigger_event
122
- * restore existing object for session
123
- */
124
- void MilterEvent::Prefire (Isolate *isolate, HandleScope &scope)
125
- {
126
- this->envelope = Local<Object>::New(isolate, this->fi_envelope->object);
127
- }
128
-
129
- /**
130
- * the default postfire event (do nothing)
131
- */
132
- void MilterEvent::Postfire (Isolate *isolate)
133
- {
134
- // naaaah-thing
135
- }
136
-
137
-
138
- /**
139
- */
140
- void MilterEvent::Fire (Isolate *isolate, bindings_t *local)
141
- {
142
- HandleScope scope (isolate);
143
- this->Prefire(isolate, scope);
144
-
145
- Envelope *env = ObjectWrap::Unwrap<Envelope>(this->envelope);
146
- env->SetCurrentEvent(this);
147
- env->SetMilterContext(this->smfi_context);
148
-
149
- this->FireWrapper(isolate, local);
150
- }
151
-
152
-
153
- /**
154
- * shared wrapper for each event implementation
155
- * XXX: maybe inline this for maximum gofast
156
- */
157
- void MilterEvent::DoFCall (Isolate *isolate, Persistent<Function> &pfunc, unsigned int argc, Local<Value> *argv)
158
- {
159
- TryCatch tc;
160
- Local<Function> fcall = Local<Function>::New(isolate, pfunc);
161
- fcall->Call(isolate->GetCurrentContext()->Global(), argc, argv);
162
- if (tc.HasCaught())
163
- FatalException(tc);
164
- }
165
-
166
-
167
- /** MilterNegotiate ***********************************************************/
168
-
169
- MilterNegotiate::MilterNegotiate (envelope_t *env,
170
- unsigned long f0_,
171
- unsigned long f1_,
172
- unsigned long f2_,
173
- unsigned long f3_,
174
- unsigned long *pf0_,
175
- unsigned long *pf1_,
176
- unsigned long *pf2_,
177
- unsigned long *pf3_)
178
- : MilterEvent(env), f0(f0_), f1(f1_), f2(f2_), f3(f3_), pf0(pf0_), pf1(pf1_), pf2(pf2_), pf3(pf3_)
179
- {
180
- //
181
- }
182
-
183
- bool MilterNegotiate::IsNegotiate () const { return true; }
184
-
185
- void MilterNegotiate::Prefire (Isolate *isolate, HandleScope &scope)
186
- {
187
- this->envelope = Envelope::NewInstance(isolate, scope);
188
- this->fi_envelope->object.Reset(isolate, this->envelope);
189
- }
190
-
191
- /**
192
- */
193
- void MilterNegotiate::FireWrapper (Isolate *isolate, bindings_t *local)
194
- {
195
- const unsigned argc = 5;
196
- Local<Value> argv[argc] = {
197
- this->envelope,
198
- Number::New(isolate, this->f0),
199
- Number::New(isolate, this->f1),
200
- Number::New(isolate, this->f2),
201
- Number::New(isolate, this->f3)
202
- };
203
- this->DoFCall(isolate, local->fcall.negotiate, argc, argv);
204
- }
205
-
206
-
207
- /**
208
- */
209
- void MilterNegotiate::Negotiate(unsigned long f0, unsigned long f1, unsigned long f2, unsigned long f3)
210
- {
211
- *pf0 = f0;
212
- *pf1 = f1;
213
- *pf2 = f2;
214
- *pf3 = f3;
215
- }
216
-
217
-
218
- /** MilterConnect *************************************************************/
219
-
220
- MilterConnect::MilterConnect (envelope_t *env, const char *host, sockaddr_in *sa)
221
- : MilterEvent(env), sz_host(host)
222
- {
223
- inet_ntop(AF_INET, &((sockaddr_in *)sa)->sin_addr, sz_addr, sizeof sz_addr);
224
- }
225
-
226
- bool MilterConnect::IsConnect () const { return true; }
227
-
228
- /**
229
- */
230
- void MilterConnect::FireWrapper (Isolate *isolate, bindings_t *local)
231
- {
232
- const unsigned argc = 3;
233
- Local<Value> argv[argc] = {
234
- this->envelope,
235
- String::NewFromUtf8(isolate, this->sz_host),
236
- String::NewFromUtf8(isolate, this->sz_addr)
237
- };
238
- this->DoFCall(isolate, local->fcall.connect, argc, argv);
239
- }
240
-
241
- const char *MilterConnect::Host() const { return this->sz_host; }
242
-
243
- const char *MilterConnect::Address() const { return this->sz_addr; }
244
-
245
-
246
- /** MilterUnknown *************************************************************/
247
-
248
- MilterUnknown::MilterUnknown (envelope_t *env, const char *command)
249
- : MilterEvent(env), sz_command(command) { }
250
-
251
- /**
252
- */
253
- void MilterUnknown::FireWrapper (Isolate *isolate, bindings_t *local)
254
- {
255
- const unsigned argc = 2;
256
- Local<Value> argv[argc] = {
257
- this->envelope,
258
- String::NewFromUtf8(isolate, this->sz_command)
259
- };
260
- this->DoFCall(isolate, local->fcall.unknown, argc, argv);
261
- }
262
-
263
-
264
- /** MilterHELO ***************************************************************/
265
-
266
- MilterHELO::MilterHELO (envelope_t *env, const char *helo)
267
- : MilterEvent(env), sz_helo(helo) { }
268
-
269
-
270
- void MilterHELO::FireWrapper (Isolate *isolate, bindings_t *local)
271
- {
272
- const unsigned argc = 2;
273
- Local<Value> argv[argc] = {
274
- this->envelope,
275
- String::NewFromUtf8(isolate, sz_helo)
276
- };
277
- this->DoFCall(isolate, local->fcall.helo, argc, argv);
278
- }
279
-
280
-
281
- /** MilterMAILFROM ***************************************************************/
282
-
283
- MilterMAILFROM::MilterMAILFROM (envelope_t *env, char **argv)
284
- : MilterEvent(env), szpp_argv(argv) { }
285
-
286
- void MilterMAILFROM::FireWrapper (Isolate *isolate, bindings_t *local)
287
- {
288
- const unsigned argc = 2;
289
- Local<Value> argv[argc] = {
290
- this->envelope,
291
- String::NewFromUtf8(isolate, szpp_argv[0])
292
- };
293
- this->DoFCall(isolate, local->fcall.envfrom, argc, argv);
294
- }
295
-
296
-
297
- /** MilterRCPTTO ***************************************************************/
298
-
299
- MilterRCPTTO::MilterRCPTTO (envelope_t *env, char **argv)
300
- : MilterEvent(env), szpp_argv(argv) { }
301
-
302
- void MilterRCPTTO::FireWrapper (Isolate *isolate, bindings_t *local)
303
- {
304
- const unsigned argc = 2;
305
- Local<Value> argv[argc] = {
306
- this->envelope,
307
- String::NewFromUtf8(isolate, szpp_argv[0])
308
- };
309
- this->DoFCall(isolate, local->fcall.envrcpt, argc, argv);
310
- }
311
-
312
-
313
- /** MilterDATA ***************************************************************/
314
-
315
- MilterDATA::MilterDATA (envelope_t *env) : MilterEvent(env) { }
316
-
317
- void MilterDATA::FireWrapper (Isolate *isolate, bindings_t *local)
318
- {
319
- const unsigned argc = 1;
320
- Local<Value> argv[argc] = {
321
- this->envelope,
322
- };
323
- this->DoFCall(isolate, local->fcall.data, argc, argv);
324
- }
325
-
326
-
327
- /** MilterHeader ***************************************************************/
328
-
329
- MilterHeader::MilterHeader (envelope_t *env, const char *name, const char *value)
330
- : MilterEvent(env), sz_name(name), sz_value(value) { }
331
-
332
- void MilterHeader::FireWrapper (Isolate *isolate, bindings_t *local)
333
- {
334
- const unsigned argc = 3;
335
- Local<Value> argv[argc] = {
336
- this->envelope,
337
- String::NewFromUtf8(isolate, sz_name),
338
- String::NewFromUtf8(isolate, sz_value)
339
- };
340
- this->DoFCall(isolate, local->fcall.header, argc, argv);
341
- }
342
-
343
-
344
- /** MilterEndHeaders ***************************************************************/
345
-
346
- MilterEndHeaders::MilterEndHeaders (envelope_t *env) : MilterEvent(env) { }
347
-
348
- void MilterEndHeaders::FireWrapper (Isolate *isolate, bindings_t *local)
349
- {
350
- const unsigned argc = 1;
351
- Local<Value> argv[argc] = {
352
- this->envelope,
353
- };
354
- this->DoFCall(isolate, local->fcall.eoh, argc, argv);
355
- }
356
-
357
-
358
- /** MilterMessageData ***************************************************************/
359
-
360
- MilterMessageData::MilterMessageData (envelope_t *env, const unsigned char *buf, const size_t len)
361
- : MilterEvent(env), buf(buf), len(len) { }
362
-
363
- void MilterMessageData::FireWrapper (Isolate *isolate, bindings_t *local)
364
- {
365
- char *nodebuf = new char[len];
366
- memcpy(nodebuf, buf, len);
367
- const unsigned argc = 3;
368
- Local<Value> argv[argc] = {
369
- this->envelope,
370
- Buffer::Use(isolate, nodebuf, len),
371
- Number::New(isolate, len)
372
- };
373
- this->DoFCall(isolate, local->fcall.body, argc, argv);
374
- }
375
-
376
-
377
- /** MilterEndMessage ***************************************************************/
378
-
379
- MilterEndMessage::MilterEndMessage (envelope_t *env) : MilterEvent(env) { }
380
-
381
- bool MilterEndMessage::IsEndMessage () const { return true; }
382
-
383
- void MilterEndMessage::FireWrapper (Isolate *isolate, bindings_t *local)
384
- {
385
- const unsigned argc = 1;
386
- Local<Value> argv[argc] = {
387
- this->envelope,
388
- };
389
- this->DoFCall(isolate, local->fcall.eom, argc, argv);
390
- }
391
-
392
-
393
- /** MilterAbort ***************************************************************/
394
-
395
- MilterAbort::MilterAbort (envelope_t *env) : MilterEvent(env) { }
396
-
397
- void MilterAbort::FireWrapper (Isolate *isolate, bindings_t *local)
398
- {
399
- const unsigned argc = 1;
400
- Local<Value> argv[argc] = {
401
- this->envelope,
402
- };
403
- this->DoFCall(isolate, local->fcall.abort, argc, argv);
404
- }
405
-
406
-
407
- /** MilterClose ***************************************************************/
408
-
409
- MilterClose::MilterClose (envelope_t *env) : MilterEvent(env) { }
410
-
411
- void MilterClose::FireWrapper (Isolate *isolate, bindings_t *local)
412
- {
413
- const unsigned argc = 1;
414
- Local<Value> argv[argc] = {
415
- this->envelope,
416
- };
417
- this->DoFCall(isolate, local->fcall.close, argc, argv);
418
- }
419
-
420
-
421
- /**
422
- */
423
- void MilterClose::Postfire (Isolate *isolate)
424
- {
425
- this->fi_envelope->object.Reset();
426
- }
package/src/events.h DELETED
@@ -1,303 +0,0 @@
1
- /**
2
- * contextual information for events
3
- * it is apparently fashionable in node.js-speak to call these "batons"
4
- *
5
- * do you like 1980s C style standards for variable names? no? haha!
6
- *
7
- * do you like encapsulation? :)
8
- *
9
- * hahaha!
10
- */
11
-
12
- #ifndef MILTER_EVENTS_H
13
- #define MILTER_EVENTS_H
14
-
15
- #include <node.h>
16
- #include <v8.h>
17
- #include <arpa/inet.h>
18
- #include <pthread.h>
19
- #include <assert.h>
20
- #include "milter.h"
21
-
22
- using namespace v8;
23
- using namespace node;
24
-
25
-
26
- /**
27
- * base class. something-something inheritance
28
- */
29
- class MilterEvent
30
- {
31
- public:
32
- /**
33
- * called by node worker to begin the main event work
34
- */
35
- void Fire (Isolate *isolate, bindings_t *local);
36
-
37
- /**
38
- * just a mutex; control of the event
39
- */
40
- bool Lock ();
41
- bool Unlock ();
42
-
43
- /**
44
- * called by libmilter worker to wait until event is ready
45
- */
46
- bool Wait ();
47
-
48
- /**
49
- * allow an event callback to set a milter result flag
50
- * returns true if libmilter thread is signaled
51
- */
52
- bool Done (Isolate *isolate, int retval);
53
-
54
- /**
55
- * allow an envelope function to ensure it is not being called at the wrong
56
- * milter stage.
57
- * most of them are allowed during EOM only.
58
- */
59
- virtual bool IsNegotiate () const;
60
- virtual bool IsConnect() const;
61
- virtual bool IsEndMessage () const;
62
-
63
- // linklist junk
64
- void Append (MilterEvent *ev);
65
- void Detatch ();
66
- MilterEvent *Next () const;
67
-
68
- /**
69
- *
70
- */
71
- void SetMilterContext (SMFICTX *context);
72
-
73
- // not sure if needed
74
- bool IsDone () const;
75
- int Result () const;
76
-
77
- virtual ~MilterEvent ();
78
-
79
- protected:
80
- explicit MilterEvent (envelope_t *env);
81
-
82
- /**
83
- * each event type must select its associated event callback from
84
- *
85
- */
86
- virtual void FireWrapper (Isolate *isolate, bindings_t *local) = 0;
87
- virtual void Prefire (Isolate *isolate, HandleScope &scope);
88
- virtual void Postfire (Isolate *isolate);
89
-
90
- /**
91
- * each event type must implement
92
- */
93
- void DoFCall (Isolate *isolate, Persistent<Function> &pfunc, unsigned int argc, Local<Value> *argv);
94
-
95
- Local<Object> envelope;
96
-
97
- envelope_t *fi_envelope;
98
- SMFICTX *smfi_context;
99
-
100
- private:
101
- MilterEvent *ev_next;
102
- bool is_done;
103
- int fi_retval;
104
-
105
- pthread_mutex_t pt_lock;
106
- pthread_cond_t pt_ready;
107
- };
108
-
109
-
110
- /**
111
- */
112
- class MilterNegotiate : public MilterEvent
113
- {
114
- public:
115
- MilterNegotiate (envelope_t *env,
116
- unsigned long f0_, unsigned long f1_, unsigned long f2_, unsigned long f3_,
117
- unsigned long *pf0_, unsigned long *pf1_, unsigned long *pf2_, unsigned long *pf3_);
118
- void FireWrapper (Isolate *isolate, bindings_t *local);
119
-
120
- bool IsNegotiate () const;
121
-
122
- /**
123
- * change negotiation settings
124
- */
125
- void Negotiate (unsigned long f0, unsigned long f1, unsigned long f2, unsigned long f3);
126
-
127
- protected:
128
- void Prefire (Isolate *isolate, HandleScope &scope);
129
-
130
- private:
131
- unsigned long f0;
132
- unsigned long f1;
133
- unsigned long f2;
134
- unsigned long f3;
135
- unsigned long *pf0;
136
- unsigned long *pf1;
137
- unsigned long *pf2;
138
- unsigned long *pf3;
139
- };
140
-
141
-
142
- /**
143
- * new connection established
144
- *
145
- * each connection is associated 1:1 with an envelope, created by this event,
146
- * which lives until either the close or abort event, that allows the node.js
147
- * script kiddie to associate each individual callback with the correct mail
148
- * session.
149
- */
150
- class MilterConnect : public MilterEvent
151
- {
152
- public:
153
- MilterConnect (envelope_t *env, const char *host, sockaddr_in *sa);
154
- void FireWrapper (Isolate *isolate, bindings_t *local);
155
-
156
- bool IsConnect() const;
157
-
158
- const char *Host() const;
159
- const char *Address() const;
160
-
161
- protected:
162
-
163
- private:
164
- const char *sz_host;
165
- char sz_addr[INET6_ADDRSTRLEN+1];
166
- };
167
-
168
-
169
- /**
170
- */
171
- class MilterUnknown : public MilterEvent
172
- {
173
- public:
174
- MilterUnknown (envelope_t *env, const char *command);
175
- void FireWrapper (Isolate *isolate, bindings_t *local);
176
-
177
- private:
178
- const char *sz_command;
179
- };
180
-
181
-
182
- /**
183
- */
184
- class MilterHELO : public MilterEvent
185
- {
186
- public:
187
- MilterHELO (envelope_t *env, const char *helo);
188
- void FireWrapper (Isolate *isolate, bindings_t *local);
189
-
190
- private:
191
- const char *sz_helo;
192
- };
193
-
194
-
195
- /**
196
- */
197
- class MilterMAILFROM : public MilterEvent
198
- {
199
- public:
200
- MilterMAILFROM (envelope_t *env, char **argv);
201
- void FireWrapper (Isolate *isolate, bindings_t *local);
202
-
203
- private:
204
- char **szpp_argv;
205
- };
206
-
207
-
208
- /**
209
- */
210
- class MilterRCPTTO : public MilterEvent
211
- {
212
- public:
213
- MilterRCPTTO (envelope_t *env, char **argv);
214
- void FireWrapper (Isolate *isolate, bindings_t *local);
215
-
216
- private:
217
- char **szpp_argv;
218
- };
219
-
220
-
221
- /**
222
- */
223
- class MilterDATA : public MilterEvent
224
- {
225
- public:
226
- MilterDATA (envelope_t *env);
227
- void FireWrapper (Isolate *isolate, bindings_t *local);
228
- };
229
-
230
-
231
- /**
232
- */
233
- class MilterHeader : public MilterEvent
234
- {
235
- public:
236
- MilterHeader (envelope_t *env, const char *name, const char *value);
237
- void FireWrapper (Isolate *isolate, bindings_t *local);
238
-
239
- private:
240
- const char *sz_name;
241
- const char *sz_value;
242
- };
243
-
244
-
245
- /**
246
- */
247
- class MilterEndHeaders : public MilterEvent
248
- {
249
- public:
250
- MilterEndHeaders (envelope_t *env);
251
- void FireWrapper (Isolate *isolate, bindings_t *local);
252
- };
253
-
254
-
255
- /**
256
- */
257
- class MilterMessageData : public MilterEvent
258
- {
259
- public:
260
- MilterMessageData (envelope_t *env, const unsigned char *buf, const size_t len);
261
- void FireWrapper (Isolate *isolate, bindings_t *local);
262
-
263
- private:
264
- const unsigned char *buf;
265
- const size_t len;
266
- };
267
-
268
-
269
- /**
270
- */
271
- class MilterEndMessage : public MilterEvent
272
- {
273
- public:
274
- MilterEndMessage (envelope_t *env);
275
- void FireWrapper (Isolate *isolate, bindings_t *local);
276
-
277
- bool IsEndMessage () const;
278
- };
279
-
280
-
281
- /**
282
- */
283
- class MilterAbort : public MilterEvent
284
- {
285
- public:
286
- MilterAbort (envelope_t *env);
287
- void FireWrapper (Isolate *isolate, bindings_t *local);
288
- };
289
-
290
-
291
- /**
292
- */
293
- class MilterClose : public MilterEvent
294
- {
295
- public:
296
- MilterClose (envelope_t *env);
297
- void FireWrapper (Isolate *isolate, bindings_t *local);
298
- protected:
299
- void Postfire (Isolate *isolate);
300
- };
301
-
302
-
303
- #endif