-
Notifications
You must be signed in to change notification settings - Fork 0
/
HKMDiscoverer.m
executable file
·1410 lines (1217 loc) · 61.6 KB
/
HKMDiscoverer.m
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
#import "HKMDiscoverer.h"
#import "HKMJSON.h"
#import "HKMLead.h"
#import "HKMReferralRecord.h"
#import <AddressBook/AddressBook.h>
#import "UIDevice-HKMHardware.h"
#import "HKMOpenUDID.h"
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
static HKMDiscoverer *_agent;
@implementation HKMDiscoverer
@synthesize server, SMSDest, appKey, /* runQueryAfterOrder, */ queryStatus, errorMessage, leads, installs, referrals;
@synthesize fbTemplate, emailTemplate, smsTemplate, twitterTemplate;
@synthesize referralMessage;
@synthesize installCode;
@synthesize skipVerificationSms;
- (id) init {
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
installCode = [[standardUserDefaults objectForKey:@"installCode"] retain];
}
contactsDictionary = [[NSMutableDictionary dictionary] retain];
// default
skipVerificationSms = NO;
return self;
}
- (void) dealloc
{
NSLog(@"HKMDiscover - dealloc invoked");
[contactsDictionary release];
}
- (BOOL) isRegistered{
if (installCode == nil || [installCode length] == 0) {
return NO;
} else {
return YES;
}
}
- (BOOL) verifyDevice:(UIViewController *)vc forceSms:(BOOL) force userName:(NSString *) userName {
if (verifyDeviceConnection != nil) {
return NO;
}
if (vc != nil) {
viewController = [vc retain];
forceVerificationSms = force;
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/newverify", server]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"appKey=%@", [appKey stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&addrHash=%@", [self getAddressbookHash:10]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&deviceModel=%@", [[UIDevice currentDevice] platformString]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&deviceOs=%@", [[UIDevice currentDevice] systemVersion]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&openUdid=%@", [[HKMOpenUDID value] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&installToken=%@", [[HKMOpenUDID value] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&macAddress=%@", [[self getMacAddress] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&verifyMessageTemplate=%@", [@"Send text to confirm your device and see which friends has this app. %installCode%" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
if (userName != nil) {
[postBody appendData:[[NSString stringWithFormat:@"&name=%@", [userName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
}
[postBody appendData:[[NSString stringWithFormat:@"&sdkVersion=%@", [SDKVERSION stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (connection) {
verifyDeviceData = [[NSMutableData data] retain];
verifyDeviceConnection = [connection retain];
}
return YES;
}
- (BOOL) queryVerifiedStatus {
if (verificationConnection != nil || installCode == nil) {
return NO;
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/queryverify", server]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"appKey=%@", [appKey stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&installCode=%@", [installCode stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&sdkVersion=%@", [SDKVERSION stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (connection) {
verificationData = [[NSMutableData data] retain];
verificationConnection = [connection retain];
}
return YES;
}
- (BOOL) discover:(int) limit {
if (discoverConnection != nil) {
return NO;
}
NSLog(@"installCode is %@", installCode);
NSString *ab = [self getAddressbook:limit];
if (ab == nil) {
return NO;
}
if (![self checkNewAddresses:ab]) {
if ([contactsDictionary count] == 0)
[self buildAddressBookDictionary];
NSLog(@"%@", [NSNotificationCenter defaultCenter]);
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_HOOK_DISCOVER_NO_CHANGE object:nil];
return YES;
}
// build dictionary for quick lookup by phone
[self buildAddressBookDictionaryAsync];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/discover", server]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"appKey=%@", [appKey stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
if (installCode != nil ) {
[postBody appendData:[[NSString stringWithFormat:@"&installCode=%@", [installCode stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
}
NSString *encodedJsonStr = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)ab, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 );
[postBody appendData:[[NSString stringWithFormat:@"&addressBook=%@", encodedJsonStr] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&deviceModel=%@", [[UIDevice currentDevice] platformString]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&deviceOs=%@", [[UIDevice currentDevice] systemVersion]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&openUdid=%@", [[HKMOpenUDID value] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&installToken=%@", [[HKMOpenUDID value] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&macAddress=%@", [[self getMacAddress] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&sdkVersion=%@", [SDKVERSION stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (connection) {
discoverData = [[NSMutableData data] retain];
discoverConnection = [connection retain];
}
// [connection release];
return YES;
}
- (BOOL) discoverWithoutVzw {
if (discoverConnection != nil) {
return NO;
}
NSLog(@"installCode is %@", installCode);
NSString *ab = [self getAddressbook:0];
if (ab == nil) {
return NO;
}
if (![self checkNewAddresses:ab]) {
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_HOOK_DISCOVER_NO_CHANGE object:nil];
return YES;
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/discover", server]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"appKey=%@", [appKey stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
if (installCode != nil ) {
[postBody appendData:[[NSString stringWithFormat:@"&installCode=%@", [installCode stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
}
NSString *encodedJsonStr = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)ab, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 );
[postBody appendData:[[NSString stringWithFormat:@"&addressBook=%@", encodedJsonStr] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&deviceModel=%@", [[UIDevice currentDevice] platformString]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&deviceOs=%@", [[UIDevice currentDevice] systemVersion]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&openUdid=%@", [[HKMOpenUDID value] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&installToken=%@", [[HKMOpenUDID value] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&macAddress=%@", [[self getMacAddress] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&queryDeviceCarrierExclusions=38"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&sdkVersion=%@", [SDKVERSION stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (connection) {
discoverData = [[NSMutableData data] retain];
discoverConnection = [connection retain];
}
// [connection release];
return YES;
}
// contacts must be an array of dictionaries
// Each dictionary has
// phone
// firstName
// lastName
- (BOOL) discoverSelected:(NSMutableArray *)contacts {
if (discoverConnection != nil) {
return NO;
}
NSLog(@"installCode is %@", installCode);
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/selectupdate", server]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"appKey=%@", [appKey stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
if (installCode != nil ) {
[postBody appendData:[[NSString stringWithFormat:@"&installCode=%@", [installCode stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
}
HKMSBJSON *jsonWriter = [[HKMSBJSON new] autorelease];
jsonWriter.humanReadable = YES;
NSString *jsonStr = [jsonWriter stringWithObject:contacts];
NSLog(@"JSON Object --> %@", jsonStr);
NSString *encodedJsonStr = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)jsonStr, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 );
[postBody appendData:[[NSString stringWithFormat:@"&addressBook=%@", encodedJsonStr] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&deviceModel=%@", [[UIDevice currentDevice] platformString]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&deviceOs=%@", [[UIDevice currentDevice] systemVersion]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&openUdid=%@", [[HKMOpenUDID value] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&installToken=%@", [[HKMOpenUDID value] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&macAddress=%@", [[self getMacAddress] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&sdkVersion=%@", [SDKVERSION stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (connection) {
discoverData = [[NSMutableData data] retain];
discoverConnection = [connection retain];
}
// [connection release];
return YES;
}
- (BOOL) queryLeads {
if (queryOrderConnection != nil) {
return NO;
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/queryleads", server]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"appKey=%@", [appKey stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&installCode=%@", [installCode stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&sdkVersion=%@", [SDKVERSION stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (connection) {
queryOrderData = [[NSMutableData data] retain];
queryOrderConnection = [connection retain];
}
// [connection release];
return YES;
}
- (BOOL) downloadShareTemplates {
if (shareTemplateConnection != nil) {
return NO;
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/template", server]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"appKey=%@", [appKey stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&installCode=%@", [installCode stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&sdkVersion=%@", [SDKVERSION stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (connection) {
shareTemplateData = [[NSMutableData data] retain];
shareTemplateConnection = [connection retain];
}
// [connection release];
return YES;
}
- (BOOL) newReferral:(NSArray *)phones withName:(NSString *)name useVirtualNumber:(BOOL) sendNow {
if (newReferralConnection != nil) {
return NO;
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/newreferral", server]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"appKey=%@", [appKey stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&installCode=%@", [installCode stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
for (NSString *phone in phones) {
NSString *encodedPhone = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)phone, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 );
[postBody appendData:[[NSString stringWithFormat:@"&phone=%@", encodedPhone] dataUsingEncoding:NSUTF8StringEncoding]];
}
if (name != nil) {
[postBody appendData:[[NSString stringWithFormat:@"&name=%@", [name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
}
[postBody appendData:[@"&useShortUrl=true" dataUsingEncoding:NSUTF8StringEncoding]];
if (sendNow) {
[postBody appendData:[@"&sendNow=true" dataUsingEncoding:NSUTF8StringEncoding]];
}
[postBody appendData:[[NSString stringWithFormat:@"&sdkVersion=%@", [SDKVERSION stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (connection) {
newReferralData = [[NSMutableData data] retain];
newReferralConnection = [connection retain];
}
// [connection release];
return YES;
}
- (BOOL) updateReferral:(BOOL) sent {
if (updateReferralConnection != nil) {
return NO;
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/updatereferral", server]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"appKey=%@", [appKey stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&installCode=%@", [installCode stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&referralId=%d", referralId] dataUsingEncoding:NSUTF8StringEncoding]];
if (sent) {
[postBody appendData:[@"&action=sent" dataUsingEncoding:NSUTF8StringEncoding]];
} else {
[postBody appendData:[@"&action=cancel" dataUsingEncoding:NSUTF8StringEncoding]];
}
[postBody appendData:[[NSString stringWithFormat:@"&sdkVersion=%@", [SDKVERSION stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (connection) {
updateReferralData = [[NSMutableData data] retain];
updateReferralConnection = [connection retain];
}
// [connection release];
return YES;
}
- (BOOL) queryInstalls:(NSString *)direction {
if (queryInstallsConnection != nil) {
return NO;
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/queryinstalls", server]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"appKey=%@", [appKey stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&installCode=%@", [installCode stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&reference=%@", [direction stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&sdkVersion=%@", [SDKVERSION stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (connection) {
queryInstallsData = [[NSMutableData data] retain];
queryInstallsConnection = [connection retain];
}
// [connection release];
return YES;
}
- (BOOL) queryReferral {
if (queryReferralConnection != nil) {
return NO;
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/queryreferral", server]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"appKey=%@", [appKey stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&installCode=%@", [installCode stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&sdkVersion=%@", [SDKVERSION stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (connection) {
queryReferralData = [[NSMutableData data] retain];
queryReferralConnection = [connection retain];
}
// [connection release];
return YES;
}
- (BOOL) newInstall {
if (newInstallConnection != nil) {
return NO;
}
if (installCode != nil && ![@"" isEqualToString:installCode]) {
return NO;
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/newinstall", server]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"appKey=%@", [appKey stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&addrHash=%@", [self getAddressbookHash:10]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&openUdid=%@", [[HKMOpenUDID value] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&installToken=%@", [[HKMOpenUDID value] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&macAddress=%@", [[self getMacAddress] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&deviceModel=%@", [[UIDevice currentDevice] platformString]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&deviceOs=%@", [[UIDevice currentDevice] systemVersion]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&sdkVersion=%@", [SDKVERSION stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (connection) {
newInstallData = [[NSMutableData data] retain];
newInstallConnection = [connection retain];
}
// [connection release];
return YES;
}
- (NSString *) getAddressbook:(int) limit {
ABAddressBookRef ab = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(ab);
CFIndex nPeople = ABAddressBookGetPersonCount(ab);
if (nPeople > MAX_ADDRESSBOOK_UPLOAD_SIZE)
nPeople = MAX_ADDRESSBOOK_UPLOAD_SIZE;
if (limit > 0 && nPeople > limit) {
nPeople = limit;
}
NSMutableArray *phones = [[NSMutableArray alloc] init];
for (int i = 0; i < nPeople; i++) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
NSString *firstNameStr = (NSString *) firstName;
if (firstNameStr == nil) {
firstNameStr = @"";
}
if (![firstNameStr canBeConvertedToEncoding:NSASCIIStringEncoding]) {
firstNameStr = @"NONASCII";
}
NSString *lastNameStr = (NSString *) lastName;
if (lastNameStr == nil) {
lastNameStr = @"";
}
if (![lastNameStr canBeConvertedToEncoding:NSASCIIStringEncoding]) {
lastNameStr = @"NONASCII";
}
ABMultiValueRef ps = ABRecordCopyValue(ref, kABPersonPhoneProperty);
CFIndex count = ABMultiValueGetCount (ps);
for (int i = 0; i < count; i++) {
CFStringRef phone = ABMultiValueCopyValueAtIndex (ps, i);
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:16];
[dic setObject:((NSString *) phone) forKey:@"phone"];
[dic setObject:((NSString *) firstNameStr) forKey:@"firstName"];
[dic setObject:((NSString *) lastNameStr) forKey:@"lastName"];
[phones addObject:dic];
[dic release];
if (phone) {
CFRelease(phone);
}
}
if (firstName) {
CFRelease(firstName);
}
if (lastName) {
CFRelease(lastName);
}
}
if (allPeople) {
CFRelease(allPeople);
}
// create json for phone and name based on phones
HKMSBJSON *jsonWriter = [[HKMSBJSON new] autorelease];
jsonWriter.humanReadable = YES;
NSString *jsonStr = [jsonWriter stringWithObject:phones];
NSLog(@"JSON Object --> %@", jsonStr);
return jsonStr;
}
- (NSString *) getAddressbookHash:(int) limit {
ABAddressBookRef ab = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(ab);
CFIndex nPeople = ABAddressBookGetPersonCount(ab);
if (limit > 0 && nPeople > limit) {
nPeople = limit;
}
NSMutableString *hashes = [NSMutableString stringWithCapacity:1024];
for (int i = 0; i < nPeople; i++) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
ABMultiValueRef ps = ABRecordCopyValue(ref, kABPersonPhoneProperty);
CFIndex count = ABMultiValueGetCount (ps);
for (int i = 0; i < count; i++) {
CFStringRef phone = ABMultiValueCopyValueAtIndex (ps, i);
NSString *fphone = [self formatPhone:((NSString *) phone)];
int hash = [self murmurHash:fphone];
[hashes appendFormat:@"%d|", hash];
NSLog(@"Murmur Hash of addresses %@ is %d", fphone, hash);
if (phone) {
CFRelease(phone);
}
}
}
if (allPeople) {
CFRelease(allPeople);
}
NSString *res = @"";
if ([hashes length] > 0) {
res = [hashes substringToIndex:([hashes length]-1)];
}
NSLog(@"Murmur Hash outcome is %@", res);
return res;
}
- (void) createVerificationSms {
fullScreen = [UIApplication sharedApplication].statusBarHidden;
NSString *platform = [[UIDevice currentDevice] platformString];
NSString *model = [[UIDevice currentDevice] model];
if (viewController != nil) {
if ([MFMessageComposeViewController canSendText] && [platform hasPrefix:@"iPhone"] && ![model isEqualToString:@"iPhone Simulator"] && !skipVerificationSms) {
NSLog(@"Show SMS confirmation");
[UIApplication sharedApplication].statusBarHidden = NO;
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
controller.body = verifyMessage;
controller.recipients = [NSArray arrayWithObjects:SMSDest, nil];
controller.messageComposeDelegate = self;
[viewController presentModalViewController:controller animated:YES];
} else {
NSLog(@"Not a SMS device. Fail silently.");
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_HOOK_NOT_SMS_DEVICE object:nil];
}
}
}
- (NSString *) lookupNameFromPhone:(NSString *)p {
double start = [[NSDate date] timeIntervalSince1970];
NSString *name;
ABAddressBookRef ab = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(ab);
CFIndex nPeople = ABAddressBookGetPersonCount(ab);
for (int i = 0; i < nPeople; i++) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
CFStringRef suffix = ABRecordCopyValue(ref, kABPersonSuffixProperty);
NSString *firstNameStr = (NSString *) firstName;
if (firstNameStr == nil) {
firstNameStr = @"";
}
NSString *lastNameStr = (NSString *) lastName;
if (lastNameStr == nil) {
lastNameStr = @"";
}
NSString *suffixStr = (NSString *) suffix;
if (suffixStr == nil) {
suffixStr = @"";
}
ABMultiValueRef ps = ABRecordCopyValue(ref, kABPersonPhoneProperty);
CFIndex count = ABMultiValueGetCount (ps);
for (int i = 0; i < count; i++) {
CFStringRef phone = ABMultiValueCopyValueAtIndex (ps, i);
if ([p isEqualToString:[self formatPhone:((NSString *) phone)]]) {
name = [NSString stringWithFormat:@"%@ %@ %@", firstNameStr, lastNameStr, suffixStr];
break;
}
if (phone) {
CFRelease(phone);
}
}
if (firstName) {
CFRelease(firstName);
}
if (lastName) {
CFRelease(lastName);
}
}
if (allPeople) {
CFRelease(allPeople);
}
double end = [[NSDate date] timeIntervalSince1970];
double difference = end - start;
NSLog(@"lookupNameFromPhone - Time elapsed %f", difference);
return name;
}
- (void) buildAddressBookDictionaryAsync {
/* Operation Queue init (autorelease) */
NSOperationQueue *queue = [NSOperationQueue new];
/* Create our NSInvocationOperation to call buildAddressBookDictionary, passing in nil */
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(buildAddressBookDictionary)
object:nil];
/* Add the operation to the queue */
[queue addOperation:operation];
[operation release];
}
- (void) buildAddressBookDictionary {
double start = [[NSDate date] timeIntervalSince1970];
[contactsDictionary removeAllObjects];
ABAddressBookRef ab = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(ab);
CFIndex nPeople = ABAddressBookGetPersonCount(ab);
for (int i = 0; i < nPeople; i++) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
ABRecordID recordId = ABRecordGetRecordID(ref); // get record id from address book record
ABMultiValueRef ps = ABRecordCopyValue(ref, kABPersonPhoneProperty);
CFIndex count = ABMultiValueGetCount (ps);
for (int i = 0; i < count; i++) {
CFStringRef phone = ABMultiValueCopyValueAtIndex (ps, i);
[contactsDictionary setObject:[NSNumber numberWithInteger:recordId] forKey:[self formatPhone:((NSString *) phone)]];
if (phone) {
CFRelease(phone);
}
}
}
if (allPeople) {
CFRelease(allPeople);
}
double end = [[NSDate date] timeIntervalSince1970];
double difference = end - start;
NSLog(@"buildAddressBookDictionary - Time elapsed %f", difference);
}
//- (void) buildAddressBookDictionary {
// double start = [[NSDate date] timeIntervalSince1970];
//
// [contactsDictionary removeAllObjects];
// ABAddressBookRef ab = ABAddressBookCreate();
//
// CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(ab);
// CFIndex nPeople = ABAddressBookGetPersonCount(ab);
//
// for (int i = 0; i < nPeople; i++) {
// ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
// CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
// CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
// CFStringRef suffix = ABRecordCopyValue(ref, kABPersonSuffixProperty);
//
// NSString *firstNameStr = (NSString *) firstName;
// if (firstNameStr == nil) {
// firstNameStr = @"";
// }
// NSString *lastNameStr = (NSString *) lastName;
// if (lastNameStr == nil) {
// lastNameStr = @"";
// }
// NSString *suffixStr = (NSString *) suffix;
// if (suffixStr == nil) {
// suffixStr = @"";
// }
//
// ABMultiValueRef ps = ABRecordCopyValue(ref, kABPersonPhoneProperty);
// CFIndex count = ABMultiValueGetCount (ps);
// for (int i = 0; i < count; i++) {
// CFStringRef phone = ABMultiValueCopyValueAtIndex (ps, i);
// [contactsDictionary setObject:[NSString stringWithFormat:@"%@ %@ %@", firstNameStr, lastNameStr, suffixStr] forKey:[self formatPhone:((NSString *) phone)]];
//
// if (phone) {
// CFRelease(phone);
// }
// }
//
// if (firstName) {
// CFRelease(firstName);
// }
// if (lastName) {
// CFRelease(lastName);
// }
// }
// if (allPeople) {
// CFRelease(allPeople);
// }
// double end = [[NSDate date] timeIntervalSince1970];
// double difference = end - start;
//
// NSLog(@"buildAddressBookDictionary - Time elapsed %f", difference);
//}
// Update lead with info from address book
- (void) updateContactDetails:(HKMLead *)intoLead {
NSNumber *contactId = [contactsDictionary objectForKey:intoLead.phone];
if (!contactId) {
NSLog(@"updateContactDetails - contact[%@] not found in dictionary", intoLead.phone);
return;
}
// Get contact from Address Book
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordID recordId = (ABRecordID)[contactId intValue];
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, recordId);
CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
CFStringRef suffix = ABRecordCopyValue(person, kABPersonSuffixProperty);
NSString *firstNameStr = (NSString *) firstName;
if (firstNameStr == nil) {
firstNameStr = @"";
}
NSString *lastNameStr = (NSString *) lastName;
if (lastNameStr == nil) {
lastNameStr = @"";
}
NSString *suffixStr = (NSString *) suffix;
if (suffixStr == nil) {
suffixStr = @"";
}
if (firstName) {
CFRelease(firstName);
}
if (lastName) {
CFRelease(lastName);
}
if (suffix) {
CFRelease(suffix);
}
NSString *fullName = [NSString stringWithFormat:@"%@ %@ %@", firstNameStr, lastNameStr, suffixStr];
intoLead.name = fullName;
// Check for contact picture
if (person != nil && ABPersonHasImageData(person)) {
if ( &ABPersonCopyImageDataWithFormat != nil ) {
// iOS >= 4.1
intoLead.image = [UIImage imageWithData:(NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail)];
} else
// iOS < 4.1
intoLead.image = [UIImage imageWithData:(NSData *)ABPersonCopyImageData(person)];
}
}
- (NSString *) formatPhone:(NSString *)p {
p = [p stringByReplacingOccurrencesOfString:@"(" withString:@""];
p = [p stringByReplacingOccurrencesOfString:@")" withString:@""];
p = [p stringByReplacingOccurrencesOfString:@" " withString:@""];
p = [p stringByReplacingOccurrencesOfString:@"-" withString:@""];
p = [p stringByReplacingOccurrencesOfString:@"+" withString:@""];
int length = [p length];
if(length == 10) {
p = [NSString stringWithFormat:@"+1%@", p];
} else if (length == 11) {
p = [NSString stringWithFormat:@"+%@", p];
}
return p;
}
- (BOOL) checkNewAddresses:(NSString *)ab {
NSString *saved = [self cachedAddresses];
if (saved == nil || ![saved isEqualToString:ab]) {
addressbook = [ab retain];
return YES;
} else {
return NO;
}
return YES;
}
- (NSString *) cachedAddresses {
NSString *saved = nil;
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
saved = [standardUserDefaults objectForKey:@"HOOKADDRESSBOOK"];
}
return saved;
}
- (NSString *) getMacAddress {
int mgmtInfoBase[6];
char *msgBuffer = NULL;
NSString *errorFlag = NULL;
size_t length;
// Setup the management Information Base (mib)
mgmtInfoBase[0] = CTL_NET; // Request network subsystem
mgmtInfoBase[1] = AF_ROUTE; // Routing table info
mgmtInfoBase[2] = 0;
mgmtInfoBase[3] = AF_LINK; // Request link layer information
mgmtInfoBase[4] = NET_RT_IFLIST; // Request all configured interfaces
// With all configured interfaces requested, get handle index
if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0)
errorFlag = @"if_nametoindex failure";
// Get the size of the data available (store in len)
else if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)
errorFlag = @"sysctl mgmtInfoBase failure";
// Alloc memory based on above call
else if ((msgBuffer = (char *) malloc(length)) == NULL)
errorFlag = @"buffer allocation failure";
// Get system information, store in buffer
else if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
{
free(msgBuffer);
errorFlag = @"sysctl msgBuffer failure";
}
else
{
// Map msgbuffer to interface message structure
struct if_msghdr *interfaceMsgStruct = (struct if_msghdr *) msgBuffer;
// Map to link-level socket structure
struct sockaddr_dl *socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);
// Copy link layer address data in socket structure to an array
unsigned char macAddress[6];
memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);
// Read from char array into a string object, into traditional Mac address format
NSString *macAddressString = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
macAddress[0], macAddress[1], macAddress[2], macAddress[3], macAddress[4], macAddress[5]];
NSLog(@"Mac Address: %@", macAddressString);
// Release the buffer memory
free(msgBuffer);
return macAddressString;
}
// Error...
NSLog(@"Error: %@", errorFlag);
return errorFlag;
}
- (int) murmurHash:(NSString *)s {
NSData *d = [s dataUsingEncoding:NSUTF8StringEncoding];
return MurmurHash2([d bytes], [d length], 0);
}
unsigned int MurmurHash2 ( const void * key, int len, unsigned int seed ) {
// 'm' and 'r' are mixing constants generated offline.
// They're not really 'magic', they just happen to work well.
const unsigned int m = 0x5bd1e995;
const int r = 24;
// Initialize the hash to a 'random' value
unsigned int h = seed ^ len;
// Mix 4 bytes at a time into the hash
const unsigned char * data = (const unsigned char *)key;
while(len >= 4)
{
unsigned int k = *(unsigned int *)data;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
// Handle the last few bytes of the input array
switch(len)
{
case 3: h ^= data[2] << 16;
case 2: h ^= data[1] << 8;
case 1: h ^= data[0];
h *= m;
};
// Do a few final mixes of the hash to ensure the last few
// bytes are well-incorporated.
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
[viewController dismissModalViewControllerAnimated:YES];
[UIApplication sharedApplication].statusBarHidden = fullScreen;
if (fullScreen) {
CGRect nFrame = viewController.view.frame;
nFrame.size.height = nFrame.size.height + 20;
nFrame.origin.y = nFrame.origin.y - 20;
viewController.view.frame = nFrame;
}
if (result == MessageComposeResultCancelled) {
if (forceVerificationSms) {
// the SMS stays. No cancel
UIAlertView* alert = [[UIAlertView alloc] init];
alert.title = @"Confirmation";
alert.message = @"You can only proceed after you send the confirmation SMS";
[alert addButtonWithTitle:@"Okay"];
// alert.cancelButtonIndex = 0;
alert.delegate = self;
[alert show];
[alert release];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_HOOK_VERIFICATION_SMS_NOT_SENT object:nil];
}
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_HOOK_VERIFICATION_SMS_SENT object:nil];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
[self createVerificationSms];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog (@"Received response");
if (connection == verifyDeviceConnection) {
[verifyDeviceData setLength:0];
}
if (connection == verificationConnection) {
[verificationData setLength:0];
}
if (connection == discoverConnection) {
[discoverData setLength:0];
}
if (connection == queryOrderConnection) {
[queryOrderData setLength:0];
}
if (connection == shareTemplateConnection) {
[shareTemplateData setLength:0];
}
if (connection == newReferralConnection) {