summaryrefslogtreecommitdiff
path: root/examples/receive-messages.c
blob: fbec62f1f0200e1bd4ae886fc4186038a1858cfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdio.h>
#include <stdlib.h>

#include <string.h>

#include <glib.h>

#include <gio/gio.h>
#include <wocky/wocky.h>

GMainLoop *mainloop;

static void
porter_closed_cb (
    GObject *source,
    GAsyncResult *res,
    gpointer user_data)
{
  WockyPorter *porter = WOCKY_PORTER (source);
  WockySession *session = WOCKY_SESSION (user_data);
  GError *error = NULL;

  if (wocky_porter_close_finish (porter, res, &error))
    {
      g_print ("Signed out\n");
    }
  else
    {
      g_warning ("Couldn't sign out cleanly: %s\n", error->message);
      g_clear_error (&error);
    }

  /* Either way, we're done. */
  g_object_unref (session);
  g_main_loop_quit (mainloop);
}

static gboolean
message_received_cb (WockyPorter *porter,
    WockyStanza *stanza,
    gpointer user_data)
{
  WockyNode *body_node = wocky_node_get_child (
      wocky_stanza_get_top_node (stanza),
      "body");
  const gchar *from = wocky_stanza_get_from (stanza);
  const gchar *message;

  /* We told the porter only to give us messages which contained a <body/>
   * element.
   */
  g_assert (body_node != NULL);
  message = body_node->content;

  if (message == NULL)
    message = "";

  g_print ("Message received from %s: “%s”\n", from, message);

  if (g_ascii_strcasecmp (message, "sign out") == 0)
    {
      WockyStanza *reply = wocky_stanza_build (WOCKY_STANZA_TYPE_MESSAGE,
          WOCKY_STANZA_SUB_TYPE_NORMAL, NULL, from,
          '(', "body",
            '$', "Say please! Didn’t your parents teach you any manners‽",
          ')', NULL);

      wocky_porter_send (porter, reply);
      g_object_unref (reply);
    }

  /* Tell the porter that we've handled this stanza; if there were any
   * lower-priority handlers, they would not be called for this stanza.
   */
  return TRUE;
}

static gboolean
sign_out_message_received_cb (WockyPorter *porter,
    WockyStanza *stanza,
    gpointer user_data)
{
  WockySession *session = WOCKY_SESSION (user_data);
  const gchar *from = wocky_stanza_get_from (stanza);

  g_print ("%s asked us nicely to sign out\n", from);
  wocky_porter_close_async (porter, NULL, porter_closed_cb, session);

  /* Returning FALSE tells the porter that other, lower-priority handlers that
   * match the stanza should be invoked — in this example, that means
   * message_received_cb().
   */
  return FALSE;
}

static void
connected_cb (
    GObject *source,
    GAsyncResult *res,
    gpointer user_data)
{
  WockyConnector *connector = WOCKY_CONNECTOR (source);
  WockyXmppConnection *connection;
  gchar *jid = NULL;
  GError *error = NULL;

  connection = wocky_connector_connect_finish (connector, res, &jid, NULL,
      &error);

  if (connection == NULL)
    {
      g_warning ("Couldn't connect: %s", error->message);
      g_clear_error (&error);
      g_main_loop_quit (mainloop);
    }
  else
    {
      WockySession *session = wocky_session_new_with_connection (connection, jid);
      WockyPorter *porter = wocky_session_get_porter (session);
      WockyStanza *stanza;

      g_print ("Connected as %s\n", jid);

      /* Register a callback for incoming <message type='chat'/> stanzas from
       * anyone, but only if it contains a <body/> element (the element that
       * actually contains the text of IMs). */
      wocky_porter_register_handler_from_anyone (porter,
          WOCKY_STANZA_TYPE_MESSAGE, WOCKY_STANZA_SUB_TYPE_CHAT,
          WOCKY_PORTER_HANDLER_PRIORITY_NORMAL,
          message_received_cb, session,
          '(', "body", ')', NULL);

      /* Register a higher-priority handler for incoming <message type='chat'/>
       * stanzas which contain a <body/> element containing the text
       * "please sign out".
       */
      wocky_porter_register_handler_from_anyone (porter,
          WOCKY_STANZA_TYPE_MESSAGE, WOCKY_STANZA_SUB_TYPE_CHAT,
          WOCKY_PORTER_HANDLER_PRIORITY_NORMAL,
          sign_out_message_received_cb, session,
          '(', "body",
            '$', "please sign out",
          ')', NULL);

      wocky_porter_start (porter);

      /* Broadcast presence for ourself, so our contacts can see us online,
       * with a status message. */
      stanza = wocky_stanza_build (WOCKY_STANZA_TYPE_PRESENCE,
          WOCKY_STANZA_SUB_TYPE_NONE, NULL, NULL,
          '(', "show",
            '$', "chat",
          ')',
          '(', "status",
            '$', "talk to me! (or tell me to “sign out”)",
          ')', NULL);

      wocky_porter_send (porter, stanza);

      g_object_unref (stanza);
      g_free (jid);
    }
}

int
main (int argc,
    char **argv)
{
  char *jid, *password;
  WockyConnector *connector;

  g_type_init ();
  wocky_init ();

  if (argc != 3)
    {
      printf ("Usage: %s <jid> <password>\n", argv[0]);
      return -1;
    }

  jid = argv[1];
  password = argv[2];

  mainloop = g_main_loop_new (NULL, FALSE);
  connector = wocky_connector_new (jid, password, NULL, NULL, NULL);
  wocky_connector_connect_async (connector, NULL, connected_cb, NULL);

  g_main_loop_run (mainloop);

  g_object_unref (connector);
  g_main_loop_unref (mainloop);
  return 0;
}