Advertisement
Guest User

Untitled

a guest
May 7th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.33 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <getopt.h>
  5. #include <loudmouth/loudmouth.h>
  6.  
  7. #define DEFAULT_RESOURCE_NAME "Loudmouth"
  8.  
  9. /* Types */
  10. struct arguments_t
  11. {
  12.     char *username;
  13.     char *password;
  14.     char *server;
  15.     char *resource;
  16.     char **recipients;
  17.     char *message;
  18.     int keep_alive_rate;
  19.     int verbose;
  20.     int port;
  21.     int ssl;
  22.     int wait_for_reply;
  23.     int recieved;
  24. };
  25.  
  26. struct xmpp_t
  27. {
  28.     LmConnection *connection;
  29.     LmMessage *message;
  30.     LmMessage *presence;
  31.     LmSSL *ssl;
  32. };
  33.  
  34. /* Functions */
  35. struct arguments_t *parse_args(int argc, char**argv);
  36. static LmHandlerResult handle_messages(LmMessageHandler *handler, LmConnection *connection, LmMessage *m, gpointer args);
  37. void free_if_not_arg(char *data, int argc, char **argv);
  38.  
  39. static LmHandlerResult handle_messages(LmMessageHandler *handler, LmConnection *connection, LmMessage *m, gpointer args)
  40. {
  41.     struct arguments_t *arguments = (struct arguments_t *)args;
  42.    
  43.     arguments->recieved = 1;
  44.     g_print("Incoming message from: %s\n", lm_message_node_get_attribute (m->node, "from"));
  45.     exit(1);
  46.    
  47.     return LM_HANDLER_RESULT_REMOVE_MESSAGE;
  48. }
  49.  
  50. int main(int argc, char **argv)
  51. {
  52.     struct arguments_t *args = NULL;
  53.     struct xmpp_t xmpp;
  54.     GError *error = NULL;
  55.     int i = 0;
  56.    
  57.     memset(&xmpp, 0, sizeof(struct xmpp_t));
  58.    
  59.     /* Parse arguments */
  60.     args = parse_args(argc, argv);
  61.    
  62.     /* Set the server */
  63.     if(args->server != NULL)
  64.     {
  65.         if (args->verbose == 1)
  66.         {
  67.             g_print("Using Server %s\n", args->server);
  68.         }
  69.        
  70.         xmpp.connection = lm_connection_new(args->server);
  71.     }
  72.     else
  73.     {
  74.         g_printerr("Please enter a server\n");
  75.         return 1;
  76.     }
  77.    
  78.     /* Check for username */       
  79.     if (args->username == NULL)
  80.     {
  81.         g_printerr("Enter a username\n");
  82.         return 1;
  83.     }
  84.    
  85.     /* Check for password */
  86.     if (args->password == NULL)
  87.     {
  88.         g_printerr("Enter a password\n");
  89.         return 1;
  90.     }
  91.    
  92.     /* Enable ssl if set */
  93.     if(args->ssl == 1)
  94.     {
  95.         if(args->verbose == 1)
  96.         {
  97.             g_print("SSL Enabled\n");
  98.         }
  99.         xmpp.ssl = lm_ssl_new(NULL, NULL, NULL, NULL);
  100.         lm_connection_set_ssl(xmpp.connection, xmpp.ssl);
  101.         lm_connection_set_port(xmpp.connection, LM_CONNECTION_DEFAULT_PORT_SSL);
  102.     }
  103.    
  104.     /* Set the port, or go with default */
  105.     if(args->port > 0 && args->port <= 65535)
  106.     {
  107.         if(args->verbose == 1)
  108.         {
  109.             g_print("Connecting to port %i\n", args->port);
  110.         }
  111.         lm_connection_set_port(xmpp.connection, args->port);
  112.     }
  113.     else
  114.     {
  115.         if(args->verbose == 1)
  116.         {
  117.             g_print("Connecting to port %i\n", LM_CONNECTION_DEFAULT_PORT);
  118.         }
  119.     }
  120.                
  121.     /* Set the keep alive rate if they specify one */
  122.     if(args->keep_alive_rate != 0)
  123.     {
  124.         if(args->verbose == 1)
  125.         {
  126.             g_print("Setting keep alive rate of %i.\n");
  127.         }
  128.  
  129.         lm_connection_set_keep_alive_rate(xmpp.connection, args->keep_alive_rate);
  130.     }
  131.  
  132.     /* Register the message handler */
  133.     LmMessageHandler *handler;
  134.     handler = lm_message_handler_new(handle_messages, args, NULL);
  135.     lm_connection_register_message_handler(xmpp.connection, handler, LM_MESSAGE_TYPE_MESSAGE, LM_HANDLER_PRIORITY_FIRST);
  136.     if(lm_message_handler_is_valid(handler) == FALSE)
  137.     {
  138.         g_printerr("Could not register message handler\n");
  139.         exit(1);
  140.     }
  141.    
  142.     /* Set the JID */
  143.     lm_connection_set_jid(xmpp.connection, args->username);
  144.    
  145.     /* Block until we get a connection */
  146.     if(!lm_connection_open_and_block(xmpp.connection, &error))
  147.     {
  148.         g_printerr("%s\n", error->message);
  149.         return 1;
  150.     }
  151.    
  152.     /* Check for resource, use the default if none specified */
  153.     if (args->resource == NULL)
  154.     {  
  155.         args->resource = malloc(strlen(DEFAULT_RESOURCE_NAME) + 1);
  156.         strcpy(args->resource, DEFAULT_RESOURCE_NAME);
  157.     }
  158.  
  159.     /* Log in */
  160.     if (!lm_connection_authenticate_and_block(xmpp.connection, args->username, args->password, args->resource, &error))
  161.     {
  162.         g_printerr("%s\n", error->message);
  163.     }  
  164.  
  165.     /* Make sure we have at least 1 recipient */
  166.     if (args->recipients[0] == NULL)
  167.     {
  168.         g_printerr("Enter at least 1 recipient\n");
  169.         return 1;
  170.     }
  171.    
  172.     /* Announce that we are logged in */
  173.     xmpp.presence = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_PRESENCE, LM_MESSAGE_SUB_TYPE_AVAILABLE);
  174.     if(!lm_connection_send(xmpp.connection, xmpp.presence, &error))
  175.     {
  176.         g_printerr("%s\n", error->message);
  177.     }
  178.    
  179.     /* Increment through the recipients list */
  180.     while(args->recipients[i] != NULL)
  181.     {
  182.         if(args->verbose == 1)
  183.         {
  184.             g_print("Sending to: %s\n", args->recipients[i]);
  185.             g_print("Message: %s\n", args->message);
  186.         }
  187.  
  188.         /* Create a message with the first recipient */
  189.         xmpp.message = lm_message_new (args->recipients[i], LM_MESSAGE_TYPE_MESSAGE);
  190.  
  191.         /* Add the text to the message's node */
  192.         if (args->message)
  193.         {
  194.             lm_message_node_add_child(xmpp.message->node, "body", args->message);
  195.             lm_message_node_set_attribute(xmpp.message->node, "type", "chat");     
  196.         }
  197.         else
  198.         {
  199.             g_print("No message specified\n", args->message);
  200.             return 1;
  201.         }
  202.  
  203.         /* Sanity Check */
  204.         if(lm_connection_get_state(xmpp.connection) != LM_CONNECTION_STATE_AUTHENTICATED)
  205.         {
  206.             g_printerr("Failed to authenicate\n");
  207.             return 1;
  208.         }
  209.        
  210.         /* Send and block for reply, or send and quit */
  211.         if (args->wait_for_reply == 1)
  212.         {
  213.             if (args->verbose == 1)
  214.             {
  215.                 g_print("Sending and waiting for reply...\n");
  216.                 g_print("XML Sent:\n%s\n", lm_message_node_to_string(lm_message_get_node(xmpp.message)));
  217.             }
  218.            
  219.             lm_connection_send(xmpp.connection, xmpp.message, &error);
  220.             if(error != NULL)
  221.             {
  222.                 g_printerr("%s\n", error->message);
  223.                 return 1;
  224.             }
  225.            
  226.             /* Wait for a reply */
  227.             int x = 0;
  228.             while(args->recieved != 1)
  229.             {
  230.                 x++;
  231.                 sleep(1);
  232.                 g_print("Waiting... %i\r", x);
  233.             }
  234.         }
  235.         else
  236.         {
  237.             if (args->verbose == 1)
  238.             {
  239.                 g_print("XML Sent:\n%s\n", lm_message_node_to_string(lm_message_get_node(xmpp.message)));
  240.             }
  241.             if(!lm_connection_send(xmpp.connection, xmpp.message, &error))
  242.             {
  243.                 g_printerr("Failed sending message: %s\n", error->message);
  244.             }
  245.         }
  246.        
  247.         i++;
  248.         lm_message_unref(xmpp.message);
  249.     }
  250.    
  251.     if (error != NULL)
  252.     {
  253.         g_printerr("%s\n", error->message);
  254.     }
  255.    
  256.     /* Cleanup */
  257.     lm_connection_close(xmpp.connection, NULL);
  258.     lm_connection_unref(xmpp.connection);
  259.     lm_message_handler_unref (handler);
  260.     if(args != NULL)
  261.     {
  262.         i = 0;
  263.        
  264.         if (args->recipients != NULL)
  265.         {
  266.             free(args->recipients);
  267.         }
  268.        
  269.         free_if_not_arg(args->message, argc, argv);
  270.         free_if_not_arg(args->resource, argc, argv);
  271.         free_if_not_arg(args->server, argc, argv);
  272.  
  273.         free(args);
  274.     }
  275.    
  276.     return 0;
  277. }
  278.  
  279. void free_if_not_arg(char *data, int argc, char **argv)
  280. {
  281.     int i;
  282.     for(i = 0; i <= argc; i++)
  283.     {
  284.         if(data == argv[i])
  285.         {
  286.             break;
  287.         }
  288.         else if(argc == i)
  289.         {
  290.             free(data);
  291.         }
  292.     }
  293. }
  294.  
  295. struct arguments_t *parse_args(int argc, char**argv)
  296. {
  297.     static struct option long_options[] =
  298.     {
  299.         {"verbose", no_argument, 0, 'v'},
  300.         {"username", required_argument, 0, 'u'},
  301.         {"password", required_argument, 0, 'p'},
  302.         {"keep-alive-rate", required_argument, 0, 'k'},
  303.         {"server", required_argument, 0, 's'},
  304.         {"ssl", no_argument, 0, 't'},
  305.         {"message", required_argument, 0, 'm'},
  306.         {"message-file", required_argument, 0, 'f'},
  307.         {"resource", required_argument, 0, 'r'},
  308.         {"wait-for-reply", no_argument, 0, 'w'},
  309.         {"recipients", required_argument, 0, 'e'},
  310.         {"help", no_argument, 0, 'h'},
  311.         {0, 0, 0, 0}
  312.     };
  313.  
  314.     struct arguments_t *arguments = NULL;
  315.     arguments = calloc(1, sizeof(struct arguments_t));
  316.     int args;
  317.     int i;
  318.     int recipients_size = 0;
  319.  
  320.     arguments->recipients = realloc(arguments->recipients, (sizeof(char *) * recipients_size) + 1);
  321.     memset(arguments->recipients, 0, sizeof(char *));
  322.    
  323.     char *result = NULL;
  324.     FILE *message_file = NULL;
  325.  
  326.     for (i = 0; i <= argc; i++)
  327.     {
  328.         int option_index = 0;
  329.         args = getopt_long(argc, argv, "vu:p:k:st:m:f:r:we:h", long_options, &option_index);
  330.        
  331.         switch(args)
  332.         {
  333.             case -1:
  334.                 break;
  335.             case 'v':
  336.                 arguments->verbose = 1;
  337.                 break;
  338.             case 'u':
  339.                 arguments->username = optarg;
  340.                 break;
  341.             case 'p':
  342.                 arguments->password = optarg;
  343.                 break;
  344.             case 'k':
  345.                 arguments->keep_alive_rate = atoi(optarg);
  346.                 break;
  347.             case 'f':
  348.             printf("yay");
  349.                 /* Read file into memory */
  350.                 size_t size;
  351.                
  352.                 if((message_file = fopen(optarg, "rb")) == NULL)
  353.                 {
  354.                     g_printerr("Could not open \"%s\"\n", optarg);
  355.                     exit(1);
  356.                 }
  357.                 /* Determine Size */
  358.                 fseek(message_file , 0 , SEEK_END);
  359.                 size = ftell(message_file);
  360.                 rewind(message_file);
  361.                 arguments->message = calloc(sizeof(char), size);
  362.                 size = fread(arguments->message, 1, size, message_file);       
  363.                 fclose(message_file);
  364.                 g_print("Message: %s\n", arguments->message);
  365.             case 't':
  366.                 arguments->ssl = 1;
  367.                 break;
  368.             case 's':
  369.                 if((result = strstr(optarg, ":")) != NULL)
  370.                 {
  371.                     arguments->port = atoi(&result[1]);
  372.                     arguments->server = calloc(1, strlen(optarg) - strlen(&result[1]) + 1);
  373.                     strncpy(arguments->server, optarg, (strlen(optarg) - strlen(&result[1])));
  374.                     if(arguments->port < 0 || arguments->port > 65535)
  375.                     {
  376.                         g_printerr("Bad port specified: %i\n", arguments->port);
  377.                         exit(1);
  378.                     }  
  379.                 }
  380.                 else
  381.                 {
  382.                     arguments->server = optarg;
  383.                 }
  384.                 break;
  385.             case 'e':
  386.                 recipients_size++;
  387.                 arguments->recipients = realloc(arguments->recipients, (sizeof(char *) * recipients_size) + 1);
  388.                 arguments->recipients[recipients_size - 1] = optarg;
  389.                 arguments->recipients[recipients_size] = NULL;
  390.                 break;
  391.             case 'm':
  392.                 arguments->message = optarg;
  393.                 break;
  394.             case 'r':
  395.                 arguments->resource = optarg;
  396.                 break;
  397.             case 'w':
  398.                 arguments->wait_for_reply = 1;
  399.                 break;
  400.             case 'h':
  401.                 g_print("Usage: %s <options>\n"
  402.                         "  --username (-u)\t\tEnter a username\n"
  403.                         "  --password (-p)\t\tEnter a password\n"
  404.                         "  --server (-s)\t\t\tEnter a server hostname.  Enter :portnumber to specify port\n"
  405.                         "  --ssl (-t)\t\t\tEnable ssl\n"
  406.                         "  --keep-alive-rate (-t)\tSet the keep alive rate\n"
  407.                         "  --resource (-r)\t\tEnter the resource\n"
  408.                         "  --message (-f)\t\tEnter the message\n"
  409.                         "  --message_file (-m)\t\tEnter a filename for the message\n"
  410.                         "  --recipients (-e)\t\tEnter the people to send the message to\n"
  411.                         "  --wait-for-reply (-w)\t\tWait for a reply and print to stdout\n"
  412.                         "  --help (-h)\t\t\tDisplay this\n"
  413.                         "  --verbose (-v)\t\tBe verbose\n", argv[0]);
  414.                 exit(0);
  415.             case '?':
  416.                 exit(1);
  417.         }
  418.     }
  419.    
  420.     return arguments;
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement