examples/lm-send-sync.c
changeset 167 7bcccfa734e2
parent 140 103227122f45
child 246 ef2e388b8412
equal deleted inserted replaced
166:2e6fae54b2fb 167:7bcccfa734e2
    61   { "message", 'm', 0, G_OPTION_ARG_STRING, &message, 
    61   { "message", 'm', 0, G_OPTION_ARG_STRING, &message, 
    62     "Message to send to recipient [default=test synchronous message]", NULL },
    62     "Message to send to recipient [default=test synchronous message]", NULL },
    63   { NULL }
    63   { NULL }
    64 };
    64 };
    65 
    65 
       
    66 static gchar *
       
    67 get_part_name (const gchar *username)
       
    68 {
       
    69 	const gchar *ch;
       
    70 
       
    71 	g_return_val_if_fail (username != NULL, NULL);
       
    72 
       
    73 	ch = strchr (username, '@');
       
    74 	if (!ch) {
       
    75 		return NULL;
       
    76 	}
       
    77 
       
    78 	return g_strndup (username, ch - username);
       
    79 }
       
    80 
    66 static void
    81 static void
    67 print_finger (const char   *fpr,
    82 print_finger (const char   *fpr,
    68 	      unsigned int  size)
    83 	      unsigned int  size)
    69 {
    84 {
    70 	gint i;
    85 	gint i;
   123 {
   138 {
   124 	GOptionContext *context;
   139 	GOptionContext *context;
   125 	GError         *error = NULL;
   140 	GError         *error = NULL;
   126         LmConnection   *connection;
   141         LmConnection   *connection;
   127 	LmMessage      *m;
   142 	LmMessage      *m;
       
   143 	gchar          *user;
   128 
   144 
   129 	context = g_option_context_new ("- test send message synchronously");
   145 	context = g_option_context_new ("- test send message synchronously");
   130 	g_option_context_add_main_entries (context, entries, NULL);
   146 	g_option_context_add_main_entries (context, entries, NULL);
   131 	g_option_context_parse (context, &argc, &argv, NULL);
   147 	g_option_context_parse (context, &argc, &argv, NULL);
   132 	g_option_context_free (context);
   148 	g_option_context_free (context);
   134 	if (!server || !username || !password || !recipient) {
   150 	if (!server || !username || !password || !recipient) {
   135 		g_printerr ("For usage, try %s --help\n", argv[0]);
   151 		g_printerr ("For usage, try %s --help\n", argv[0]);
   136 		return EXIT_FAILURE;
   152 		return EXIT_FAILURE;
   137 	}
   153 	}
   138 
   154 
       
   155 	if (username && strchr (username, '@') == NULL) {
       
   156 		g_printerr ("LmSendSync: Username must have an '@' included\n");
       
   157 		return EXIT_FAILURE;
       
   158 	}
       
   159 
   139         connection = lm_connection_new (server);
   160         connection = lm_connection_new (server);
   140 	lm_connection_set_port (connection, port);
   161 	lm_connection_set_port (connection, port);
       
   162 	lm_connection_set_jid (connection, username);
   141 
   163 
   142 	if (fingerprint) {
   164 	if (fingerprint) {
   143 		LmSSL *ssl;
   165 		LmSSL *ssl;
   144 		char  *p;
   166 		char  *p;
   145 		int    i;
   167 		int    i;
   164         if (!lm_connection_open_and_block (connection, &error)) {
   186         if (!lm_connection_open_and_block (connection, &error)) {
   165                 g_printerr ("LmSendSync: Could not open a connection: %s\n", error->message);
   187                 g_printerr ("LmSendSync: Could not open a connection: %s\n", error->message);
   166 		return EXIT_FAILURE;
   188 		return EXIT_FAILURE;
   167         }
   189         }
   168 
   190 
       
   191 	user = get_part_name (username);
   169 	if (!lm_connection_authenticate_and_block (connection,
   192 	if (!lm_connection_authenticate_and_block (connection,
   170 						   username, password, resource,
   193 						   user, password, resource,
   171 						   &error)) {
   194 						   &error)) {
       
   195 		g_free (user);
   172 		g_printerr ("LmSendSync: Failed to authenticate: %s\n", error->message);
   196 		g_printerr ("LmSendSync: Failed to authenticate: %s\n", error->message);
   173 		return EXIT_FAILURE;
   197 		return EXIT_FAILURE;
   174 	}
   198 	}
       
   199 	g_free (user);
   175 	
   200 	
   176 	m = lm_message_new (recipient, LM_MESSAGE_TYPE_MESSAGE);
   201 	m = lm_message_new (recipient, LM_MESSAGE_TYPE_MESSAGE);
   177 	lm_message_node_add_child (m->node, "body", message);
   202 	lm_message_node_add_child (m->node, "body", message);
   178 	
   203 	
   179 	if (!lm_connection_send (connection, m, &error)) {
   204 	if (!lm_connection_send (connection, m, &error)) {