Replace functions which called once with their bodies
[pidgin-git.git] / libpurple / example / nullclient.c
blob75eb6ed1deecdf567eeaf099fd2f5868ade935d7
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 #else
34 # include <unistd.h>
35 #endif
37 #include "defines.h"
39 /*** Conversation uiops ***/
40 static void
41 null_write_conv(PurpleConversation *conv, PurpleMessage *msg)
43 time_t mtime = purple_message_get_time(msg);
45 printf("(%s) %s %s: %s\n",
46 purple_conversation_get_name(conv),
47 purple_utf8_strftime("(%H:%M:%S)", localtime(&mtime)),
48 purple_message_get_author_alias(msg),
49 purple_message_get_contents(msg));
52 static PurpleConversationUiOps null_conv_uiops =
54 .write_conv = null_write_conv,
57 static void
58 null_ui_init(void)
60 /**
61 * This should initialize the UI components for all the modules. Here we
62 * just initialize the UI for conversations.
64 purple_conversations_set_ui_ops(&null_conv_uiops);
67 static PurpleCoreUiOps null_core_uiops =
69 .ui_init = null_ui_init,
72 static void
73 init_libpurple(void)
75 /* Set a custom user directory (optional) */
76 purple_util_set_user_dir(CUSTOM_USER_DIRECTORY);
78 /* We do not want any debugging for now to keep the noise to a minimum. */
79 purple_debug_set_enabled(FALSE);
81 /* Set the core-uiops, which is used to
82 * - initialize the ui specific preferences.
83 * - initialize the debug ui.
84 * - initialize the ui components for all the modules.
85 * - uninitialize the ui components for all the modules when the core terminates.
87 purple_core_set_ui_ops(&null_core_uiops);
89 /* Now that all the essential stuff has been set, let's try to init the core. It's
90 * necessary to provide a non-NULL name for the current ui to the core. This name
91 * is used by stuff that depends on this ui, for example the ui-specific plugins. */
92 if (!purple_core_init(UI_ID)) {
93 /* Initializing the core failed. Terminate. */
94 fprintf(stderr,
95 "libpurple initialization failed. Dumping core.\n"
96 "Please report this!\n");
97 abort();
100 /* Set path to search for plugins. The core (libpurple) takes care of loading the
101 * core-plugins, which includes the in-tree protocols. So it is not essential to add
102 * any path here, but it might be desired, especially for ui-specific plugins. */
103 purple_plugins_add_search_path(CUSTOM_PLUGIN_PATH);
104 purple_plugins_refresh();
106 /* Load the preferences. */
107 purple_prefs_load();
109 /* Load the desired plugins. The client should save the list of loaded plugins in
110 * the preferences using purple_plugins_save_loaded(PLUGIN_SAVE_PREF) */
111 purple_plugins_load_saved(PLUGIN_SAVE_PREF);
114 static void
115 signed_on(PurpleConnection *gc, gpointer null)
117 PurpleAccount *account = purple_connection_get_account(gc);
118 printf("Account connected: %s %s\n", purple_account_get_username(account), purple_account_get_protocol_id(account));
121 static void
122 connect_to_signals_for_demonstration_purposes_only(void)
124 static int handle;
125 purple_signal_connect(purple_connections_get_handle(), "signed-on", &handle,
126 PURPLE_CALLBACK(signed_on), NULL);
129 #if defined(_WIN32) || defined(__BIONIC__)
130 #ifndef PASS_MAX
131 # define PASS_MAX 1024
132 #endif
133 static gchar *
134 getpass(const gchar *prompt)
136 static gchar buff[PASS_MAX + 1];
137 guint i = 0;
139 g_fprintf(stderr, "%s", prompt);
140 fflush(stderr);
142 while (i < sizeof(buff) - 1) {
143 #ifdef __BIONIC__
144 buff[i] = getc(stdin);
145 #else
146 buff[i] = _getch();
147 #endif
148 if (buff[i] == '\r' || buff[i] == '\n')
149 break;
150 i++;
152 buff[i] = '\0';
153 g_fprintf(stderr, "\n");
155 return buff;
157 #endif /* _WIN32 || __BIONIC__ */
159 int main(int argc, char *argv[])
161 GList *list, *iter;
162 int i, num;
163 GList *names = NULL;
164 const char *protocol = NULL;
165 char name[128];
166 char *password;
167 GMainLoop *loop = g_main_loop_new(NULL, FALSE);
168 PurpleAccount *account;
169 PurpleSavedStatus *status;
170 char *res;
172 #ifndef _WIN32
173 /* libpurple's built-in DNS resolution forks processes to perform
174 * blocking lookups without blocking the main process. It does not
175 * handle SIGCHLD itself, so if the UI does not you quickly get an army
176 * of zombie subprocesses marching around.
178 signal(SIGCHLD, SIG_IGN);
179 #endif
181 init_libpurple();
183 printf("libpurple initialized.\n");
185 list = purple_protocols_get_all();
186 for (i = 0, iter = list; iter; iter = iter->next) {
187 PurpleProtocol *protocol = iter->data;
188 if (protocol && purple_protocol_get_name(protocol)) {
189 printf("\t%d: %s\n", i++, purple_protocol_get_name(protocol));
190 names = g_list_append(names, (gpointer)purple_protocol_get_id(protocol));
193 g_list_free(list);
195 num = -1;
196 while (num < 0 || num >= i) {
197 printf("Select the protocol [0-%d]: ", i - 1);
198 res = fgets(name, sizeof(name), stdin);
199 if (!res) {
200 fprintf(stderr, "Failed to get protocol selection.");
201 abort();
203 if (sscanf(name, "%d", &num) != 1) {
204 num = -1;
207 protocol = g_list_nth_data(names, num);
208 if (!protocol) {
209 fprintf(stderr, "Failed to get protocol.");
210 abort();
213 printf("Username: ");
214 res = fgets(name, sizeof(name), stdin);
215 if (!res) {
216 fprintf(stderr, "Failed to read user name.");
217 abort();
219 name[strlen(name) - 1] = 0; /* strip the \n at the end */
221 /* Create the account */
222 account = purple_account_new(name, protocol);
224 /* Get the password for the account */
225 password = getpass("Password: ");
226 purple_account_set_password(account, password, NULL, NULL);
228 /* It's necessary to enable the account first. */
229 purple_account_set_enabled(account, UI_ID, TRUE);
231 /* Now, to connect the account(s), create a status and activate it. */
232 status = purple_savedstatus_new(NULL, PURPLE_STATUS_AVAILABLE);
233 purple_savedstatus_activate(status);
235 connect_to_signals_for_demonstration_purposes_only();
237 g_main_loop_run(loop);
239 return 0;