-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.titleop.cpx
423 lines (399 loc) · 9.61 KB
/
group.titleop.cpx
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
/*
Copyright (c) 2008 Radu Andrei Stefan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
interface
void inline intpushback(int *d, int &pos, int val, int len=512);
void inline charpushback(char *d, int &pos, char val, int len=2048);
void inline charappend(char *d, int &pos, const char *val, int len=2048);
implementation
uses group;
string group::expandtitle(string compressed, vector<int> data, int start, int ignl, int backsp)
{
char proc[2048];
int proclen=0;
int i;
int startpar=start;
int total=0;
int parcnt=0;
for (i=0; i<(int) compressed.length(); i++)
{
char ch=compressed[i];
if (ch<0) total++;
if (ch<0 && ch!='\x83') startpar++;
if (ch=='\x83') parcnt++;
}
if (total+start>(int) data.size())
{
fprintf(stderr,"Problem expanding name: ran out of data %d/%d\n"
,start+total,data.size());
return compressed;
}
for (i=0; i<(int) compressed.length(); i++)
{
int n1,n2,flg;
char ch=compressed[i];
if (ch=='&' && backsp)
{
int pos=i+1;
int val;
if (matchstr(compressed,pos,"SL;"))
{
charpushback(proc, proclen, '/');
i=pos-1;
continue;
}
if (matchstr(compressed,pos,"BS;"))
{
charpushback(proc, proclen, '\\');
i=pos-1;
continue;
}
if (matchstr(compressed,pos,"T;"))
{
charpushback(proc, proclen, '\t');
i=pos-1;
continue;
}
if (matchhextwo(compressed,pos,val))
{
matchch(compressed,pos,';');
charpushback(proc, proclen, val);
continue;
}
}
if (ch>=0)
{
charpushback(proc, proclen, ch);
continue;
}
if (ch=='\x82')
{
unpacktwo(data[start++],n1,n2,flg);
charpushback(proc, proclen, '[');
charappend(proc, proclen, unpackntos(n1).c_str());
charpushback(proc, proclen, '/');
charappend(proc, proclen, unpackntos(n2).c_str());
charpushback(proc, proclen, ']');
continue;
}
if (ch=='\x83')
{
parcnt--;
if (parcnt==0 && ignl) continue;
unpacktwo(data[startpar++],n1,n2,flg);
if (flg) charappend(proc, proclen, " yEnc ");
charpushback(proc, proclen, '(');
charappend(proc, proclen, unpackntos(n1).c_str());
charpushback(proc, proclen, '/');
charappend(proc, proclen, unpackntos(n2).c_str());
charpushback(proc, proclen, ')');
continue;
}
if (ch=='\x84')
{
charpushback(proc, proclen, '.');
charappend(proc, proclen, expandext(data[start++]).c_str());
continue;
}
if (ch=='\x85')
{
charappend(proc, proclen, unpackntos(data[start++]).c_str());
continue;
}
}
proc[proclen]=0;
string res=proc;
return res;
}
string group::expandtitle(fileinfo &cf, int backsp)
{
return expandtitle(artnames.v[cf.info[1]], cf.info, 2, 1, backsp);
}
string group::expandtitle(int fid, int backsp)
{
fileinfo &cf=fis.fil[fid];
return expandtitle(artnames.v[cf.info[1]], cf.info, 2, 1, backsp);
}
int parsedate(string date)
{
return 0;
}
void inline intpushback(int *d, int &pos, int val, int len)
{
if (pos>=len) return;
d[pos++]=val;
}
void inline charpushback(char *d, int &pos, char val, int len)
{
if (pos>=len-1) return;
d[pos++]=val;
d[pos]=0;
}
void inline charappend(char *d, int &pos, const char *val, int len)
{
while (val[0]!=0)
{
if (pos>=len-1) break;
d[pos++]=val[0];
val++;
}
d[pos]=0;
}
void group::processtitle(string title, string &vprocessed, vector<int> &vdata, int &haspart)
{
char proc[2048];
int data[512];
int datp[512];
int proclen=0;
int datalen=0;
int datplen=0;
int i;
char buf[10];
char cuch=0,pvch;
int yencbefore=0;
int tmp;
haspart=0;
/*
int located=title.find("ftd 613075",0);
if (located>=0)
printf(" ");
*/
for (i=0; i<(int) title.length(); i++)
{
pvch=cuch;
cuch=title[i];
if (cuch<32) // signed
{
sprintf(buf,"&%02x;",(unsigned char) cuch);
charappend(proc,proclen,buf);
continue;
}
if (cuch=='[')
{
int n1,n2,d1,d2,pos=i+1;
if ( matchnr(title,pos,n1,d1)
&& (d1<5)
&& matchch(title,pos,'/')
&& matchnr(title,pos,n2,d2)
&& (d2<5)
&& matchch(title,pos,']')
)
{
i=pos-1;
haspart=0;
charpushback(proc,proclen,'\x82');
intpushback(data,datalen,packnumbers(n1,d1,n2,d2,0));
continue;
}
}
if (cuch=='(')
{
int n1,n2,pos=i+1;
if ( matchnrpk(title,pos,n1)
&& (n1<0x8000)
&& matchch(title,pos,'/')
&& matchnrpk(title,pos,n2)
&& (n2<0x8000)
&& matchch(title,pos,')')
)
{
i=pos-1;
haspart=1;
charpushback(proc,proclen,'\x83');
intpushback(datp,datplen,packtwo(n1,n2,yencbefore));
continue;
}
}
yencbefore=0;
tmp=i;
if (cuch==' ') if (matchstr(title,tmp," yEnc ("))
{
i+=5;
yencbefore=1;
continue;
}
if (cuch=='.')
{
int extid,pos=i+1;
if (matchext(title,pos,extid))
{
i=pos-1;
charpushback(proc,proclen,'\x84');
intpushback(data,datalen,extid);
continue;
}
}
if (isdigit(cuch) && !isdigit(pvch))
{
int tmp=i;
int n1,n2,p2,p3,p0,flg,vs;
char ch;
matchnrpk(title,tmp,n1);
p2=tmp;
if (pvch=='"' && matchch(title,p2,'"'))
{
i=p2-1;
charpushback(proc,proclen,'\x85');
charpushback(proc,proclen,'"');
intpushback(data,datalen,n1);
continue;
}
ch=0;
p0=p2=tmp;
if (matchvset(title,p2,"/\0 / \0/ \0\0",flg)) ch='\x86';
else if (matchvset(title,p2,"-\0 - \0..\0...\0\0",flg)) ch='\x87';
else if (matchvset(title,p2,")-(\0\0",flg)) ch='\x87';
else if (matchvsetcs(title,p2," of \0 van \0/\0of \0of\0\0",vs,flg)) ch='\x88'+vs;
if (ch)
{
p3=p2;
if (matchnrpk(title,p2,n2) && n1<0x8000 && n2<0x8000)
{
i=p2-1;
charpushback(proc,proclen,'\x85');
while (p0<p3) charpushback(proc,proclen,title[p0++]);
charpushback(proc,proclen,'\x85');
intpushback(data,datalen,n1);
intpushback(data,datalen,n2);
continue;
}
}
}
if (cuch=='/') { charappend(proc,proclen,"&SL;"); continue; }
if (cuch=='\\') { charappend(proc,proclen,"&BS;"); continue; }
if (cuch=='\t') { charappend(proc,proclen,"&T;"); continue; }
charpushback(proc,proclen,cuch);
}
vprocessed=proc;
vdata=vector<int>(datalen+datplen);
for (i=0; i<datalen; i++) vdata[i]=data[i];
for (i=0; i<datplen; i++) vdata[datalen+i]=datp[i];
}
string group::msgidcleanup(string postid, int &part, int &pof)
{
int pos=0,cs;
string res=postid;
part=-1;
pof=-1;
matchch(postid,pos,'<');
do {
if (!matchstrvcase(postid,pos,"part",cs)) continue;
if (!matchnr(postid,pos,part,cs)) continue;
if (!matchstrvcase(postid,pos,"of",cs)) continue;
if (!matchnr(postid,pos,pof,cs)) continue;
matchch(postid,pos,'.');
res=postid.substr(pos,postid.length());
pos=0;
} while (0);
if (pos==1) res=postid.substr(pos,postid.length());
int lsplit=res.rfind("@",res.length());
if (lsplit>6) res=res.substr(0,lsplit);
return res;
}
int suitablefilename(string s)
{
int i,fnd,suit;
int sl=s.length();
static const char *tofind="rar\0zip\0par\0vol\0avi\0mp3\0cue\0txt\0nzb\0nfo\0txt\0ace\0flac\0"
"htm\0wav\0wmv\0\0";
if (s.length()<2) return 0;
fnd=0;
suit=0;
for (i=0; i<sl; i++)
{
char ch=s[i];
if (ch=='.')
{
suit+=1;
if (i==sl-4 || i==sl-5)
suit+=10;
int pos=i+1;
int q1,q2;
if (matchvsetcs(s,pos,tofind,q1,q2)) pos+=20;
}
if (!isdigit(ch) && !isspace(ch))
{
fnd=1;
}
}
if (!fnd) return 0;
return suit+1;
}
string group::escapefilename(const string &s)
{
char proc[2048];
int proclen=0;
int i;
for (i=0; i<(int) s.length(); i++)
{
char ch=s[i];
if (ch<32) // signed
{
char buf[8];
sprintf(buf,"&%02x;",(unsigned char) ch);
charappend(proc,proclen,buf);
continue;
}
if (ch=='/') { charappend(proc,proclen,"&SL;"); continue; }
if (ch=='\\') { charappend(proc,proclen,"&BS;"); continue; }
if (ch=='\t') { charappend(proc,proclen,"&T;"); continue; }
charpushback(proc,proclen,ch);
}
proc[proclen]=0;
return proc;
}
int group::producefilename(const string &s)
{
string res;
string opt;
res=s;
const int LCUT=768;
int quotpos;
int bestsuitablequoted=0;
//int sl=s.length();
quotpos=s.find("\"",0);
while (quotpos>=0)
{
int qp2=s.find("\"",quotpos+1);
if (qp2<quotpos+1) break;
string res2=s.substr(quotpos+1,qp2-quotpos-1);
int suit=suitablefilename(res2);
if (suit>bestsuitablequoted)
{
bestsuitablequoted=suit;
res=res2;
}
quotpos=s.find("\"",qp2+1);
}
res=escapefilename(res);
if ((int) res.length()>LCUT) res=res.substr(0,5)+".."+res.substr(res.length()-LCUT+7,LCUT-7);
opt=res;
int cnt=112;
int resl=res.length();
int extp=resl;
int i;
for (i=resl-1; i>=0 && i>resl-12; i--)
if (res[i]=='.') { extp=i; }
if (extp==resl) extp=0;
while (filenames.find(opt,0)>=0)
{
opt=res.substr(0,extp)+".("+unpackntos(cnt)+")"+res.substr(extp,resl);
cnt++;
}
return filenames.find(opt);
}