File: simple-client.c

package info (click to toggle)
chatty 0.8.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,448 kB
  • sloc: ansic: 57,146; sql: 5,122; xml: 158; cpp: 39; makefile: 22; sh: 11; lisp: 8; javascript: 6
file content (194 lines) | stat: -rw-r--r-- 5,933 bytes parent folder | download | duplicates (2)
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
194
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
/* client.c
 *
 * Copyright 2022 Purism SPC
 *
 * Author(s):
 *   Mohammed Sadiq <sadiq@sadiqpk.org>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later OR CC0-1.0
 */

#include <stdio.h>

#ifndef CMATRIX_USE_EXPERIMENTAL_API
# define CMATRIX_USE_EXPERIMENTAL_API
#endif /* CMATRIX_USE_EXPERIMENTAL_API */
#include "cmatrix.h"

CmMatrix *matrix;
CmClient *client;

static void
simple_account_sync_cb (CmClient  *cm_client,
                        CmRoom    *room,
                        GPtrArray *events,
                        GError    *err,
                        gpointer   unused)
{
  g_message ("\n");

  if (room && events)
    {
      for (guint i = 0; i < events->len; i++)
        {
          gpointer event;

          event = events->pdata[i];
          g_message ("here: %d", cm_event_get_m_type (event));

          if (CM_IS_ROOM_MESSAGE_EVENT (event) &&
              cm_room_message_event_get_msg_type (event))
            {
              g_message ("text message: %s", cm_room_message_event_get_body (event));
            }
        }
    }

  if (err)
    g_warning ("client error: %s", err->message);

  g_message ("\n");
}

static void
simple_matrix_save_cb (GObject      *object,
                       GAsyncResult *result,
                       gpointer      user_data)
{
  g_autoptr(GError) error = NULL;

  cm_matrix_save_client_finish (matrix, result, &error);

  if (error)
    g_warning ("Error saving client: %s", error->message);

  /* Now, enable the client, and the client will start to sync, executing the callback on events */
  cm_client_set_enabled (client, TRUE);
}

static void
simple_get_homeserver_cb (GObject      *object,
                          GAsyncResult *result,
                          gpointer      user_data)
{
  g_autoptr(GError) error = NULL;
  const char *server;
  char homeserver[255];

  server = cm_client_get_homeserver_finish (client, result, &error);

  /* It's okay to not able to get homeserver from username */
  if (error)
    g_warning ("Failed to guess/verify homeserver: %s", error->message);
  else if (server)
    g_message ("autofetched homeserver: %s", server);

  /* Not having a homeserver set means we failed to guess it from provided login id */
  /* So ask user for one. */
  while (!cm_client_get_homeserver (client))
    {
      printf ("Input your Matrix homeserver address: ");
      scanf ("%s", homeserver);
      if (!cm_client_set_homeserver (client, homeserver))
        g_warning ("'%s' is not a valid homeserver uri (did you forget to "
                   "prefix with 'https://')", homeserver);
    }

  /* The sync callback runs for every /sync response and other interesting events (like wrong password error) */
  cm_client_set_sync_callback (client,
                               simple_account_sync_cb,
                               NULL, NULL);
  cm_matrix_save_client_async (matrix, client, NULL,
                               simple_matrix_save_cb, NULL);

}

static void
simple_joined_rooms_changed_cb (GListModel *list,
                                guint       position,
                                guint       removed,
                                guint       added,
                                gpointer    user_data)
{
  g_print ("\n\n\n");

  g_message ("joined rooms changed");
  g_message ("total number of items: %u", g_list_model_get_n_items (list));

  for (guint i = 0; i < g_list_model_get_n_items (list); i++)
    {
      g_autoptr(CmRoom) room = NULL;

      room = g_list_model_get_item (list, i);

      g_message ("room name: %s, room id: %s",
                 cm_room_get_name (room),
                 cm_room_get_id (room));
    }

  g_print ("\n\n\n\n");
}

static void
simple_matrix_open_cb (GObject      *object,
                       GAsyncResult *result,
                       gpointer      user_data)
{
  g_autoptr(GError) error = NULL;
  char username[255], password[255];
  GListModel *joined_rooms;
  CmAccount *account;

  if (!cm_matrix_open_finish (matrix, result, &error))
    g_error ("Error opening db: %s", error->message);

  g_print ("input your Matrix username: ");
  scanf ("%s", username);
  g_print ("input your Matrix password: ");
  scanf ("%s", password);
  g_print ("\n");

  g_message ("username: %s, password: %s", username, password);

  client = cm_matrix_client_new (matrix);
  account = cm_client_get_account (client);
  joined_rooms = cm_client_get_joined_rooms (client);
  g_signal_connect_object (joined_rooms, "items-changed",
                           G_CALLBACK (simple_joined_rooms_changed_cb), client,
                           0);

  if (!cm_account_set_login_id (account, username))
    g_error ("'%s' isn't a valid username", username);
  cm_client_set_password (client, password);
  cm_client_set_device_name (client, "Example CMatrix");

  /* try if we can get a valid homeserver from username */
  cm_client_get_homeserver_async (client, NULL,
                                  simple_get_homeserver_cb, NULL);
}

int
main (void)
{
  GMainLoop *main_loop;
  g_autofree char *db_dir = NULL;
  g_autofree char *data_dir = NULL;
  g_autofree char *cache_dir = NULL;

  /* Initialize the library */
  cm_init (TRUE);

  /* Create a matrix object */
  data_dir = g_build_filename (g_get_user_data_dir (), "CMatrix", "simple-client", NULL);
  cache_dir = g_build_filename (g_get_user_cache_dir (), "CMatrix", "simple-client", NULL);
  matrix = cm_matrix_new (data_dir, cache_dir, "com.example.CMatrix", FALSE);

  /* Ask matrix to open/create the db which will be used to store keys, session data, etc. */
  db_dir = g_build_filename (g_get_user_data_dir (), "CMatrix", "simple-client", NULL);
  cm_matrix_open_async (matrix, db_dir, "matrix.db", NULL,
                        simple_matrix_open_cb, NULL);

  main_loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (main_loop);
}