-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1802 lines (1650 loc) · 67.4 KB
/
main.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) 1998-2001 Nullsoft, Inc.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
======================================================================================================================
C H A N G E L O G
2.5.d - ??
2.6.02 - 2002/06/14 (rar) Lots o stuff has changed. Includes support for *.css file., change some of the way
(rar) this app logs stuff. added some global variables to help reduce redundant code. It'll get there.
2.6.05 - 2002/06/16 (rar) Added support for *.js file. Incorporated use of the WebDings font for control symbols.
2.6.10 - 2002/06/17 (rar) removed [p] button which clears the playlist and adds the selected song as the sole song to the liast
(rar) added [c] in [p]'s place, which allows you to cue a song to play direct after the song that is currently playing.
2.6.11 - 2002/06/19 (rar) Added Support for rebots.txt file, which basically disallows indexing of anything from this
"server". Content resides within this program.
(rar) Modified Cache expire time (from 1 hour to 1 year)
2.6.12 - 2002/07/07 (rar) Added \r's to ROBOTS.TXT output string.
2002/11/27 (rar) Added Self Installer using NSIS.
2.6.13 - 2002/11/27 (mm ) Added end-of-playlist actions
(mm ) Added cover art options
2.7.1 - 2002/11/29 (rar) Merge Michael Michon's changes into main.
2.7.2 - 2003/01/11 (rar) added doRecursiveAddDB prototype to list of function prototypes
(rar) printf statement to clear the line in the console when MakeDB is run
(rar) removed call to link *.js file since functions are only for windows CE and are commented out at
the moment. For some reason, the functions within were causing javascript errors
2.7.3 - 2003/01/12 (rar) added abiltiy to rescan files
(rar) modified the layout to include controls in middle frame
(rar) added 'listen' feature, which allows user to listen to the shoutcast stream directly from wwwinamp, and not have to
visit the shoutcast server
(rar) sprinkled a few comments here and there
(rar) change font size to use %, not px
(rar) updated copyright statement
2.7.4 - 2003/02/01 (rar) added cover art size, Winamp Class Name and library page size to options config file
(rar) Formatted vanilla config for easier readability, and created subsections for grouped items
(rar) added database list sorting
(rar) redid how the cover art shows up, when enabled. The size is now configureable, and will show up in a nested table to
the left in the song search.
(rar) updated the interface, with new color scheme and controls images. Nifty
(rar) started some work on reworking the LoadConfig. Trying to get it to be a little bit smarter
(rar) added support for multiple instances of winamp/wwwinamp. Good for broadcasting multiple servers off of one machine. see readme for more details.
(rar) Much more code commenting, cleanup, reorganization.
(rar) fixed a bug that would cause wwwinamp to crash when loading the playlist window & there were no items in the media library
(mm ) fixed BUG676947 [ 676947 ] Trailing slash on 'Listen' HyperLink
(mm ) fixed BUG683202 [ 683202 ] Cover art doesn't show with multiple paths
(mm ) added RFE683207 [ 683207 ] Library update should be general update
(mm ) added RFE683222 [ 683222 ] optional HTML header snippet
(mm ) added separate size for database list and now playing cover art
(mm ) added logic to prevent adding duplicate songs, usually caused by accidential double clicking.
(mm ) added ability to enqueue an entire folder by clicking on the folder name
(rar) fixed bug with "log into mp3j mode" button & html target tag
(rar) added a critical section mutex to media library update function
(rar) removed the need attach "m=left" on operation calls. this will save bandwith.
(rar) moved update button to media library window
(rar) redid how cover art works, uses dynamic urls and not folder names for url.
2.7.5 - 2003/03/05 (released 2.7.5)
2.7.6 - 2003/08/19 (rar) changed code to print out server address upon successful start up
(rar) made modifications to log_printf error messages in several spots, as well as cleaned up some verbiage, more work to be done
2.7.7 - 2003/12/07 (rar) made changes that caused access violations while loading files with long names. change wsprintf to strncpy. Also changed some test to make it less confusing to figure out how to use configuration files.
2.7.8 - 2003/12/30 (rar) included source as part of distro
(rar) added auto select to input field for search box
(rar) got sidetracked and re-formated output of startup messages
(rar) added user:pass to Interfaces output for easier cutting & pasting
(rar) added search google button
3.0.0 - 2005/11/16 (epn) fixed bug of 'adding duplicate album' after playlist clears from "end of list song add"
(epn) fixed bug where 'end of playlist' action would be triggered if you pressed 'STOP' on last track
(epn) added 'end of playlist' option 'play current playlist from beginning' to config file
(epn) added ability for administrator to Shutdown Server
TODO:
(epn) changed page and button layout. better able to skin and customize wwwinamp
(epn) able to allow users to download files from library
3.0.1 - 2006/08/24 (blg) changed frame layout, optimised for 1024 wide browser
(blg) removed window size operations from controls frame
(blg) added first and last links to back forward controls in library
(blg) fixed library position counters
(blg) added title attribute to ambiguous hrefs
(blg) library frame loads all by default (due to new frame layout)
(blg) moved library position counters to seperate lines, so links are always in same spot
(blg) library navigation links and position counters now at top of list as well as bottom
(blg) moved copyright info to playlist frame to increase visibility
(blg) fixed td and table width for currently playing track to maintain display consistency
3.0.1 b2 - 2006/09/23 (blg) added button allowing you to 'Jump' to certain part of library by entering a number in the search field
TODO:
(blg) a-z linking in library view (tho I have no idea how)
(blg) move playlist controls to playlist frame
*/
#define _CRT_SECURE_NO_DEPRECATE
#define DEBUG 0
#define SERV_VER "3.0.1 beta 2 (Built: " __DATE__ " " __TIME__ ")"
#define SERV_NAME "WWWinamp"
#define SERV_NAME_LONG "WWWinamp Remote Control Server"
#define COPYRIGHT "(C) Copyright 1998-2001 Nullsoft, Inc. All Rights Reserved."
#define BRANDING "Nullsoft"
#define BRANDING_URL "http://www.enusbaum.com/"
#define BRANDING_COPYRIGHT "(C) Copyright 2002-2003 Halo 8 Productions, Inc. All Rights Reserved."
#define MODIFICATION_COPYRIGHT "(C) Copyright 2005 <a href=\"http://www.enusbaum.com/\" target=\"_blank\">Eric Nusbaum</a>. All Rights Reserved"
#define MODIFICATION_COPYRIGHT1 "(C) Copyright 2006 <a href=\"http://www.realityloop.com/\" target=\"_blank\">Brian Gilbert</a>. All Rights Reserved"
#define ADMIN_URL "/mp3j"
#define STYLE_URL "/wwwinamp.css"
#define JSCRIPT_URL "/wwwinamp.js"
#define ROBOTS_URL "/robots.txt"
#define CONTROL_URL "/controls.gif"
#define CONTROLBG_URL "/controls.bg.gif"
#define CONTROLADM_URL "/controls.mp3j.gif"
#define SPACER_URL "/spacer.gif"
#define FOLDER_URL "/folder.gif"
#define PLAYLIST_DEFAULT "?m=left#playing"
/* ======================================================================================================================
INCLUDES
*/
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <stdarg.h> // allows passing an unknown number parameters
#include <signal.h>
#include <process.h>
#include <time.h>
#include <limits.h> // used for INT_MAX declaration
#include <sys/types.h>
#include <sys/stat.h>
#include "frontend.h"
#include "ipc_pe.h"
#ifndef min
#define min(x,y) (( x ) < ( y ) ? ( x ) : ( y ))
#define max(x,y) (( y ) < ( x ) ? ( x ) : ( y ))
#endif
/* ======================================================================================================================
TYPES
*/
typedef struct {
struct sockaddr_in sin;
int msgsock,s;
} cnx_inf;
typedef struct {
char *leading_path;
char file[MAX_PATH];
} dbType;
/* ======================================================================================================================
FUNCTION PROTOTYPES
*/
int main(int argc, char **argv);
int LoadConfig(void);
int getip(int which, struct sockaddr_in *sin);
int init_socketlib(int which);
int recv_string(int s, char *str, int maxlen);
int OpenSocket(short port, int mc);
int WaitForConnection(int sock,struct sockaddr_in *sin);
int sock_printf(int sock, char *fmt, ...);
int sock_send(int sock, char *buffer,int size);
int myrand(void);
int httpgetFile( int sock, char *fileName, char *ContentType, char *cacheString );
int hasCoverArt( char *fn );
char *getTimeStr(long alt_time);
char *gethost(struct sockaddr_in *sin);
char *extension(char *fn);
void mysrand (unsigned int seed);
void encodeLP(char *out, char *in);
void quit();
void makeDB();
void doRecursiveAddDB(char *leading_path, char *this_path);
void fixstring(char *in, char *out);
void unfixstring(char *in, char *out, int maxlen);
void log_printf(char *format, ...);
void launchthread(void *threadproc, void *data);
void setnonblock(int msgsock, int i);
void CloseSocket(int sock);
void http_handlereq(char *url, char *user_agent, char *encodedlp, int sock, struct sockaddr_in *sin);
void quitthread();
unsigned int WINAPI HandleConnection(void *p);
/* ======================================================================================================================
GLOBAL VARIABLES
*/
// configured/validated in LoadConfig()
short g_dst_port = 80;
int g_show_cover_art = 0;
int g_perform_lookups = 0;
int g_show_requests = 0;
int g_left_refresh = 60; // left panel refresh rate
int g_library_page_size = 500;
int g_cover_art_size_db = 100;
int g_cover_art_size_playing = 150;
char g_acclp[512] = ""; // encoded access password
char g_admlp[512] = ""; // encoded admin password
char g_acclp_c[512] = ""; // unencoded access password
char g_admlp_c[512] = ""; // unencoded admin password
char g_dst_ip[128] = "";
char g_ext_types[128] = "";
char g_sc_server[1024] = ""; // shoucast server info, null/empty for none
char g_winamp_class_name[128] = "Winamp v1.x";
char g_db_path[MAX_PATH] = "";
char g_log_file[MAX_PATH] = "";
char g_winamp_dir[MAX_PATH] = "C:\\Program Files\\Winamp";
char g_db_filelist[MAX_PATH] = "";
char g_filler_stream_url[MAX_PATH] = "";
char g_html_include_file[MAX_PATH] = "include.html";
char g_cover_art_root_dir[MAX_PATH] = "";
enum eop_modes{ eop_mode_silence=0, eop_mode_random=1, eop_mode_stream=2, eop_mode_repeat=3} g_eop_action = eop_mode_silence;
// rest of the global config
CRITICAL_SECTION log_mutex;
CRITICAL_SECTION library_mutex;
CRITICAL_SECTION playlist_mutex;
dbType *database;
int g_count;
int database_used;
int database_size;
int g_log = 1;
int g_playing_standby = 0;
int g_rand_next = 1;
int g_last_track = INT_MAX; // index to last added track
int g_last_action = 0; //Last action performed
int g_shutdown = 0;
char g_config_file[MAX_PATH];
char g_working_exe[MAX_PATH];
char g_working_dir[MAX_PATH];
char g_winamp_exe[MAX_PATH];
char g_working_full[MAX_PATH];
char g_working_name[MAX_PATH];
char g_ext_type_list[128] = "mp3;ogg;wav";
char g_cover_art_filename[256] = "folder.jpg";
char g_debug[16] = "DEBUG:\n\t";
char* g_include_html = 0;
/* ======================================================================================================================
FUNCTION IMPLEMENTATIONS
*/
int main(int argc, char **argv) {
int MainSocket;
char log_area[16]="[main]", *q;
struct sockaddr_in sin;
// added to get hostname in output
int iError;
char szHostName[20];
HOSTENT *hHostent;
InitializeCriticalSection(&log_mutex);
InitializeCriticalSection(&library_mutex);
InitializeCriticalSection(&playlist_mutex);
// get arg[0], the execution path and filename.
GetModuleFileName( NULL, g_working_full, MAX_PATH );
//set up g_working_dir
strncpy( g_working_dir, g_working_full, MAX_PATH ); // strncpy(s, t, n) copy n chars from s to t
q = g_working_dir + strlen(g_working_dir);
while (q >= g_working_dir && *q != '\\') q--;
*++q=0;
// set up g_working_exe
strncpy( g_working_exe, g_working_full, MAX_PATH );
q = g_working_exe + strlen(g_working_exe);
while (q >= g_working_exe && *q != '\\') q--;
strcpy( g_working_exe, ++q );
// set up g_working_name (get the wwwinamp portion to find the rest of the files.
strncpy( g_working_name, g_working_exe, MAX_PATH );
q = g_working_name + strlen(g_working_name);
while (q >= g_working_name && *q != '.') q--; *q=0;
// print out header to show what's going on...
printf("\n" "-- " SERV_NAME_LONG " " SERV_VER "\n" "-- " BRANDING_COPYRIGHT "\n" "-- " COPYRIGHT "\n" "-- " MODIFICATION_COPYRIGHT "\n" "-- Use \"%s config-file.ini\" to specify an configuration file.\n\n", g_working_exe);
if (argc > 1) { // our only argument to wwwinamp should be the config file name
if ( strstr(argv[1],"\\") ) strcpy( g_config_file, argv[1] ); // appears to be in a separate directory (\\ = \) so use whole string
else wsprintf( g_config_file, "%s%s", g_working_dir, argv[1] ); // looks like simple file name, so we append the current working dir
}
else wsprintf( g_config_file, "%s%s.ini", g_working_dir, g_working_name ); // we don't have a command line argument... so just use the default...
// Debug Statements
if (DEBUG) printf(
"%s"
"g_working_full: %s\n\t"
"g_working_dir : %s\n\t"
"g_working_exe : %s\n\t"
"g_working_name: %s\n\t"
"g_config_file : %s\n\t"
,
g_debug,
g_working_full,
g_working_dir,
g_working_exe,
g_working_name,
g_config_file
);
if ( LoadConfig() ) quit();
// Initialzing port
log_printf( "%s Initializing Port: ", log_area, g_dst_port );
if ( init_socketlib(1) < 0 ) {
printf( "Failure.\n" );
quit();
}
else printf( "Successful.\n" );
// Opening Socket
log_printf( "%s Opening Socket: ", log_area );
MainSocket = OpenSocket(g_dst_port,32);
if (MainSocket < 0) {
printf( "Failure.\n" );
quit();
}
else printf( "Successful.\n" );
makeDB();
iError=gethostname(szHostName,sizeof(szHostName));
// Tell the user that we could not get the host name
if(iError==SOCKET_ERROR) {
log_printf("%s ERROR: Could not get the host name.\n", log_area);
}
hHostent=gethostbyname(szHostName);
// Tell the user that we could not get the host address
if(hHostent==NULL) {
log_printf("%s ERROR: Could not get the host address.\n", log_area);
}
else {
// debug log_printf("Host name : %s\n", hHostent->h_name);
// debug log_printf("IP Address : %s\n", inet_ntoa(*((struct in_addr*)hHostent->h_addr)));
log_printf( "%s User Interface: http://%s@%s:%d\n", log_area, g_acclp_c, inet_ntoa(*((struct in_addr*)hHostent->h_addr)), g_dst_port );
log_printf( "%s Admin Interface: http://%s@%s:%d%s\n", log_area, g_admlp_c, inet_ntoa(*((struct in_addr*)hHostent->h_addr)), g_dst_port, ADMIN_URL );
}
log_printf("%s %s Start-up: Successful. System available for users.\n\n", log_area, SERV_NAME, g_dst_port);
mysrand( (unsigned)time( NULL ) );
while (g_shutdown == 0) {
int msgsock;
cnx_inf *c = (cnx_inf*) calloc( 1, sizeof(cnx_inf) );
msgsock = WaitForConnection( MainSocket, &sin );
memcpy( &c->sin, &sin, sizeof(sin) );
c->msgsock = msgsock;
c->s = MainSocket;
launchthread( (void*)HandleConnection, (void *)c );
}
CloseSocket(MainSocket);
log_printf( "%s Shutting down socket.", log_area );
init_socketlib(0);
return 0;
}
int LoadConfig(void) {
FILE *conf=NULL;
FILE* includeFile;
int line_number = 0;
char log_area[8] = "[conf]";
char buffer[1024];
char *configarray[] = {
"AccessLoginPassword",
"AdminLoginPassword",
"NameLookups",
"ShowRequests",
"ShowCoverArt",
"Port",
"RefreshRate",
"LibraryPageSize",
"CoverArtSizeLibrary",
"CoverArtSizePlaylist",
"EndOfPlaylistAction",
"WinampDir",
"LogFile",
"DBPath",
"DBFileList",
"Extensions",
"ShoutCastServer",
"WinampClassName",
"CoverArtFilename",
"FillerStreamURL",
"CoverArtRootDir",
"HTMLIncludeFile",
"IP"
};
conf = fopen( g_config_file, "rb" );
log_printf( "%s Loading Configuration: %s.\n", log_area, g_config_file );
if (!conf) {
printf( "%s ERROR: couldn't find config file.\n", log_area );
return -1;
}
while (1) {
int i = 0;
char *pbuffer;
char *tok;
fgets(buffer, sizeof(buffer), conf);
if ( feof(conf) ) break; // end of file, quit
line_number++; // increment the line number
// terminate the string where we find a carriage return or line feed
while ( buffer[strlen(buffer)-1]=='\n' || buffer[strlen(buffer)-1]=='\r' ) buffer[strlen(buffer)-1]='\0';
pbuffer=buffer;
while (*pbuffer == ' ' || *pbuffer == '\t') pbuffer++; // skip white space
// if we encounter a ; [ or #, or if it's empty, then we just go to the next line
if (!*pbuffer || *pbuffer == ';' || *pbuffer == '[' || *pbuffer == '#') continue;
tok=pbuffer;
while (*pbuffer != '=' && *pbuffer != '\r' && *pbuffer) pbuffer++;
if (!*pbuffer) { // basically, if we got to the end of the string with no '='
log_printf( "%s ERROR: line %d: invalid configuration syntax.\n", log_area, line_number );
return -1;
}
*pbuffer++=0;
for (i=0; i < sizeof(configarray)/sizeof(configarray[0]) && stricmp( configarray[i], tok ); i++); // after done, i will = index of configuration
if (i >= sizeof(configarray)/sizeof(configarray[0])) {
log_printf( "%s ERROR: line %d: invalid configuration option.\n", log_area, line_number);
return -1;
}
tok=pbuffer;
while (*tok == ' ') tok++;
switch (i) {
// Login & Password Strings
case 0: encodeLP(g_acclp, tok); strcpy(g_acclp_c, tok); break; // "AccessLoginPassword"
case 1: encodeLP(g_admlp, tok); strcpy(g_admlp_c, tok); break; // "AdminLoginPassword"
// boolean values
case 2: g_perform_lookups = !!atoi(tok); break; // "NameLookups"
case 3: g_show_requests = !!atoi(tok); break; // "ShowRequests"
case 4: g_show_cover_art = !!atoi(tok); break; // "ShowCoverArt"
// Integer Values // atoi() returns zero if it can't find anything int in the string.
case 5: g_dst_port = atoi(tok); break; // "Port"
case 6: g_left_refresh = atoi(tok); break; // "RefreshRate"
case 7: g_library_page_size = atoi(tok); break; // "LibraryPageSize"
case 8: g_cover_art_size_db = atoi(tok); break; // "CoverArtSizeDB"
case 9: g_cover_art_size_playing = atoi(tok); break; // "CoverArtSizePlaying"
case 10: g_eop_action = atoi(tok); break; // "EndOfPlaylistAction"
// strings
case 11: strcpy(g_winamp_dir, tok); break; // "WinampDir"
case 12: strcpy(g_log_file, tok); break; // "LogFile"
case 13: strcpy(g_db_path, tok); break; // "DBPath"
case 14: strcpy(g_db_filelist, tok); break; // "DBFileList"
case 15: strcpy(g_ext_types, tok); break; // "Extensions"
case 16: strcpy(g_sc_server, tok); break; // "ShoutCastServer"
case 17: strcpy(g_winamp_class_name, tok); break; // "WinampClassName"
case 18: strcpy(g_cover_art_filename, tok); break; // "CoverArtFilename"
case 19: strcpy(g_filler_stream_url, tok); break; // "FillerStreamURL"
case 20: strcpy(g_cover_art_root_dir, tok); break; // "CoverArtRootDir"
case 21: strcpy(g_html_include_file, tok); break; // "HTMLIncludeFile"
case 22: if (stricmp(tok,"ANY")) strcpy(g_dst_ip,tok); break; // "IP"
default: break;
}
}
fclose(conf);
// check to see if the page sizes are sane, and if they're not, make them so
g_library_page_size = (g_library_page_size > 10000) ? 10000 : g_library_page_size;
g_library_page_size = (g_library_page_size < 25) ? 25 : g_library_page_size;
if (DEBUG) printf ( "g_library_page_size set to %d\n", g_library_page_size);
if (DEBUG) printf ( "g_winamp_class_name to %s\n", g_winamp_class_name);
// set up the log file
if (!stricmp(g_log_file,"none") || !stricmp(g_log_file,"/dev/null") || strlen(g_log_file) < 1) g_log=0;
else {
if (!strstr(g_log_file,"\\")) {
char a[MAX_PATH] = "";
strcat(a, g_working_dir);
strcat(a, g_log_file);
strcpy(g_log_file, a);
}
}
// set up the file types
if (g_ext_types[0]) {
char *p=g_ext_type_list;
strcpy(g_ext_type_list,g_ext_types);
g_ext_type_list[strlen(g_ext_type_list)+1]=0;
while (p && *p) {
p=strstr(p,";");
if (p && *p) *p++=0;
}
}
wsprintf( g_winamp_exe, "%s\\winamp.exe", g_winamp_dir );
if(g_include_html!=0) free(g_include_html); // deallocate the previous text
// load the include file, if it exists
if(includeFile=fopen(g_html_include_file,"r")){
struct _stat buf;
long fileSize;
int index=0;
_stat(g_html_include_file, &buf );
log_printf( "%s loading HTML include file: %s.\n", log_area, g_html_include_file);
fileSize=buf.st_size;
g_include_html=malloc(fileSize+1);
while(!feof(includeFile)){
index+=fread(g_include_html+index,1,fileSize,includeFile);
}
g_include_html[index]=0; // null-terminate the string after read from file
fclose(includeFile);
}
log_printf( "%s Successfully Loaded Configuration.\n", log_area );
return 0;
}
unsigned int WINAPI HandleConnection(void *p) {
cnx_inf *c=(cnx_inf *)p;
int sock=(int)c->msgsock;
int i=0;
char req[1024];
char user_agent[1024]="";
char encodedlp[1024]="";
char buf[1024];
setnonblock(sock,1);
if (recv_string(sock,req,sizeof(req)) > 2) {
while (recv_string(sock,buf,sizeof(buf)) > 2) {
buf[sizeof(buf)-1]=0;
if (!strnicmp(buf,"User-Agent:",strlen("User-Agent:"))) {
char *p=buf+strlen("User-Agent:");
while (*p == ' ') p++;
strncpy(user_agent,p,sizeof(user_agent)-1);
user_agent[sizeof(user_agent)-1]=0;
}
if (!strnicmp(buf,"Authorization: Basic ",strlen("Authorization: Basic "))) {
char *p=buf+strlen("Authorization: Basic ");
while (*p == ' ') p++;
strncpy(encodedlp,p,sizeof(encodedlp)-1);
encodedlp[sizeof(encodedlp)-1]=0;
}
}
if (!strnicmp(req,"GET ",4)) {
char *p=req+4,*p2,*p3;
p3=p2=p;
while (*p2 != ' ' && *p2) p2++;
if (p2 == p) {
p="/";
}
else {
if (*p2) *p2=0;
if (strstr(p,"//")) {
p=strstr(p,"//");
p+=3;
while (*p != '/' && *p) p++;
if (!*p) p="/";
}
}
setnonblock(sock,0);
http_handlereq(p, user_agent, encodedlp, sock, &c->sin);
}
}
CloseSocket(sock);
free(p);
quitthread();
return 0;
}
static void parselist(char *out, char *in) {
int nt=8, inquotes=0, neednull=0;
char c;
while (*in) {
c=*in++;
if (c >= 'A' && c <= 'Z') c+='a'-'A';
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
neednull=1;
*out++=c;
}
else if (c == '\"') {
inquotes=!inquotes;
if (!inquotes) {
*out++=0;
if (!nt--) break;
neednull=0;
}
}
else {
if (inquotes) *out++=c;
else if (neednull) {
*out++=0;
if (!nt--) break;
neednull=0;
}
}
}
*out++=0;
*out++=0;
}
static int in_string(char *string, char *substring) {
int len = strlen(substring)-2;
if (len<0) return 1;
while (*string) {
if ((string[0]|32) == (substring[0]|32) && (string[1]|32) == (substring[1]|32)) {
if (len < 1 || !strnicmp(string+2,substring+2,len)) return 1;
}
string++;
}
return 0;
}
static int substr_search(char *bigtext, char *littletext_list) {
while (*littletext_list) {
if (!in_string(bigtext,littletext_list)) return 0;
while (*littletext_list) littletext_list++;
littletext_list++;
}
return 1;
}
void fixstring(char *in, char *out) {
while (*in) {
if ((*in >= 'A' && *in <= 'Z')||
(*in >= 'a' && *in <= 'z')||
(*in >= '0' && *in <= '9')) *out++=*in++;
else {
int i=*in++;
wsprintf(out,"%%%02X",i);
out += 3;
}
}
*out=0;
}
void unfixstring(char *in, char *out, int maxlen) {
while (*in && maxlen) {
if (*in == '%' && in[1] != '%' && in[1]) {
int a=0;
int b=0;
for ( b = 0; b < 2; b ++) {
int r=in[1+b];
if (r>='0'&&r<='9') r-='0';
else if (r>='a'&&r<='z') r-='a'-10;
else if (r>='A'&&r<='Z') r-='A'-10;
else break;
a*=16;
a+=r;
}
if (b < 2) *out++=*in++;
else {
*out++=a;
in += 3;
}
}
else *out++=*in++;
maxlen--;
}
*out=0;
}
char *getTimeStr(long alt_time) {
static char timeret[50];
int alt_hour, alt_min, alt_sec;
alt_hour = alt_time/3600;
alt_min = (alt_time % 3600) / 60;
alt_sec = alt_time % 60;
if (alt_hour) wsprintf(timeret,"%dh %02dm %02ds", alt_hour, alt_min, alt_sec);
else wsprintf(timeret,"%dm %02ds", alt_min, alt_sec);
return timeret;
}
int getip(int which, struct sockaddr_in *sin) {
if (which==3) return (sin->sin_addr.s_addr>>24);
if (which==2) return ((sin->sin_addr.s_addr&0xff0000)>>16);
if (which==1) return ((sin->sin_addr.s_addr&0xff00)>>8);
return (sin->sin_addr.s_addr&0xff);
}
char *gethost(struct sockaddr_in *sin) {
static char hname[256];
char ipaddr[16] = "";
wsprintf(ipaddr, "%d.%d.%d.%d", getip(0,sin),getip(1,sin),getip(2,sin),getip(3,sin));
if (g_perform_lookups) {
int ip=inet_addr(ipaddr);
struct hostent *p = gethostbyaddr((char *)&ip,4,AF_INET);
if (p) wsprintf(hname,"%s",p->h_name);
else wsprintf(hname, "%s", ipaddr);
}
else wsprintf(hname, "%s", ipaddr);
return hname;
}
int recv_string(int s, char *str, int maxlen) {
int sleeps=0;
int p=0;
do {
int t;
do {
t=recv(s,str+p,1,0);
if (t != 1) {
if (!t || (t < 0 && WSAGetLastError() != WSAEWOULDBLOCK)) { str[p]=0; return -1; }
Sleep(100);
if (sleeps++ > 10*10) { str[p]=0; return -1; }
}
if (str[p] == '\r') t=0;
} while (t!=1);
} while (str[p] != '\n' && ++p < maxlen-1);
str[p--]=0;
while (str[p] == '\n' && p > 0) {
str[p--]=0;
}
if (p < 0) p = 0;
return p;
}
void launchthread(void *threadproc, void *data) {
_beginthread(threadproc,0,data);
}
void quitthread() {
_endthread();
}
void setnonblock(int msgsock, int i) {
ioctlsocket(msgsock,FIONBIO,&i);
}
int OpenSocket(short port, int mc) {
char *p=g_dst_ip;
int sock;
struct sockaddr_in sin;
sock = socket(AF_INET,SOCK_STREAM,0);
if (sock < 0) return -1;
memset((char *) &sin, 0,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons( port );
if (*p) sin.sin_addr.s_addr = inet_addr(p);
if (!*p || !sin.sin_addr.s_addr) sin.sin_addr.s_addr=INADDR_ANY;
if (bind (sock,(struct sockaddr *)&sin,sizeof(sin))) {
CloseSocket(sock);
return -1;
}
if (listen(sock,mc)==-1) {
CloseSocket(sock);
return -1;
}
return sock;
}
void CloseSocket(int sock) {
shutdown(sock, 2);
closesocket(sock);
}
int init_socketlib(int which) {
WSADATA wsaData;
if (which) {
if ( WSAStartup( MAKEWORD(1, 1), &wsaData ) ) return -1;
}
else {
WSACleanup();
}
return 0;
}
char *extension(char *fn) {
char *s = fn + strlen(fn);
while (s >= fn && *s != '.' && *s != '\\') s--;
if (s < fn) return "";
if (*s == '\\') return "";
return (s+1);
}
void quit() {
printf("Hit any key to close...");
fflush(stdout);
getch();
exit(1);
}
void log_printf(char *format, ...) {
char buf[1024];
time_t t;
struct tm *tm;
va_list ar;
va_start(ar,format);
t = time(NULL);
tm = localtime(&t);
wsprintf(buf,"[%02d/%02d/%02d %02d:%02d:%02d]%s",
tm->tm_mon+1,
tm->tm_mday,
tm->tm_year+1900,
tm->tm_hour,
tm->tm_min,
tm->tm_sec,
format);
EnterCriticalSection(&log_mutex);
if (g_log) {
FILE *fp = fopen(g_log_file,"a+");
if (fp) {
vfprintf(fp,buf,ar);
fclose(fp);
}
}
vprintf(buf,ar);
fflush(stdout);
LeaveCriticalSection(&log_mutex);
va_end(ar);
}
static int in_list(char *list, char *v) {
while (*list) {
if (!stricmp(v,list)) return 1;
list+=strlen(list)+1;
}
return 0;
}
int sock_printf(int sock, char *fmt, ...) {
char buffer[2048]={0,};
char *p=buffer;
int i;
va_list ar;
va_start(ar,fmt);
vsprintf(buffer,fmt,ar);
va_end(ar);
i=strlen(buffer);
while (i > 0) {
int r=send(sock,p,i,0);
if (r > 0) i-=r;
else {
if (r < 0) {
if (WSAGetLastError() != WSAEWOULDBLOCK) break;
else Sleep(250);
}
}
}
return strlen(buffer)-i;
}
int sock_send(int sock, char *buffer,int size) {
char *p=buffer;
int i=size;
while (i > 0) {
int r=send(sock,p,i,0);
if (r > 0) i-=r;
else {
if (r < 0) {
if (WSAGetLastError() != WSAEWOULDBLOCK) break;
else Sleep(250);
}
}
}
return size-i;
}
void doRecursiveAddDB(char *leading_path, char *this_path) {
char log_area[20] = "doadddb -->";
char maskstr[MAX_PATH];
WIN32_FIND_DATA d;
HANDLE h;
char *leading_path_suffixed;
wsprintf(maskstr,"%s%s",leading_path,leading_path[strlen(leading_path)-1]=='\\' ? "" : "\\");
leading_path_suffixed=strdup(maskstr);
if (*this_path) {
strcat(maskstr,this_path);
if (this_path[strlen(this_path)-1]!='\\') strcat(maskstr,"\\");
}
strcat(maskstr,"*.*");
h = FindFirstFile(maskstr,&d);
//printf("%s\n",maskstr);
if ( h != INVALID_HANDLE_VALUE ) {
do {
if ( !(d.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) ) {
char *p=extension(d.cFileName);
if (*p && in_list(g_ext_type_list,p)) {
if (database_used >= database_size) {
if (database) {
database_size*=2;
database = realloc(database,database_size*sizeof(dbType));
}
else {
database_size=32;
database = malloc(database_size*sizeof(dbType));
}
}
wsprintf(database[database_used].file,"%s%s%s",this_path,this_path[0]?"\\":"",d.cFileName);
database[database_used].leading_path=leading_path_suffixed;
database_used++;
if (!(database_used%16)) {
int x;
char s[1024];
wsprintf(s,"Scanning: %s",d.cFileName);
x=79-strlen(s); while (x-- > 0) strcat(s," ");
s[79]=0;
printf("%s\r",s);
}
g_count++;
}
}
else {
if (d.cFileName[0] != '.') {
char ps[MAX_PATH];
wsprintf(ps,"%s%s%s",this_path,this_path[0]?"\\":"",d.cFileName);
doRecursiveAddDB( leading_path, ps );
}
}
} while (FindNextFile(h,&d));
FindClose(h);
}
}
int _deepcompare( const char *name1, const char *name2, int debug ) {
int dirCmp;
char *st1 = strstr( name1, "\\" ), d1[MAX_PATH], *p1;
char *st2 = strstr( name2, "\\" ), d2[MAX_PATH], *p2;
if ( !st1 && !st2 ) return strnicmp( name1, name2, MAX_PATH ); // both are files, evaluate using stricmp.
if ( st1 && !st2 ) return 1; // 1st file is a dir and 2nd isn't. 2nd gets precedence
if ( !st1 && st2 ) return -1; // 2nd file is a dir and 1st isn't. 1st gets precedence
// now we are getting tricky...
// 1st and 2nd are dirs, we need to compare the 2 dir names and
// if they are the same, recurse, else return the stricmp.
strncpy( d1, name1, MAX_PATH); p1 = d1; while (p1 && *p1++) if (*p1 == '\\') *p1=0; // get 1st dir name in 2nd
strncpy( d2, name2, MAX_PATH); p2 = d2; while (p2 && *p2++) if (*p2 == '\\') *p2=0; // get 1st dir name in 2nd
dirCmp = strnicmp( d1, d2, MAX_PATH );
if (dirCmp) return dirCmp;
else return _deepcompare( ++st1, ++st2, debug );
}
int _compare( const dbType *arg1, const dbType *arg2 ) {
// basically get the file name from the dbType objects, and pass to _deepcompare.
char file1[MAX_PATH];
char file2[MAX_PATH];
int test;
strncpy(file1, arg1->file, MAX_PATH);
strncpy(file2, arg2->file, MAX_PATH);
test = _deepcompare( file1, file2, 0 );
return test;
}
void encodeLP(char *out, char *in) {
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int shift = 0;
int accum = 0;
while (*in) {
if (*in) {
accum <<= 8;
shift += 8;
accum |= *in++;
}
while ( shift >= 6 ) {
shift -= 6;
*out++ = alphabet[(accum >> shift) & 0x3F];
}
}
if (shift == 4) {
*out++ = alphabet[(accum & 0xF)<<2];
*out++ = '=';
}
else
if (shift == 2) {
*out++ = alphabet[(accum & 0x3)<<4];
*out++ = '=';
*out++ = '=';
}
*out++=0;