Mercurial > pidgin
comparison libpurple/example/nullclient.c @ 15822:32c366eeeb99
sed -ie 's/gaim/purple/g'
| author | Sean Egan <seanegan@gmail.com> |
|---|---|
| date | Mon, 19 Mar 2007 07:01:17 +0000 |
| parents | cd02b303b81f |
| children | 65b45106bfbb |
comparison
equal
deleted
inserted
replaced
| 15821:84b0f9b23ede | 15822:32c366eeeb99 |
|---|---|
| 44 #include <unistd.h> | 44 #include <unistd.h> |
| 45 | 45 |
| 46 #include "defines.h" | 46 #include "defines.h" |
| 47 | 47 |
| 48 /** | 48 /** |
| 49 * The following eventloop functions are used in both pidgin and gaim-text. If your | 49 * The following eventloop functions are used in both pidgin and purple-text. If your |
| 50 * application uses glib mainloop, you can safely use this verbatim. | 50 * application uses glib mainloop, you can safely use this verbatim. |
| 51 */ | 51 */ |
| 52 #define GAIM_GLIB_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR) | 52 #define PURPLE_GLIB_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR) |
| 53 #define GAIM_GLIB_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL) | 53 #define PURPLE_GLIB_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL) |
| 54 | 54 |
| 55 typedef struct _GaimGLibIOClosure { | 55 typedef struct _PurpleGLibIOClosure { |
| 56 GaimInputFunction function; | 56 PurpleInputFunction function; |
| 57 guint result; | 57 guint result; |
| 58 gpointer data; | 58 gpointer data; |
| 59 } GaimGLibIOClosure; | 59 } PurpleGLibIOClosure; |
| 60 | 60 |
| 61 static void gaim_glib_io_destroy(gpointer data) | 61 static void purple_glib_io_destroy(gpointer data) |
| 62 { | 62 { |
| 63 g_free(data); | 63 g_free(data); |
| 64 } | 64 } |
| 65 | 65 |
| 66 static gboolean gaim_glib_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data) | 66 static gboolean purple_glib_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data) |
| 67 { | 67 { |
| 68 GaimGLibIOClosure *closure = data; | 68 PurpleGLibIOClosure *closure = data; |
| 69 GaimInputCondition gaim_cond = 0; | 69 PurpleInputCondition purple_cond = 0; |
| 70 | 70 |
| 71 if (condition & GAIM_GLIB_READ_COND) | 71 if (condition & PURPLE_GLIB_READ_COND) |
| 72 gaim_cond |= GAIM_INPUT_READ; | 72 purple_cond |= PURPLE_INPUT_READ; |
| 73 if (condition & GAIM_GLIB_WRITE_COND) | 73 if (condition & PURPLE_GLIB_WRITE_COND) |
| 74 gaim_cond |= GAIM_INPUT_WRITE; | 74 purple_cond |= PURPLE_INPUT_WRITE; |
| 75 | 75 |
| 76 closure->function(closure->data, g_io_channel_unix_get_fd(source), | 76 closure->function(closure->data, g_io_channel_unix_get_fd(source), |
| 77 gaim_cond); | 77 purple_cond); |
| 78 | 78 |
| 79 return TRUE; | 79 return TRUE; |
| 80 } | 80 } |
| 81 | 81 |
| 82 static guint glib_input_add(gint fd, GaimInputCondition condition, GaimInputFunction function, | 82 static guint glib_input_add(gint fd, PurpleInputCondition condition, PurpleInputFunction function, |
| 83 gpointer data) | 83 gpointer data) |
| 84 { | 84 { |
| 85 GaimGLibIOClosure *closure = g_new0(GaimGLibIOClosure, 1); | 85 PurpleGLibIOClosure *closure = g_new0(PurpleGLibIOClosure, 1); |
| 86 GIOChannel *channel; | 86 GIOChannel *channel; |
| 87 GIOCondition cond = 0; | 87 GIOCondition cond = 0; |
| 88 | 88 |
| 89 closure->function = function; | 89 closure->function = function; |
| 90 closure->data = data; | 90 closure->data = data; |
| 91 | 91 |
| 92 if (condition & GAIM_INPUT_READ) | 92 if (condition & PURPLE_INPUT_READ) |
| 93 cond |= GAIM_GLIB_READ_COND; | 93 cond |= PURPLE_GLIB_READ_COND; |
| 94 if (condition & GAIM_INPUT_WRITE) | 94 if (condition & PURPLE_INPUT_WRITE) |
| 95 cond |= GAIM_GLIB_WRITE_COND; | 95 cond |= PURPLE_GLIB_WRITE_COND; |
| 96 | 96 |
| 97 channel = g_io_channel_unix_new(fd); | 97 channel = g_io_channel_unix_new(fd); |
| 98 closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, | 98 closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, |
| 99 gaim_glib_io_invoke, closure, gaim_glib_io_destroy); | 99 purple_glib_io_invoke, closure, purple_glib_io_destroy); |
| 100 | 100 |
| 101 g_io_channel_unref(channel); | 101 g_io_channel_unref(channel); |
| 102 return closure->result; | 102 return closure->result; |
| 103 } | 103 } |
| 104 | 104 |
| 105 static GaimEventLoopUiOps glib_eventloops = | 105 static PurpleEventLoopUiOps glib_eventloops = |
| 106 { | 106 { |
| 107 g_timeout_add, | 107 g_timeout_add, |
| 108 g_source_remove, | 108 g_source_remove, |
| 109 glib_input_add, | 109 glib_input_add, |
| 110 g_source_remove, | 110 g_source_remove, |
| 112 }; | 112 }; |
| 113 /*** End of the eventloop functions. ***/ | 113 /*** End of the eventloop functions. ***/ |
| 114 | 114 |
| 115 /*** Conversation uiops ***/ | 115 /*** Conversation uiops ***/ |
| 116 static void | 116 static void |
| 117 null_write_conv(GaimConversation *conv, const char *who, const char *alias, | 117 null_write_conv(PurpleConversation *conv, const char *who, const char *alias, |
| 118 const char *message, GaimMessageFlags flags, time_t mtime) | 118 const char *message, PurpleMessageFlags flags, time_t mtime) |
| 119 { | 119 { |
| 120 const char *name; | 120 const char *name; |
| 121 if (alias && *alias) | 121 if (alias && *alias) |
| 122 name = alias; | 122 name = alias; |
| 123 else if (who && *who) | 123 else if (who && *who) |
| 124 name = who; | 124 name = who; |
| 125 else | 125 else |
| 126 name = NULL; | 126 name = NULL; |
| 127 | 127 |
| 128 printf("(%s) %s %s: %s\n", gaim_conversation_get_name(conv), | 128 printf("(%s) %s %s: %s\n", purple_conversation_get_name(conv), |
| 129 gaim_utf8_strftime("(%H:%M:%S)", localtime(&mtime)), | 129 purple_utf8_strftime("(%H:%M:%S)", localtime(&mtime)), |
| 130 name, message); | 130 name, message); |
| 131 } | 131 } |
| 132 | 132 |
| 133 static GaimConversationUiOps null_conv_uiops = | 133 static PurpleConversationUiOps null_conv_uiops = |
| 134 { | 134 { |
| 135 .write_conv = null_write_conv | 135 .write_conv = null_write_conv |
| 136 }; | 136 }; |
| 137 | 137 |
| 138 static void | 138 static void |
| 140 { | 140 { |
| 141 /** | 141 /** |
| 142 * This should initialize the UI components for all the modules. Here we | 142 * This should initialize the UI components for all the modules. Here we |
| 143 * just initialize the UI for conversations. | 143 * just initialize the UI for conversations. |
| 144 */ | 144 */ |
| 145 gaim_conversations_set_ui_ops(&null_conv_uiops); | 145 purple_conversations_set_ui_ops(&null_conv_uiops); |
| 146 } | 146 } |
| 147 | 147 |
| 148 static GaimCoreUiOps null_core_uiops = | 148 static PurpleCoreUiOps null_core_uiops = |
| 149 { | 149 { |
| 150 NULL, | 150 NULL, |
| 151 NULL, | 151 NULL, |
| 152 null_ui_init, | 152 null_ui_init, |
| 153 NULL | 153 NULL |
| 155 | 155 |
| 156 static void | 156 static void |
| 157 init_libpurple() | 157 init_libpurple() |
| 158 { | 158 { |
| 159 /* Set a custom user directory (optional) */ | 159 /* Set a custom user directory (optional) */ |
| 160 gaim_util_set_user_dir(CUSTOM_USER_DIRECTORY); | 160 purple_util_set_user_dir(CUSTOM_USER_DIRECTORY); |
| 161 | 161 |
| 162 /* We do not want any debugging for now to keep the noise to a minimum. */ | 162 /* We do not want any debugging for now to keep the noise to a minimum. */ |
| 163 gaim_debug_set_enabled(FALSE); | 163 purple_debug_set_enabled(FALSE); |
| 164 | 164 |
| 165 /* Set the core-uiops, which is used to | 165 /* Set the core-uiops, which is used to |
| 166 * - initialize the ui specific preferences. | 166 * - initialize the ui specific preferences. |
| 167 * - initialize the debug ui. | 167 * - initialize the debug ui. |
| 168 * - initialize the ui components for all the modules. | 168 * - initialize the ui components for all the modules. |
| 169 * - uninitialize the ui components for all the modules when the core terminates. | 169 * - uninitialize the ui components for all the modules when the core terminates. |
| 170 */ | 170 */ |
| 171 gaim_core_set_ui_ops(&null_core_uiops); | 171 purple_core_set_ui_ops(&null_core_uiops); |
| 172 | 172 |
| 173 /* Set the uiops for the eventloop. If your client is glib-based, you can safely | 173 /* Set the uiops for the eventloop. If your client is glib-based, you can safely |
| 174 * copy this verbatim. */ | 174 * copy this verbatim. */ |
| 175 gaim_eventloop_set_ui_ops(&glib_eventloops); | 175 purple_eventloop_set_ui_ops(&glib_eventloops); |
| 176 | 176 |
| 177 /* Set path to search for plugins. The core (libpurple) takes care of loading the | 177 /* Set path to search for plugins. The core (libpurple) takes care of loading the |
| 178 * core-plugins, which includes the protocol-plugins. So it is not essential to add | 178 * core-plugins, which includes the protocol-plugins. So it is not essential to add |
| 179 * any path here, but it might be desired, especially for ui-specific plugins. */ | 179 * any path here, but it might be desired, especially for ui-specific plugins. */ |
| 180 gaim_plugins_add_search_path(CUSTOM_PLUGIN_PATH); | 180 purple_plugins_add_search_path(CUSTOM_PLUGIN_PATH); |
| 181 | 181 |
| 182 /* Now that all the essential stuff has been set, let's try to init the core. It's | 182 /* Now that all the essential stuff has been set, let's try to init the core. It's |
| 183 * necessary to provide a non-NULL name for the current ui to the core. This name | 183 * necessary to provide a non-NULL name for the current ui to the core. This name |
| 184 * is used by stuff that depends on this ui, for example the ui-specific plugins. */ | 184 * is used by stuff that depends on this ui, for example the ui-specific plugins. */ |
| 185 if (!gaim_core_init(UI_ID)) { | 185 if (!purple_core_init(UI_ID)) { |
| 186 /* Initializing the core failed. Terminate. */ | 186 /* Initializing the core failed. Terminate. */ |
| 187 fprintf(stderr, | 187 fprintf(stderr, |
| 188 "libpurple initialization failed. Dumping core.\n" | 188 "libpurple initialization failed. Dumping core.\n" |
| 189 "Please report this!\n"); | 189 "Please report this!\n"); |
| 190 abort(); | 190 abort(); |
| 191 } | 191 } |
| 192 | 192 |
| 193 /* Create and load the buddylist. */ | 193 /* Create and load the buddylist. */ |
| 194 gaim_set_blist(gaim_blist_new()); | 194 purple_set_blist(purple_blist_new()); |
| 195 gaim_blist_load(); | 195 purple_blist_load(); |
| 196 | 196 |
| 197 /* Load the preferences. */ | 197 /* Load the preferences. */ |
| 198 gaim_prefs_load(); | 198 purple_prefs_load(); |
| 199 | 199 |
| 200 /* Load the desired plugins. The client should save the list of loaded plugins in | 200 /* Load the desired plugins. The client should save the list of loaded plugins in |
| 201 * the preferences using gaim_plugins_save_loaded(PLUGIN_SAVE_PREF) */ | 201 * the preferences using purple_plugins_save_loaded(PLUGIN_SAVE_PREF) */ |
| 202 gaim_plugins_load_saved(PLUGIN_SAVE_PREF); | 202 purple_plugins_load_saved(PLUGIN_SAVE_PREF); |
| 203 | 203 |
| 204 /* Load the pounces. */ | 204 /* Load the pounces. */ |
| 205 gaim_pounces_load(); | 205 purple_pounces_load(); |
| 206 } | 206 } |
| 207 | 207 |
| 208 static void | 208 static void |
| 209 signed_on(GaimConnection *gc, gpointer null) | 209 signed_on(PurpleConnection *gc, gpointer null) |
| 210 { | 210 { |
| 211 GaimAccount *account = gaim_connection_get_account(gc); | 211 PurpleAccount *account = purple_connection_get_account(gc); |
| 212 printf("Account connected: %s %s\n", account->username, account->protocol_id); | 212 printf("Account connected: %s %s\n", account->username, account->protocol_id); |
| 213 } | 213 } |
| 214 | 214 |
| 215 static void | 215 static void |
| 216 connect_to_signals_for_demonstration_purposes_only() | 216 connect_to_signals_for_demonstration_purposes_only() |
| 217 { | 217 { |
| 218 static int handle; | 218 static int handle; |
| 219 gaim_signal_connect(gaim_connections_get_handle(), "signed-on", &handle, | 219 purple_signal_connect(purple_connections_get_handle(), "signed-on", &handle, |
| 220 GAIM_CALLBACK(signed_on), NULL); | 220 PURPLE_CALLBACK(signed_on), NULL); |
| 221 } | 221 } |
| 222 | 222 |
| 223 int main() | 223 int main() |
| 224 { | 224 { |
| 225 GList *iter; | 225 GList *iter; |
| 227 GList *names = NULL; | 227 GList *names = NULL; |
| 228 const char *prpl; | 228 const char *prpl; |
| 229 char name[128]; | 229 char name[128]; |
| 230 char *password; | 230 char *password; |
| 231 GMainLoop *loop = g_main_loop_new(NULL, FALSE); | 231 GMainLoop *loop = g_main_loop_new(NULL, FALSE); |
| 232 GaimAccount *account; | 232 PurpleAccount *account; |
| 233 GaimSavedStatus *status; | 233 PurpleSavedStatus *status; |
| 234 | 234 |
| 235 init_libpurple(); | 235 init_libpurple(); |
| 236 | 236 |
| 237 printf("libpurple initialized.\n"); | 237 printf("libpurple initialized.\n"); |
| 238 | 238 |
| 239 iter = gaim_plugins_get_protocols(); | 239 iter = purple_plugins_get_protocols(); |
| 240 for (i = 0; iter; iter = iter->next) { | 240 for (i = 0; iter; iter = iter->next) { |
| 241 GaimPlugin *plugin = iter->data; | 241 PurplePlugin *plugin = iter->data; |
| 242 GaimPluginInfo *info = plugin->info; | 242 PurplePluginInfo *info = plugin->info; |
| 243 if (info && info->name) { | 243 if (info && info->name) { |
| 244 printf("\t%d: %s\n", i++, info->name); | 244 printf("\t%d: %s\n", i++, info->name); |
| 245 names = g_list_append(names, info->id); | 245 names = g_list_append(names, info->id); |
| 246 } | 246 } |
| 247 } | 247 } |
| 253 printf("Username: "); | 253 printf("Username: "); |
| 254 fgets(name, sizeof(name), stdin); | 254 fgets(name, sizeof(name), stdin); |
| 255 name[strlen(name) - 1] = 0; /* strip the \n at the end */ | 255 name[strlen(name) - 1] = 0; /* strip the \n at the end */ |
| 256 | 256 |
| 257 /* Create the account */ | 257 /* Create the account */ |
| 258 account = gaim_account_new(name, prpl); | 258 account = purple_account_new(name, prpl); |
| 259 | 259 |
| 260 /* Get the password for the account */ | 260 /* Get the password for the account */ |
| 261 password = getpass("Password: "); | 261 password = getpass("Password: "); |
| 262 gaim_account_set_password(account, password); | 262 purple_account_set_password(account, password); |
| 263 | 263 |
| 264 /* It's necessary to enable the account first. */ | 264 /* It's necessary to enable the account first. */ |
| 265 gaim_account_set_enabled(account, UI_ID, TRUE); | 265 purple_account_set_enabled(account, UI_ID, TRUE); |
| 266 | 266 |
| 267 /* Now, to connect the account(s), create a status and activate it. */ | 267 /* Now, to connect the account(s), create a status and activate it. */ |
| 268 status = gaim_savedstatus_new(NULL, GAIM_STATUS_AVAILABLE); | 268 status = purple_savedstatus_new(NULL, PURPLE_STATUS_AVAILABLE); |
| 269 gaim_savedstatus_activate(status); | 269 purple_savedstatus_activate(status); |
| 270 | 270 |
| 271 connect_to_signals_for_demonstration_purposes_only(); | 271 connect_to_signals_for_demonstration_purposes_only(); |
| 272 | 272 |
| 273 g_main_loop_run(loop); | 273 g_main_loop_run(loop); |
| 274 | 274 |
