examples/lm-send-async.c
changeset 518 cdd6a0c5b439
parent 517 6fabea75bea7
child 664 f57b1b61e1fe
equal deleted inserted replaced
517:6fabea75bea7 518:cdd6a0c5b439
    45 static gchar     *fingerprint = NULL;
    45 static gchar     *fingerprint = NULL;
    46 static gchar     *message = "test asynchronous message";
    46 static gchar     *message = "test asynchronous message";
    47 
    47 
    48 static GOptionEntry entries[] = 
    48 static GOptionEntry entries[] = 
    49 {
    49 {
    50   { "server", 's', 0, G_OPTION_ARG_STRING, &server, 
    50     { "server", 's', 0, G_OPTION_ARG_STRING, &server, 
    51     "Server to connect to", NULL },
    51       "Server to connect to", NULL },
    52   { "port", 'P', 0, G_OPTION_ARG_INT, &port, 
    52     { "port", 'P', 0, G_OPTION_ARG_INT, &port, 
    53     "Port to connect to [default=5222]", NULL },
    53       "Port to connect to [default=5222]", NULL },
    54   { "username", 'u', 0, G_OPTION_ARG_STRING, &username, 
    54     { "username", 'u', 0, G_OPTION_ARG_STRING, &username, 
    55     "Username to connect with (user@server.org)", NULL },
    55       "Username to connect with (user@server.org)", NULL },
    56   { "password", 'p', 0, G_OPTION_ARG_STRING, &password, 
    56     { "password", 'p', 0, G_OPTION_ARG_STRING, &password, 
    57     "Password to try", NULL },
    57       "Password to try", NULL },
    58   { "resource", 'r', 0, G_OPTION_ARG_STRING, &resource, 
    58     { "resource", 'r', 0, G_OPTION_ARG_STRING, &resource, 
    59     "Resource connect with [default=lm-send-async]", NULL },
    59       "Resource connect with [default=lm-send-async]", NULL },
    60   { "recipient", 'R', 0, G_OPTION_ARG_STRING, &recipient, 
    60     { "recipient", 'R', 0, G_OPTION_ARG_STRING, &recipient, 
    61     "Recipient to send the message to (e.g. user@server.org)", NULL },
    61       "Recipient to send the message to (e.g. user@server.org)", NULL },
    62   { "fingerprint", 'f', 0, G_OPTION_ARG_STRING, &fingerprint, 
    62     { "fingerprint", 'f', 0, G_OPTION_ARG_STRING, &fingerprint, 
    63     "SSL Fingerprint to use", NULL },
    63       "SSL Fingerprint to use", NULL },
    64   { "message", 'm', 0, G_OPTION_ARG_STRING, &message, 
    64     { "message", 'm', 0, G_OPTION_ARG_STRING, &message, 
    65     "Message to send to recipient [default=test message]", NULL },
    65       "Message to send to recipient [default=test message]", NULL },
    66   { NULL }
    66     { NULL }
    67 };
    67 };
    68 
    68 
    69 static gchar *
    69 static gchar *
    70 get_part_name (const gchar *username)
    70 get_part_name (const gchar *username)
    71 {
    71 {
    72 	const gchar *ch;
    72     const gchar *ch;
    73 
    73 
    74 	g_return_val_if_fail (username != NULL, NULL);
    74     g_return_val_if_fail (username != NULL, NULL);
    75 
    75 
    76 	ch = strchr (username, '@');
    76     ch = strchr (username, '@');
    77 	if (!ch) {
    77     if (!ch) {
    78 		return NULL;
    78         return NULL;
    79 	}
    79     }
    80 
    80 
    81 	return g_strndup (username, ch - username);
    81     return g_strndup (username, ch - username);
    82 }
    82 }
    83 
    83 
    84 static void
    84 static void
    85 print_finger (const char   *fpr,
    85 print_finger (const char   *fpr,
    86 	      unsigned int  size)
    86               unsigned int  size)
    87 {
    87 {
    88 	gint i;
    88     gint i;
    89 	for (i = 0; i < size-1; i++) {
    89     for (i = 0; i < size-1; i++) {
    90 		g_printerr ("%02X:", fpr[i]);
    90         g_printerr ("%02X:", fpr[i]);
    91 	}
    91     }
    92 	
    92     
    93 	g_printerr ("%02X", fpr[size-1]);
    93     g_printerr ("%02X", fpr[size-1]);
    94 }
    94 }
    95 
    95 
    96 static LmSSLResponse
    96 static LmSSLResponse
    97 ssl_cb (LmSSL       *ssl, 
    97 ssl_cb (LmSSL       *ssl, 
    98 	LmSSLStatus  status, 
    98         LmSSLStatus  status, 
    99 	gpointer     ud)
    99         gpointer     ud)
   100 {
   100 {
   101 	g_print ("LmSendAsync: SSL status:%d\n", status);
   101     g_print ("LmSendAsync: SSL status:%d\n", status);
   102 
   102 
   103 	switch (status) {
   103     switch (status) {
   104 	case LM_SSL_STATUS_NO_CERT_FOUND:
   104     case LM_SSL_STATUS_NO_CERT_FOUND:
   105 		g_printerr ("LmSendAsync: No certificate found!\n");
   105         g_printerr ("LmSendAsync: No certificate found!\n");
   106 		break;
   106         break;
   107 	case LM_SSL_STATUS_UNTRUSTED_CERT:
   107     case LM_SSL_STATUS_UNTRUSTED_CERT:
   108 		g_printerr ("LmSendAsync: Certificate is not trusted!\n"); 
   108         g_printerr ("LmSendAsync: Certificate is not trusted!\n"); 
   109 		break;
   109         break;
   110 	case LM_SSL_STATUS_CERT_EXPIRED:
   110     case LM_SSL_STATUS_CERT_EXPIRED:
   111 		g_printerr ("LmSendAsync: Certificate has expired!\n"); 
   111         g_printerr ("LmSendAsync: Certificate has expired!\n"); 
   112 		break;
   112         break;
   113 	case LM_SSL_STATUS_CERT_NOT_ACTIVATED:
   113     case LM_SSL_STATUS_CERT_NOT_ACTIVATED:
   114 		g_printerr ("LmSendAsync: Certificate has not been activated!\n"); 
   114         g_printerr ("LmSendAsync: Certificate has not been activated!\n"); 
   115 		break;
   115         break;
   116 	case LM_SSL_STATUS_CERT_HOSTNAME_MISMATCH:
   116     case LM_SSL_STATUS_CERT_HOSTNAME_MISMATCH:
   117 		g_printerr ("LmSendAsync: Certificate hostname does not match expected hostname!\n"); 
   117         g_printerr ("LmSendAsync: Certificate hostname does not match expected hostname!\n"); 
   118 		break;
   118         break;
   119 	case LM_SSL_STATUS_CERT_FINGERPRINT_MISMATCH: {
   119     case LM_SSL_STATUS_CERT_FINGERPRINT_MISMATCH: {
   120 		const char *fpr = lm_ssl_get_fingerprint (ssl);
   120         const char *fpr = lm_ssl_get_fingerprint (ssl);
   121 		g_printerr ("LmSendAsync: Certificate fingerprint does not match expected fingerprint!\n"); 
   121         g_printerr ("LmSendAsync: Certificate fingerprint does not match expected fingerprint!\n"); 
   122 		g_printerr ("LmSendAsync: Remote fingerprint: ");
   122         g_printerr ("LmSendAsync: Remote fingerprint: ");
   123 		print_finger (fpr, 16);
   123         print_finger (fpr, 16);
   124 
   124 
   125 		g_printerr ("\n"
   125         g_printerr ("\n"
   126 			    "LmSendAsync: Expected fingerprint: ");
   126                     "LmSendAsync: Expected fingerprint: ");
   127 		print_finger (expected_fingerprint, 16);
   127         print_finger (expected_fingerprint, 16);
   128 		g_printerr ("\n");
   128         g_printerr ("\n");
   129 		break;
   129         break;
   130 	}
   130     }
   131 	case LM_SSL_STATUS_GENERIC_ERROR:
   131     case LM_SSL_STATUS_GENERIC_ERROR:
   132 		g_printerr ("LmSendAsync: Generic SSL error!\n"); 
   132         g_printerr ("LmSendAsync: Generic SSL error!\n"); 
   133 		break;
   133         break;
   134 	}
   134     }
   135 
   135 
   136 	return LM_SSL_RESPONSE_CONTINUE;
   136     return LM_SSL_RESPONSE_CONTINUE;
   137 }
   137 }
   138 
   138 
   139 static void
   139 static void
   140 connection_auth_cb (LmConnection *connection, 
   140 connection_auth_cb (LmConnection *connection, 
   141                     gboolean      success, 
   141                     gboolean      success, 
   142                     gpointer      user_data)
   142                     gpointer      user_data)
   143 {
   143 {
   144 	if (success) {
   144     if (success) {
   145 		GError    *error = NULL;
   145         GError    *error = NULL;
   146 		LmMessage *m;
   146         LmMessage *m;
   147 
   147 
   148 		g_print ("LmSendAsync: Authenticated successfully\n");
   148         g_print ("LmSendAsync: Authenticated successfully\n");
   149 		
   149         
   150 		m = lm_message_new (recipient, LM_MESSAGE_TYPE_MESSAGE);
   150         m = lm_message_new (recipient, LM_MESSAGE_TYPE_MESSAGE);
   151 		lm_message_node_add_child (m->node, "body", message);
   151         lm_message_node_add_child (m->node, "body", message);
   152 		
   152         
   153 		if (!lm_connection_send (connection, m, &error)) {
   153         if (!lm_connection_send (connection, m, &error)) {
   154 			g_printerr ("LmSendAsync: Failed to send message:'%s'\n", 
   154             g_printerr ("LmSendAsync: Failed to send message:'%s'\n", 
   155 				 lm_message_node_to_string (m->node));
   155                         lm_message_node_to_string (m->node));
   156 		} else {
   156         } else {
   157 			g_print ("LmSendAsync: Sent message:'%s'\n", 
   157             g_print ("LmSendAsync: Sent message:'%s'\n", 
   158 				 lm_message_node_to_string (m->node));
   158                      lm_message_node_to_string (m->node));
   159 			test_success = TRUE;
   159             test_success = TRUE;
   160 		}
   160         }
   161 		
   161         
   162 		lm_message_unref (m);
   162         lm_message_unref (m);
   163 	} else {
   163     } else {
   164 		g_printerr ("LmSendAsync: Failed to authenticate\n");
   164         g_printerr ("LmSendAsync: Failed to authenticate\n");
   165 	}
   165     }
   166 
   166 
   167 	lm_connection_close (connection, NULL);
   167     lm_connection_close (connection, NULL);
   168 	g_main_loop_quit (main_loop);
   168     g_main_loop_quit (main_loop);
   169 }
   169 }
   170 
   170 
   171 static void
   171 static void
   172 connection_open_cb (LmConnection *connection,
   172 connection_open_cb (LmConnection *connection,
   173 		    gboolean      success,
   173                     gboolean      success,
   174 		    gpointer      user_data)
   174                     gpointer      user_data)
   175 {
   175 {
   176 	if (success) {
   176     if (success) {
   177 		gchar *user;
   177         gchar *user;
   178 
   178 
   179 		user = get_part_name (username);
   179         user = get_part_name (username);
   180 		if (!lm_connection_authenticate (connection, user, 
   180         if (!lm_connection_authenticate (connection, user, 
   181 						 password, resource,
   181                                          password, resource,
   182 						 connection_auth_cb, 
   182                                          connection_auth_cb, 
   183 						 NULL, FALSE,  NULL)) {
   183                                          NULL, FALSE,  NULL)) {
   184 			g_free (user);
   184             g_free (user);
   185 			g_printerr ("LmSendAsync: Failed to send authentication\n");
   185             g_printerr ("LmSendAsync: Failed to send authentication\n");
   186 			g_main_loop_quit (main_loop);
   186             g_main_loop_quit (main_loop);
   187 			return;
   187             return;
   188 		}
   188         }
   189 
   189 
   190 		g_free (user);
   190         g_free (user);
   191 
   191 
   192 		g_print ("LmSendAsync: Sent authentication message\n");
   192         g_print ("LmSendAsync: Sent authentication message\n");
   193 	} else {
   193     } else {
   194 		g_printerr ("LmSendAsync: Failed to connect\n");
   194         g_printerr ("LmSendAsync: Failed to connect\n");
   195 		g_main_loop_quit (main_loop);
   195         g_main_loop_quit (main_loop);
   196 	}
   196     }
   197 }
   197 }
   198 
   198 
   199 int
   199 int
   200 main (int argc, char **argv)
   200 main (int argc, char **argv)
   201 {
   201 {
   202 	GMainContext   *main_context;
   202     GMainContext   *main_context;
   203 	GOptionContext *context;
   203     GOptionContext *context;
   204         LmConnection   *connection;
   204     LmConnection   *connection;
   205 
   205 
   206 	context = g_option_context_new ("- test send message asynchronously");
   206     context = g_option_context_new ("- test send message asynchronously");
   207 	g_option_context_add_main_entries (context, entries, NULL);
   207     g_option_context_add_main_entries (context, entries, NULL);
   208 	g_option_context_parse (context, &argc, &argv, NULL);
   208     g_option_context_parse (context, &argc, &argv, NULL);
   209 	g_option_context_free (context);
   209     g_option_context_free (context);
   210 	
   210     
   211 	if (!username || !password || !recipient) {
   211     if (!username || !password || !recipient) {
   212 		g_printerr ("For usage, try %s --help\n", argv[0]);
   212         g_printerr ("For usage, try %s --help\n", argv[0]);
   213 		return EXIT_FAILURE;
   213         return EXIT_FAILURE;
   214 	}
   214     }
   215 
   215 
   216 	if (username && strchr (username, '@') == NULL) {
   216     if (username && strchr (username, '@') == NULL) {
   217 		g_printerr ("LmSendAsync: Username must have an '@' included\n");
   217         g_printerr ("LmSendAsync: Username must have an '@' included\n");
   218 		return EXIT_FAILURE;
   218         return EXIT_FAILURE;
   219 	}
   219     }
   220 
   220 
   221 	main_context = g_main_context_new ();
   221     main_context = g_main_context_new ();
   222         connection = lm_connection_new_with_context (server, main_context);
   222     connection = lm_connection_new_with_context (server, main_context);
   223 	lm_connection_set_port (connection, port);
   223     lm_connection_set_port (connection, port);
   224 	lm_connection_set_jid (connection, username);
   224     lm_connection_set_jid (connection, username);
   225 
   225 
   226 	if (fingerprint) {
   226     if (fingerprint) {
   227 		LmSSL *ssl;
   227         LmSSL *ssl;
   228 		char  *p;
   228         char  *p;
   229 		int    i;
   229         int    i;
   230 		
   230         
   231 		if (port == LM_CONNECTION_DEFAULT_PORT) {
   231         if (port == LM_CONNECTION_DEFAULT_PORT) {
   232 			lm_connection_set_port (connection,
   232             lm_connection_set_port (connection,
   233 						LM_CONNECTION_DEFAULT_PORT_SSL);
   233                                     LM_CONNECTION_DEFAULT_PORT_SSL);
   234 		}
   234         }
   235 
   235 
   236 		for (i = 0, p = fingerprint; *p && *(p+1); i++, p += 3) {
   236         for (i = 0, p = fingerprint; *p && *(p+1); i++, p += 3) {
   237 			expected_fingerprint[i] = (unsigned char) g_ascii_strtoull (p, NULL, 16);
   237             expected_fingerprint[i] = (unsigned char) g_ascii_strtoull (p, NULL, 16);
   238 		}
   238         }
   239 	
   239     
   240 		ssl = lm_ssl_new (expected_fingerprint,
   240         ssl = lm_ssl_new (expected_fingerprint,
   241 				  (LmSSLFunction) ssl_cb,
   241                           (LmSSLFunction) ssl_cb,
   242 				  NULL, NULL);
   242                           NULL, NULL);
   243 	
   243     
   244                 lm_ssl_use_starttls (ssl, TRUE, FALSE);
   244         lm_ssl_use_starttls (ssl, TRUE, FALSE);
   245 
   245 
   246 		lm_connection_set_ssl (connection, ssl);
   246         lm_connection_set_ssl (connection, ssl);
   247 		lm_ssl_unref (ssl);
   247         lm_ssl_unref (ssl);
   248 	}
   248     }
   249 
   249 
   250 	if (!lm_connection_open (connection, 
   250     if (!lm_connection_open (connection, 
   251                                  (LmResultFunction) connection_open_cb,
   251                              (LmResultFunction) connection_open_cb,
   252                                  NULL, NULL, NULL)) {
   252                              NULL, NULL, NULL)) {
   253                 g_printerr ("LmSendAsync: Could not open a connection\n");
   253         g_printerr ("LmSendAsync: Could not open a connection\n");
   254 		return EXIT_FAILURE;
   254         return EXIT_FAILURE;
   255         }
   255     }
   256 
   256 
   257         main_loop = g_main_loop_new (main_context, FALSE);
   257     main_loop = g_main_loop_new (main_context, FALSE);
   258         g_main_loop_run (main_loop);
   258     g_main_loop_run (main_loop);
   259 
   259 
   260 	return (test_success ? EXIT_SUCCESS : EXIT_FAILURE);
   260     return (test_success ? EXIT_SUCCESS : EXIT_FAILURE);
   261 }
   261 }