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
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
33 #include "win32/win32dep.h"
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
;
49 } PurpleGLibIOClosure
;
51 static void purple_glib_io_destroy(gpointer 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
),
72 static guint
glib_input_add(gint fd
, PurpleInputCondition condition
, PurpleInputFunction function
,
75 PurpleGLibIOClosure
*closure
= g_new0(PurpleGLibIOClosure
, 1);
77 GIOCondition cond
= 0;
79 closure
->function
= function
;
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
);
90 channel
= g_io_channel_unix_new(fd
);
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
=
106 #if GLIB_CHECK_VERSION(2,14,0)
107 g_timeout_add_seconds
,
117 /*** End of the eventloop functions. ***/
119 /*** Conversation uiops ***/
121 null_write_conv(PurpleConversation
*conv
, const char *who
, const char *alias
,
122 const char *message
, PurpleMessageFlags flags
, time_t mtime
)
127 else if (who
&& *who
)
132 printf("(%s) %s %s: %s\n", purple_conversation_get_name(conv
),
133 purple_utf8_strftime("(%H:%M:%S)", localtime(&mtime
)),
137 static PurpleConversationUiOps null_conv_uiops
=
139 NULL
, /* create_conversation */
140 NULL
, /* destroy_conversation */
141 NULL
, /* write_chat */
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 */
149 NULL
, /* has_focus */
150 NULL
, /* custom_smiley_add */
151 NULL
, /* custom_smiley_write */
152 NULL
, /* custom_smiley_close */
153 NULL
, /* send_confirm */
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
=
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. */
216 "libpurple initialization failed. Dumping core.\n"
217 "Please report this!\n");
221 /* Create and load the buddylist. */
222 purple_set_blist(purple_blist_new());
225 /* Load the preferences. */
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();
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
);
244 connect_to_signals_for_demonstration_purposes_only(void)
247 purple_signal_connect(purple_connections_get_handle(), "signed-on", &handle
,
248 PURPLE_CALLBACK(signed_on
), NULL
);
251 int main(int argc
, char *argv
[])
259 GMainLoop
*loop
= g_main_loop_new(NULL
, FALSE
);
260 PurpleAccount
*account
;
261 PurpleSavedStatus
*status
;
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
);
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
);
289 fprintf(stderr
, "Failed to gets protocol selection.");
292 sscanf(name
, "%d", &num
);
293 prpl
= g_list_nth_data(names
, num
);
295 printf("Username: ");
296 res
= fgets(name
, sizeof(name
), stdin
);
298 fprintf(stderr
, "Failed to read user name.");
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
);