Migrate certificates, icons, logs to XDG dirs
[pidgin-git.git] / libpurple / example / nullclient.c
blobca0d114cb6228d26b0dd5229c9ede1687ad91ca3
1 /*
2 * pidgin
4 * Pidgin is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
24 #include "purple.h"
26 #include <glib.h>
27 #include <glib/gprintf.h>
29 #include <signal.h>
30 #include <string.h>
31 #ifdef _WIN32
32 # include <conio.h>
33 # include "win32/win32dep.h"
34 #else
35 # include <unistd.h>
36 #endif
38 #include "defines.h"
40 /**
41 * The following eventloop functions are used in both pidgin and purple-text. If your
42 * application uses glib mainloop, you can safely use this verbatim.
44 #define PURPLE_GLIB_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
45 #define PURPLE_GLIB_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
47 typedef struct _PurpleGLibIOClosure {
48 PurpleInputFunction function;
49 guint result;
50 gpointer data;
51 } PurpleGLibIOClosure;
53 static void purple_glib_io_destroy(gpointer data)
55 g_free(data);
58 static gboolean purple_glib_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
60 PurpleGLibIOClosure *closure = data;
61 PurpleInputCondition purple_cond = 0;
63 if (condition & PURPLE_GLIB_READ_COND)
64 purple_cond |= PURPLE_INPUT_READ;
65 if (condition & PURPLE_GLIB_WRITE_COND)
66 purple_cond |= PURPLE_INPUT_WRITE;
68 closure->function(closure->data, g_io_channel_unix_get_fd(source),
69 purple_cond);
71 return TRUE;
74 static guint glib_input_add(gint fd, PurpleInputCondition condition,
75 PurpleInputFunction function, gpointer data)
77 PurpleGLibIOClosure *closure = g_new0(PurpleGLibIOClosure, 1);
78 GIOChannel *channel;
79 GIOCondition cond = 0;
81 closure->function = function;
82 closure->data = data;
84 if (condition & PURPLE_INPUT_READ)
85 cond |= PURPLE_GLIB_READ_COND;
86 if (condition & PURPLE_INPUT_WRITE)
87 cond |= PURPLE_GLIB_WRITE_COND;
89 #if defined _WIN32 && !defined WINPIDGIN_USE_GLIB_IO_CHANNEL
90 channel = wpurple_g_io_channel_win32_new_socket(fd);
91 #else
92 channel = g_io_channel_unix_new(fd);
93 #endif
94 closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT,
95 cond, purple_glib_io_invoke, closure, purple_glib_io_destroy);
97 g_io_channel_unref(channel);
98 return closure->result;
101 static PurpleEventLoopUiOps glib_eventloops =
103 g_timeout_add,
104 g_source_remove,
105 glib_input_add,
106 g_source_remove,
107 NULL,
108 g_timeout_add_seconds,
110 /* padding */
111 NULL,
112 NULL,
113 NULL,
114 NULL
116 /*** End of the eventloop functions. ***/
118 /*** Conversation uiops ***/
119 static void
120 null_write_conv(PurpleConversation *conv, PurpleMessage *msg)
122 time_t mtime = purple_message_get_time(msg);
124 printf("(%s) %s %s: %s\n",
125 purple_conversation_get_name(conv),
126 purple_utf8_strftime("(%H:%M:%S)", localtime(&mtime)),
127 purple_message_get_author_alias(msg),
128 purple_message_get_contents(msg));
131 static PurpleConversationUiOps null_conv_uiops =
133 NULL, /* create_conversation */
134 NULL, /* destroy_conversation */
135 NULL, /* write_chat */
136 NULL, /* write_im */
137 null_write_conv, /* write_conv */
138 NULL, /* chat_add_users */
139 NULL, /* chat_rename_user */
140 NULL, /* chat_remove_users */
141 NULL, /* chat_update_user */
142 NULL, /* present */
143 NULL, /* has_focus */
144 NULL, /* send_confirm */
145 NULL,
146 NULL,
147 NULL,
148 NULL
151 static void
152 null_ui_init(void)
155 * This should initialize the UI components for all the modules. Here we
156 * just initialize the UI for conversations.
158 purple_conversations_set_ui_ops(&null_conv_uiops);
161 static PurpleCoreUiOps null_core_uiops =
163 NULL,
164 NULL,
165 null_ui_init,
166 NULL,
168 /* padding */
169 NULL,
170 NULL,
171 NULL,
172 NULL,
173 NULL
176 static void
177 init_libpurple(void)
179 /* Set a custom user directory (optional) */
180 purple_util_set_user_dir(CUSTOM_USER_DIRECTORY);
182 /* We do not want any debugging for now to keep the noise to a minimum. */
183 purple_debug_set_enabled(FALSE);
185 /* Set the core-uiops, which is used to
186 * - initialize the ui specific preferences.
187 * - initialize the debug ui.
188 * - initialize the ui components for all the modules.
189 * - uninitialize the ui components for all the modules when the core terminates.
191 purple_core_set_ui_ops(&null_core_uiops);
193 /* Set the uiops for the eventloop. If your client is glib-based, you can safely
194 * copy this verbatim. */
195 purple_eventloop_set_ui_ops(&glib_eventloops);
197 /* Now that all the essential stuff has been set, let's try to init the core. It's
198 * necessary to provide a non-NULL name for the current ui to the core. This name
199 * is used by stuff that depends on this ui, for example the ui-specific plugins. */
200 if (!purple_core_init(UI_ID)) {
201 /* Initializing the core failed. Terminate. */
202 fprintf(stderr,
203 "libpurple initialization failed. Dumping core.\n"
204 "Please report this!\n");
205 abort();
208 /* Set path to search for plugins. The core (libpurple) takes care of loading the
209 * core-plugins, which includes the in-tree protocols. So it is not essential to add
210 * any path here, but it might be desired, especially for ui-specific plugins. */
211 purple_plugins_add_search_path(CUSTOM_PLUGIN_PATH);
212 purple_plugins_refresh();
214 /* Load the preferences. */
215 purple_prefs_load();
217 /* Load the desired plugins. The client should save the list of loaded plugins in
218 * the preferences using purple_plugins_save_loaded(PLUGIN_SAVE_PREF) */
219 purple_plugins_load_saved(PLUGIN_SAVE_PREF);
222 static void
223 signed_on(PurpleConnection *gc, gpointer null)
225 PurpleAccount *account = purple_connection_get_account(gc);
226 printf("Account connected: %s %s\n", purple_account_get_username(account), purple_account_get_protocol_id(account));
229 static void
230 connect_to_signals_for_demonstration_purposes_only(void)
232 static int handle;
233 purple_signal_connect(purple_connections_get_handle(), "signed-on", &handle,
234 PURPLE_CALLBACK(signed_on), NULL);
237 #if defined(_WIN32) || defined(__BIONIC__)
238 #ifndef PASS_MAX
239 # define PASS_MAX 1024
240 #endif
241 static gchar *
242 getpass(const gchar *prompt)
244 static gchar buff[PASS_MAX + 1];
245 guint i = 0;
247 g_fprintf(stderr, "%s", prompt);
248 fflush(stderr);
250 while (i < sizeof(buff) - 1) {
251 #ifdef __BIONIC__
252 buff[i] = getc(stdin);
253 #else
254 buff[i] = _getch();
255 #endif
256 if (buff[i] == '\r' || buff[i] == '\n')
257 break;
258 i++;
260 buff[i] = '\0';
261 g_fprintf(stderr, "\n");
263 return buff;
265 #endif /* _WIN32 || __BIONIC__ */
267 int main(int argc, char *argv[])
269 GList *list, *iter;
270 int i, num;
271 GList *names = NULL;
272 const char *protocol = NULL;
273 char name[128];
274 char *password;
275 GMainLoop *loop = g_main_loop_new(NULL, FALSE);
276 PurpleAccount *account;
277 PurpleSavedStatus *status;
278 char *res;
280 #ifndef _WIN32
281 /* libpurple's built-in DNS resolution forks processes to perform
282 * blocking lookups without blocking the main process. It does not
283 * handle SIGCHLD itself, so if the UI does not you quickly get an army
284 * of zombie subprocesses marching around.
286 signal(SIGCHLD, SIG_IGN);
287 #endif
289 init_libpurple();
291 printf("libpurple initialized.\n");
293 list = purple_protocols_get_all();
294 for (i = 0, iter = list; iter; iter = iter->next) {
295 PurpleProtocol *protocol = iter->data;
296 if (protocol && purple_protocol_get_name(protocol)) {
297 printf("\t%d: %s\n", i++, purple_protocol_get_name(protocol));
298 names = g_list_append(names, (gpointer)purple_protocol_get_id(protocol));
301 g_list_free(list);
303 printf("Select the protocol [0-%d]: ", i-1);
304 res = fgets(name, sizeof(name), stdin);
305 if (!res) {
306 fprintf(stderr, "Failed to gets protocol selection.");
307 abort();
309 if (sscanf(name, "%d", &num) == 1)
310 protocol = g_list_nth_data(names, num);
311 if (!protocol) {
312 fprintf(stderr, "Failed to gets protocol.");
313 abort();
316 printf("Username: ");
317 res = fgets(name, sizeof(name), stdin);
318 if (!res) {
319 fprintf(stderr, "Failed to read user name.");
320 abort();
322 name[strlen(name) - 1] = 0; /* strip the \n at the end */
324 /* Create the account */
325 account = purple_account_new(name, protocol);
327 /* Get the password for the account */
328 password = getpass("Password: ");
329 purple_account_set_password(account, password, NULL, NULL);
331 /* It's necessary to enable the account first. */
332 purple_account_set_enabled(account, UI_ID, TRUE);
334 /* Now, to connect the account(s), create a status and activate it. */
335 status = purple_savedstatus_new(NULL, PURPLE_STATUS_AVAILABLE);
336 purple_savedstatus_activate(status);
338 connect_to_signals_for_demonstration_purposes_only();
340 g_main_loop_run(loop);
342 return 0;