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
27 #include <glib/gprintf.h>
33 # include "win32/win32dep.h"
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
;
51 } PurpleGLibIOClosure
;
53 static void purple_glib_io_destroy(gpointer 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
),
74 static guint
glib_input_add(gint fd
, PurpleInputCondition condition
,
75 PurpleInputFunction function
, gpointer data
)
77 PurpleGLibIOClosure
*closure
= g_new0(PurpleGLibIOClosure
, 1);
79 GIOCondition cond
= 0;
81 closure
->function
= function
;
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
);
92 channel
= g_io_channel_unix_new(fd
);
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
=
108 g_timeout_add_seconds
,
116 /*** End of the eventloop functions. ***/
118 /*** Conversation uiops ***/
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 */
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 */
143 NULL
, /* has_focus */
144 NULL
, /* send_confirm */
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
=
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. */
203 "libpurple initialization failed. Dumping core.\n"
204 "Please report this!\n");
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. */
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
);
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
));
230 connect_to_signals_for_demonstration_purposes_only(void)
233 purple_signal_connect(purple_connections_get_handle(), "signed-on", &handle
,
234 PURPLE_CALLBACK(signed_on
), NULL
);
237 #if defined(_WIN32) || defined(__BIONIC__)
239 # define PASS_MAX 1024
242 getpass(const gchar
*prompt
)
244 static gchar buff
[PASS_MAX
+ 1];
247 g_fprintf(stderr
, "%s", prompt
);
250 while (i
< sizeof(buff
) - 1) {
252 buff
[i
] = getc(stdin
);
256 if (buff
[i
] == '\r' || buff
[i
] == '\n')
261 g_fprintf(stderr
, "\n");
265 #endif /* _WIN32 || __BIONIC__ */
267 int main(int argc
, char *argv
[])
272 const char *protocol
= NULL
;
275 GMainLoop
*loop
= g_main_loop_new(NULL
, FALSE
);
276 PurpleAccount
*account
;
277 PurpleSavedStatus
*status
;
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
);
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
));
303 printf("Select the protocol [0-%d]: ", i
-1);
304 res
= fgets(name
, sizeof(name
), stdin
);
306 fprintf(stderr
, "Failed to gets protocol selection.");
309 if (sscanf(name
, "%d", &num
) == 1)
310 protocol
= g_list_nth_data(names
, num
);
312 fprintf(stderr
, "Failed to gets protocol.");
316 printf("Username: ");
317 res
= fgets(name
, sizeof(name
), stdin
);
319 fprintf(stderr
, "Failed to read user name.");
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
);