home *** CD-ROM | disk | FTP | other *** search
/ PC World 2004 November / PCWorld_2004-11_cd.bin / software / temacd / poweroff / pwroff30.exe / src / servercommands.c < prev    next >
C/C++ Source or Header  |  2003-07-28  |  23KB  |  761 lines

  1. #include "poweroff.h"
  2.  
  3. #define SET_ACTION(a) { ps->action=a; DisplaySettings(hWnd,ps); ReturnClientOK(hWnd,ps); }
  4. #define SET_WHO(w) { ps->who=w; DisplaySettings(hWnd,ps); ReturnClientOK(hWnd,ps); }
  5. #define SET_WHEN(w) { ps->when=w; DisplaySettings(hWnd,ps); ReturnClientOK(hWnd,ps); }
  6. #define SET_SCHEDULE(s) { ps->schedule.schedule=s; ReturnClientOK(hWnd,ps); }
  7. #define SET_BOOLEAN(p,b) \
  8.   { \
  9.     if (value==NULL) \
  10.       ReturnClientError(hWnd,ps,"No value specified"); \
  11.     else if (!_stricmp(value,"on")) \
  12.       { p=1; DisplaySettings(hWnd,ps); ReturnClientOK(hWnd,ps); } \
  13.     else if (!_stricmp(value,"off")) \
  14.       { p=0; DisplaySettings(hWnd,ps); ReturnClientOK(hWnd,ps); } \
  15.     else \
  16.       ReturnClientError(hWnd,ps,"Invalid value specified"); \
  17.   }
  18. #define ADD_DAY(d) { if (str[0]) { strcat(str,","); strcat(str,d);} else strcpy(str,d); }
  19. #define CHECK_IF_AUTHENTICATED { if (!ps->authenticated) { ReturnClientError(hWnd,ps,"Access is denied"); CloseClientSocket(hWnd,ps); return; } }
  20.  
  21. char *Trim(char *str)
  22. {
  23.   int x;
  24.   
  25.   while (str[0]==' ' || str[0]=='\t' || str[0]=='\n') /* trim spaces at the front */
  26.     memmove(&str[0],&str[1],strlen(str));
  27.   for (x=strlen(str)-1;x>=0 && (str[x]==' ' || str[x]=='\t' || str[x]=='\n');x--) /* trim spaces at the end */
  28.     str[x]=0;
  29.   return str;
  30. }
  31.  
  32. int ReplaceString(char *str,char *srch,char *repl)
  33. {
  34.   size_t x;
  35.   int replaced=0;
  36.   
  37.   for (x=0;x<strlen(str);x++)
  38.   {
  39.     if (x>strlen(str)-strlen(srch))
  40.     {
  41.       break;
  42.     }
  43.     if (memcmp(&str[x],srch,strlen(srch))==0)
  44.     {
  45.       memmove(&str[x],&str[x+strlen(srch)],strlen(str)-x-strlen(srch)+1);
  46.       memmove(&str[x+strlen(repl)],&str[x],strlen(str)-x+1);
  47.       memcpy(&str[x],repl,strlen(repl));
  48.       x+=strlen(repl)-1;
  49.       replaced=1;
  50.     }
  51.   }
  52.   return replaced;
  53. }
  54.  
  55. void ReturnClientError(HWND hWnd,PowerSettings *ps, char *error)
  56. {
  57.   char str[500];
  58.  
  59.   Log("ReturnClientError start");
  60.   if (ps->clientsocket!=INVALID_SOCKET)
  61.   {
  62.     sprintf(str,"%d %s\r\n",COMMAND_ERROR,error);
  63.     Log("Sending error %s to client",str);
  64.     if (send(ps->clientsocket,str,strlen(str),0)==SOCKET_ERROR)
  65.     {
  66.       ShowSocketError(ps,"send");
  67.       CloseClientSocket(hWnd,ps);
  68.       return;
  69.     }
  70.   }
  71.   Log("ReturnClientError end");
  72. }
  73.  
  74. void ReturnClientInfo(HWND hWnd,PowerSettings *ps, char *info, ...)
  75. {
  76.   va_list param;
  77.   char tmpstr[500],str[500];
  78.  
  79.   Log("ReturnClientInfo start");
  80.   va_start(param,info);
  81.   vsprintf(tmpstr,info,param);
  82.   va_end(param);
  83.  
  84.   sprintf(str,"%d %s\r\n",COMMAND_INFO,tmpstr);
  85.   Log("sending info %s to client",str);
  86.   if (send(ps->clientsocket,str,strlen(str),0)==SOCKET_ERROR)
  87.   {
  88.     ShowSocketError(ps,"send");
  89.     CloseClientSocket(hWnd,ps);
  90.     return;
  91.   }
  92.   Log("ReturnClientInfo end");
  93. }
  94.  
  95. void ReturnClientOK(HWND hWnd,PowerSettings *ps)
  96. {
  97.   char str[10];
  98.  
  99.   Log("ReturnClientOK start");
  100.   sprintf(str,"%d OK\r\n",COMMAND_OK);
  101.   Log("sending ok %s to client",str);
  102.   if (send(ps->clientsocket,str,strlen(str),0)==SOCKET_ERROR)
  103.   {
  104.     ShowSocketError(ps,"send");
  105.     CloseClientSocket(hWnd,ps);
  106.     return;
  107.   }
  108.   Log("ReturnClientOK end");
  109. }
  110.  
  111. void ShowClientHelp(HWND hWnd,PowerSettings *ps)
  112. {
  113.   Log("ShowClientHelp start");
  114.   ReturnClientInfo(hWnd,ps,"You can use the following commands to control Poweroff");
  115.   ReturnClientInfo(hWnd,ps,"QUIT\tTerminates the connection");
  116.   ReturnClientInfo(hWnd,ps,"HELP\tShows this help");
  117.   ReturnClientInfo(hWnd,ps,"PASSWORD p\tAuthenticates the client with a password");
  118.   ReturnClientInfo(hWnd,ps,"ACTION a\tThe action to perform (logoff,reboot,shutdown,poweroff,standby,hibernate,lock,wol,monitor_off,monitor_on,no_action)");
  119.   ReturnClientInfo(hWnd,ps,"WHO w\tWho must perform the action (local,remote)");
  120.   ReturnClientInfo(hWnd,ps,"WHEN w\tWhen must the action be performed (immediate,scheduled,after_process)");
  121.   ReturnClientInfo(hWnd,ps,"SIMULATE s\tSimulate the action (on,off)");
  122.   ReturnClientInfo(hWnd,ps,"COMPUTER c\tThe remote computer name");
  123.   ReturnClientInfo(hWnd,ps,"MAC m\tThe remote computer mac address (WOL)");
  124.   ReturnClientInfo(hWnd,ps,"IP i\tThe remote computer ip address (WOL)");
  125.   ReturnClientInfo(hWnd,ps,"SUBNET s\tThe remote computer subnet mask (WOL)");
  126.   ReturnClientInfo(hWnd,ps,"WARNING w\tShow warning before doing the action (on,off)");
  127.   ReturnClientInfo(hWnd,ps,"WARNTIME w\tThe number of seconds to show the warning");
  128.   ReturnClientInfo(hWnd,ps,"MESSAGE m\tThe warning message");
  129.   ReturnClientInfo(hWnd,ps,"PLAYSOUND p\tPlay a sound when showing the warning (on,off)");
  130.   ReturnClientInfo(hWnd,ps,"SOUNDFILE s\tThe WAV file to play");
  131.   ReturnClientInfo(hWnd,ps,"RUNPRG r\tRuns a program before doing the action (on,off)");
  132.   ReturnClientInfo(hWnd,ps,"PROGRAM p\tThe program to run");
  133.   ReturnClientInfo(hWnd,ps,"DIRECTORY d\tThe startup directory for the program");
  134.   ReturnClientInfo(hWnd,ps,"SCHEDULE s\tThe type of schedule (fixed_day,daily,day_of_month,after_x_seconds)");
  135.   ReturnClientInfo(hWnd,ps,"TIME t\tThe time when to do the action (hh:mm)");
  136.   ReturnClientInfo(hWnd,ps,"DATE d\tThe date when to do the action (dd/mm/yyyy)");
  137.   ReturnClientInfo(hWnd,ps,"DAYS d\tThe days of the week when to do the action (m,t,w,th,f,s,su)");
  138.   ReturnClientInfo(hWnd,ps,"WEEKS d\tThe weeks of the month when to do the action (1,2,3,4,5)");
  139.   ReturnClientInfo(hWnd,ps,"DAY d\tThe day of the month when to do the action (1-31)");
  140.   ReturnClientInfo(hWnd,ps,"WAIT w\tThe number of seconds to wait before checking the time");
  141.   ReturnClientInfo(hWnd,ps,"SECONDS w\tThe number of seconds to wait before doing the action");
  142.   ReturnClientInfo(hWnd,ps,"PROCESS p\tWait for process p to finish");
  143.   ReturnClientInfo(hWnd,ps,"FORCE f\tForce applications to close (on,off)");
  144.   ReturnClientInfo(hWnd,ps,"CANCEL c\tAllow the user to cancel the action (on,off)");
  145.   ReturnClientInfo(hWnd,ps,"SHOW\tShow current settings");
  146.   ReturnClientInfo(hWnd,ps,"VERSION\tShow current version of poweroff");
  147.   ReturnClientInfo(hWnd,ps,"DOIT\tDo the action");
  148.   ReturnClientInfo(hWnd,ps,"STOP\tStop the current scheduled action");
  149.   Log("ShowClientHelp end");
  150. }
  151.  
  152. void ShowSettingsToClient(HWND hWnd,PowerSettings *ps)
  153. {
  154.   char str[500];
  155.  
  156.   Log("ShowSettingsToClient start");
  157.   ReturnClientInfo(hWnd,ps,"These settings are currently in effect");
  158.   switch (ps->action)
  159.   {
  160.     case LOGOFF:
  161.       ReturnClientInfo(hWnd,ps,"ACTION\tLOGOFF");
  162.       break;
  163.     case REBOOT:
  164.       ReturnClientInfo(hWnd,ps,"ACTION\tREBOOT");
  165.       break;
  166.     case SHUTDOWN:
  167.       ReturnClientInfo(hWnd,ps,"ACTION\tSHUTDOWN");
  168.       break;
  169.     case POWEROFF:
  170.       ReturnClientInfo(hWnd,ps,"ACTION\tPOWEROFF");
  171.       break;
  172.     case STANDBY:
  173.       ReturnClientInfo(hWnd,ps,"ACTION\tSTANDBY");
  174.       break;
  175.     case HIBERNATE:
  176.       ReturnClientInfo(hWnd,ps,"ACTION\tHIBERNATE");
  177.       break;
  178.     case LOCK:
  179.       ReturnClientInfo(hWnd,ps,"ACTION\tLOCK");
  180.       break;
  181.     case WAKE_ON_LAN:
  182.       ReturnClientInfo(hWnd,ps,"ACTION\tWOL");
  183.       break;
  184.     case MONITOR_OFF:
  185.       ReturnClientInfo(hWnd,ps,"ACTION\tMONITOR_OFF");
  186.       break;
  187.     case MONITOR_ON:
  188.       ReturnClientInfo(hWnd,ps,"ACTION\tMONITOR_ON");
  189.       break;
  190.     case NO_ACTION:
  191.       ReturnClientInfo(hWnd,ps,"ACTION\tNO_ACTION");
  192.       break;
  193.     default:
  194.       ReturnClientInfo(hWnd,ps,"ACTION\tUNKNOWN");
  195.       break;
  196.   }
  197.   switch (ps->who)
  198.   {
  199.     case LOCAL_COMPUTER:
  200.       ReturnClientInfo(hWnd,ps,"WHO\t\tLOCAL");
  201.       break;
  202.     case REMOTE_COMPUTER:
  203.       ReturnClientInfo(hWnd,ps,"WHO\t\tREMOTE");
  204.       break;
  205.     default:
  206.       ReturnClientInfo(hWnd,ps,"WHO\t\tUNKNOWN");
  207.       break;
  208.   }
  209.   switch (ps->when)
  210.   {
  211.     case IMMEDIATE:
  212.       ReturnClientInfo(hWnd,ps,"WHEN\tIMMEDIATE");
  213.       break;
  214.     case SCHEDULED:
  215.       ReturnClientInfo(hWnd,ps,"WHEN\tSCHEDULED");
  216.       break;
  217.     case PROCESS:
  218.       ReturnClientInfo(hWnd,ps,"WHEN\tAFTER_PROCESS");
  219.       break;
  220.     default:
  221.       ReturnClientInfo(hWnd,ps,"WHEN\tUNKNOWN");
  222.       break;
  223.   }
  224.   ReturnClientInfo(hWnd,ps,"SIMULATE\t%s",ps->simulate?"ON":"OFF");
  225.   ReturnClientInfo(hWnd,ps,"COMPUTER\t%s",ps->remote.computer_name);
  226.   ReturnClientInfo(hWnd,ps,"MAC\t\t%s",ps->remote.mac_address);
  227.   ReturnClientInfo(hWnd,ps,"IP\t\t%s",ps->remote.ip_address);
  228.   ReturnClientInfo(hWnd,ps,"SUBNET\t%s",ps->remote.subnet_mask);
  229.   ReturnClientInfo(hWnd,ps,"WARNING\t%s",ps->options.warning?"ON":"OFF");
  230.   ReturnClientInfo(hWnd,ps,"WARNTIME\t%d",ps->warning.seconds);
  231.   strcpy(str,ps->warning.message);
  232.   ReplaceString(str,"\r\n","\\n");
  233.   ReturnClientInfo(hWnd,ps,"MESSAGE\t%s",str);
  234.   ReturnClientInfo(hWnd,ps,"PLAYSOUND\t%s",ps->warning.play_sound?"ON":"OFF");
  235.   ReturnClientInfo(hWnd,ps,"SOUNDFILE\t%s",ps->warning.sound_file);
  236.   ReturnClientInfo(hWnd,ps,"RUNPRG\t%s",ps->options.run_program?"ON":"OFF");
  237.   ReturnClientInfo(hWnd,ps,"PROGRAM\t%s",ps->program.program);
  238.   ReturnClientInfo(hWnd,ps,"DIRECTORY\t%s",ps->program.directory);
  239.   switch (ps->schedule.schedule)
  240.   {
  241.     case FIXED_DAY:
  242.       ReturnClientInfo(hWnd,ps,"SCHEDULE\tFIXED_DAY");
  243.       break;
  244.     case DAILY:
  245.       ReturnClientInfo(hWnd,ps,"SCHEDULE\tDAILY");
  246.       break;
  247.     case DAY_OF_MONTH:
  248.       ReturnClientInfo(hWnd,ps,"SCHEDULE\tDAY_OF_MONTH");
  249.       break;
  250.     case AFTER_X_SECONDS:
  251.       ReturnClientInfo(hWnd,ps,"SCHEDULE\tAFTER_X_SECONDS");
  252.       break;
  253.     default:
  254.       ReturnClientInfo(hWnd,ps,"SCHEDULE\tUNKNOWN");
  255.       break;
  256.   }
  257.   ReturnClientInfo(hWnd,ps,"TIME\t%02d:%02d",ps->schedule.time.wHour,ps->schedule.time.wMinute);
  258.   ReturnClientInfo(hWnd,ps,"DATE\t%02d/%02d/%04d",ps->schedule.date.wDay,ps->schedule.date.wMonth,ps->schedule.date.wYear);
  259.   str[0]='\0';
  260.   if (ps->schedule.monday)
  261.     ADD_DAY("m")
  262.   if (ps->schedule.tuesday)
  263.     ADD_DAY("t")
  264.   if (ps->schedule.wednesday)
  265.     ADD_DAY("w")
  266.   if (ps->schedule.thursday)
  267.     ADD_DAY("th")
  268.   if (ps->schedule.friday)
  269.     ADD_DAY("f")
  270.   if (ps->schedule.saturday)
  271.     ADD_DAY("s")
  272.   if (ps->schedule.sunday)
  273.     ADD_DAY("su")
  274.   ReturnClientInfo(hWnd,ps,"DAYS\t%s",str);
  275.   str[0]='\0';
  276.   if (ps->schedule.week1)
  277.     ADD_DAY("1")
  278.   if (ps->schedule.week2)
  279.     ADD_DAY("2")
  280.   if (ps->schedule.week3)
  281.     ADD_DAY("3")
  282.   if (ps->schedule.week4)
  283.     ADD_DAY("4")
  284.   if (ps->schedule.week5)
  285.     ADD_DAY("5")
  286.   ReturnClientInfo(hWnd,ps,"WEEKS\t%s",str);
  287.   ReturnClientInfo(hWnd,ps,"DAY\t\t%d",ps->schedule.day);
  288.   ReturnClientInfo(hWnd,ps,"WAIT\t%d",ps->schedule.wait);
  289.   ReturnClientInfo(hWnd,ps,"SECONDS\t%d",ps->schedule.seconds);
  290.   ReturnClientInfo(hWnd,ps,"PROCESS\t%s",ps->process.process);
  291.   ReturnClientInfo(hWnd,ps,"FORCE\t%s",ps->options.force?"ON":"OFF");
  292.   ReturnClientInfo(hWnd,ps,"CANCEL\t%s",ps->options.allow_cancel?"ON":"OFF");
  293.   Log("ShowSettingsToClient end");
  294. }
  295.  
  296. void ProcessClientCommand(HWND hWnd,PowerSettings *ps,char *command)
  297. {
  298.   char *value;
  299.  
  300.   Log("ProcessClientCommand start, command=%s",command);
  301.   if (strlen(command)>0 && command[strlen(command)-1]=='\n')
  302.     command[strlen(command)-1]='\0';
  303.   value=strchr(command,' ');
  304.   if (value)
  305.   {
  306.     *value='\0';
  307.     value++;
  308.     Trim(value);
  309.   }
  310.   Log("command=%s, value=%s",command,value);
  311.   if (!_stricmp(command,"quit"))
  312.   {
  313.     ReturnClientInfo(hWnd,ps,"Bye bye");
  314.     CloseClientSocket(hWnd,ps);
  315.   }
  316.   else if (!_stricmp(command,"help"))
  317.     ShowClientHelp(hWnd,ps);
  318.   else if (!_stricmp(command,"password"))
  319.   {
  320.     if (value==NULL)
  321.     {
  322.       if (ps->remote_control.password[0]!='\0')
  323.         ReturnClientError(hWnd,ps,"Invalid password");
  324.       else
  325.       {
  326.         ps->authenticated=1;
  327.         ReturnClientOK(hWnd,ps);
  328.       }
  329.     }
  330.     else if (strcmp(EncodePassword(value),ps->remote_control.password))
  331.       ReturnClientError(hWnd,ps,"Invalid password");
  332.     else
  333.     {
  334.       ps->authenticated=1;
  335.       ReturnClientOK(hWnd,ps);
  336.     }
  337.   }
  338.   else if (!_stricmp(command,"action"))
  339.   {
  340.     CHECK_IF_AUTHENTICATED;
  341.     if (value==NULL)
  342.       ReturnClientError(hWnd,ps,"No value specified");
  343.     else if (!_stricmp(value,"logoff"))
  344.       SET_ACTION(LOGOFF)
  345.     else if (!_stricmp(value,"reboot"))
  346.       SET_ACTION(REBOOT)
  347.     else if (!_stricmp(value,"shutdown"))
  348.       SET_ACTION(SHUTDOWN)
  349.     else if (!_stricmp(value,"poweroff"))
  350.       SET_ACTION(POWEROFF)
  351.     else if (!_stricmp(value,"standby"))
  352.       SET_ACTION(STANDBY)
  353.     else if (!_stricmp(value,"hibernate"))
  354.       SET_ACTION(HIBERNATE)
  355.     else if (!_stricmp(value,"lock"))
  356.       SET_ACTION(LOCK)
  357.     else if (!_stricmp(value,"wol"))
  358.       SET_ACTION(WAKE_ON_LAN)
  359.     else if (!_stricmp(value,"monitor_off"))
  360.       SET_ACTION(MONITOR_OFF)
  361.     else if (!_stricmp(value,"monitor_on"))
  362.       SET_ACTION(MONITOR_ON)
  363.     else if (!_stricmp(value,"no_action"))
  364.       SET_ACTION(NO_ACTION)
  365.     else
  366.       ReturnClientError(hWnd,ps,"Invalid value specified");
  367.   }
  368.   else if (!_stricmp(command,"who"))
  369.   {
  370.     CHECK_IF_AUTHENTICATED;
  371.     if (value==NULL)
  372.       ReturnClientError(hWnd,ps,"No value specified");
  373.     else if (!_stricmp(value,"local"))
  374.       SET_WHO(LOCAL_COMPUTER)
  375.     else if (!_stricmp(value,"remote"))
  376.       SET_WHO(REMOTE_COMPUTER)
  377.     else
  378.       ReturnClientError(hWnd,ps,"Invalid value specified");
  379.   }
  380.   else if (!_stricmp(command,"when"))
  381.   {
  382.     CHECK_IF_AUTHENTICATED;
  383.     if (value==NULL)
  384.       ReturnClientError(hWnd,ps,"No value specified");
  385.     else if (!_stricmp(value,"immediate"))
  386.       SET_WHEN(IMMEDIATE)
  387.     else if (!_stricmp(value,"scheduled"))
  388.       SET_WHEN(SCHEDULED)
  389.     else if (!_stricmp(value,"after_process"))
  390.       SET_WHEN(PROCESS)
  391.     else
  392.       ReturnClientError(hWnd,ps,"Invalid value specified");
  393.   }
  394.   else if (!_stricmp(command,"simulate"))
  395.   {
  396.     CHECK_IF_AUTHENTICATED;
  397.     SET_BOOLEAN(ps->simulate,value);
  398.   }
  399.   else if (!_stricmp(command,"computer"))
  400.   {
  401.     CHECK_IF_AUTHENTICATED;
  402.     if (value==NULL)
  403.       ReturnClientError(hWnd,ps,"No value specified");
  404.     else
  405.     {
  406.       strcpy(ps->remote.computer_name,value);
  407.       ReturnClientOK(hWnd,ps);
  408.     }
  409.   }
  410.   else if (!_stricmp(command,"mac"))
  411.   {
  412.     CHECK_IF_AUTHENTICATED;
  413.     if (value==NULL)
  414.       ReturnClientError(hWnd,ps,"No value specified");
  415.     else
  416.     {
  417.       if (IsValidMACAddress(value))
  418.       {
  419.         strcpy(ps->remote.mac_address,value);
  420.         ReturnClientOK(hWnd,ps);
  421.       }
  422.       else
  423.         ReturnClientError(hWnd,ps,"Invalid MAC address");
  424.     }
  425.   }
  426.   else if (!_stricmp(command,"ip"))
  427.   {
  428.     CHECK_IF_AUTHENTICATED;
  429.     if (value==NULL)
  430.       ReturnClientError(hWnd,ps,"No value specified");
  431.     else
  432.     {
  433.       if (IsValidIPAddress(value))
  434.       {
  435.         strcpy(ps->remote.ip_address,value);
  436.         ReturnClientOK(hWnd,ps);
  437.       }
  438.       else
  439.         ReturnClientError(hWnd,ps,"Invalid IP address");
  440.     }
  441.   }
  442.   else if (!_stricmp(command,"subnet"))
  443.   {
  444.     CHECK_IF_AUTHENTICATED;
  445.     if (value==NULL)
  446.       ReturnClientError(hWnd,ps,"No value specified");
  447.     else
  448.     {
  449.       if (IsValidIPAddress(value))
  450.       {
  451.         strcpy(ps->remote.subnet_mask,value);
  452.         ReturnClientOK(hWnd,ps);
  453.       }
  454.       else
  455.         ReturnClientError(hWnd,ps,"Invalid subnet mask");
  456.     }
  457.   }
  458.   else if (!_stricmp(command,"warning"))
  459.   {
  460.     CHECK_IF_AUTHENTICATED;
  461.     SET_BOOLEAN(ps->options.warning,value);
  462.   }
  463.   else if (!_stricmp(command,"warntime"))
  464.   {
  465.     CHECK_IF_AUTHENTICATED;
  466.     if (value==NULL)
  467.       ReturnClientError(hWnd,ps,"No value specified");
  468.     else
  469.     {
  470.       ps->warning.seconds=atoi(value);
  471.       ReturnClientOK(hWnd,ps);
  472.     }
  473.   }
  474.   else if (!_stricmp(command,"message"))
  475.   {
  476.     CHECK_IF_AUTHENTICATED;
  477.     if (value==NULL)
  478.       ps->warning.message[0]='\0';
  479.     else
  480.     {
  481.       strcpy(ps->warning.message,value);
  482.       ReplaceString(ps->warning.message,"\\n","\r\n");
  483.     }
  484.     ReturnClientOK(hWnd,ps);
  485.   }
  486.   else if (!_stricmp(command,"playsound"))
  487.   {
  488.     CHECK_IF_AUTHENTICATED;
  489.     SET_BOOLEAN(ps->warning.play_sound,value);
  490.   }
  491.   else if (!_stricmp(command,"soundfile"))
  492.   {
  493.     CHECK_IF_AUTHENTICATED;
  494.     if (value==NULL)
  495.       ReturnClientError(hWnd,ps,"No value specified");
  496.     else
  497.     {
  498.       strcpy(ps->warning.sound_file,value);
  499.       ReturnClientOK(hWnd,ps);
  500.     }
  501.   }
  502.   else if (!_stricmp(command,"runprg"))
  503.   {
  504.     CHECK_IF_AUTHENTICATED;
  505.     SET_BOOLEAN(ps->options.run_program,value);
  506.   }
  507.   else if (!_stricmp(command,"program"))
  508.   {
  509.     CHECK_IF_AUTHENTICATED;
  510.     if (value==NULL)
  511.       ReturnClientError(hWnd,ps,"No value specified");
  512.     else
  513.     {
  514.       strcpy(ps->program.program,value);
  515.       ReturnClientOK(hWnd,ps);
  516.     }
  517.   }
  518.   else if (!_stricmp(command,"directory"))
  519.   {
  520.     CHECK_IF_AUTHENTICATED;
  521.     if (value==NULL)
  522.       ps->program.directory[0]='\0';
  523.     else
  524.       strcpy(ps->program.directory,value);
  525.     ReturnClientOK(hWnd,ps);
  526.   }
  527.   else if (!_stricmp(command,"schedule"))
  528.   {
  529.     CHECK_IF_AUTHENTICATED;
  530.     if (value==NULL)
  531.       ReturnClientError(hWnd,ps,"No value specified");
  532.     else if (!_stricmp(value,"fixed_day"))
  533.       SET_SCHEDULE(FIXED_DAY)
  534.     else if (!_stricmp(value,"daily"))
  535.       SET_SCHEDULE(DAILY)
  536.     else if (!_stricmp(value,"day_of_month"))
  537.       SET_SCHEDULE(DAY_OF_MONTH)
  538.     else if (!_stricmp(value,"after_x_seconds"))
  539.       SET_SCHEDULE(AFTER_X_SECONDS)
  540.     else
  541.       ReturnClientError(hWnd,ps,"Invalid value specified");
  542.   }
  543.   else if (!_stricmp(command,"time"))
  544.   {
  545.     CHECK_IF_AUTHENTICATED;
  546.     if (value==NULL)
  547.       ReturnClientError(hWnd,ps,"No value specified");
  548.     else
  549.     {
  550.       int hours,minutes;
  551.  
  552.       if (sscanf(value,"%d:%d",&hours,&minutes)!=2 || hours<0 || hours>23 || minutes<0 || minutes>59)
  553.         ReturnClientError(hWnd,ps,"Invalid time specified");
  554.       else
  555.       {
  556.         GetLocalTime(&ps->schedule.time);
  557.         ps->schedule.time.wHour=hours;
  558.         ps->schedule.time.wMinute=minutes;
  559.         if (ps->schedule.date.wYear==0)
  560.           GetLocalTime(&ps->schedule.date);
  561.         ReturnClientOK(hWnd,ps);
  562.       }
  563.     }
  564.   }
  565.   else if (!_stricmp(command,"date"))
  566.   {
  567.     CHECK_IF_AUTHENTICATED;
  568.     if (value==NULL)
  569.       ReturnClientError(hWnd,ps,"No value specified");
  570.     else
  571.     {
  572.       int days,months,years;
  573.  
  574.       if (sscanf(value,"%d/%d/%d",&days,&months,&years)!=3 || days<0 || days>31 || months<0 || months>12 || years<0 || years>9999)
  575.         ReturnClientError(hWnd,ps,"Invalid date specified");
  576.       else
  577.       {
  578.         GetLocalTime(&ps->schedule.date);
  579.         ps->schedule.date.wDay=days;
  580.         ps->schedule.date.wMonth=months;
  581.         ps->schedule.date.wYear=years;
  582.         ps->schedule.schedule=FIXED_DAY;
  583.         ReturnClientOK(hWnd,ps);
  584.       }
  585.     }
  586.   }
  587.   else if (!_stricmp(command,"days"))
  588.   {
  589.     CHECK_IF_AUTHENTICATED;
  590.     if (value==NULL)
  591.       ReturnClientError(hWnd,ps,"No value specified");
  592.     else
  593.     {
  594.       char days[10][10];
  595.       int n,y,error=0;
  596.  
  597.       ps->schedule.monday=0;
  598.       ps->schedule.tuesday=0;
  599.       ps->schedule.wednesday=0;
  600.       ps->schedule.thursday=0;
  601.       ps->schedule.friday=0;
  602.       ps->schedule.saturday=0;
  603.       ps->schedule.sunday=0;
  604.       n=sscanf(value,"%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%s",days[0],days[1],days[2],days[3],days[4],days[5],days[6]);
  605.       for (y=0;y<n;y++)
  606.       {
  607.         if (!strcmp(days[y],"m"))
  608.           ps->schedule.monday=1;
  609.         else if (!strcmp(days[y],"t"))
  610.           ps->schedule.tuesday=1;
  611.         else if (!strcmp(days[y],"w"))
  612.           ps->schedule.wednesday=1;
  613.         else if (!strcmp(days[y],"th"))
  614.           ps->schedule.thursday=1;
  615.         else if (!strcmp(days[y],"f"))
  616.           ps->schedule.friday=1;
  617.         else if (!strcmp(days[y],"s"))
  618.           ps->schedule.saturday=1;
  619.         else if (!strcmp(days[y],"su"))
  620.           ps->schedule.sunday=1;
  621.         else
  622.           error=1;
  623.       }
  624.       if (error)
  625.         ReturnClientError(hWnd,ps,"Invalid day specified");
  626.       else
  627.         ReturnClientOK(hWnd,ps);
  628.     }
  629.   }
  630.   else if (!_stricmp(command,"weeks"))
  631.   {
  632.     CHECK_IF_AUTHENTICATED;
  633.     if (value==NULL)
  634.       ReturnClientError(hWnd,ps,"No value specified");
  635.     else
  636.     {
  637.       char days[10][10];
  638.       int n,y,error=0;
  639.  
  640.       ps->schedule.week1=0;
  641.       ps->schedule.week2=0;
  642.       ps->schedule.week3=0;
  643.       ps->schedule.week4=0;
  644.       ps->schedule.week5=0;
  645.       n=sscanf(value,"%[^,],%[^,],%[^,],%[^,],%s",days[0],days[1],days[2],days[3],days[4]);
  646.       for (y=0;y<n;y++)
  647.       {
  648.         if (!strcmp(days[y],"1"))
  649.           ps->schedule.week1=1;
  650.         else if (!strcmp(days[y],"2"))
  651.           ps->schedule.week2=1;
  652.         else if (!strcmp(days[y],"3"))
  653.           ps->schedule.week3=1;
  654.         else if (!strcmp(days[y],"4"))
  655.           ps->schedule.week4=1;
  656.         else if (!strcmp(days[y],"5"))
  657.           ps->schedule.week5=1;
  658.         else
  659.           error=1;
  660.       }
  661.       if (error)
  662.         ReturnClientError(hWnd,ps,"Invalid week specified");
  663.       else
  664.         ReturnClientOK(hWnd,ps);
  665.     }
  666.   }
  667.   else if (!_stricmp(command,"day"))
  668.   {
  669.     CHECK_IF_AUTHENTICATED;
  670.     if (value==NULL)
  671.       ReturnClientError(hWnd,ps,"No value specified");
  672.     else
  673.     {
  674.       ps->schedule.day=atoi(value);
  675.       ReturnClientOK(hWnd,ps);
  676.     }
  677.   }
  678.   else if (!_stricmp(command,"seconds"))
  679.   {
  680.     CHECK_IF_AUTHENTICATED;
  681.     if (value==NULL)
  682.       ReturnClientError(hWnd,ps,"No value specified");
  683.     else
  684.     {
  685.       ps->schedule.seconds=atoi(value);
  686.       ReturnClientOK(hWnd,ps);
  687.     }
  688.   }
  689.   else if (!_stricmp(command,"wait"))
  690.   {
  691.     CHECK_IF_AUTHENTICATED;
  692.     if (value==NULL)
  693.       ReturnClientError(hWnd,ps,"No value specified");
  694.     else
  695.     {
  696.       ps->schedule.wait=atoi(value);
  697.       ReturnClientOK(hWnd,ps);
  698.     }
  699.   }
  700.   else if (!_stricmp(command,"process"))
  701.   {
  702.     CHECK_IF_AUTHENTICATED;
  703.     if (value==NULL)
  704.       ReturnClientError(hWnd,ps,"No value specified");
  705.     else
  706.     {
  707.       strcpy(ps->process.process,value);
  708.       ReturnClientOK(hWnd,ps);
  709.     }
  710.   }
  711.   else if (!_stricmp(command,"force"))
  712.   {
  713.     CHECK_IF_AUTHENTICATED;
  714.     SET_BOOLEAN(ps->options.force,value);
  715.   }
  716.   else if (!_stricmp(command,"cancel"))
  717.   {
  718.     CHECK_IF_AUTHENTICATED;
  719.     SET_BOOLEAN(ps->options.allow_cancel,value);
  720.   }
  721.   else if (!_stricmp(command,"show"))
  722.   {
  723.     CHECK_IF_AUTHENTICATED;
  724.     ShowSettingsToClient(hWnd,ps);
  725.   }
  726.   else if (!_stricmp(command,"version"))
  727.   {
  728.     ReturnClientInfo(hWnd,ps,"%s",POWEROFF_VERSION);
  729.   }
  730.   else if (!_stricmp(command,"doit"))
  731.   {
  732.     char str[500];
  733.  
  734.     CHECK_IF_AUTHENTICATED;
  735.     if (!SanityCheck(hWnd,ps,str))
  736.     {
  737.       ReturnClientOK(hWnd,ps);
  738.       DoIt(hWnd,ps);
  739.     }
  740.     else
  741.       ReturnClientError(hWnd,ps,str);
  742.   }
  743.   else if (!_stricmp(command,"stop"))
  744.   {
  745.     if (ps->active_timer)
  746.     {
  747.       KillTimer(hWnd,ps->active_timer);
  748.       ps->active_timer=0;
  749.       ReturnClientOK(hWnd,ps);
  750.     }
  751.     else
  752.       ReturnClientError(hWnd,ps,"No active command");
  753.   }
  754.   else
  755.   {
  756.     ReturnClientError(hWnd,ps,"Invalid command");
  757.   }
  758.   Log("ProcessClientCommand end");
  759. }
  760.  
  761.