Standardize all protocol header guard macros.
[pidgin-git.git] / libpurple / example / nullclient.c
blobf2d4f5e980cd66653e0b214e2859876010232eaf
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 NULL, /* create_conversation */
55 NULL, /* destroy_conversation */
56 NULL, /* write_chat */
57 NULL, /* write_im */
58 null_write_conv, /* write_conv */
59 NULL, /* chat_add_users */
60 NULL, /* chat_rename_user */
61 NULL, /* chat_remove_users */
62 NULL, /* chat_update_user */
63 NULL, /* present */
64 NULL, /* has_focus */
65 NULL, /* send_confirm */
66 NULL,
67 NULL,
68 NULL,
69 NULL
72 static void
73 null_ui_init(void)
75 /**
76 * This should initialize the UI components for all the modules. Here we
77 * just initialize the UI for conversations.
79 purple_conversations_set_ui_ops(&null_conv_uiops);
82 static PurpleCoreUiOps null_core_uiops =
84 NULL,
85 NULL,
86 null_ui_init,
87 NULL,
89 /* padding */
90 NULL,
91 NULL,
92 NULL,
93 NULL,
94 NULL
97 static void
98 init_libpurple(void)
100 /* Set a custom user directory (optional) */
101 purple_util_set_user_dir(CUSTOM_USER_DIRECTORY);
103 /* We do not want any debugging for now to keep the noise to a minimum. */
104 purple_debug_set_enabled(FALSE);
106 /* Set the core-uiops, which is used to
107 * - initialize the ui specific preferences.
108 * - initialize the debug ui.
109 * - initialize the ui components for all the modules.
110 * - uninitialize the ui components for all the modules when the core terminates.
112 purple_core_set_ui_ops(&null_core_uiops);
114 /* Now that all the essential stuff has been set, let's try to init the core. It's
115 * necessary to provide a non-NULL name for the current ui to the core. This name
116 * is used by stuff that depends on this ui, for example the ui-specific plugins. */
117 if (!purple_core_init(UI_ID)) {
118 /* Initializing the core failed. Terminate. */
119 fprintf(stderr,
120 "libpurple initialization failed. Dumping core.\n"
121 "Please report this!\n");
122 abort();
125 /* Set path to search for plugins. The core (libpurple) takes care of loading the
126 * core-plugins, which includes the in-tree protocols. So it is not essential to add
127 * any path here, but it might be desired, especially for ui-specific plugins. */
128 purple_plugins_add_search_path(CUSTOM_PLUGIN_PATH);
129 purple_plugins_refresh();
131 /* Load the preferences. */
132 purple_prefs_load();
134 /* Load the desired plugins. The client should save the list of loaded plugins in
135 * the preferences using purple_plugins_save_loaded(PLUGIN_SAVE_PREF) */
136 purple_plugins_load_saved(PLUGIN_SAVE_PREF);
139 static void
140 signed_on(PurpleConnection *gc, gpointer null)
142 PurpleAccount *account = purple_connection_get_account(gc);
143 printf("Account connected: %s %s\n", purple_account_get_username(account), purple_account_get_protocol_id(account));
146 static void
147 connect_to_signals_for_demonstration_purposes_only(void)
149 static int handle;
150 purple_signal_connect(purple_connections_get_handle(), "signed-on", &handle,
151 PURPLE_CALLBACK(signed_on), NULL);
154 #if defined(_WIN32) || defined(__BIONIC__)
155 #ifndef PASS_MAX
156 # define PASS_MAX 1024
157 #endif
158 static gchar *
159 getpass(const gchar *prompt)
161 static gchar buff[PASS_MAX + 1];
162 guint i = 0;
164 g_fprintf(stderr, "%s", prompt);
165 fflush(stderr);
167 while (i < sizeof(buff) - 1) {
168 #ifdef __BIONIC__
169 buff[i] = getc(stdin);
170 #else
171 buff[i] = _getch();
172 #endif
173 if (buff[i] == '\r' || buff[i] == '\n')
174 break;
175 i++;
177 buff[i] = '\0';
178 g_fprintf(stderr, "\n");
180 return buff;
182 #endif /* _WIN32 || __BIONIC__ */
184 int main(int argc, char *argv[])
186 GList *list, *iter;
187 int i, num;
188 GList *names = NULL;
189 const char *protocol = NULL;
190 char name[128];
191 char *password;
192 GMainLoop *loop = g_main_loop_new(NULL, FALSE);
193 PurpleAccount *account;
194 PurpleSavedStatus *status;
195 char *res;
197 #ifndef _WIN32
198 /* libpurple's built-in DNS resolution forks processes to perform
199 * blocking lookups without blocking the main process. It does not
200 * handle SIGCHLD itself, so if the UI does not you quickly get an army
201 * of zombie subprocesses marching around.
203 signal(SIGCHLD, SIG_IGN);
204 #endif
206 init_libpurple();
208 printf("libpurple initialized.\n");
210 list = purple_protocols_get_all();
211 for (i = 0, iter = list; iter; iter = iter->next) {
212 PurpleProtocol *protocol = iter->data;
213 if (protocol && purple_protocol_get_name(protocol)) {
214 printf("\t%d: %s\n", i++, purple_protocol_get_name(protocol));
215 names = g_list_append(names, (gpointer)purple_protocol_get_id(protocol));
218 g_list_free(list);
220 printf("Select the protocol [0-%d]: ", i-1);
221 res = fgets(name, sizeof(name), stdin);
222 if (!res) {
223 fprintf(stderr, "Failed to gets protocol selection.");
224 abort();
226 if (sscanf(name, "%d", &num) == 1)
227 protocol = g_list_nth_data(names, num);
228 if (!protocol) {
229 fprintf(stderr, "Failed to gets protocol.");
230 abort();
233 printf("Username: ");
234 res = fgets(name, sizeof(name), stdin);
235 if (!res) {
236 fprintf(stderr, "Failed to read user name.");
237 abort();
239 name[strlen(name) - 1] = 0; /* strip the \n at the end */
241 /* Create the account */
242 account = purple_account_new(name, protocol);
244 /* Get the password for the account */
245 password = getpass("Password: ");
246 purple_account_set_password(account, password, NULL, NULL);
248 /* It's necessary to enable the account first. */
249 purple_account_set_enabled(account, UI_ID, TRUE);
251 /* Now, to connect the account(s), create a status and activate it. */
252 status = purple_savedstatus_new(NULL, PURPLE_STATUS_AVAILABLE);
253 purple_savedstatus_activate(status);
255 connect_to_signals_for_demonstration_purposes_only();
257 g_main_loop_run(loop);
259 return 0;