Fix crashes when filenames end up being NULL in some prpls.
[pidgin-git.git] / libpurple / connection.h
blob8dc476daa8f010742ad8c1ad30e1f8c22dae7695
1 /**
2 * @file connection.h Connection API
3 * @ingroup core
4 * @see @ref connection-signals
5 */
7 /* purple
9 * Purple is the legal property of its developers, whose names are too numerous
10 * to list here. Please refer to the COPYRIGHT file distributed with this
11 * source distribution.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
27 #ifndef _PURPLE_CONNECTION_H_
28 #define _PURPLE_CONNECTION_H_
30 /** @copydoc _PurpleConnection */
31 typedef struct _PurpleConnection PurpleConnection;
33 /**
34 * Flags to change behavior of the client for a given connection.
36 typedef enum
38 PURPLE_CONNECTION_HTML = 0x0001, /**< Connection sends/receives in 'HTML'. */
39 PURPLE_CONNECTION_NO_BGCOLOR = 0x0002, /**< Connection does not send/receive
40 background colors. */
41 PURPLE_CONNECTION_AUTO_RESP = 0x0004, /**< Send auto responses when away. */
42 PURPLE_CONNECTION_FORMATTING_WBFO = 0x0008, /**< The text buffer must be formatted as a whole */
43 PURPLE_CONNECTION_NO_NEWLINES = 0x0010, /**< No new lines are allowed in outgoing messages */
44 PURPLE_CONNECTION_NO_FONTSIZE = 0x0020, /**< Connection does not send/receive font sizes */
45 PURPLE_CONNECTION_NO_URLDESC = 0x0040, /**< Connection does not support descriptions with links */
46 PURPLE_CONNECTION_NO_IMAGES = 0x0080, /**< Connection does not support sending of images */
47 PURPLE_CONNECTION_ALLOW_CUSTOM_SMILEY = 0x0100 /**< Connection supports sending and receiving custom smileys */
49 } PurpleConnectionFlags;
51 typedef enum
53 PURPLE_DISCONNECTED = 0, /**< Disconnected. */
54 PURPLE_CONNECTED, /**< Connected. */
55 PURPLE_CONNECTING /**< Connecting. */
57 } PurpleConnectionState;
59 /** Possible errors that can cause a connection to be closed.
60 * @since 2.3.0
62 typedef enum
64 /** There was an error sending or receiving on the network socket, or
65 * there was some protocol error (such as the server sending malformed
66 * data).
68 PURPLE_CONNECTION_ERROR_NETWORK_ERROR = 0,
69 /** The username supplied was not valid. */
70 PURPLE_CONNECTION_ERROR_INVALID_USERNAME = 1,
71 /** The username, password or some other credential was incorrect. Use
72 * #PURPLE_CONNECTION_ERROR_INVALID_USERNAME instead if the username
73 * is known to be invalid.
75 PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED = 2,
76 /** libpurple doesn't speak any of the authentication methods the
77 * server offered.
79 PURPLE_CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE = 3,
80 /** libpurple was built without SSL support, and the connection needs
81 * SSL.
83 PURPLE_CONNECTION_ERROR_NO_SSL_SUPPORT = 4,
84 /** There was an error negotiating SSL on this connection, or the
85 * server does not support encryption but an account option was set to
86 * require it.
88 PURPLE_CONNECTION_ERROR_ENCRYPTION_ERROR = 5,
89 /** Someone is already connected to the server using the name you are
90 * trying to connect with.
92 PURPLE_CONNECTION_ERROR_NAME_IN_USE = 6,
94 /** The username/server/other preference for the account isn't valid.
95 * For instance, on IRC the screen name cannot contain white space.
96 * This reason should not be used for incorrect passwords etc: use
97 * #PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED for that.
99 * @todo This reason really shouldn't be necessary. Usernames and
100 * other account preferences should be validated when the
101 * account is created.
103 PURPLE_CONNECTION_ERROR_INVALID_SETTINGS = 7,
105 /** The server did not provide a SSL certificate. */
106 PURPLE_CONNECTION_ERROR_CERT_NOT_PROVIDED = 8,
107 /** The server's SSL certificate could not be trusted. */
108 PURPLE_CONNECTION_ERROR_CERT_UNTRUSTED = 9,
109 /** The server's SSL certificate has expired. */
110 PURPLE_CONNECTION_ERROR_CERT_EXPIRED = 10,
111 /** The server's SSL certificate is not yet valid. */
112 PURPLE_CONNECTION_ERROR_CERT_NOT_ACTIVATED = 11,
113 /** The server's SSL certificate did not match its hostname. */
114 PURPLE_CONNECTION_ERROR_CERT_HOSTNAME_MISMATCH = 12,
115 /** The server's SSL certificate does not have the expected
116 * fingerprint.
118 PURPLE_CONNECTION_ERROR_CERT_FINGERPRINT_MISMATCH = 13,
119 /** The server's SSL certificate is self-signed. */
120 PURPLE_CONNECTION_ERROR_CERT_SELF_SIGNED = 14,
121 /** There was some other error validating the server's SSL certificate.
123 PURPLE_CONNECTION_ERROR_CERT_OTHER_ERROR = 15,
125 /** Some other error occurred which fits into none of the other
126 * categories.
128 /* purple_connection_error_reason() in connection.c uses the fact that
129 * this is the last member of the enum when sanity-checking; if other
130 * reasons are added after it, the check must be updated.
132 PURPLE_CONNECTION_ERROR_OTHER_ERROR = 16
133 } PurpleConnectionError;
135 /** Holds the type of an error along with its description. */
136 typedef struct
138 /** The type of error. */
139 PurpleConnectionError type;
140 /** A localised, human-readable description of the error. */
141 char *description;
142 } PurpleConnectionErrorInfo;
144 #include <time.h>
146 #include "account.h"
147 #include "plugin.h"
148 #include "status.h"
149 #include "sslconn.h"
151 /** Connection UI operations. Used to notify the user of changes to
152 * connections, such as being disconnected, and to respond to the
153 * underlying network connection appearing and disappearing. UIs should
154 * call #purple_connections_set_ui_ops() with an instance of this struct.
156 * @see @ref ui-ops
158 typedef struct
160 /** When an account is connecting, this operation is called to notify
161 * the UI of what is happening, as well as which @a step out of @a
162 * step_count has been reached (which might be displayed as a progress
163 * bar).
164 * @see #purple_connection_update_progress
166 void (*connect_progress)(PurpleConnection *gc,
167 const char *text,
168 size_t step,
169 size_t step_count);
171 /** Called when a connection is established (just before the
172 * @ref signed-on signal).
174 void (*connected)(PurpleConnection *gc);
175 /** Called when a connection is ended (between the @ref signing-off
176 * and @ref signed-off signals).
178 void (*disconnected)(PurpleConnection *gc);
180 /** Used to display connection-specific notices. (Pidgin's Gtk user
181 * interface implements this as a no-op; #purple_connection_notice(),
182 * which uses this operation, is not used by any of the protocols
183 * shipped with libpurple.)
185 void (*notice)(PurpleConnection *gc, const char *text);
187 /** Called when an error causes a connection to be disconnected.
188 * Called before #disconnected.
189 * @param text a localized error message.
190 * @see #purple_connection_error
191 * @deprecated in favour of
192 * #PurpleConnectionUiOps.report_disconnect_reason.
194 void (*report_disconnect)(PurpleConnection *gc, const char *text);
196 /** Called when libpurple discovers that the computer's network
197 * connection is active. On Linux, this uses Network Manager if
198 * available; on Windows, it uses Win32's network change notification
199 * infrastructure.
201 void (*network_connected)(void);
202 /** Called when libpurple discovers that the computer's network
203 * connection has gone away.
205 void (*network_disconnected)(void);
207 /** Called when an error causes a connection to be disconnected.
208 * Called before #disconnected. This op is intended to replace
209 * #report_disconnect. If both are implemented, this will be called
210 * first; however, there's no real reason to implement both.
211 * @param reason why the connection ended, if known, or
212 * #PURPLE_CONNECTION_ERROR_OTHER_ERROR, if not.
213 * @param text a localized message describing the disconnection
214 * in more detail to the user.
215 * @see #purple_connection_error_reason
216 * @since 2.3.0
218 void (*report_disconnect_reason)(PurpleConnection *gc,
219 PurpleConnectionError reason,
220 const char *text);
222 void (*_purple_reserved1)(void);
223 void (*_purple_reserved2)(void);
224 void (*_purple_reserved3)(void);
225 } PurpleConnectionUiOps;
228 /* Represents an active connection on an account. */
229 struct _PurpleConnection
231 PurplePlugin *prpl; /**< The protocol plugin. */
232 PurpleConnectionFlags flags; /**< Connection flags. */
234 PurpleConnectionState state; /**< The connection state. */
236 PurpleAccount *account; /**< The account being connected to. */
237 char *password; /**< The password used. */
238 int inpa; /**< The input watcher. */
240 GSList *buddy_chats; /**< A list of active chats
241 (#PurpleConversation structs of type
242 #PURPLE_CONV_TYPE_CHAT). */
243 void *proto_data; /**< Protocol-specific data. */
245 char *display_name; /**< How you appear to other people. */
246 guint keepalive; /**< Keep-alive. */
248 /** Wants to Die state. This is set when the user chooses to log out, or
249 * when the protocol is disconnected and should not be automatically
250 * reconnected (incorrect password, etc.). prpls should rely on
251 * purple_connection_error_reason() to set this for them rather than
252 * setting it themselves.
253 * @see purple_connection_error_is_fatal
255 gboolean wants_to_die;
257 guint disconnect_timeout; /**< Timer used for nasty stack tricks */
258 time_t last_received; /**< When we last received a packet. Set by the
259 prpl to avoid sending unneeded keepalives */
262 #ifdef __cplusplus
263 extern "C" {
264 #endif
266 /**************************************************************************/
267 /** @name Connection API */
268 /**************************************************************************/
269 /*@{*/
271 #if !(defined PURPLE_DISABLE_DEPRECATED) || (defined _PURPLE_CONNECTION_C_)
273 * This function should only be called by purple_account_connect()
274 * in account.c. If you're trying to sign on an account, use that
275 * function instead.
277 * Creates a connection to the specified account and either connects
278 * or attempts to register a new account. If you are logging in,
279 * the connection uses the current active status for this account.
280 * So if you want to sign on as "away," for example, you need to
281 * have called purple_account_set_status(account, "away").
282 * (And this will call purple_account_connect() automatically).
284 * @param account The account the connection should be connecting to.
285 * @param regist Whether we are registering a new account or just
286 * trying to do a normal signon.
287 * @param password The password to use.
289 * @deprecated As this is internal, we should make it private in 3.0.0.
291 void purple_connection_new(PurpleAccount *account, gboolean regist,
292 const char *password);
293 #endif
295 #if !(defined PURPLE_DISABLE_DEPRECATED) || (defined _PURPLE_CONNECTION_C_)
297 * This function should only be called by purple_account_unregister()
298 * in account.c.
300 * Tries to unregister the account on the server. If the account is not
301 * connected, also creates a new connection.
303 * @param account The account to unregister
304 * @param password The password to use.
305 * @param cb Optional callback to be called when unregistration is complete
306 * @param user_data user data to pass to the callback
308 * @deprecated As this is internal, we should make it private in 3.0.0.
310 void purple_connection_new_unregister(PurpleAccount *account, const char *password, PurpleAccountUnregistrationCb cb, void *user_data);
311 #endif
313 #if !(defined PURPLE_DISABLE_DEPRECATED) || (defined _PURPLE_CONNECTION_C_)
315 * Disconnects and destroys a PurpleConnection.
317 * This function should only be called by purple_account_disconnect()
318 * in account.c. If you're trying to sign off an account, use that
319 * function instead.
321 * @param gc The purple connection to destroy.
323 * @deprecated As this is internal, we should make it private in 3.0.0.
325 void purple_connection_destroy(PurpleConnection *gc);
326 #endif
329 * Sets the connection state. PRPLs should call this and pass in
330 * the state #PURPLE_CONNECTED when the account is completely
331 * signed on. What does it mean to be completely signed on? If
332 * the core can call prpl->set_status, and it successfully changes
333 * your status, then the account is online.
335 * @param gc The connection.
336 * @param state The connection state.
338 void purple_connection_set_state(PurpleConnection *gc, PurpleConnectionState state);
341 * Sets the connection's account.
343 * @param gc The connection.
344 * @param account The account.
346 void purple_connection_set_account(PurpleConnection *gc, PurpleAccount *account);
349 * Sets the connection's displayed name.
351 * @param gc The connection.
352 * @param name The displayed name.
354 void purple_connection_set_display_name(PurpleConnection *gc, const char *name);
357 * Returns the connection state.
359 * @param gc The connection.
361 * @return The connection state.
363 PurpleConnectionState purple_connection_get_state(const PurpleConnection *gc);
366 * Returns TRUE if the account is connected, otherwise returns FALSE.
368 * @return TRUE if the account is connected, otherwise returns FALSE.
370 #define PURPLE_CONNECTION_IS_CONNECTED(gc) \
371 (purple_connection_get_state(gc) == PURPLE_CONNECTED)
374 * Returns the connection's account.
376 * @param gc The connection.
378 * @return The connection's account.
380 PurpleAccount *purple_connection_get_account(const PurpleConnection *gc);
383 * Returns the protocol plugin managing a connection.
385 * @param gc The connection.
387 * @return The protocol plugin.
388 * @since 2.4.0
390 PurplePlugin * purple_connection_get_prpl(const PurpleConnection *gc);
393 * Returns the connection's password.
395 * @param gc The connection.
397 * @return The connection's password.
399 const char *purple_connection_get_password(const PurpleConnection *gc);
402 * Returns the connection's displayed name.
404 * @param gc The connection.
406 * @return The connection's displayed name.
408 const char *purple_connection_get_display_name(const PurpleConnection *gc);
411 * Updates the connection progress.
413 * @param gc The connection.
414 * @param text Information on the current step.
415 * @param step The current step.
416 * @param count The total number of steps.
418 void purple_connection_update_progress(PurpleConnection *gc, const char *text,
419 size_t step, size_t count);
422 * Displays a connection-specific notice.
424 * @param gc The connection.
425 * @param text The notice text.
427 void purple_connection_notice(PurpleConnection *gc, const char *text);
430 * Closes a connection with an error.
432 * @param gc The connection.
433 * @param reason The error text, which may not be @c NULL.
434 * @deprecated in favour of #purple_connection_error_reason. Calling
435 * @c purple_connection_error(gc, text) is equivalent to calling
436 * @c purple_connection_error_reason(gc, reason, text) where @c reason is
437 * #PURPLE_CONNECTION_ERROR_OTHER_ERROR if @c gc->wants_to_die is @c TRUE, and
438 * #PURPLE_CONNECTION_ERROR_NETWORK_ERROR if not. (This is to keep
439 * auto-reconnection behaviour the same when using old prpls which don't use
440 * reasons yet.)
442 void purple_connection_error(PurpleConnection *gc, const char *reason);
445 * Closes a connection with an error and a human-readable description of the
446 * error. It also sets @c gc->wants_to_die to the value of
447 * #purple_connection_error_is_fatal(@a reason), mainly for
448 * backwards-compatibility.
450 * @param gc the connection which is closing.
451 * @param reason why the connection is closing.
452 * @param description a non-@c NULL localized description of the error.
453 * @since 2.3.0
455 void
456 purple_connection_error_reason (PurpleConnection *gc,
457 PurpleConnectionError reason,
458 const char *description);
461 * Closes a connection due to an SSL error; this is basically a shortcut to
462 * turning the #PurpleSslErrorType into a #PurpleConnectionError and a
463 * human-readable string and then calling purple_connection_error_reason().
464 * @since 2.3.0
466 void
467 purple_connection_ssl_error (PurpleConnection *gc,
468 PurpleSslErrorType ssl_error);
471 * Reports whether a disconnection reason is fatal (in which case the account
472 * should probably not be automatically reconnected) or transient (so
473 * auto-reconnection is a good idea).
474 * For instance, #PURPLE_CONNECTION_ERROR_NETWORK_ERROR is a temporary error,
475 * which might be caused by losing the network connection, so <tt>
476 * purple_connection_error_is_fatal (PURPLE_CONNECTION_ERROR_NETWORK_ERROR)</tt>
477 * is @c FALSE. On the other hand,
478 * #PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED probably indicates a
479 * misconfiguration of the account which needs the user to go fix it up, so
480 * <tt> purple_connection_error_is_fatal
481 * (PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED)</tt> is @c TRUE.
483 * (This function is meant to replace checking PurpleConnection.wants_to_die.)
485 * @return @c TRUE if the account should not be automatically reconnected, and
486 * @c FALSE otherwise.
487 * @since 2.3.0
489 gboolean
490 purple_connection_error_is_fatal (PurpleConnectionError reason);
492 /*@}*/
494 /**************************************************************************/
495 /** @name Connections API */
496 /**************************************************************************/
497 /*@{*/
500 * Disconnects from all connections.
502 void purple_connections_disconnect_all(void);
505 * Returns a list of all active connections. This does not
506 * include connections that are in the process of connecting.
508 * @constreturn A list of all active connections.
510 GList *purple_connections_get_all(void);
513 * Returns a list of all connections in the process of connecting.
515 * @constreturn A list of connecting connections.
517 GList *purple_connections_get_connecting(void);
520 * Checks if gc is still a valid pointer to a gc.
522 * @return @c TRUE if gc is valid.
525 * TODO: Eventually this bad boy will be removed, because it is
526 * a gross fix for a crashy problem.
528 #define PURPLE_CONNECTION_IS_VALID(gc) (g_list_find(purple_connections_get_all(), (gc)) != NULL)
530 /*@}*/
532 /**************************************************************************/
533 /** @name UI Registration Functions */
534 /**************************************************************************/
535 /*@{*/
538 * Sets the UI operations structure to be used for connections.
540 * @param ops The UI operations structure.
542 void purple_connections_set_ui_ops(PurpleConnectionUiOps *ops);
545 * Returns the UI operations structure used for connections.
547 * @return The UI operations structure in use.
549 PurpleConnectionUiOps *purple_connections_get_ui_ops(void);
551 /*@}*/
553 /**************************************************************************/
554 /** @name Connections Subsystem */
555 /**************************************************************************/
556 /*@{*/
559 * Initializes the connections subsystem.
561 void purple_connections_init(void);
564 * Uninitializes the connections subsystem.
566 void purple_connections_uninit(void);
569 * Returns the handle to the connections subsystem.
571 * @return The connections subsystem handle.
573 void *purple_connections_get_handle(void);
575 /*@}*/
578 #ifdef __cplusplus
580 #endif
582 #endif /* _PURPLE_CONNECTION_H_ */