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

  1. #include "poweroff.h"
  2.  
  3. int SendClientCommand(HWND hWnd,PowerSettings *ps,char *errorstr,char *command,...)
  4. {
  5.   va_list param;
  6.   char tmpstr[500],str[500];
  7.   char *value;
  8.   int ret;
  9.  
  10.   Log("SendClientCommand start");
  11.   va_start(param,command);
  12.   vsprintf(tmpstr,command,param);
  13.   va_end(param);
  14.   sprintf(str,"%s\n",tmpstr);
  15.  
  16.   if (ps->clientsocket!=INVALID_SOCKET)
  17.   {
  18.     Log("Sending %s to remote machine",str);
  19.     if (send(ps->clientsocket,str,strlen(str),0)==SOCKET_ERROR)
  20.     {
  21.       CloseClientSocket(hWnd,ps);
  22.       return -1;
  23.     }
  24.     Log("Waiting for a reply");
  25.     if ((ret=recv(ps->clientsocket,str,500,0))==SOCKET_ERROR || ret<=0)
  26.     {
  27.       CloseClientSocket(hWnd,ps);
  28.       return -1;
  29.     }
  30.     Log("Received %s from remote machine",str);
  31.     str[ret]=0;
  32.     while (strlen(str)>0 && (str[strlen(str)-1]=='\n' || str[strlen(str)-1]=='\r'))
  33.       str[strlen(str)-1]='\0';
  34.     value=strchr(str,' ');
  35.     if (value)
  36.     {
  37.       *value='\0';
  38.       value++;
  39.       Trim(value);
  40.     }
  41.     if (str[0]=='2')
  42.     {
  43.       Log("SendClientCommand end");
  44.       return 0;
  45.     }
  46.     else if (str[0]=='5')
  47.     {
  48.       strcpy(errorstr,value);
  49.       Log("SendClientCommand ended with error %s",errorstr);
  50.       return -2;
  51.     }
  52.     else
  53.     {
  54.       strcpy(errorstr,"Unknown response received");
  55.       Log("SendClientCommand ended with error %s",errorstr);
  56.       return -2;
  57.     }
  58.   }
  59.   Log("SendClientCommand end");
  60.   return 0;
  61. }
  62.  
  63. int GetServerVersion(HWND hWnd,PowerSettings *ps,char *errorstr)
  64. {
  65.   int ret,version,release,major,minor,bugfix;
  66.   char str[500],*value;
  67.  
  68.   Log("GetServerVersion start");
  69.   if (ps->clientsocket!=INVALID_SOCKET)
  70.   {
  71.     Log("Sending VERSION to remote machine");
  72.     if (send(ps->clientsocket,"VERSION\n",8,0)==SOCKET_ERROR)
  73.     {
  74.       CloseClientSocket(hWnd,ps);
  75.       return -1;
  76.     }
  77.     Log("Waiting for a reply");
  78.     if ((ret=recv(ps->clientsocket,str,500,0))==SOCKET_ERROR || ret<=0)
  79.     {
  80.       CloseClientSocket(hWnd,ps);
  81.       return -1;
  82.     }
  83.     Log("Received %s from remote machine",str);
  84.     str[ret]=0;
  85.     while (strlen(str)>0 && (str[strlen(str)-1]=='\n' || str[strlen(str)-1]=='\r'))
  86.       str[strlen(str)-1]='\0';
  87.     value=strchr(str,' ');
  88.     if (value)
  89.     {
  90.       *value='\0';
  91.       value++;
  92.       Trim(value);
  93.     }
  94.     if (str[0]=='2')
  95.     {
  96.       Log("SendClientCommand end");
  97.       sscanf(value,"%d.%d.%d.%d",&release,&major,&minor,&bugfix);
  98.       version=bugfix+minor*100+major*10000+release*1000000;
  99.       Log("version=%d",version);
  100.     }
  101.     else if (str[0]=='5')
  102.     {
  103.       strcpy(errorstr,value);
  104.       Log("SendClientCommand ended with error %s",errorstr);
  105.       return -2;
  106.     }
  107.     else
  108.     {
  109.       strcpy(errorstr,"Unknown response received");
  110.       Log("SendClientCommand ended with error %s",errorstr);
  111.       return -2;
  112.     }
  113.   }
  114.   Log("GetServerVersion end");
  115.   return version;
  116. }
  117.  
  118. int SendRemoteCommand(HWND hWnd,PowerSettings *ps,char *errorstr)
  119. {
  120.   int ret,version;
  121.   char str[500];
  122.  
  123.   Log("SendRemoteCommand start");
  124.   if ((ret=OpenServerConnection(hWnd,ps,ps->remote.computer_name,errorstr))!=0)
  125.     return ret;
  126.   if ((version=GetServerVersion(hWnd,ps,errorstr))==0)
  127.     return 1;
  128.   if ((ret=SendClientCommand(hWnd,ps,errorstr,"PASSWORD %s",ps->remote.password))!=0)
  129.     return ret;
  130.   switch (ps->action)
  131.   {
  132.     case LOGOFF:
  133.       if ((ret=SendClientCommand(hWnd,ps,errorstr,"ACTION LOGOFF"))!=0)
  134.         return ret;
  135.       break;
  136.     case REBOOT:
  137.       if ((ret=SendClientCommand(hWnd,ps,errorstr,"ACTION REBOOT"))!=0)
  138.         return ret;
  139.       break;
  140.     case SHUTDOWN:
  141.       if ((ret=SendClientCommand(hWnd,ps,errorstr,"ACTION SHUTDOWN"))!=0)
  142.         return ret;
  143.       break;
  144.     case POWEROFF:
  145.       if ((ret=SendClientCommand(hWnd,ps,errorstr,"ACTION POWEROFF"))!=0)
  146.         return ret;
  147.       break;
  148.     case STANDBY:
  149.       if ((ret=SendClientCommand(hWnd,ps,errorstr,"ACTION STANDBY"))!=0)
  150.         return ret;
  151.       break;
  152.     case HIBERNATE:
  153.       if ((ret=SendClientCommand(hWnd,ps,errorstr,"ACTION HIBERNATE"))!=0)
  154.         return ret;
  155.       break;
  156.     case LOCK:
  157.       if ((ret=SendClientCommand(hWnd,ps,errorstr,"ACTION LOCK"))!=0)
  158.         return ret;
  159.       break;
  160.     case WAKE_ON_LAN:
  161.       if ((ret=SendClientCommand(hWnd,ps,errorstr,"ACTION WAKE_ON_LAN"))!=0)
  162.         return ret;
  163.       break;
  164.     case MONITOR_OFF:
  165.       if (version<3000100)
  166.       {
  167.         strcpy(errorstr,"Version of poweroff server must be at least 3.0.1.0 to support this action");
  168.         return -2;
  169.       }
  170.       if ((ret=SendClientCommand(hWnd,ps,errorstr,"ACTION MONITOR_OFF"))!=0)
  171.         return ret;
  172.       break;
  173.     case MONITOR_ON:
  174.       if (version<3000100)
  175.       {
  176.         strcpy(errorstr,"Version of poweroff server must be at least 3.0.1.0 to support this action");
  177.         return -2;
  178.       }
  179.       if ((ret=SendClientCommand(hWnd,ps,errorstr,"ACTION MONITOR_ON"))!=0)
  180.         return ret;
  181.       break;
  182.     case NO_ACTION:
  183.       if (version<3000100)
  184.       {
  185.         strcpy(errorstr,"Version of poweroff server must be at least 3.0.1.0 to support this action");
  186.         return -2;
  187.       }
  188.       if ((ret=SendClientCommand(hWnd,ps,errorstr,"ACTION NO_ACTION"))!=0)
  189.         return ret;
  190.       break;
  191.     default:
  192.       if ((ret=SendClientCommand(hWnd,ps,errorstr,"ACTION UNKNOWN"))!=0)
  193.         return ret;
  194.       break;
  195.   }
  196.   if ((ret=SendClientCommand(hWnd,ps,errorstr,"WHO LOCAL"))!=0)
  197.     return ret;
  198.   if (ps->remote.schedule_remote)
  199.   {
  200.     switch (ps->when)
  201.     {
  202.       case IMMEDIATE:
  203.         if ((ret=SendClientCommand(hWnd,ps,errorstr,"WHEN IMMEDIATE"))!=0)
  204.           return ret;
  205.         break;
  206.       case SCHEDULED:
  207.         if ((ret=SendClientCommand(hWnd,ps,errorstr,"WHEN SCHEDULED"))!=0)
  208.           return ret;
  209.         break;
  210.       case PROCESS:
  211.         if ((ret=SendClientCommand(hWnd,ps,errorstr,"WHEN AFTER_PROCESS"))!=0)
  212.           return ret;
  213.         break;
  214.       default:
  215.         if ((ret=SendClientCommand(hWnd,ps,errorstr,"WHEN UNKNOWN"))!=0)
  216.           return ret;
  217.         break;
  218.     }
  219.   }
  220.   else
  221.   {
  222.     if ((ret=SendClientCommand(hWnd,ps,errorstr,"WHEN IMMEDIATE"))!=0)
  223.       return ret;
  224.   }
  225.   if ((ret=SendClientCommand(hWnd,ps,errorstr,"SIMULATE %s",ps->simulate?"ON":"OFF"))!=0)
  226.     return ret;
  227.   if (ps->options.warning)
  228.   {
  229.     if ((ret=SendClientCommand(hWnd,ps,errorstr,"WARNING %s",ps->options.warning?"ON":"OFF"))!=0)
  230.       return ret;
  231.     if ((ret=SendClientCommand(hWnd,ps,errorstr,"WARNTIME %d",ps->warning.seconds))!=0)
  232.       return ret;
  233.     strcpy(str,ps->warning.message);
  234.     ReplaceString(str,"\r\n","\\n");
  235.     ReplaceString(str,"\n","\\n");
  236.     if ((ret=SendClientCommand(hWnd,ps,errorstr,"MESSAGE %s",str))!=0)
  237.       return ret;
  238.     if ((ret=SendClientCommand(hWnd,ps,errorstr,"PLAYSOUND %s",ps->warning.play_sound?"ON":"OFF"))!=0)
  239.       return ret;
  240.     if (ps->warning.play_sound)
  241.     {
  242.       if ((ret=SendClientCommand(hWnd,ps,errorstr,"SOUNDFILE %s",ps->warning.sound_file))!=0)
  243.         return ret;
  244.     }
  245.   }
  246.   if ((ret=SendClientCommand(hWnd,ps,errorstr,"RUNPRG %s",ps->options.run_program?"ON":"OFF"))!=0)
  247.     return ret;
  248.   if (ps->options.run_program)
  249.   {
  250.     if ((ret=SendClientCommand(hWnd,ps,errorstr,"PROGRAM %s",ps->program.program))!=0)
  251.       return ret;
  252.     if ((ret=SendClientCommand(hWnd,ps,errorstr,"DIRECTORY %s",ps->program.directory))!=0)
  253.       return ret;
  254.   }
  255.   if (ps->when==SCHEDULED)
  256.   {
  257.     switch (ps->schedule.schedule)
  258.     {
  259.       case FIXED_DAY:
  260.         if ((ret=SendClientCommand(hWnd,ps,errorstr,"SCHEDULE FIXED_DAY"))!=0)
  261.           return ret;
  262.         if ((ret=SendClientCommand(hWnd,ps,errorstr,"DATE %02d/%02d/%04d",ps->schedule.date.wDay,ps->schedule.date.wMonth,ps->schedule.date.wYear))!=0)
  263.           return ret;
  264.         break;
  265.       case DAILY:
  266.         if ((ret=SendClientCommand(hWnd,ps,errorstr,"SCHEDULE DAILY"))!=0)
  267.           return ret;
  268.         str[0]='\0';
  269.         if (ps->schedule.monday)
  270.           ADD_DAY("m")
  271.         if (ps->schedule.tuesday)
  272.           ADD_DAY("t")
  273.         if (ps->schedule.wednesday)
  274.           ADD_DAY("w")
  275.         if (ps->schedule.thursday)
  276.           ADD_DAY("th")
  277.         if (ps->schedule.friday)
  278.           ADD_DAY("f")
  279.         if (ps->schedule.saturday)
  280.           ADD_DAY("s")
  281.         if (ps->schedule.sunday)
  282.           ADD_DAY("su")
  283.         if ((ret=SendClientCommand(hWnd,ps,errorstr,"DAYS %s",str))!=0)
  284.           return ret;
  285.         str[0]='\0';
  286.         if (ps->schedule.week1)
  287.           ADD_DAY("1")
  288.         if (ps->schedule.week2)
  289.           ADD_DAY("2")
  290.         if (ps->schedule.week3)
  291.           ADD_DAY("3")
  292.         if (ps->schedule.week4)
  293.           ADD_DAY("4")
  294.         if (ps->schedule.week5)
  295.           ADD_DAY("5")
  296.         if ((ret=SendClientCommand(hWnd,ps,errorstr,"WEEKS %s",str))!=0)
  297.           return ret;
  298.         break;
  299.       case DAY_OF_MONTH:
  300.         if ((ret=SendClientCommand(hWnd,ps,errorstr,"SCHEDULE DAY_OF_MONTH"))!=0)
  301.           return ret;
  302.         if ((ret=SendClientCommand(hWnd,ps,errorstr,"DAY %d",ps->schedule.day))!=0)
  303.           return ret;
  304.         break;
  305.       case AFTER_X_SECONDS:
  306.         if ((ret=SendClientCommand(hWnd,ps,errorstr,"SCHEDULE AFTER_X_SECONDS"))!=0)
  307.           return ret;
  308.         if ((ret=SendClientCommand(hWnd,ps,errorstr,"SECONDS %d",ps->schedule.seconds))!=0)
  309.           return ret;
  310.         break;
  311.       default:
  312.         if ((ret=SendClientCommand(hWnd,ps,errorstr,"SCHEDULE UNKNOWN"))!=0)
  313.           return ret;
  314.         break;
  315.     }
  316.     if (ps->schedule.schedule!=AFTER_X_SECONDS)
  317.     {
  318.       if ((ret=SendClientCommand(hWnd,ps,errorstr,"TIME %02d:%02d",ps->schedule.time.wHour,ps->schedule.time.wMinute))!=0)
  319.         return ret;
  320.     }
  321.     if ((ret=SendClientCommand(hWnd,ps,errorstr,"WAIT %d",ps->schedule.wait))!=0)
  322.       return ret;
  323.   }
  324.   if (ps->when==PROCESS)
  325.   {
  326.     if ((ret=SendClientCommand(hWnd,ps,errorstr,"PROCESS %s",ps->process.process))!=0)
  327.       return ret;
  328.   }
  329.   if ((ret=SendClientCommand(hWnd,ps,errorstr,"FORCE %s",ps->options.force?"ON":"OFF"))!=0)
  330.     return ret;
  331.   if ((ret=SendClientCommand(hWnd,ps,errorstr,"CANCEL %s",ps->options.allow_cancel?"ON":"OFF"))!=0)
  332.     return ret;
  333.   if ((ret=SendClientCommand(hWnd,ps,errorstr,"DOIT"))!=0)
  334.     return ret;
  335.   CloseClientSocket(hWnd,ps);
  336.   Log("SendRemoteCommand end");
  337.   return 0;
  338. }