-
Notifications
You must be signed in to change notification settings - Fork 4
/
auction.c
1380 lines (1241 loc) · 38.7 KB
/
auction.c
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2002, 2003, 2004, Scott Nicol <[email protected]>
* All rights reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* for strcasestr prototype in string.h */
#define _GNU_SOURCE
#include "util.h"
#include "auction.h"
#include "buffer.h"
#include "http.h"
#include "html.h"
#include "history.h"
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#if defined(WIN32)
# define strcasecmp(s1, s2) stricmp((s1), (s2))
# define sleep(t) _sleep((t) * 1000)
# define strncasecmp(s1, s2, len) strnicmp((s1), (s2), (len))
#else
# include <unistd.h>
#endif
#define newRemain(aip) (aip->endTime - time(NULL) - aip->latency - options.bidtime)
#define TOKEN_FOUND_UIID (1 << 0)
#define TOKEN_FOUND_STOK (1 << 1)
#define TOKEN_FOUND_SRT (1 << 2)
#define TOKEN_FOUND_ALL (TOKEN_FOUND_UIID | TOKEN_FOUND_STOK | TOKEN_FOUND_SRT)
typedef struct _headerattr
{
char* name;
int occurence;
int direction;
int mandatory;
char* value;
} headerAttr_t, headerVal_t;
typedef enum searchType { st_attribute, st_value } searchType_t;
static time_t loginTime = 0; /* Time of last login */
static time_t defaultLoginInterval = 12 * 60 * 60; /* ebay login interval */
static int acceptBid(const char *pagename, auctionInfo *aip);
static int bid(auctionInfo *aip);
static int ebayLogin(auctionInfo *aip, time_t interval);
static int findAttr(char* src, size_t srcLen, headerAttr_t* attr);
static int forceEbayLogin(auctionInfo *aip);
static char *getIdInternal(char *s, size_t len);
static int getInfoTiming(auctionInfo *aip, time_t *timeToFirstByte);
static int getQuantity(int want, int available);
static int getVals(char* src, size_t srcLen, headerVal_t* vals);
static int makeBidError(const pageInfo_t *pageInfo, auctionInfo *aip);
static int match(memBuf_t *mp, const char *str);
static int parseBid(memBuf_t *mp, auctionInfo *aip);
static int preBid(auctionInfo *aip);
static int parsePreBid(memBuf_t *mp, auctionInfo *aip);
static int printMyItemsRow(char **row, int printNewline);
static int signinFormSearch(char* src, size_t srcLen, headerAttr_t* searchdef, searchType_t searchfor);
static int watch(auctionInfo *aip);
/*
* attempt to match some input, neglecting case, ignoring \r and \n.
* returns 0 on success, -1 on failure
*/
static int
match(memBuf_t *mp, const char *str)
{
const char *cursor;
int c;
log(("\n\nmatch(\"%s\")\n\n", str));
cursor = str;
while ((c = memGetc(mp)) != EOF) {
if (options.debug)
logChar(c);
if (tolower(c) == (int)*cursor) {
if (*++cursor == '\0') {
if (options.debug)
logChar(EOF);
return 0;
}
} else if (c != '\n' && c != '\r')
cursor = str;
}
if (options.debug)
logChar(EOF);
return -1;
}
static const char PAGEID[] = "Page id: ";
static const char PAGEID2[] = "pageId:";
static const char SRCID[] = "srcId: ";
static const char SRCID2[] = "id:\"";
/*
* Get page info, including pagename variable, page id and srcid comments.
*/
pageInfo_t *
getPageInfo(memBuf_t *mp)
{
const char *line;
pageInfo_t p = {NULL, NULL, NULL}, *pp;
int needPageName = 1;
int needPageId = 1;
int needSrcId = 1;
int needMore = 3;
char *title = NULL;
log(("getPageInfo():\n"));
memReset(mp);
while (needMore && (line = getTag(mp))) {
char *tmp;
if (!strcasecmp(line, "title") ||
!strcasecmp(line, "h1 class=\"page-title__main\"")) {
line = getNonTag(mp);
if (line) title = myStrdup(line);
continue;
}
if (0 == strncmp(line, "script", 6)) {
char *line2 = getNonTag(mp);
char *end;
if (needPageId && (tmp = strstr(line2, PAGEID2))) {
p.pageId = myStrdup(tmp + strlen(PAGEID2));
end = strchr(p.pageId, ',');
if (*end) {
*end = '\0';
}
--needMore;
--needPageId;
}
if (needSrcId && (tmp = strstr(line2, SRCID2))) {
p.srcId = myStrdup(tmp + strlen(SRCID2));
end = strchr(p.srcId, '"');
if (*end) {
*end = '\0';
}
--needMore;
--needSrcId;
}
continue;
} else if (0 == strcmp(line, "h1 class=\"page-title__main\"")) {
char *line2 = getNonTag(mp);
--needMore;
--needPageName;
p.pageName = myStrdup(line2);
continue;
} else if (strncmp(line, "!--", 3)) {
continue;
}
if (needPageName && (tmp = strstr(line, PAGENAME))) {
if ((tmp = getPageNameInternal(tmp))) {
--needMore;
--needPageName;
p.pageName = myStrdup(tmp);
}
} else if (needPageId && (tmp = strstr(line, PAGEID))) {
if ((tmp = getIdInternal(tmp, sizeof(PAGEID)))) {
--needMore;
--needPageId;
p.pageId = myStrdup(tmp);
}
} else if (needSrcId && (tmp = strstr(line, SRCID))) {
if ((tmp = getIdInternal(tmp, sizeof(SRCID)))) {
--needMore;
--needSrcId;
p.srcId = myStrdup(tmp);
}
}
}
if (needPageName && title) {
log(("using title as page name: %s", title));
p.pageName = title;
--needPageName;
--needMore;
title = NULL;
}
if (title) free(title);
log(("getPageInfo(): pageName = %s, pageId = %s, srcId = %s\n", nullStr(p.pageName), nullStr(p.pageId), nullStr(p.srcId)));
memReset(mp);
if (needMore == 3) {
return NULL;
}
pp = (pageInfo_t *)myMalloc(sizeof(pageInfo_t));
pp->pageName = p.pageName;
pp->pageId = p.pageId;
pp->srcId = p.srcId;
return pp;
}
static char *
getIdInternal(char *s, size_t len)
{
char *id = s + len - 1;
char *dash = strchr(id, '-');
if (!*dash) {
log(("getIdInternal(): Cannot find trailing dash: %s\n", id));
return NULL;
}
*dash = '\0';
log(("getIdInternal(): id = %s\n", id));
return id;
}
/*
* Free a pageInfo_t and it's internal members.
*/
void
freePageInfo(pageInfo_t *pp)
{
if (pp) {
free(pp->pageName);
free(pp->pageId);
free(pp->srcId);
free(pp);
}
}
/*
* Calculate quantity to bid on. If it is a dutch auction, never
* bid on more than 1 less item than what is available.
*/
static int
getQuantity(int want, int available)
{
if (want == 1 || available == 1)
return 1;
if (available > want)
return want;
return available - 1;
}
static const char HISTORY_URL[] = "http://%s/ws/eBayISAPI.dll?ViewBids&item=%s";
/*
* getInfo(): Get info on auction from bid history page.
*
* returns:
* 0 OK
* 1 error (badly formatted page, etc) set auctionError
*/
int
getInfo(auctionInfo *aip)
{
return getInfoTiming(aip, NULL);
}
/*
* getInfoTiming(): Get info on auction from bid history page.
*
* returns:
* 0 OK
* 1 error (badly formatted page, etc) set auctionError
*/
static int
getInfoTiming(auctionInfo *aip, time_t *timeToFirstByte)
{
int i, ret;
time_t start;
log(("\n\n*** getInfo auction %s price %s user %s\n", aip->auction, aip->bidPriceStr, options.username));
if (ebayLogin(aip, 0))
return 1;
for (i = 0; i < 3; ++i) {
memBuf_t *mp = NULL;
if (!aip->query) {
size_t urlLen = sizeof(HISTORY_URL) + strlen(options.historyHost) + strlen(aip->auction) - (2*2);
aip->query = (char *)myMalloc(urlLen);
sprintf(aip->query, HISTORY_URL, options.historyHost, aip->auction);
}
start = time(NULL);
if (!(mp = httpGet(aip->query, NULL))) {
freeMembuf(mp);
return httpError(aip);
}
ret = parseBidHistory(mp, aip, start, timeToFirstByte, 0);
freeMembuf(mp);
if (i == 0 && ret == 1 && aip->auctionError == ae_mustsignin) {
if (forceEbayLogin(aip))
break;
} else if (aip->auctionError == ae_notime)
/* Blank time remaining -- give it another chance */
sleep(2);
else
break;
}
return ret;
}
/*
* Note: quant=1 is just to dupe eBay into allowing the pre-bid to get
* through. Actual quantity will be sent with bid.
*/
static const char PRE_BID_URL[] = "http://%s/ws/eBayISAPI.dll?MfcISAPICommand=MakeBid&fb=2&co_partner_id=&item=%s&maxbid=%s&quant=%s";
/*
* Get bid key
*
* returns 0 on success, 1 on failure.
*/
static int
preBid(auctionInfo *aip)
{
memBuf_t *mp = NULL;
int quantity = getQuantity(options.quantity, aip->quantity);
char quantityStr[12]; /* must hold an int */
size_t urlLen;
char *url;
int ret = 0;
if (ebayLogin(aip, 0))
return 1;
sprintf(quantityStr, "%d", quantity);
urlLen = sizeof(PRE_BID_URL) + strlen(options.prebidHost) + strlen(aip->auction) + strlen(aip->bidPriceStr) + strlen(quantityStr) - (4*2);
url = (char *)myMalloc(urlLen);
sprintf(url, PRE_BID_URL, options.prebidHost, aip->auction, aip->bidPriceStr, quantityStr);
log(("\n\n*** preBid(): url is %s\n", url));
mp = httpGet(url, NULL);
free(url);
if (!mp)
return httpError(aip);
ret = parsePreBid(mp, aip);
freeMembuf(mp);
return ret;
}
static int
parsePreBid(memBuf_t *mp, auctionInfo *aip)
{
int ret = 0;
int found = 0; //Used as binary store - 1=uiid,10=stok,100=srt
memReset(mp);
while (!match(mp, "name=\"uiid\"")) {
char *start, *value, *end;
for (start = mp->readptr; start >= mp->memory && *start != '<'; --start)
;
value = strcasestr(start, "value=\"");
end = strchr(start, '>');
if (!value || !end || value > end)
continue;
free(aip->biduiid);
mp->readptr = value + 7;
aip->biduiid = myStrdup(getUntil(mp, '\"'));
log(("preBid(): biduiid is \"%s\"", aip->biduiid));
found |= TOKEN_FOUND_UIID;
break;
}
memReset(mp);
while (!match(mp, "name=\"stok\"")) {
char *start, *value, *end;
for (start = mp->readptr; start >= mp->memory && *start != '<'; --start)
;
value = strcasestr(start, "value=\"");
end = strchr(start, '>');
if (!value || !end || value > end)
continue;
free(aip->bidstok);
mp->readptr = value + 7;
aip->bidstok = myStrdup(getUntil(mp, '\"'));
log(("preBid(): bidstok is \"%s\"", aip->bidstok));
found |= TOKEN_FOUND_STOK;
break;
}
memReset(mp);
while (!match(mp, "name=\"srt\"")) {
char *start, *value, *end;
for (start = mp->readptr; start >= mp->memory && *start != '<'; --start)
;
value = strcasestr(start, "value=\"");
end = strchr(start, '>');
if (!value || !end || value > end)
continue;
free(aip->bidsrt);
mp->readptr = value + 7;
aip->bidsrt = myStrdup(getUntil(mp, '\"'));
log(("preBid(): bidsrt is \"%s\"", aip->bidsrt));
found |= TOKEN_FOUND_SRT;
break;
}
if ((found & TOKEN_FOUND_ALL) != TOKEN_FOUND_ALL) {
pageInfo_t *pageInfo = getPageInfo(mp);
ret = makeBidError(pageInfo, aip);
if (ret < 0) {
ret = auctionError(aip, ae_bidtokens, NULL);
bugReport("preBid", __FILE__, __LINE__, aip, mp, optiontab, "cannot find bid token (found=%d)", found);
}
freePageInfo(pageInfo);
}
return ret;
}
static const char LOGIN_1_URL[] = "https://%s/ws/eBayISAPI.dll?SignIn";
static const char LOGIN_2_URL[] = "https://%s/ws/eBayISAPI.dll?co_partnerId=2&siteid=0&UsingSSL=1";
static const char LOGIN_DATA[] = "refId=®Url=%s&MfcISAPICommand=SignInWelcome&bhid=DEF_CI&UsingSSL=1&inputversion=2&lse=false&lsv=&mid=%s&kgver=1&kgupg=1&kgstate=&omid=&hmid=&rhr=f&srt=%s&siteid=0&co_partnerId=2&ru=&pp=&pa1=&pa2=&pa3=&i1=-1&pageType=-1&rtmData=&usid=%s&afbpmName=sess1&kgct=&userid_otp=&sgnBt=Continue&otp=&keepMeSignInOption3=1&userid=%s&%s=%s&runId2=%s&%s=%s&pass=%s&keepMeSignInOption2=1&keepMeSignInOption=1";
static const char* id="id=\"";
static const char* id2="value=\"";
static const int USER_NUM=0;
static const int PASS_NUM=1;
static const int REGURL=0;
static const int MID=1;
static const int SRT=2;
static const int USID=3;
static const int RUNID2=4;
static headerAttr_t headerAttrs[] = {"<label for=\"userid\">", 1, 1, 1, NULL,
"\"password\"", 1, -1, 1, NULL};
static headerVal_t headerVals[] = {"regUrl", 1, 1, 1, NULL,
"mid", 1, 1, 1, NULL,
"srt", 1, 1, 1, NULL,
"usid", 1, 1, 1, NULL,
"runId2", 1, 1, 0, NULL};
static int
signinFormSearch(char* src, size_t srcLen, headerAttr_t* searchdef, searchType_t searchfor)
{
char* start = src;
char* end = src + srcLen;
char* search = NULL;
char pattern[128];
char res[4096];
int i;
if(searchfor == st_attribute)
strcpy(pattern, searchdef->name);
else
sprintf(pattern, "name=\"%s\"", searchdef->name);
for(i = 0; i < searchdef->occurence; i++) {
search = strstr(start, pattern);
if( search == NULL )
{
searchdef->value = (char *)myMalloc(1);
strncpy(searchdef->value, "\0", 1);
return searchdef->mandatory;
}
start = search;
start += strlen(pattern);
}
while(src != search && end != search ) {
search += (searchdef->direction);
if(!strncmp(search, (searchfor == st_attribute ? id : id2),
(searchfor == st_attribute ? strlen(id) : strlen(id2))) ) {
search += (searchfor == st_attribute ? strlen(id) : strlen(id2));
memset(res, '\0', sizeof(res));
for(i = 0;
((searchfor == st_value) || isdigit(*search)) && (*search) != '"' && i < sizeof(res);
res[i++] = *search++);
searchdef->value = (char *)myMalloc(strlen(res) + 1);
strncpy(searchdef->value, (char*) &res, strlen(res) + 1);
if (options.debug)
dlog("%s(): %s=%s", (searchfor == st_attribute ? "findAttr" : "searchvalue"),
searchdef->name, searchdef->value);
return 0;
}
}
if( searchdef->value == NULL )
{
searchdef->value = (char *)myMalloc(1);
strncpy(searchdef->value, "\0", 1);
return searchdef->mandatory;
}
return searchdef->mandatory;
}
static int
findAttr(char* src, size_t srcLen, headerAttr_t* attr)
{
return signinFormSearch(src, srcLen, attr, st_attribute);
}
static int
getVals(char* src, size_t srcLen, headerVal_t* vals)
{
return signinFormSearch(src, srcLen, vals, st_value);
}
/*
* Force an ebay login.
*
* Returns 0 on success, 1 on failure.
*/
static int
forceEbayLogin(auctionInfo *aip)
{
loginTime = 0;
return ebayLogin(aip, 0);
}
/*
* Ebay login. Make sure loging has been done with the given interval.
*
* Returns 0 on success, 1 on failure.
*/
static int
ebayLogin(auctionInfo *aip, time_t interval)
{
memBuf_t *mp = NULL;
size_t urlLen;
char *url, *data, *logdata;
pageInfo_t *pp;
int ret = 0;
char *password;
int i;
/* negative value forces login */
if (loginTime > 0) {
if (interval == 0)
interval = defaultLoginInterval; /* default: 12 hours */
if ((time(NULL) - loginTime) <= interval)
return 0;
}
cleanupCurlStuff();
if (initCurlStuff())
return auctionError(aip, ae_unknown, NULL);
urlLen = sizeof(LOGIN_1_URL) + strlen(options.loginHost) - (1*2);
url = (char *)myMalloc(urlLen);
sprintf(url, LOGIN_1_URL, options.loginHost);
mp = httpGet(url, NULL);
free(url);
if (!mp)
return httpError(aip);
// Get all atrributes and values needed
for(i = 0; i < sizeof(headerAttrs)/sizeof(headerAttr_t); i++)
if(findAttr(mp->memory, mp->size, &headerAttrs[i]))
bugReport("ebayLogin", __FILE__, __LINE__, aip, mp, optiontab,
"findAttr cannot find %s", headerAttrs[i].name);
for(i = 0; i < sizeof(headerVals)/sizeof(headerVal_t); i++)
if(getVals(mp->memory, mp->size, &headerVals[i]))
bugReport("ebayLogin", __FILE__, __LINE__, aip, mp, optiontab,
"getVals cannot find %s", headerVals[i].name);
freeMembuf(mp);
mp = NULL;
urlLen = sizeof(LOGIN_2_URL) + strlen(options.loginHost) - (1*2);
password = getPassword();
url = (char *)myMalloc(urlLen);
sprintf(url, LOGIN_2_URL, options.loginHost);
data = (char *)myMalloc( sizeof(LOGIN_DATA)
+ strlen(headerAttrs[USER_NUM].value)
+ strlen(headerAttrs[PASS_NUM].value)
+ strlen(options.usernameEscape) * 2
+ strlen(password) * 2
+ strlen(headerVals[REGURL].value)
+ strlen(headerVals[MID].value)
+ strlen(headerVals[SRT].value)
+ strlen(headerVals[USID].value)
+ strlen(headerVals[RUNID2].value)
- (11*2)
);
logdata = (char *)myMalloc( sizeof(LOGIN_DATA)
+ strlen(headerAttrs[USER_NUM].value)
+ strlen(headerAttrs[PASS_NUM].value)
+ strlen(options.usernameEscape) * 2
+ 5 * 2
+ strlen(headerVals[REGURL].value)
+ strlen(headerVals[MID].value)
+ strlen(headerVals[SRT].value)
+ strlen(headerVals[USID].value)
+ strlen(headerVals[RUNID2].value)
- (11*2)
);
sprintf(data, LOGIN_DATA, headerVals[REGURL].value,
headerVals[MID].value,
headerVals[SRT].value,
headerVals[USID].value,
options.usernameEscape,
headerAttrs[USER_NUM].value,
options.usernameEscape,
headerVals[RUNID2].value,
headerAttrs[PASS_NUM].value,
password,
password
);
freePassword(password);
sprintf(logdata, LOGIN_DATA, headerVals[REGURL].value,
headerVals[MID].value,
headerVals[SRT].value,
headerVals[USID].value,
options.usernameEscape,
headerAttrs[USER_NUM].value,
options.usernameEscape,
headerVals[RUNID2].value,
headerAttrs[PASS_NUM].value,
"*****",
"*****"
);
// Using POST method instead of GET
log(("HTTP POST login: %s", url));
mp = httpPost(url, data, logdata);
// Free memory
for(i=0; i < sizeof(headerAttrs)/sizeof(headerAttr_t); free(headerAttrs[i++].value));
for(i=0; i < sizeof(headerVals)/sizeof(headerVal_t); free(headerVals[i++].value));
free(url);
free(data);
free(logdata);
if (!mp)
return httpError(aip);
if ((pp = getPageInfo(mp))) {
log(("ebayLogin(): pagename = \"%s\", pageid = \"%s\", srcid = \"%s\"", nullStr(pp->pageName), nullStr(pp->pageId), nullStr(pp->srcId)));
/*
* Pagename is usually MyeBaySummary, but it seems as though
* it can be any MyeBay page, and eBay is not consistent with
* naming of MyeBay pages (MyeBay, MyEbay, myebay, ...) so
* esniper must use strncasecmp().
*/
if ((pp->srcId && !strcmp(pp->srcId, "SignInAlertSupressor"))||
(pp->pageName &&
(!strncasecmp(pp->pageName, "MyeBay", 6) ||
!strncasecmp(pp->pageName, "My eBay", 7) ||
!strncasecmp(pp->pageName, "Watch list", 10) ||
!strncasecmp(pp->pageName, "Purchase History", 16) ||
!strncasecmp(pp->pageName, " Black Friday", 13) ||
!strncasecmp(pp->pageName, "Black Friday", 12) ||
!strncasecmp(pp->pageName, "Electronics", 11))
))
loginTime = time(NULL);
else if (pp->pageName &&
(!strcmp(pp->pageName, "Welcome to eBay") ||
!strcmp(pp->pageName, "Welcome to eBay - Sign in - Error") ||
!strcmp(pp->pageName, "Welcome to eBay - Error")))
ret = auctionError(aip, ae_badpass, NULL);
else if (pp->pageName && !strcmp(pp->pageName, "PageSignIn"))
ret = auctionError(aip, ae_login, NULL);
else if (pp->pageName && !strcmp(pp->pageName, "Reset your password"))
ret = auctionError(aip, ae_manualaction, NULL);
else if (pp->srcId && !strcmp(pp->srcId, "Captcha.xsl"))
ret = auctionError(aip, ae_captcha, NULL);
else {
ret = auctionError(aip, ae_login, NULL);
bugReport("ebayLogin", __FILE__, __LINE__, aip, mp, optiontab, "unknown pageinfo");
}
} else {
log(("ebayLogin(): pageinfo is NULL\n"));
ret = auctionError(aip, ae_login, NULL);
bugReport("ebayLogin", __FILE__, __LINE__, aip, mp, optiontab, "pageinfo is NULL");
}
freeMembuf(mp);
freePageInfo(pp);
return ret;
}
/*
* acceptBid: handle all known AcceptBid pages.
*
* Returns -1 if page not recognized, 0 if bid accepted, 1 if bid not accepted.
*/
static int
acceptBid(const char *pagename, auctionInfo *aip)
{
static const char ACCEPTBID[] = "AcceptBid_";
static const char HIGHBID[] = "HighBidder";
static const char OUTBID[] = "Outbid";
static const char RESERVENOTMET[] = "ReserveNotMet";
if (!strcmp(pagename, "Bid confirmation"))
return aip->bidResult = 0;
if (!pagename ||
strncmp(pagename, ACCEPTBID, sizeof(ACCEPTBID) - 1))
return -1;
pagename += sizeof(ACCEPTBID) - 1;
/*
* valid pagenames include AcceptBid_HighBidder,
* AcceptBid_HighBidder_rebid, possibly others.
*/
if (!strncmp(pagename, HIGHBID, sizeof(HIGHBID) - 1))
return aip->bidResult = 0;
/*
* valid pagenames include AcceptBid_Outbid, AcceptBid_Outbid_rebid,
* possibly others.
*/
if (!strncmp(pagename, OUTBID, sizeof(OUTBID) - 1))
return aip->bidResult = auctionError(aip, ae_outbid, NULL);
/*
* valid pagenames include AcceptBid_ReserveNotMet,
* AcceptBid_ReserveNotMet_rebid, possibly others.
*/
if (!strncmp(pagename, RESERVENOTMET, sizeof(RESERVENOTMET) - 1))
return aip->bidResult = auctionError(aip, ae_reservenotmet, NULL);
/* unknown AcceptBid page */
return -1;
}
/*
* makeBidError: handle all known MakeBidError pages.
*
* Returns -1 if page not recognized, 0 if bid accepted, 1 if bid not accepted.
*/
static int
makeBidError(const pageInfo_t *pageInfo, auctionInfo *aip)
{
static const char MAKEBIDERROR[] = "MakeBidError";
const char *pagename = pageInfo->pageName;
if (!pagename) {
const char *srcId = pageInfo->srcId;
if (srcId && !strcasecmp(srcId, "ViewItem"))
return aip->bidResult = auctionError(aip, ae_ended, NULL);
else
return -1;
}
if (!strcasecmp(pagename, "Place bid"))
return aip->bidResult = auctionError(aip, ae_outbid, NULL);
if (!strcasecmp(pagename, "eBay Alerts"))
return aip->bidResult = auctionError(aip, ae_alert, NULL);
if (!strcasecmp(pagename, "Buyer Requirements"))
return aip->bidResult = auctionError(aip, ae_buyerrequirements, NULL);
if (!strcasecmp(pagename, "PageSignIn"))
return aip->bidResult = auctionError(aip, ae_mustsignin, NULL);
if (!strncasecmp(pagename, "BidManager", 10) ||
!strncasecmp(pagename, "BidAssistant", 12))
return aip->bidResult = auctionError(aip, ae_bidassistant, NULL);
if (strncasecmp(pagename, MAKEBIDERROR, sizeof(MAKEBIDERROR) - 1))
return -1;
pagename += sizeof(MAKEBIDERROR) - 1;
if (!*pagename ||
!strcasecmp(pagename, "AuctionEnded"))
return aip->bidResult = auctionError(aip, ae_ended, NULL);
if (!strcasecmp(pagename, "AuctionEnded_BINblock") ||
!strcasecmp(pagename, "AuctionEnded_BINblock "))
return aip->bidResult = auctionError(aip, ae_cancelled, NULL);
if (!strcasecmp(pagename, "Password"))
return aip->bidResult = auctionError(aip, ae_badpass, NULL);
if (!strcasecmp(pagename, "MinBid"))
return aip->bidResult = auctionError(aip, ae_bidprice, NULL);
if (!strcasecmp(pagename, "BuyerBlockPref"))
return aip->bidResult = auctionError(aip, ae_buyerblockpref, NULL);
if (!strcasecmp(pagename, "BuyerBlockPrefDoesNotShipToLocation"))
return aip->bidResult = auctionError(aip, ae_buyerblockprefdoesnotshiptolocation, NULL);
if (!strcasecmp(pagename, "BuyerBlockPrefNoLinkedPaypalAccount"))
return aip->bidResult = auctionError(aip, ae_buyerblockprefnolinkedpaypalaccount, NULL);
if (!strcasecmp(pagename, "HighBidder"))
return aip->bidResult = auctionError(aip, ae_highbidder, NULL);
if (!strcasecmp(pagename, "CannotBidOnItem"))
return aip->bidResult = auctionError(aip, ae_cannotbid, NULL);
if (!strcasecmp(pagename, "DutchSameBidQuantity"))
return aip->bidResult = auctionError(aip, ae_dutchsamebidquantity, NULL);
if (!strcasecmp(pagename, "BuyerBlockPrefItemCountLimitExceeded"))
return aip->bidResult = auctionError(aip, ae_buyerblockprefitemcountlimitexceeded, NULL);
if (!strcasecmp(pagename, "BidGreaterThanBin_BINblock"))
return aip->bidResult = auctionError(aip, ae_bidgreaterthanbin_binblock, NULL);
/* unknown MakeBidError page */
return -1;
}
/*
* Parse bid result.
*
* Returns:
* 0: OK
* 1: error
*/
static int
parseBid(memBuf_t *mp, auctionInfo *aip)
{
/*
* The following sometimes have more characters after them, for
* example AcceptBid_HighBidder_rebid (you were already the high
* bidder and placed another bid).
*/
pageInfo_t *pageInfo = getPageInfo(mp);
int ret;
aip->bidResult = -1;
log(("parseBid(): pagename = %s\n", pageInfo->pageName));
if ((ret = acceptBid(pageInfo->pageName, aip)) >= 0 ||
(ret = makeBidError(pageInfo, aip)) >= 0) {
;
} else {
bugReport("parseBid", __FILE__, __LINE__, aip, mp, optiontab, "unknown pagename");
printLog(stdout, "Cannot determine result of bid\n");
ret = 0; /* prevent another bid */
}
freePageInfo(pageInfo);
return ret;
} /* parseBid() */
static const char BID_URL[] = "http://%s/ws/eBayISAPI.dll?MfcISAPICommand=MakeBid&maxbid=%s&quant=%s&mode=1&uiid=%s&co_partnerid=2&user=%s&fb=2&item=%s&stok=%s&srt=%s";
/*
* Place bid.
*
* Returns:
* 0: OK
* 1: error
*/
static int
bid(auctionInfo *aip)
{
memBuf_t *mp = NULL;
size_t urlLen;
char *url, *logUrl, *tmpUsername, *tmpUiid, *tmpStok, *tmpSrt;
int ret;
int quantity = getQuantity(options.quantity, aip->quantity);
char quantityStr[12]; /* must hold an int */
if (!aip->biduiid || !aip->bidstok || !aip->bidsrt)
return auctionError(aip, ae_bidtokens, NULL);
if (ebayLogin(aip, 0))
return 1;
sprintf(quantityStr, "%d", quantity);
/* create url */
urlLen = sizeof(BID_URL) + strlen(options.bidHost) + strlen(aip->bidPriceStr) + strlen(quantityStr) + strlen(aip->biduiid) + strlen(options.usernameEscape) + strlen(aip->auction) + strlen(aip->bidstok) + strlen(aip->bidsrt) - (8*2);
url = (char *)myMalloc(urlLen);
sprintf(url, BID_URL, options.bidHost, aip->bidPriceStr, quantityStr, aip->biduiid, options.usernameEscape, aip->auction, aip->bidstok, aip->bidsrt);
logUrl = (char *)myMalloc(urlLen);
tmpUsername = stars(strlen(options.usernameEscape));
tmpUiid = stars(strlen(aip->biduiid));
tmpStok = stars(strlen(aip->bidstok));
tmpSrt = stars(strlen(aip->bidsrt));
sprintf(logUrl, BID_URL, options.bidHost, aip->bidPriceStr, quantityStr, tmpUiid, tmpUsername, aip->auction, tmpStok, tmpSrt);
free(tmpUsername);
free(tmpUiid);
free(tmpStok);
free(tmpSrt);
if (!options.bid) {
printLog(stdout, "Bidding disabled\n");
log(("\n\nbid(): query url:\n%s\n", logUrl));
ret = aip->bidResult = 0;
} else if (!(mp = httpGet(url, logUrl))) {
ret = httpError(aip);
} else {
ret = parseBid(mp, aip);
}
free(url);
free(logUrl);
freeMembuf(mp);
return ret;
} /* bid() */
/*
* watch(): watch auction until it is time to bid
*
* returns:
* 0 OK
* 1 Error
*/
static int
watch(auctionInfo *aip)
{
int errorCount = 0;
long remain = LONG_MIN;
unsigned int sleepTime = 0;
log(("*** WATCHING auction %s price-each %s quantity %d bidtime %ld\n", aip->auction, aip->bidPriceStr, options.quantity, options.bidtime));
for (;;) {
time_t tmpLatency;
time_t start = time(NULL);
time_t timeToFirstByte = 0;
int ret = getInfoTiming(aip, &timeToFirstByte);
time_t end = time(NULL);
if (timeToFirstByte == 0)
timeToFirstByte = end;
tmpLatency = (timeToFirstByte - start);
if ((tmpLatency >= 0) && (tmpLatency < 600))
aip->latency = tmpLatency;
printLog(stdout, "Latency: %d seconds\n", aip->latency);
if (ret) {
printAuctionError(aip, stderr);
/*
* Fatal error? We allow up to 50 errors, then quit.
* eBay "unavailable" doesn't count towards the total.
*/
if (aip->auctionError == ae_unavailable) {
if (remain >= 0)
remain = newRemain(aip);
if (remain == LONG_MIN || remain > 86400) {
/* typical eBay maintenance period
* is two hours. Sleep for half that
* amount of time.
*/
printLog(stdout, "%s: Will try again, sleeping for an hour\n", timestamp());
sleepTime = 3600;
sleep(sleepTime);
continue;
}
} else if (remain == LONG_MIN) {
/* first time through? Give it 3 chances then
* make the error fatal.
*/
int j;
for (j = 0; ret && j < 3 && aip->auctionError == ae_notitle; ++j)
ret = getInfo(aip);
if (ret)
return 1;
remain = newRemain(aip);
} else {
/* non-fatal error */
log(("ERROR %d!!!\n", ++errorCount));
if (errorCount > 50)
return auctionError(aip, ae_toomany, NULL);
printLog(stdout, "Cannot find auction - internet or eBay problem?\nWill try again after sleep.\n");
remain = newRemain(aip);
}
} else if (!isValidBidPrice(aip))
return auctionError(aip, ae_bidprice, NULL);
else
remain = newRemain(aip);
/*
* Check login when we are close to bidding.
*/
if (remain <= 300) {
if (ebayLogin(aip, defaultLoginInterval - 600))
return 1;
remain = newRemain(aip);
}
/*
* if we're less than two minutes away, get bid key
*/
if (remain <= 150 && !aip->biduiid && aip->auctionError == ae_none) {
int i;
printf("\n");
for (i = 0; i < 5; ++i) {
/* ae_bidtokens is used when the page loaded
* but failed for some unknown reason.
* Do not try again in this situation.
*/
if (!preBid(aip) ||
aip->auctionError == ae_bidtokens)