C 7
Answer.c Guest on 21st November 2020 10:04:12 AM
  1.  
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <process.h>
  7. #include <conio.h>
  8. #include <time.h>
  9. #include <dos.h>  /* Dos-specific functions are used to get date/time. */
  10.  
  11. #include "common.h"    /* Definitions/routines Al uses lots. */
  12. #include "modem.h"     /* Routines for accessing the modem.  */
  13. #include "voicemdm.h"  /* Voicemodem routines.               */
  14.  
  15. #define MAXIMUM_MESSAGE_NUMBER 100
  16.  
  17. FILE *message_file;
  18. int port_number;       /* ...from the command line. */
  19. int rings_to_answer_in=4,ring_count,next_message_number,call_beeper_flag=0;
  20. int ring_result;  /* For saving the return code from the ring-detecting subroutine. */
  21. int message_number,result;
  22.  
  23.  
  24. /*------------------------- Subroutines -------------------------------------  */
  25.  
  26. void serious_error(char *the_message)
  27. {
  28. fossil_close(port_number,0);
  29. printf("Serious error encountered; fossil port closed.\n");
  30. fatal_error(the_message);
  31. }
  32.  
  33. void owner_options(void)
  34. {
  35. int password_retry_count=0;
  36. while (1)                   /* Ask for password until ok or too many retries. */
  37.   {
  38.   voicemdm_play_file(port_number,"PROMPTS\\ENTERPAS.MSG",0);
  39.   voicemdm_play_beep(port_number);
  40.   voicemdm_record_dtmf_string(port_number,15);  /* Get password. */
  41.   if (!strcmp(voicemdm_dtmf_string,"1234"))  /* Correct password? */
  42.     break;
  43.   if (!strcmp(voicemdm_dtmf_string,"12345"))  /* Get ready to receive update password? */
  44.     break;
  45.   voicemdm_play_file(port_number,"PROMPTS\\INVALID.MSG",0);
  46.   if (password_retry_count++ == 3)
  47.     return;
  48.   }
  49. if (!strcmp(voicemdm_dtmf_string,"12345"))  /* Update password entered? */
  50.   {
  51.   /* zzz This is where we could call a batch file to receive the file.  */
  52.   /*     It should probably also hang up, so that things don't goof up. */
  53.   return;
  54.   }
  55. voicemdm_play_file(port_number,"PROMPTS\\GOODPASS.MSG",0);
  56. printf("Getting ready to play one beep for each message on file.\n");
  57. message_number = 0;
  58. while (message_number < MAXIMUM_MESSAGE_NUMBER)
  59.   {
  60.   itoa(message_number,temp_string,10);
  61.   strcat(temp_string,".MSG");
  62.   message_file = fopen(temp_string,"rb");
  63.   if (message_file == NULL)  /* No message file with this number? */
  64.     {
  65.     message_number++;
  66.     continue;
  67.     }
  68.   fclose(message_file);
  69.   printf("  Playing beep for message number %d now.\n",message_number);
  70.   voicemdm_play_beep(port_number);  /* One beep for each message we have. */
  71.   wait(0);  /* Wait until end of second. */
  72.   message_number++;
  73.   }
  74. rings_to_answer_in = 4;
  75. message_number = 0;
  76. while (message_number < MAXIMUM_MESSAGE_NUMBER)  /* Play all messages. */
  77.   {
  78.   itoa(message_number,temp_string,10);
  79.   strcat(temp_string,".MSG");
  80.   message_file = fopen(temp_string,"rb");
  81.   if (message_file == NULL)  /* No message file with this number? */
  82.     {
  83.     message_number++;
  84.     continue;
  85.     }
  86.   fclose(message_file);
  87.   printf("  Playing message number %d (file %s).\n",message_number,temp_string);
  88.   voicemdm_play_file(port_number,"PROMPTS\\PLAYINGM.MSG",0);
  89.   voicemdm_play_file(port_number,temp_string,0);
  90.   voicemdm_play_beep(port_number);
  91.   voicemdm_play_file(port_number,"PROMPTS\\MENU1.MSG",0);
  92.   result = voicemdm_dtmf_string[0];
  93.   if (!result)   /* No selection made during outgoing message? */
  94.     {
  95.     voicemdm_play_beep(port_number);
  96.     result = voicemdm_record_dtmf_key(port_number,10);
  97.     }
  98.   if (result == 0)  /* Timeout?    */
  99.     {
  100.     voicemdm_play_file(port_number,"PROMPTS\\INVALID.MSG",0);
  101.     return;      /*  Just quit. */
  102.     }
  103.   if (result == (int)'3')                    /* Delete? */
  104.     {
  105.     voicemdm_play_file(port_number,"PROMPTS\\DELETING.MSG",0);
  106.     printf("Deleting message file %s now....\n",temp_string);
  107.     unlink(temp_string);
  108.     message_number++;
  109.     continue;
  110.     }
  111.   if (result == (int)'6')                    /* Next message? */
  112.     {
  113.     voicemdm_play_file(port_number,"PROMPTS\\SKIPPING.MSG",0);
  114.     printf("Skipping to next message.\n");
  115.     message_number++;
  116.     continue;
  117.     }
  118.   if (result == (int)'7')          /* Repeat message? */
  119.     {
  120.     printf("Repeating message.\n");
  121.     continue;
  122.     }
  123.   /* Just leave the same message number, so it's processed again. */
  124.   voicemdm_play_file(port_number,"PROMPTS\\INVALID.MSG",0);
  125.   printf("Staying on message %d because of invalid input.\n",message_number);
  126.   }
  127. voicemdm_play_file(port_number,"PROMPTS\\NO_MORE.MSG",0);
  128. return;  /* Indicate that no messages were saved during this call. */
  129. }
  130.  
  131. int process_call(void)
  132. {
  133. printf("Playing greeting message....\n");
  134. voicemdm_play_file(port_number,"PROMPTS\\GREETING.MSG",0);
  135. if (voicemdm_dtmf_string[0] == '*')  /* * pressed during greeting? */
  136.   {
  137.   owner_options();
  138.   return(1);
  139.   }
  140. voicemdm_play_beep(port_number);
  141. printf("Recording incoming message now....\n");
  142. itoa(next_message_number,temp_string,10);
  143. strcat(temp_string,".MSG");
  144. if (voicemdm_record_file(port_number,temp_string,0))
  145.   {
  146.   printf("Message not saved due to dial or busy tone or the like during recording.\n");
  147.   return(1);
  148.   }
  149. if (voicemdm_dtmf_string[0] == '*')  /* * pressed while recording message? */
  150.   {
  151.   owner_options();
  152.   return(1);
  153.   }
  154. voicemdm_play_beep(port_number);
  155. call_beeper_flag = 1;
  156. rings_to_answer_in = 2;
  157. return(0);
  158. }
  159.  
  160. void check_beeper_flag(void)
  161. {
  162. printf("Checking beeper flag to see if we should beep someone.\n");
  163. if (!call_beeper_flag)
  164.   return;
  165. printf("  Beeper flag set; calling Al's beeper now....\n");
  166. wait(3);
  167. printf("  Setting modem parameters for dial-out.\n");
  168. send_to_modem(port_number,"AT #BDR=16 #CLS=8 #VRA=55 #VRN=100");
  169. send_to_modem(port_number,CR_string);
  170. if (waitfor(port_number,"OK",3,0))
  171.   {
  172.   printf("Unable to set up modem for dial-out!%s\n",BELL_string);
  173.   wait(1);
  174.   return;
  175.   }
  176. wait(1);
  177. printf("  Dialing beeper now.\n");
  178. send_to_modem(port_number,"*70,,ATDT668-2675");
  179. send_to_modem(port_number,CR_string);
  180. if (waitfor(port_number,"VCON",15,0))
  181.   {
  182.   printf("  Quitting beeper dial-out; didn't get VCON for 15 seconds after dialing!%s\n",BELL_string);
  183.   return;
  184.   }
  185. if (ring_result == 1)  /* Which phone number did this call arrive on? */
  186.   strcpy(voicemdm_dtmf_string,"3839853");
  187. else
  188.   strcpy(voicemdm_dtmf_string,"3161995");
  189. if (!voicemdm_play_dtmf_string(port_number))  /* Everything ok? */
  190.   call_beeper_flag = 0;                    /*  Reset flag.   */
  191. wait(1);
  192. send_to_modem(port_number,"ATH");
  193. send_to_modem(port_number,CR_string);
  194. if (waitfor(port_number,"OK",5,0))   /* Some sort of trouble hanging up? */
  195.   {
  196.   printf("  Trouble hanging up!%s\n",BELL_string);
  197.   call_beeper_flag = 1;
  198.   return;
  199.   }
  200. wait(1);
  201. trash_modem_output(port_number,0);
  202. printf("  Done calling beeper.\n");
  203. call_beeper_flag = 0;     /* Get rid of flag now that we've called. */
  204. }
  205.  
  206.  
  207. /*------------------------------- Main Program -------------------------------*/
  208.  
  209. int main(int argc, char *argv[])
  210. {
  211. char input_char;
  212.  
  213. if (argc <= 1)                 /* Our 1 required argument not specified? */
  214.   serious_error("Missing argument; supply the comm. port number.");
  215. port_number = atoi(argv[1]);
  216.  
  217. if (fossil_init(port_number))
  218.   serious_error("Unable to initialize comm. port / fossil driver");
  219. fossil_set_handshaking(port_number,2);
  220. fossil_set_dtr(port_number,1);
  221.  
  222. while (1)     /* This is the answering machine loop, from RING to hangup. */
  223.   {
  224.   voicemdm_reset_modem(port_number);
  225.   voicemdm_set_modem_parameters(port_number);  /* This needs to be done for each call. */
  226.   ring_count = 0;
  227.  
  228.   printf("Searching for next unused message number.\n");
  229.   next_message_number = 0;
  230.   while (1)
  231.     {
  232.     itoa(next_message_number,temp_string,10);
  233.     strcat(temp_string,".MSG");
  234.     message_file = fopen(temp_string,"rb");
  235.     if (message_file == NULL)
  236.       break;
  237.     fclose(message_file);
  238.     next_message_number++;
  239.     }
  240.   printf("  The next unused message number is %d.\n",next_message_number);
  241.   if (next_message_number)  /* 0.MSG on file? */
  242.     rings_to_answer_in = 2;
  243.   printf("  The phone will be answered in %d rings this time.\n",rings_to_answer_in);
  244.   if (call_beeper_flag)
  245.     printf("  Beeper flag is set!  We'll call beeper soon.\n");
  246.   else
  247.     printf("  Beeper flag not set right now.\n");
  248.  
  249.   printf("Waiting for a call; hit escape to end program....\n");
  250.   ring_result = voicemdm_wait_for_ring(port_number,rings_to_answer_in,180);
  251.   if (ring_result == 0)  /* Escape key pressed? */
  252.     break;          /*  Regular exit.      */
  253.   if (ring_result == 3)  /* We timed out?  Check things. */
  254.     {
  255.     if (next_message_number)  /* Does the file 0.MSG exist, in effect? */
  256.       printf("There are messages on the answering machine!\n%s",BELL_string);
  257.     check_beeper_flag();
  258.     continue;
  259.     }
  260.   if (ring_result == 4)  /* Special key ('r') hit while waiting for ring? */
  261.     {
  262.     printf("LOCAL RECORDING: Enter the name of the file to record: ");
  263.     gets(temp_string);
  264.     voicemdm_record_file(port_number,temp_string,1);
  265.     continue;
  266.     }
  267.   if (ring_result == 5)  /* Special key 'p' hi while waiting for ring? */
  268.     {
  269.     printf("LOCAL PLAYING: Enter the name of the file to play: ");
  270.     gets(temp_string);
  271.     voicemdm_play_file(port_number,temp_string,1);
  272.     continue;
  273.     }
  274.   printf("Answering call now....\n");
  275.   send_to_modem(port_number,"ATA");   /* Answer the call. */
  276.   send_to_modem(port_number,CR_string);
  277.   if (waitfor(port_number,"VCON",7,0))
  278.     serious_error("Didn't get VCON after ATA command.");
  279.   if (!ring_result)     /* Regular old rings or double rings? */
  280.     {
  281.     printf("This call came in on the primary telephone number (single rings detected).\n");
  282.     process_call();
  283.     }
  284.   else
  285.     {
  286.     printf("This call came in on the second telephone number (double rings detected).\n");
  287.     process_call();  /* We got double-rings; we could put another routine here.*/
  288.     }
  289.  
  290.   printf("Hanging up now.\n");
  291.   voicemdm_play_file(port_number,"PROMPTS\\GOODBYE.MSG",0);
  292.   send_to_modem(port_number,"ATH");          /* Hang up now. */
  293.   send_to_modem(port_number,CR_string);
  294.   if (waitfor(port_number,"OK",5,0))   /* Did get get OK? */
  295.     serious_error("Couldn't hang up the darned modem!");
  296.   check_beeper_flag();
  297.   }
  298. printf("Resetting modem.\n");
  299. send_to_modem(port_number,"ATZ");
  300. send_to_modem(port_number,CR_string);
  301. if (waitfor(port_number,"OK",5,0))
  302.   serious_error("Didn't get OK after ATZ command to reset modem!");
  303. printf("Closing modem port and dropping DTR.\n");
  304. fossil_close(port_number,0);   /* Close port, dropping DTR. */
  305. printf("Successful exit!\n");
  306. return(0);
  307. }

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.