File: mail_notify.sql

package info (click to toggle)
virtuoso-opensource 7.2.5.1%2Bdfsg1-0.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 285,240 kB
  • sloc: ansic: 641,220; sql: 490,413; xml: 269,570; java: 83,893; javascript: 79,900; cpp: 36,927; sh: 31,653; cs: 25,702; php: 12,690; yacc: 10,227; lex: 7,601; makefile: 7,129; jsp: 4,523; awk: 1,697; perl: 1,013; ruby: 1,003; python: 326
file content (257 lines) | stat: -rw-r--r-- 7,768 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
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
--
--  $Id$
--
--  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
--  project.
--
--  Copyright (C) 1998-2018 OpenLink Software
--
--  This project 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; only version 2 of the License, dated June 1991.
--
--  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, write to the Free Software Foundation, Inc.,
--  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--

create procedure nntpf_encode_subject (in txt any)
{
  declare enc any;
  txt := wa_wide_to_utf8 (txt);
  txt := trim (txt);
  enc :=  encode_base64 (txt);
  enc := replace (enc, '\r\n', '');
  txt := concat ('=?UTF-8?B?', enc, '?=');
  return txt;
};

create procedure nntpf_transform_message (in sender varchar, in recv varchar, in grp varchar, in subj varchar, in dat varchar,
    in _repl varchar, inout _body any, inout head any, inout ses any)
{
  declare hdr, h any;
  declare body varchar;

  string_output_flush (ses);
  http (sprintf ('Sender: %s\r\n', sender), ses);
  http (sprintf ('To: %s\r\n', recv), ses);
  http (sprintf ('Reply-To: %s\r\n', _repl), ses);

  hdr := deserialize (head);
  h := hdr[0];

  for (declare i int, i := 0; i < length (h); i := i + 2)
     {
       declare kwd varchar;
       kwd := lower (h[i]);
       if (kwd not in ('to', 'reply-to', 'newsgroups', 'charset'))
	 {
	   http (h[i]||': ', ses);

	   if (kwd <> 'subject')
	     http (h[i+1], ses);
	   else
	     {
	       declare orig varchar;
	       orig := h[i+1];
               nntpf_decode_subj (orig);
	       if (lower (orig) like 're:%')
		 {
		   orig := substring (orig, 4, length (orig));
		   orig := 'Re: [' || grp || '] ' || nntpf_encode_subject (orig);
		 }
	       else
		 {
		   orig := '[' || grp || '] ' || nntpf_encode_subject (orig);
		 }
	       http (orig, ses);
	     }

	   if (kwd = 'content-type')
	     {
	       declare enc any;
	       enc := get_keyword_ucase ('charset', h);
	       if (enc is not null)
		 http (sprintf ('; charset=%s', enc), ses);
	     }
	   http ('\r\n', ses);
	 }
     }

  http ('\r\n', ses);

  body := subseq (_body, hdr[1][0], hdr[1][1]);

  http (body, ses);

};


create procedure nntpf_send_message (in mail_server varchar, in sender varchar, in recv varchar, inout mail_messages any)
{
  declare body varchar;
  declare continue handler for sqlstate '*' { dbg_obj_print ('An error during smtp_send');  };

  body := string_output_string (mail_messages);
  string_output_flush (mail_messages);

--  dbg_obj_print (body);

  commit work;
--  dbg_obj_print ('sending...');
  smtp_send (mail_server, sender, recv, body);
--  dbg_obj_print ('done...');
};


create procedure NNTPF_MAIL_NOTIFICATIONS ()
{
  declare mail_server, mail_messages, ses, dat, sender, _repl, _domain any;
  declare inx int;

  if ((select WS_USE_DEFAULT_SMTP from WA_SETTINGS) = 1)
    {
      mail_server := cfg_item_value(virtuoso_ini_path(), 'HTTPServer', 'DefaultMailServer');
    }
  else
    {
      mail_server := (select WS_SMTP from WA_SETTINGS);
    }

  _domain := (select top 1 WS_DEFAULT_MAIL_DOMAIN from WA_SETTINGS);
  if (not length (_domain))
    _domain := sys_stat ('st_host_name');

  dat := sprintf ('Date: %s\r\n', date_rfc1123 (now ()));
  sender := (select top 1 U_E_MAIL from DB.DBA.SYS_USERS where U_NAME = 'dav');
  mail_messages := string_output ();
  ses := string_output ();

  for select NS_MAIL as _ns_mail, NS_GROUP as _ns_group, NS_THREAD_ID as _ns_thread_id, NS_TYPE as _ns_type, NS_DIGEST as _ns_digest, NS_TS as _ns_ts, NG_TYPE as _ng_type, NG_NAME as _ng_name from NNTPF_SUBS, NEWS_GROUPS
    where NS_GROUP = NG_GROUP and NS_TS < now () do
    {
      inx := 0;
      _repl := _ng_name || '@' || _domain;
       if (_ns_type = 1)
	 {
	   http (dat, ses);
	   http (sprintf ('Sender: %s\r\n', sender), ses);
	   http (sprintf ('To: %s\r\n', _ns_mail), ses);
	   --http (sprintf ('Reply-To: %s\r\n', _repl), ses);
	   http (sprintf ('Subject: [%s] Digest #%d\r\n', _ng_name, _NS_DIGEST), ses);
	   http (sprintf ('Content-Type: text/plain\r\n\r\n'), ses);
	 }
       for select FTHR_MESS_ID, FTHR_REFER, FTHR_SUBJ, FTHR_DATE, FTHR_MESS_DETAILS, FTHR_TOPIC_ID from NNFE_THR where
	  FTHR_GROUP = _ns_group and FTHR_DATE > _ns_ts do
	  {
	      declare dummy any;

	      if (_ns_thread_id = '' or FTHR_TOPIC_ID = _ns_thread_id)
	        {
	          declare body, head any;
	          select NM_BODY, NM_HEAD into body, head from NEWS_MSG where
	      	  NM_ID = FTHR_MESS_ID and NM_TYPE = _ng_type;
	          if (_ns_type = 0) -- individual mails
	            {
		      nntpf_transform_message (sender, _ns_mail, _ng_name, FTHR_SUBJ, dat, _repl, body, head, mail_messages);
	              nntpf_send_message (mail_server, sender, _ns_mail, mail_messages);
	            }
		  else
		    {
		      declare _from any;
		      _from := deserialize (FTHR_MESS_DETAILS);
		      _from := _from [0];

		      inx := inx + 1;
		      http (sprintf ('Message #%d\r\n', inx), mail_messages);
		      http (sprintf ('   Subject: %s\r\n', FTHR_SUBJ), mail_messages);
		      http (sprintf ('   From: %s\r\n', _from), mail_messages);
		      http (sprintf ('   Date: %s\r\n', date_rfc1123 (FTHR_DATE)), mail_messages);
		      http ('---------------------------\r\n\r\n', mail_messages);
		    }
	        }
	  }
       if (_ns_type = 1 and inx > 0)
         {
	   http (sprintf ('There are %d messages in this issue.\r\nTopics in this digest: \r\n\r\n', inx), ses);
	   http (string_output_string (mail_messages), ses);
	   nntpf_send_message (mail_server, sender, _ns_mail, ses);
         }
       string_output_flush (ses);
       string_output_flush (mail_messages);
       -- enable the bellow
       update NNTPF_SUBS set NS_TS = now (), NS_DIGEST = _ns_digest + 1 where
         NS_MAIL = _ns_mail and NS_GROUP = _ns_group and NS_THREAD_ID = _ns_thread_id;
    }
};

-- !!! check various flags etc.
create procedure NNTPF_MAIL_VALIDATE (in gname varchar)
{
  declare grp any;
  declare exit handler for not found
    {
      return 0;
    };
  select NG_GROUP into grp from NEWS_GROUPS where NG_NAME = gname;
  if (ns_rest (grp, 1) = 0)
    return 0;
  result ('__news_group:' || gname);
  return 1;
};

create procedure NNTPF_NEW_MAIL (in gname varchar, in _msg any)
{
  declare gid, tree, head, _start, _end, ses any;

  if (gname not like '__news_group:%')
    return 0;

  gname := substring (gname, 14, length (gname));

  declare exit handler for not found
    {
      return 0;
    };
  select NG_GROUP into gid from NEWS_GROUPS where NG_NAME = gname;

  -- XXX: insert the message into the news group
  tree := mime_tree (_msg);
  head := tree[0];
  _start := tree[1][0];
  _end :=   tree[1][1];

  ses := string_output ();

  http (sprintf ('Newsgroups: %s\r\n', gname), ses);

  --for (declare int i; i := 0; i < length (head); i := i + 2)
  --  {
  --    http (sprintf ('%s: %s\r\n', head[i], head[i+1]), ses);
  --  }

  --http ('\r\n', ses);
  --http (subseq (_msg, _start, _end), ses);

  http (_msg, ses);

  _msg := string_output_string (ses);

--  dbg_obj_print (gname, _msg);

  -- XXX: put handler here
  ns_post (_msg);

  return 1;
};

--nntpf_mail_notifications ();

insert soft DB.DBA.SYS_SCHEDULED_EVENT (SE_NAME, SE_START, SE_SQL, SE_INTERVAL)
		values ('Send the NNTP subscriptions', now(), 'NNTPF_MAIL_NOTIFICATIONS ()', 1440)
;