examples/lm-send-async.c
author Ryan Schmidt <git@ryandesign.com>
Fri, 09 Jul 2021 10:38:43 -0500
changeset 742 ad6ffcac88a4
parent 690 7ccf2113ec5f
permissions -rw-r--r--
Fix typo: GnuTSL -> GnuTLS committer: GitHub <noreply@github.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
517
6fabea75bea7 Make the emacs header changes to tests and examples
Mikael Hallendal <micke@imendio.com>
parents: 486
diff changeset
     1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
     2
/*
140
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
     3
 * Copyright (C) 2003-2006 Imendio AB
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
     4
 *
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
     5
 * This library is free software; you can redistribute it and/or
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
     6
 * modify it under the terms of the GNU Library General Public
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
     7
 * License as published by the Free Software Foundation; either
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
     8
 * version 2 of the License, or (at your option) any later version.
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
     9
 *
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    10
 * This library is distributed in the hope that it will be useful,
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    13
 * Library General Public License for more details.
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    14
 *
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    15
 * You should have received a copy of the GNU Library General Public
690
7ccf2113ec5f Update the postal address of the FSF
Frank Zschockelt <lm@freakysoft.de>
parents: 664
diff changeset
    16
 * License along with this program; if not, see <https://www.gnu.org/licenses>
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    17
 */
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    18
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    19
/*
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    20
 * Description:
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    21
 * A little program that let you send jabber messages to another person.
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    22
 *
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    23
 * Build instructions:
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    24
 * gcc -o lm-send-async `pkg-config --cflags --libs loudmouth-1.0` lm-send-async.c
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    25
 */
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    26
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    27
#include <stdlib.h>
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    28
#include <string.h>
140
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    29
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    30
#include <loudmouth/loudmouth.h>
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    31
140
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    32
static GMainLoop *main_loop;
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    33
static gboolean   test_success = FALSE;
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    34
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    35
static gchar      expected_fingerprint[20];
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    36
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    37
static gchar     *server = NULL;
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    38
static gint       port = 5222;
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    39
static gchar     *username = NULL;
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    40
static gchar     *password = NULL;
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    41
static gchar     *resource = "lm-send-async";
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    42
static gchar     *recipient = NULL;
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    43
static gchar     *fingerprint = NULL;
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    44
static gchar     *message = "test asynchronous message";
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    45
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    46
static GOptionEntry entries[] =
140
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    47
{
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    48
    { "server", 's', 0, G_OPTION_ARG_STRING, &server,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    49
      "Server to connect to", NULL },
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    50
    { "port", 'P', 0, G_OPTION_ARG_INT, &port,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    51
      "Port to connect to [default=5222]", NULL },
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    52
    { "username", 'u', 0, G_OPTION_ARG_STRING, &username,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    53
      "Username to connect with (user@server.org)", NULL },
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    54
    { "password", 'p', 0, G_OPTION_ARG_STRING, &password,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    55
      "Password to try", NULL },
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    56
    { "resource", 'r', 0, G_OPTION_ARG_STRING, &resource,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    57
      "Resource connect with [default=lm-send-async]", NULL },
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    58
    { "recipient", 'R', 0, G_OPTION_ARG_STRING, &recipient,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    59
      "Recipient to send the message to (e.g. user@server.org)", NULL },
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    60
    { "fingerprint", 'f', 0, G_OPTION_ARG_STRING, &fingerprint,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    61
      "SSL Fingerprint to use", NULL },
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    62
    { "message", 'm', 0, G_OPTION_ARG_STRING, &message,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    63
      "Message to send to recipient [default=test message]", NULL },
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    64
    { NULL }
140
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    65
};
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    66
167
7bcccfa734e2 * examples/lm-send-async.c:
mr <mr>
parents: 140
diff changeset
    67
static gchar *
7bcccfa734e2 * examples/lm-send-async.c:
mr <mr>
parents: 140
diff changeset
    68
get_part_name (const gchar *username)
7bcccfa734e2 * examples/lm-send-async.c:
mr <mr>
parents: 140
diff changeset
    69
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    70
    const gchar *ch;
167
7bcccfa734e2 * examples/lm-send-async.c:
mr <mr>
parents: 140
diff changeset
    71
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    72
    g_return_val_if_fail (username != NULL, NULL);
167
7bcccfa734e2 * examples/lm-send-async.c:
mr <mr>
parents: 140
diff changeset
    73
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    74
    ch = strchr (username, '@');
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    75
    if (!ch) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    76
        return NULL;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    77
    }
167
7bcccfa734e2 * examples/lm-send-async.c:
mr <mr>
parents: 140
diff changeset
    78
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    79
    return g_strndup (username, ch - username);
167
7bcccfa734e2 * examples/lm-send-async.c:
mr <mr>
parents: 140
diff changeset
    80
}
7bcccfa734e2 * examples/lm-send-async.c:
mr <mr>
parents: 140
diff changeset
    81
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    82
static void
140
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    83
print_finger (const char   *fpr,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    84
              unsigned int  size)
140
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    85
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    86
    gint i;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    87
    for (i = 0; i < size-1; i++) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    88
        g_printerr ("%02X:", fpr[i]);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    89
    }
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    90
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    91
    g_printerr ("%02X", fpr[size-1]);
140
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    92
}
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    93
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    94
static LmSSLResponse
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    95
ssl_cb (LmSSL       *ssl,
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    96
        LmSSLStatus  status,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    97
        gpointer     ud)
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    98
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    99
    g_print ("LmSendAsync: SSL status:%d\n", status);
140
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
   100
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   101
    switch (status) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   102
    case LM_SSL_STATUS_NO_CERT_FOUND:
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   103
        g_printerr ("LmSendAsync: No certificate found!\n");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   104
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   105
    case LM_SSL_STATUS_UNTRUSTED_CERT:
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   106
        g_printerr ("LmSendAsync: Certificate is not trusted!\n");
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   107
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   108
    case LM_SSL_STATUS_CERT_EXPIRED:
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   109
        g_printerr ("LmSendAsync: Certificate has expired!\n");
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   110
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   111
    case LM_SSL_STATUS_CERT_NOT_ACTIVATED:
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   112
        g_printerr ("LmSendAsync: Certificate has not been activated!\n");
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   113
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   114
    case LM_SSL_STATUS_CERT_HOSTNAME_MISMATCH:
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   115
        g_printerr ("LmSendAsync: Certificate hostname does not match expected hostname!\n");
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   116
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   117
    case LM_SSL_STATUS_CERT_FINGERPRINT_MISMATCH: {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   118
        const char *fpr = lm_ssl_get_fingerprint (ssl);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   119
        g_printerr ("LmSendAsync: Certificate fingerprint does not match expected fingerprint!\n");
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   120
        g_printerr ("LmSendAsync: Remote fingerprint: ");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   121
        print_finger (fpr, 16);
140
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
   122
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   123
        g_printerr ("\n"
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   124
                    "LmSendAsync: Expected fingerprint: ");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   125
        print_finger (expected_fingerprint, 16);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   126
        g_printerr ("\n");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   127
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   128
    }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   129
    case LM_SSL_STATUS_GENERIC_ERROR:
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   130
        g_printerr ("LmSendAsync: Generic SSL error!\n");
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   131
        break;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   132
    }
140
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
   133
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   134
    return LM_SSL_RESPONSE_CONTINUE;
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   135
}
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   136
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   137
static void
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   138
connection_auth_cb (LmConnection *connection,
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   139
                    gboolean      success,
140
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
   140
                    gpointer      user_data)
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   141
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   142
    if (success) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   143
        GError    *error = NULL;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   144
        LmMessage *m;
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   145
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   146
        g_print ("LmSendAsync: Authenticated successfully\n");
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   147
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   148
        m = lm_message_new (recipient, LM_MESSAGE_TYPE_MESSAGE);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   149
        lm_message_node_add_child (m->node, "body", message);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   150
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   151
        if (!lm_connection_send (connection, m, &error)) {
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   152
            g_printerr ("LmSendAsync: Failed to send message:'%s'\n",
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   153
                        lm_message_node_to_string (m->node));
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   154
        } else {
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   155
            g_print ("LmSendAsync: Sent message:'%s'\n",
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   156
                     lm_message_node_to_string (m->node));
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   157
            test_success = TRUE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   158
        }
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   159
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   160
        lm_message_unref (m);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   161
    } else {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   162
        g_printerr ("LmSendAsync: Failed to authenticate\n");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   163
    }
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   164
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   165
    lm_connection_close (connection, NULL);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   166
    g_main_loop_quit (main_loop);
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   167
}
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   168
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   169
static void
140
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
   170
connection_open_cb (LmConnection *connection,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   171
                    gboolean      success,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   172
                    gpointer      user_data)
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   173
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   174
    if (success) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   175
        gchar *user;
167
7bcccfa734e2 * examples/lm-send-async.c:
mr <mr>
parents: 140
diff changeset
   176
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   177
        user = get_part_name (username);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   178
        if (!lm_connection_authenticate (connection, user,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   179
                                         password, resource,
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   180
                                         connection_auth_cb,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   181
                                         NULL, FALSE,  NULL)) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   182
            g_free (user);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   183
            g_printerr ("LmSendAsync: Failed to send authentication\n");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   184
            g_main_loop_quit (main_loop);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   185
            return;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   186
        }
167
7bcccfa734e2 * examples/lm-send-async.c:
mr <mr>
parents: 140
diff changeset
   187
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   188
        g_free (user);
167
7bcccfa734e2 * examples/lm-send-async.c:
mr <mr>
parents: 140
diff changeset
   189
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   190
        g_print ("LmSendAsync: Sent authentication message\n");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   191
    } else {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   192
        g_printerr ("LmSendAsync: Failed to connect\n");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   193
        g_main_loop_quit (main_loop);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   194
    }
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   195
}
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   196
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   197
int
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   198
main (int argc, char **argv)
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   199
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   200
    GMainContext   *main_context;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   201
    GOptionContext *context;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   202
    LmConnection   *connection;
140
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
   203
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   204
    context = g_option_context_new ("- test send message asynchronously");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   205
    g_option_context_add_main_entries (context, entries, NULL);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   206
    g_option_context_parse (context, &argc, &argv, NULL);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   207
    g_option_context_free (context);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   208
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   209
    if (!username || !password || !recipient) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   210
        g_printerr ("For usage, try %s --help\n", argv[0]);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   211
        return EXIT_FAILURE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   212
    }
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   213
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   214
    if (username && strchr (username, '@') == NULL) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   215
        g_printerr ("LmSendAsync: Username must have an '@' included\n");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   216
        return EXIT_FAILURE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   217
    }
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   218
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   219
    main_context = g_main_context_new ();
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   220
    connection = lm_connection_new_with_context (server, main_context);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   221
    lm_connection_set_port (connection, port);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   222
    lm_connection_set_jid (connection, username);
486
3946c7f48a20 Attempt to use STARTTLS in lm-send-async and test-lm examples.
Mikael Hallendal <micke@imendio.com>
parents: 485
diff changeset
   223
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   224
    if (fingerprint) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   225
        LmSSL *ssl;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   226
        char  *p;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   227
        int    i;
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   228
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   229
        if (port == LM_CONNECTION_DEFAULT_PORT) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   230
            lm_connection_set_port (connection,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   231
                                    LM_CONNECTION_DEFAULT_PORT_SSL);
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   232
        }
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   233
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   234
        for (i = 0, p = fingerprint; *p && *(p+1); i++, p += 3) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   235
            expected_fingerprint[i] = (unsigned char) g_ascii_strtoull (p, NULL, 16);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   236
        }
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   237
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   238
        ssl = lm_ssl_new (expected_fingerprint,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   239
                          (LmSSLFunction) ssl_cb,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   240
                          NULL, NULL);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   241
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   242
        lm_ssl_use_starttls (ssl, TRUE, FALSE);
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   243
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   244
        lm_connection_set_ssl (connection, ssl);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   245
        lm_ssl_unref (ssl);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   246
    }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   247
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   248
    if (!lm_connection_open (connection,
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   249
                             (LmResultFunction) connection_open_cb,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   250
                             NULL, NULL, NULL)) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   251
        g_printerr ("LmSendAsync: Could not open a connection\n");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   252
        return EXIT_FAILURE;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   253
    }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   254
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   255
    main_loop = g_main_loop_new (main_context, FALSE);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   256
    g_main_loop_run (main_loop);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   257
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   258
    return (test_success ? EXIT_SUCCESS : EXIT_FAILURE);
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   259
}