Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <getopt.h>
- #include <loudmouth/loudmouth.h>
- #define DEFAULT_RESOURCE_NAME "Loudmouth"
- /* Types */
- struct arguments_t
- {
- char *username;
- char *password;
- char *server;
- char *resource;
- char **recipients;
- char *message;
- int keep_alive_rate;
- int verbose;
- int port;
- int ssl;
- int wait_for_reply;
- int recieved;
- };
- struct xmpp_t
- {
- LmConnection *connection;
- LmMessage *message;
- LmMessage *presence;
- LmSSL *ssl;
- };
- /* Functions */
- struct arguments_t *parse_args(int argc, char**argv);
- static LmHandlerResult handle_messages(LmMessageHandler *handler, LmConnection *connection, LmMessage *m, gpointer args);
- void free_if_not_arg(char *data, int argc, char **argv);
- static LmHandlerResult handle_messages(LmMessageHandler *handler, LmConnection *connection, LmMessage *m, gpointer args)
- {
- struct arguments_t *arguments = (struct arguments_t *)args;
- arguments->recieved = 1;
- g_print("Incoming message from: %s\n", lm_message_node_get_attribute (m->node, "from"));
- exit(1);
- return LM_HANDLER_RESULT_REMOVE_MESSAGE;
- }
- int main(int argc, char **argv)
- {
- struct arguments_t *args = NULL;
- struct xmpp_t xmpp;
- GError *error = NULL;
- int i = 0;
- memset(&xmpp, 0, sizeof(struct xmpp_t));
- /* Parse arguments */
- args = parse_args(argc, argv);
- /* Set the server */
- if(args->server != NULL)
- {
- if (args->verbose == 1)
- {
- g_print("Using Server %s\n", args->server);
- }
- xmpp.connection = lm_connection_new(args->server);
- }
- else
- {
- g_printerr("Please enter a server\n");
- return 1;
- }
- /* Check for username */
- if (args->username == NULL)
- {
- g_printerr("Enter a username\n");
- return 1;
- }
- /* Check for password */
- if (args->password == NULL)
- {
- g_printerr("Enter a password\n");
- return 1;
- }
- /* Enable ssl if set */
- if(args->ssl == 1)
- {
- if(args->verbose == 1)
- {
- g_print("SSL Enabled\n");
- }
- xmpp.ssl = lm_ssl_new(NULL, NULL, NULL, NULL);
- lm_connection_set_ssl(xmpp.connection, xmpp.ssl);
- lm_connection_set_port(xmpp.connection, LM_CONNECTION_DEFAULT_PORT_SSL);
- }
- /* Set the port, or go with default */
- if(args->port > 0 && args->port <= 65535)
- {
- if(args->verbose == 1)
- {
- g_print("Connecting to port %i\n", args->port);
- }
- lm_connection_set_port(xmpp.connection, args->port);
- }
- else
- {
- if(args->verbose == 1)
- {
- g_print("Connecting to port %i\n", LM_CONNECTION_DEFAULT_PORT);
- }
- }
- /* Set the keep alive rate if they specify one */
- if(args->keep_alive_rate != 0)
- {
- if(args->verbose == 1)
- {
- g_print("Setting keep alive rate of %i.\n");
- }
- lm_connection_set_keep_alive_rate(xmpp.connection, args->keep_alive_rate);
- }
- /* Register the message handler */
- LmMessageHandler *handler;
- handler = lm_message_handler_new(handle_messages, args, NULL);
- lm_connection_register_message_handler(xmpp.connection, handler, LM_MESSAGE_TYPE_MESSAGE, LM_HANDLER_PRIORITY_FIRST);
- if(lm_message_handler_is_valid(handler) == FALSE)
- {
- g_printerr("Could not register message handler\n");
- exit(1);
- }
- /* Set the JID */
- lm_connection_set_jid(xmpp.connection, args->username);
- /* Block until we get a connection */
- if(!lm_connection_open_and_block(xmpp.connection, &error))
- {
- g_printerr("%s\n", error->message);
- return 1;
- }
- /* Check for resource, use the default if none specified */
- if (args->resource == NULL)
- {
- args->resource = malloc(strlen(DEFAULT_RESOURCE_NAME) + 1);
- strcpy(args->resource, DEFAULT_RESOURCE_NAME);
- }
- /* Log in */
- if (!lm_connection_authenticate_and_block(xmpp.connection, args->username, args->password, args->resource, &error))
- {
- g_printerr("%s\n", error->message);
- }
- /* Make sure we have at least 1 recipient */
- if (args->recipients[0] == NULL)
- {
- g_printerr("Enter at least 1 recipient\n");
- return 1;
- }
- /* Announce that we are logged in */
- xmpp.presence = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_PRESENCE, LM_MESSAGE_SUB_TYPE_AVAILABLE);
- if(!lm_connection_send(xmpp.connection, xmpp.presence, &error))
- {
- g_printerr("%s\n", error->message);
- }
- /* Increment through the recipients list */
- while(args->recipients[i] != NULL)
- {
- if(args->verbose == 1)
- {
- g_print("Sending to: %s\n", args->recipients[i]);
- g_print("Message: %s\n", args->message);
- }
- /* Create a message with the first recipient */
- xmpp.message = lm_message_new (args->recipients[i], LM_MESSAGE_TYPE_MESSAGE);
- /* Add the text to the message's node */
- if (args->message)
- {
- lm_message_node_add_child(xmpp.message->node, "body", args->message);
- lm_message_node_set_attribute(xmpp.message->node, "type", "chat");
- }
- else
- {
- g_print("No message specified\n", args->message);
- return 1;
- }
- /* Sanity Check */
- if(lm_connection_get_state(xmpp.connection) != LM_CONNECTION_STATE_AUTHENTICATED)
- {
- g_printerr("Failed to authenicate\n");
- return 1;
- }
- /* Send and block for reply, or send and quit */
- if (args->wait_for_reply == 1)
- {
- if (args->verbose == 1)
- {
- g_print("Sending and waiting for reply...\n");
- g_print("XML Sent:\n%s\n", lm_message_node_to_string(lm_message_get_node(xmpp.message)));
- }
- lm_connection_send(xmpp.connection, xmpp.message, &error);
- if(error != NULL)
- {
- g_printerr("%s\n", error->message);
- return 1;
- }
- /* Wait for a reply */
- int x = 0;
- while(args->recieved != 1)
- {
- x++;
- sleep(1);
- g_print("Waiting... %i\r", x);
- }
- }
- else
- {
- if (args->verbose == 1)
- {
- g_print("XML Sent:\n%s\n", lm_message_node_to_string(lm_message_get_node(xmpp.message)));
- }
- if(!lm_connection_send(xmpp.connection, xmpp.message, &error))
- {
- g_printerr("Failed sending message: %s\n", error->message);
- }
- }
- i++;
- lm_message_unref(xmpp.message);
- }
- if (error != NULL)
- {
- g_printerr("%s\n", error->message);
- }
- /* Cleanup */
- lm_connection_close(xmpp.connection, NULL);
- lm_connection_unref(xmpp.connection);
- lm_message_handler_unref (handler);
- if(args != NULL)
- {
- i = 0;
- if (args->recipients != NULL)
- {
- free(args->recipients);
- }
- free_if_not_arg(args->message, argc, argv);
- free_if_not_arg(args->resource, argc, argv);
- free_if_not_arg(args->server, argc, argv);
- free(args);
- }
- return 0;
- }
- void free_if_not_arg(char *data, int argc, char **argv)
- {
- int i;
- for(i = 0; i <= argc; i++)
- {
- if(data == argv[i])
- {
- break;
- }
- else if(argc == i)
- {
- free(data);
- }
- }
- }
- struct arguments_t *parse_args(int argc, char**argv)
- {
- static struct option long_options[] =
- {
- {"verbose", no_argument, 0, 'v'},
- {"username", required_argument, 0, 'u'},
- {"password", required_argument, 0, 'p'},
- {"keep-alive-rate", required_argument, 0, 'k'},
- {"server", required_argument, 0, 's'},
- {"ssl", no_argument, 0, 't'},
- {"message", required_argument, 0, 'm'},
- {"message-file", required_argument, 0, 'f'},
- {"resource", required_argument, 0, 'r'},
- {"wait-for-reply", no_argument, 0, 'w'},
- {"recipients", required_argument, 0, 'e'},
- {"help", no_argument, 0, 'h'},
- {0, 0, 0, 0}
- };
- struct arguments_t *arguments = NULL;
- arguments = calloc(1, sizeof(struct arguments_t));
- int args;
- int i;
- int recipients_size = 0;
- arguments->recipients = realloc(arguments->recipients, (sizeof(char *) * recipients_size) + 1);
- memset(arguments->recipients, 0, sizeof(char *));
- char *result = NULL;
- FILE *message_file = NULL;
- for (i = 0; i <= argc; i++)
- {
- int option_index = 0;
- args = getopt_long(argc, argv, "vu:p:k:st:m:f:r:we:h", long_options, &option_index);
- switch(args)
- {
- case -1:
- break;
- case 'v':
- arguments->verbose = 1;
- break;
- case 'u':
- arguments->username = optarg;
- break;
- case 'p':
- arguments->password = optarg;
- break;
- case 'k':
- arguments->keep_alive_rate = atoi(optarg);
- break;
- case 'f':
- printf("yay");
- /* Read file into memory */
- size_t size;
- if((message_file = fopen(optarg, "rb")) == NULL)
- {
- g_printerr("Could not open \"%s\"\n", optarg);
- exit(1);
- }
- /* Determine Size */
- fseek(message_file , 0 , SEEK_END);
- size = ftell(message_file);
- rewind(message_file);
- arguments->message = calloc(sizeof(char), size);
- size = fread(arguments->message, 1, size, message_file);
- fclose(message_file);
- g_print("Message: %s\n", arguments->message);
- case 't':
- arguments->ssl = 1;
- break;
- case 's':
- if((result = strstr(optarg, ":")) != NULL)
- {
- arguments->port = atoi(&result[1]);
- arguments->server = calloc(1, strlen(optarg) - strlen(&result[1]) + 1);
- strncpy(arguments->server, optarg, (strlen(optarg) - strlen(&result[1])));
- if(arguments->port < 0 || arguments->port > 65535)
- {
- g_printerr("Bad port specified: %i\n", arguments->port);
- exit(1);
- }
- }
- else
- {
- arguments->server = optarg;
- }
- break;
- case 'e':
- recipients_size++;
- arguments->recipients = realloc(arguments->recipients, (sizeof(char *) * recipients_size) + 1);
- arguments->recipients[recipients_size - 1] = optarg;
- arguments->recipients[recipients_size] = NULL;
- break;
- case 'm':
- arguments->message = optarg;
- break;
- case 'r':
- arguments->resource = optarg;
- break;
- case 'w':
- arguments->wait_for_reply = 1;
- break;
- case 'h':
- g_print("Usage: %s <options>\n"
- " --username (-u)\t\tEnter a username\n"
- " --password (-p)\t\tEnter a password\n"
- " --server (-s)\t\t\tEnter a server hostname. Enter :portnumber to specify port\n"
- " --ssl (-t)\t\t\tEnable ssl\n"
- " --keep-alive-rate (-t)\tSet the keep alive rate\n"
- " --resource (-r)\t\tEnter the resource\n"
- " --message (-f)\t\tEnter the message\n"
- " --message_file (-m)\t\tEnter a filename for the message\n"
- " --recipients (-e)\t\tEnter the people to send the message to\n"
- " --wait-for-reply (-w)\t\tWait for a reply and print to stdout\n"
- " --help (-h)\t\t\tDisplay this\n"
- " --verbose (-v)\t\tBe verbose\n", argv[0]);
- exit(0);
- case '?':
- exit(1);
- }
- }
- return arguments;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement