MD_Init.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include <stdio.h>
  2. #if defined(WIN32) || defined(_MSC_VER)
  3. #include <winsock2.h>
  4. #else
  5. #include <termios.h>
  6. #include <signal.h>
  7. #endif
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <time.h>
  12. #include "MagiDoor.h"
  13. #if defined(WIN32) || defined(_MSC_VER)
  14. #define PATH_SEP '\\'
  15. #else
  16. #define PATH_SEP '/'
  17. #endif
  18. MDDoorControl_t mdcontrol;
  19. #if defined(WIN32) || defined(_MSC_VER)
  20. #else
  21. struct termios ttysave;
  22. #endif
  23. time_t mdtimeout;
  24. time_t mdtimeremaining;
  25. static void md_cfg_read_line(char *buffer, int len, FILE *fptr) {
  26. fgets(buffer, len, fptr);
  27. if (buffer[strlen(buffer) - 1] == '\n') {
  28. buffer[strlen(buffer) - 1] = '\0';
  29. }
  30. if (buffer[strlen(buffer) - 1] == '\r') {
  31. buffer[strlen(buffer) - 1] = '\0';
  32. }
  33. }
  34. int read_door32(const char *dropfile) {
  35. FILE *fptr;
  36. char buffer[256];
  37. char *ptr1;
  38. fptr = fopen(dropfile, "r");
  39. if (!fptr) {
  40. return -1;
  41. }
  42. md_cfg_read_line(buffer, 256, fptr); // com type
  43. md_cfg_read_line(buffer, 256, fptr); // socket handle
  44. mdcontrol.socket = strtol(buffer, NULL, 10);
  45. md_cfg_read_line(buffer, 256, fptr); // baud rate
  46. md_cfg_read_line(buffer, 256, fptr); // BBS ID
  47. md_cfg_read_line(buffer, 256, fptr); // User record pos (1 based)
  48. md_cfg_read_line(buffer, 256, fptr); // User's real name
  49. ptr1 = strrchr(buffer, ' ');
  50. if (ptr1 != NULL) {
  51. *ptr1 = '\0';
  52. ptr1++;
  53. strncpy(mdcontrol.user_firstname, buffer, 32);
  54. strncpy(mdcontrol.user_lastname, ptr1, 32);
  55. } else {
  56. strncpy(mdcontrol.user_firstname, buffer, 32);
  57. memset(mdcontrol.user_lastname, '\0', 32);
  58. }
  59. md_cfg_read_line(buffer, 256, fptr); // User's handle
  60. strncpy(mdcontrol.user_alias, buffer, 32);
  61. md_cfg_read_line(buffer, 256, fptr); // User's sec level
  62. mdcontrol.user_seclevel = strtol(buffer, NULL, 10);
  63. md_cfg_read_line(buffer, 256, fptr); // time left (minutes)
  64. mdcontrol.user_timeleft = strtol(buffer, NULL, 10) * 60;
  65. md_cfg_read_line(buffer, 256, fptr); // emulation
  66. md_cfg_read_line(buffer, 256, fptr); // node no
  67. mdcontrol.node = strtol(buffer, NULL, 10);
  68. return 0;
  69. }
  70. int read_doorsys(const char *dropfile) {
  71. FILE *fptr;
  72. char buffer[256];
  73. char *ptr1;
  74. fptr = fopen(dropfile, "r");
  75. if (!fptr) {
  76. return -1;
  77. }
  78. md_cfg_read_line(buffer, 256, fptr); // Comport
  79. md_cfg_read_line(buffer, 256, fptr); // Ebaud
  80. md_cfg_read_line(buffer, 256, fptr); // data bits
  81. md_cfg_read_line(buffer, 256, fptr); // node number
  82. mdcontrol.node = strtol(buffer, NULL, 10);
  83. md_cfg_read_line(buffer, 256, fptr); // Lbaud
  84. md_cfg_read_line(buffer, 256, fptr); // Screen Display
  85. md_cfg_read_line(buffer, 256, fptr); // Printer On
  86. md_cfg_read_line(buffer, 256, fptr); // Page Bell
  87. md_cfg_read_line(buffer, 256, fptr); // Caller alarm
  88. md_cfg_read_line(buffer, 256, fptr); // User Name
  89. ptr1 = strrchr(buffer, ' ');
  90. if (ptr1 != NULL) {
  91. *ptr1 = '\0';
  92. ptr1++;
  93. strncpy(mdcontrol.user_firstname, buffer, 32);
  94. strncpy(mdcontrol.user_lastname, ptr1, 32);
  95. } else {
  96. strncpy(mdcontrol.user_firstname, buffer, 32);
  97. memset(mdcontrol.user_lastname, '\0', 32);
  98. }
  99. md_cfg_read_line(buffer, 256, fptr); // Location
  100. strncpy(mdcontrol.user_location, buffer, 32);
  101. md_cfg_read_line(buffer, 256, fptr); // voice phone
  102. md_cfg_read_line(buffer, 256, fptr); // data phone
  103. md_cfg_read_line(buffer, 256, fptr); // password
  104. md_cfg_read_line(buffer, 256, fptr); // sec level
  105. mdcontrol.user_seclevel = strtol(buffer, NULL, 10);
  106. md_cfg_read_line(buffer, 256, fptr); // calls
  107. md_cfg_read_line(buffer, 256, fptr); // last login
  108. md_cfg_read_line(buffer, 256, fptr); // time left (seconds)
  109. mdcontrol.user_timeleft = strtol(buffer, NULL, 10);
  110. md_cfg_read_line(buffer, 256, fptr); // time left (minutes)
  111. md_cfg_read_line(buffer, 256, fptr); // graphics
  112. md_cfg_read_line(buffer, 256, fptr); // screen len
  113. md_cfg_read_line(buffer, 256, fptr); // usermode
  114. md_cfg_read_line(buffer, 256, fptr); // extra 1
  115. md_cfg_read_line(buffer, 256, fptr); // extra 2
  116. md_cfg_read_line(buffer, 256, fptr); // expiry date
  117. md_cfg_read_line(buffer, 256, fptr); // rec num
  118. md_cfg_read_line(buffer, 256, fptr); // protocol
  119. md_cfg_read_line(buffer, 256, fptr); // uploads
  120. md_cfg_read_line(buffer, 256, fptr); // downloads
  121. md_cfg_read_line(buffer, 256, fptr); // Lim down k
  122. md_cfg_read_line(buffer, 256, fptr); // Lim down k2
  123. md_cfg_read_line(buffer, 256, fptr); // DOB
  124. md_cfg_read_line(buffer, 256, fptr); // User Base
  125. md_cfg_read_line(buffer, 256, fptr); // Message Base
  126. md_cfg_read_line(buffer, 256, fptr); // Sysop Name
  127. strncpy(mdcontrol.sysop_name, buffer, 32);
  128. md_cfg_read_line(buffer, 256, fptr); // Handle
  129. strncpy(mdcontrol.user_alias, buffer, 32);
  130. md_cfg_read_line(buffer, 256, fptr); // Next Event
  131. md_cfg_read_line(buffer, 256, fptr); // Error Free
  132. md_cfg_read_line(buffer, 256, fptr); // Always N
  133. md_cfg_read_line(buffer, 256, fptr); // Always Y
  134. md_cfg_read_line(buffer, 256, fptr); // Def Colour
  135. md_cfg_read_line(buffer, 256, fptr); // Always 0
  136. md_cfg_read_line(buffer, 256, fptr); // Last Login 2
  137. md_cfg_read_line(buffer, 256, fptr); // Time Login
  138. md_cfg_read_line(buffer, 256, fptr); // Last Login Time
  139. md_cfg_read_line(buffer, 256, fptr); // Max Integer
  140. md_cfg_read_line(buffer, 256, fptr); // Downs today
  141. md_cfg_read_line(buffer, 256, fptr); // Upload K
  142. md_cfg_read_line(buffer, 256, fptr); // Download K
  143. md_cfg_read_line(buffer, 256, fptr); // Comment
  144. md_cfg_read_line(buffer, 256, fptr); // Always 0
  145. md_cfg_read_line(buffer, 256, fptr); // posted
  146. fclose(fptr);
  147. return 0;
  148. }
  149. #if !defined(WIN32) && !defined(_MSC_VER)
  150. void md_sighuphandler(int i)
  151. {
  152. md_exit(-1);
  153. }
  154. #endif
  155. void md_exit(int exitcode) {
  156. if (mdcontrol.socket == -1) {
  157. #if !defined(WIN32) && !defined(_MSC_VER)
  158. tcsetattr(STDIN_FILENO, TCSANOW, &ttysave);
  159. #endif
  160. } else {
  161. // nothing
  162. }
  163. exit(exitcode);
  164. }
  165. void md_init(const char *dropfile, int socket) {
  166. char *filename = strrchr(dropfile, PATH_SEP);
  167. #if !defined(WIN32) && !defined(_MSC_VER)
  168. struct termios ttystate;
  169. signal(SIGHUP, &md_sighuphandler);
  170. #endif
  171. int ret = -1;
  172. mdcontrol.socket = socket;
  173. if (filename == NULL) {
  174. filename = (char *)dropfile;
  175. } else {
  176. filename++;
  177. }
  178. if (strcasecmp(filename, "door.sys") == 0) {
  179. ret = read_doorsys(dropfile);
  180. } else if (strcasecmp(filename, "door32.sys") == 0) {
  181. ret = read_door32(dropfile);
  182. } else {
  183. fprintf(stderr, "Unsupported dropfile: %s\n", filename);
  184. exit(-1);
  185. }
  186. if (ret == -1) {
  187. fprintf(stderr, "Unable to open dropfile: %s\n", dropfile);
  188. exit(-1);
  189. }
  190. if (mdcontrol.socket == -1) {
  191. #if !defined(WIN32) && !defined(_MSC_VER)
  192. tcgetattr(STDIN_FILENO, &ttystate);
  193. ttysave = ttystate;
  194. ttystate.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO);
  195. ttystate.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR | INPCK | ISTRIP | IXON | PARMRK);
  196. ttystate.c_oflag &= ~OPOST;
  197. ttystate.c_cc[VMIN] = 1;
  198. ttystate.c_cc[VTIME] = 0;
  199. tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
  200. #endif
  201. } else {
  202. #if defined(WIN32) || defined(_MSC_VER)
  203. WSADATA wsa;
  204. WSAStartup(WINSOCK_VERSION, &wsa);
  205. #endif
  206. }
  207. mdtimeremaining = time(NULL) + mdcontrol.user_timeleft;
  208. mdtimeout = time(NULL) + 900;
  209. return;
  210. }