Increment version number
[pidgin-git.git] / libpurple / example / nullclient.c
blob31fc0a230f2fa120179534e275af8981b9495b93
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>
28 #include <signal.h>
29 #include <string.h>
30 #ifndef _WIN32
31 #include <unistd.h>
32 #else
33 #include "win32/win32dep.h"
34 #endif
36 #include "defines.h"
38 /**
39 * The following eventloop functions are used in both pidgin and purple-text. If your
40 * application uses glib mainloop, you can safely use this verbatim.
42 #define PURPLE_GLIB_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
43 #define PURPLE_GLIB_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
45 typedef struct _PurpleGLibIOClosure {
46 PurpleInputFunction function;
47 guint result;
48 gpointer data;
49 } PurpleGLibIOClosure;
51 static void purple_glib_io_destroy(gpointer data)
53 g_free(data);
56 static gboolean purple_glib_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
58 PurpleGLibIOClosure *closure = data;
59 PurpleInputCondition purple_cond = 0;
61 if (condition & PURPLE_GLIB_READ_COND)
62 purple_cond |= PURPLE_INPUT_READ;
63 if (condition & PURPLE_GLIB_WRITE_COND)
64 purple_cond |= PURPLE_INPUT_WRITE;
66 closure->function(closure->data, g_io_channel_unix_get_fd(source),
67 purple_cond);
69 return TRUE;
72 static guint glib_input_add(gint fd, PurpleInputCondition condition, PurpleInputFunction function,
73 gpointer data)
75 PurpleGLibIOClosure *closure = g_new0(PurpleGLibIOClosure, 1);
76 GIOChannel *channel;
77 GIOCondition cond = 0;
79 closure->function = function;
80 closure->data = data;
82 if (condition & PURPLE_INPUT_READ)
83 cond |= PURPLE_GLIB_READ_COND;
84 if (condition & PURPLE_INPUT_WRITE)
85 cond |= PURPLE_GLIB_WRITE_COND;
87 #if defined _WIN32 && !defined WINPIDGIN_USE_GLIB_IO_CHANNEL
88 channel = wpurple_g_io_channel_win32_new_socket(fd);
89 #else
90 channel = g_io_channel_unix_new(fd);
91 #endif
92 closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond,
93 purple_glib_io_invoke, closure, purple_glib_io_destroy);
95 g_io_channel_unref(channel);
96 return closure->result;
99 static PurpleEventLoopUiOps glib_eventloops =
101 g_timeout_add,
102 g_source_remove,
103 glib_input_add,
104 g_source_remove,
105 NULL,
106 #if GLIB_CHECK_VERSION(2,14,0)
107 g_timeout_add_seconds,
108 #else
109 NULL,
110 #endif
112 /* padding */
113 NULL,
114 NULL,
115 NULL
117 /*** End of the eventloop functions. ***/
119 /*** Conversation uiops ***/
120 static void
121 null_write_conv(PurpleConversation *conv, const char *who, const char *alias,
122 const char *message, PurpleMessageFlags flags, time_t mtime)
124 const char *name;
125 if (alias && *alias)
126 name = alias;
127 else if (who && *who)
128 name = who;
129 else
130 name = NULL;
132 printf("(%s) %s %s: %s\n", purple_conversation_get_name(conv),
133 purple_utf8_strftime("(%H:%M:%S)", localtime(&mtime)),
134 name, message);
137 static PurpleConversationUiOps null_conv_uiops =
139 NULL, /* create_conversation */
140 NULL, /* destroy_conversation */
141 NULL, /* write_chat */
142 NULL, /* write_im */
143 null_write_conv, /* write_conv */
144 NULL, /* chat_add_users */
145 NULL, /* chat_rename_user */
146 NULL, /* chat_remove_users */
147 NULL, /* chat_update_user */
148 NULL, /* present */
149 NULL, /* has_focus */
150 NULL, /* custom_smiley_add */
151 NULL, /* custom_smiley_write */
152 NULL, /* custom_smiley_close */
153 NULL, /* send_confirm */
154 NULL,
155 NULL,
156 NULL,
157 NULL
160 static void
161 null_ui_init(void)
164 * This should initialize the UI components for all the modules. Here we
165 * just initialize the UI for conversations.
167 purple_conversations_set_ui_ops(&null_conv_uiops);
170 static PurpleCoreUiOps null_core_uiops =
172 NULL,
173 NULL,
174 null_ui_init,
175 NULL,
177 /* padding */
178 NULL,
179 NULL,
180 NULL,
181 NULL
184 static void
185 init_libpurple(void)
187 /* Set a custom user directory (optional) */
188 purple_util_set_user_dir(CUSTOM_USER_DIRECTORY);
190 /* We do not want any debugging for now to keep the noise to a minimum. */
191 purple_debug_set_enabled(FALSE);
193 /* Set the core-uiops, which is used to
194 * - initialize the ui specific preferences.
195 * - initialize the debug ui.
196 * - initialize the ui components for all the modules.
197 * - uninitialize the ui components for all the modules when the core terminates.
199 purple_core_set_ui_ops(&null_core_uiops);
201 /* Set the uiops for the eventloop. If your client is glib-based, you can safely
202 * copy this verbatim. */
203 purple_eventloop_set_ui_ops(&glib_eventloops);
205 /* Set path to search for plugins. The core (libpurple) takes care of loading the
206 * core-plugins, which includes the protocol-plugins. So it is not essential to add
207 * any path here, but it might be desired, especially for ui-specific plugins. */
208 purple_plugins_add_search_path(CUSTOM_PLUGIN_PATH);
210 /* Now that all the essential stuff has been set, let's try to init the core. It's
211 * necessary to provide a non-NULL name for the current ui to the core. This name
212 * is used by stuff that depends on this ui, for example the ui-specific plugins. */
213 if (!purple_core_init(UI_ID)) {
214 /* Initializing the core failed. Terminate. */
215 fprintf(stderr,
216 "libpurple initialization failed. Dumping core.\n"
217 "Please report this!\n");
218 abort();
221 /* Create and load the buddylist. */
222 purple_set_blist(purple_blist_new());
223 purple_blist_load();
225 /* Load the preferences. */
226 purple_prefs_load();
228 /* Load the desired plugins. The client should save the list of loaded plugins in
229 * the preferences using purple_plugins_save_loaded(PLUGIN_SAVE_PREF) */
230 purple_plugins_load_saved(PLUGIN_SAVE_PREF);
232 /* Load the pounces. */
233 purple_pounces_load();
236 static void
237 signed_on(PurpleConnection *gc, gpointer null)
239 PurpleAccount *account = purple_connection_get_account(gc);
240 printf("Account connected: %s %s\n", account->username, account->protocol_id);
243 static void
244 connect_to_signals_for_demonstration_purposes_only(void)
246 static int handle;
247 purple_signal_connect(purple_connections_get_handle(), "signed-on", &handle,
248 PURPLE_CALLBACK(signed_on), NULL);
251 int main(int argc, char *argv[])
253 GList *iter;
254 int i, num;
255 GList *names = NULL;
256 const char *prpl;
257 char name[128];
258 char *password;
259 GMainLoop *loop = g_main_loop_new(NULL, FALSE);
260 PurpleAccount *account;
261 PurpleSavedStatus *status;
262 char *res;
264 #ifndef _WIN32
265 /* libpurple's built-in DNS resolution forks processes to perform
266 * blocking lookups without blocking the main process. It does not
267 * handle SIGCHLD itself, so if the UI does not you quickly get an army
268 * of zombie subprocesses marching around.
270 signal(SIGCHLD, SIG_IGN);
271 #endif
273 init_libpurple();
275 printf("libpurple initialized.\n");
277 iter = purple_plugins_get_protocols();
278 for (i = 0; iter; iter = iter->next) {
279 PurplePlugin *plugin = iter->data;
280 PurplePluginInfo *info = plugin->info;
281 if (info && info->name) {
282 printf("\t%d: %s\n", i++, info->name);
283 names = g_list_append(names, info->id);
286 printf("Select the protocol [0-%d]: ", i-1);
287 res = fgets(name, sizeof(name), stdin);
288 if (!res) {
289 fprintf(stderr, "Failed to gets protocol selection.");
290 abort();
292 sscanf(name, "%d", &num);
293 prpl = g_list_nth_data(names, num);
295 printf("Username: ");
296 res = fgets(name, sizeof(name), stdin);
297 if (!res) {
298 fprintf(stderr, "Failed to read user name.");
299 abort();
301 name[strlen(name) - 1] = 0; /* strip the \n at the end */
303 /* Create the account */
304 account = purple_account_new(name, prpl);
306 /* Get the password for the account */
307 password = getpass("Password: ");
308 purple_account_set_password(account, password);
310 /* It's necessary to enable the account first. */
311 purple_account_set_enabled(account, UI_ID, TRUE);
313 /* Now, to connect the account(s), create a status and activate it. */
314 status = purple_savedstatus_new(NULL, PURPLE_STATUS_AVAILABLE);
315 purple_savedstatus_activate(status);
317 connect_to_signals_for_demonstration_purposes_only();
319 g_main_loop_run(loop);
321 return 0;