StringsPlugin.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include "../../structures.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. struct pluginlink * mypl;
  10. int count_load_str_proxy_from_file=0,count_load_str_admin_from_file=0;
  11. int count_str_proxy_in_3proxy=0,count_str_admin_in_3proxy=0;
  12. char ** old_proxy_table=NULL;
  13. char ** old_admin_table=NULL;
  14. struct schedule myschedule;
  15. char **load_string(FILE *f,int max_count_str, int *countloadstr,
  16. char *start,char *stop,char **table_3proxy)
  17. {
  18. int cstr=0,i=0;
  19. char tmpbuf1[1024],*rstr,*pt=NULL,*p=NULL;
  20. char **old_table;
  21. tmpbuf1[0]='\0';
  22. fseek(f,0,SEEK_SET);
  23. /*find start service section*/
  24. while(!feof(f))
  25. {
  26. fgets(tmpbuf1, 1023,f);
  27. if ((strstr(tmpbuf1,start))!=NULL) { i++; break; }
  28. tmpbuf1[0]='\0';
  29. }
  30. if (i==0){
  31. fprintf(stderr,"Error StringsPlugin: No start section %s strings! \n",start);
  32. return NULL;
  33. }
  34. /*create table for old strings */
  35. old_table=(char **)mypl->myalloc(max_count_str*sizeof(char *));
  36. memset(old_table,0,max_count_str*sizeof(char *));
  37. /*load from file new strings */
  38. i=0;
  39. while ( !feof(f) || i< max_count_str)
  40. {
  41. fgets(tmpbuf1, 1023,f);
  42. if ((strstr(tmpbuf1,stop))!=NULL) { break; }
  43. if (strstr(tmpbuf1,"[end]")==NULL)
  44. {
  45. /* find and replace \n \r*/
  46. rstr = tmpbuf1;
  47. while (*rstr!='\0')
  48. {
  49. if (*rstr=='\r' || *rstr=='\n' )
  50. { *rstr='\0'; rstr++; }
  51. rstr++;
  52. }
  53. while ((rstr=strstr(tmpbuf1,"\\n")))
  54. {
  55. if (rstr!=NULL){ *rstr='\r'; rstr++; *rstr='\n'; }
  56. }
  57. /* add string */
  58. if (pt!=NULL) { cstr=cstr+(int)strlen(pt); }
  59. cstr=cstr+(int)strlen(tmpbuf1)+1;
  60. p = (char *)mypl->myalloc(cstr);
  61. if (pt!=NULL)
  62. {
  63. strcpy(p, pt);
  64. strcat(p, tmpbuf1);
  65. mypl->myfree(pt);
  66. }
  67. else
  68. { strcpy(p, tmpbuf1); }
  69. pt=p; cstr=0;
  70. }
  71. else
  72. {
  73. /* save old string */
  74. old_table[i]=table_3proxy[i];
  75. /* replace string */
  76. table_3proxy[i]=pt;
  77. pt=NULL; i++;
  78. }
  79. }
  80. if(pt)mypl->myfree(pt);
  81. *countloadstr=i;
  82. if (i==0) { mypl->myfree(old_table); old_table=NULL; }
  83. return old_table;
  84. }
  85. /*-------------------------------------------------------------------*/
  86. static int restore_old_table(void * v)
  87. {
  88. int i; char *p=NULL;
  89. /* restore old proxy table */
  90. if(old_proxy_table)
  91. {
  92. for(i=0; i < count_str_proxy_in_3proxy; i++){
  93. p=mypl->proxy_table[i];
  94. mypl->proxy_table[i]=old_proxy_table[i];
  95. mypl->myfree(p);
  96. }
  97. mypl->myfree(old_proxy_table);
  98. old_proxy_table = NULL;
  99. }
  100. p=NULL;
  101. /* restore old admin table */
  102. if(old_admin_table)
  103. {
  104. for(i=0; i < count_str_admin_in_3proxy; i++){
  105. p=mypl->admin_table[i];
  106. mypl->admin_table[i]=old_admin_table[i];
  107. mypl->myfree(p);
  108. }
  109. mypl->myfree(old_admin_table);
  110. old_admin_table = NULL;
  111. }
  112. /*return 1 delete job, 0 no delete!!! :)*/
  113. return 1;
  114. }
  115. /*-------------------------------------------------------------------*/
  116. #ifdef _WIN32
  117. BOOL WINAPI DllMain( HINSTANCE hModule,
  118. DWORD ul_reason_for_call,
  119. LPVOID lpReserved
  120. )
  121. {
  122. if (ul_reason_for_call == DLL_PROCESS_DETACH)
  123. {
  124. if(old_proxy_table) { restore_old_table(NULL); }
  125. }
  126. return TRUE;
  127. }
  128. #endif
  129. #ifdef WATCOM
  130. #pragma aux start "*" parm caller [ ] value struct float struct routine [eax] modify [eax ecx edx]
  131. #undef PLUGINCALL
  132. #define PLUGINCALL
  133. #endif
  134. PLUGINAPI int PLUGINCALL start(struct pluginlink * pluginlink,
  135. int argc, char** argv)
  136. {
  137. FILE *f=NULL;
  138. mypl=pluginlink;
  139. if(old_proxy_table||old_admin_table) restore_old_table(NULL);
  140. if(!(f=fopen(argv[1],"r"))) return 1001;
  141. /*count string service PROXY in 3proxy */
  142. count_str_proxy_in_3proxy=0;
  143. while( mypl->proxy_table[count_str_proxy_in_3proxy] != NULL )
  144. { count_str_proxy_in_3proxy++; }
  145. /*count string service ADMIN in 3proxy */
  146. count_str_admin_in_3proxy=0;
  147. while( mypl->admin_table[count_str_admin_in_3proxy] != NULL )
  148. { count_str_admin_in_3proxy++; }
  149. /*---- load string for PROXY service ----*/
  150. old_proxy_table=load_string(f,count_str_proxy_in_3proxy,
  151. &count_load_str_proxy_from_file,
  152. "[--proxy--]","[/--proxy--]",
  153. mypl->proxy_table);
  154. if (old_proxy_table == NULL)
  155. {
  156. fprintf(stderr,"Error StringsPlugin: No load string from file %s \
  157. for service PROXY !\n",argv[1]);
  158. }
  159. if(count_str_proxy_in_3proxy!= count_load_str_proxy_from_file)
  160. {
  161. fprintf(stderr,"Warning StringsPlugin: Count string for service PROXY in\
  162. 3proxy not equality count string in file %s \n",argv[1]);
  163. }
  164. /*---- load string for ADMIN service ----*/
  165. old_admin_table=load_string(f,count_str_admin_in_3proxy,
  166. &count_load_str_admin_from_file,
  167. "[--admin--]","[/--admin--]",
  168. mypl->admin_table);
  169. if (old_admin_table == NULL)
  170. {
  171. fprintf(stderr,"Error StringsPlugin: No load string from file %s \
  172. for service ADMIN !\n",argv[1]);
  173. }
  174. if(count_str_admin_in_3proxy!= count_load_str_admin_from_file)
  175. {
  176. fprintf(stderr,"Warning StringsPlugin: Count string for service ADMIN in\
  177. 3proxy not equality count string in file %s\n",argv[1]);
  178. }
  179. fclose(f);
  180. /* create job shedule for processing reload */
  181. if(*pluginlink->schedule!=&myschedule){
  182. memset(&myschedule,0,sizeof(struct schedule));
  183. myschedule.type=NONE;
  184. myschedule.function=restore_old_table;
  185. myschedule.next = *pluginlink->schedule;
  186. *pluginlink->schedule=&myschedule;
  187. }
  188. return 0;
  189. }
  190. #ifdef __cplusplus
  191. extern }
  192. #endif