source: protocols/nogaim.h @ 8d7cc55

Last change on this file since 8d7cc55 was 9e83b15, checked in by dequis <dx@…>, at 2018-07-03T05:58:47Z

Add a hash table to speed up bee_user_by_handle()

This maintains a hash table next to the linked list, which results in
negligible additional memory usage (~300kb for 10k users) but allows
instant lookups.

This was a big problem with discord, which has huge user lists and joins
everyone to every channel. In my test, the GUILD_SYNC event for 10k-50k
user lists is now approximately 5 times faster.

This hash table based code is only used if handle_cmp is either
exact or case-insensitive string comparison (g_ascii_strcasecmp or
strcmp/g_strcmp0).

The old function that goes through the bee->users linked list is now
called bee_user_by_handle_slow() and used for protocols with unusual
handle_cmp functions - skimming through the code, just oscar.
May revisit this if it happens to more meaningful protocols.

The case-insensitive hashtable functions are copied from irssi, which is
also GPLv2. I renamed them from g_ to b_ (g_istr_equal to b_istr_equal)

  • Property mode set to 100644
File size: 15.7 KB
Line 
1/********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2012 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/*
8 * nogaim, soon to be known as im_api. Not a separate product (unless
9 * someone would be interested in such a thing), just a new name.
10 *
11 * Gaim without gaim - for BitlBee
12 *
13 * This file contains functions called by the Gaim IM-modules. It contains
14 * some struct and type definitions from Gaim.
15 *
16 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
17 *                          (and possibly other members of the Gaim team)
18 * Copyright 2002-2012 Wilmer van der Gaast <wilmer@gaast.net>
19 */
20
21/*
22  This program is free software; you can redistribute it and/or modify
23  it under the terms of the GNU General Public License as published by
24  the Free Software Foundation; either version 2 of the License, or
25  (at your option) any later version.
26
27  This program is distributed in the hope that it will be useful,
28  but WITHOUT ANY WARRANTY; without even the implied warranty of
29  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  GNU General Public License for more details.
31
32  You should have received a copy of the GNU General Public License with
33  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
34  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
35  Fifth Floor, Boston, MA  02110-1301  USA
36*/
37
38#ifndef _NOGAIM_H
39#define _NOGAIM_H
40
41#if (__sun)
42#include <inttypes.h>
43#else
44#include <stdint.h>
45#endif
46
47#include "bitlbee.h"
48#include "account.h"
49#include "proxy.h"
50#include "query.h"
51#include "md5.h"
52#include "ft.h"
53
54#define BUDDY_ALIAS_MAXLEN 388   /* because MSN names can be 387 characters */
55
56#define WEBSITE "http://www.bitlbee.org/"
57
58/* Sharing flags between all kinds of things. I just hope I won't hit any
59   limits before 32-bit machines become extinct. ;-) */
60#define OPT_LOGGED_IN   0x00000001
61#define OPT_LOGGING_OUT 0x00000002
62#define OPT_AWAY        0x00000004
63#define OPT_MOBILE      0x00000008
64#define OPT_DOES_HTML   0x00000010
65#define OPT_LOCALBUDDY  0x00000020 /* For nicks local to one groupchat */
66#define OPT_SLOW_LOGIN  0x00000040 /* I.e. Twitter Oauth @ login time */
67#define OPT_TYPING      0x00000100 /* Some pieces of code make assumptions */
68#define OPT_THINKING    0x00000200 /* about these values... Stupid me! */
69#define OPT_NOOTR       0x00001000 /* protocol not suitable for OTR */
70#define OPT_PONGS       0x00010000 /* Service sends us keep-alives */
71#define OPT_PONGED      0x00020000 /* Received a keep-alive during last interval */
72#define OPT_SELFMESSAGE 0x00080000 /* A message sent by self from another location */
73
74/* ok. now the fun begins. first we create a connection structure */
75struct im_connection {
76        account_t *acc;
77        uint32_t flags;
78
79        /* each connection then can have its own protocol-specific data */
80        void *proto_data;
81
82        /* all connections need an input watcher */
83        int inpa;
84        guint keepalive;
85
86        /* buddy list stuff. there is still a global groups for the buddy list, but
87         * we need to maintain our own set of buddies, and our own permit/deny lists */
88        GSList *permit;
89        GSList *deny;
90        int permdeny;
91
92        char *away;
93
94        /* BitlBee */
95        bee_t *bee;
96
97        GSList *groupchats;
98        GSList *chatlist;
99        GHashTable *bee_users;
100};
101
102struct groupchat {
103        struct im_connection *ic;
104
105        /* stuff used just for chat */
106        /* The in_room variable is a list of handles (not nicks!), kind of
107         * "nick list". This is how you can check who is in the group chat
108         * already, for example to avoid adding somebody two times. */
109        GList *in_room;
110        //GList *ignored;
111
112        //struct groupchat *next;
113        /* The title variable contains the ID you gave when you created the
114         * chat using imcb_chat_new(). */
115        char *title;
116        /* Use imcb_chat_topic() to change this variable otherwise the user
117         * won't notice the topic change. */
118        char *topic;
119        char joined;
120        /* This is for you, you can add your own structure here to extend this
121         * structure for your protocol's needs. */
122        void *data;
123        void *ui_data;
124};
125
126struct buddy {
127        char name[80];
128        char show[BUDDY_ALIAS_MAXLEN];
129        int present;
130        time_t signon;
131        time_t idle;
132        int uc;
133        guint caps; /* woohoo! */
134        void *proto_data; /* what a hack */
135        struct im_connection *ic; /* the connection it belongs to */
136};
137
138struct buddy_action {
139        char *name;
140        char *description;
141};
142
143/* This enum takes a few things from libpurple and a few things from old OPT_ flags.
144 * The only flag that was used before this struct was PRPL_OPT_NOOTR.
145 *
146 * The libpurple ones only use the same values as the PurpleProtocolOptions
147 * enum for convenience, but there's no promise of direct compatibility with
148 * those values. As of libpurple 2.8.0 they use up to 0x800 (1 << 11), which is
149 * a nice coincidence.
150 */
151typedef enum {
152        /* The protocol doesn't use passwords
153         * Mirrors libpurple's OPT_PROTO_NO_PASSWORD */
154        PRPL_OPT_NO_PASSWORD = 1 << 4,
155
156        /* The protocol doesn't require passwords, but may use them
157         * Mirrors libpurple's OPT_PROTO_PASSWORD_OPTIONAL */
158        PRPL_OPT_PASSWORD_OPTIONAL = 1 << 7,
159
160        /* The protocol is not suitable for OTR, see OPT_NOOTR */
161        PRPL_OPT_NOOTR = 1 << 12,
162} prpl_options_t;
163
164struct prpl {
165        int options;
166        /* You should set this to the name of your protocol.
167         * - The user sees this name ie. when imcb_log() is used. */
168        const char *name;
169        void *data;
170        /* Maximum Message Size of this protocol.
171         * - Introduced for OTR, in order to fragment large protocol messages.
172         * - 0 means "unlimited". */
173        unsigned int mms;
174
175        /* Added this one to be able to add per-account settings, don't think
176         * it should be used for anything else. You are supposed to use the
177         * set_add() function to add new settings. */
178        void (* init)           (account_t *);
179        /* The typical usage of the login() function:
180         * - Create an im_connection using imcb_new() from the account_t parameter.
181         * - Initialize your myproto_data struct - you should store all your protocol-specific data there.
182         * - Save your custom structure to im_connection->proto_data.
183         * - Use proxy_connect() to connect to the server.
184         */
185        void (* login)          (account_t *);
186        /* Implementing this function is optional. */
187        void (* keepalive)      (struct im_connection *);
188        /* In this function you should:
189         * - Tell the server about you are logging out.
190         * - g_free() your myproto_data struct as BitlBee does not know how to
191         *   properly do so.
192         */
193        void (* logout)         (struct im_connection *);
194
195        /* This function is called when the user wants to send a message to a handle.
196         * - 'to' is a handle, not a nick
197         * - 'flags' may be ignored
198         */
199        int (* buddy_msg)      (struct im_connection *, char *to, char *message, int flags);
200        /* This function is called then the user uses the /away IRC command.
201         * - 'state' contains the away reason.
202         * - 'message' may be ignored if your protocol does not support it.
203         */
204        void (* set_away)       (struct im_connection *, char *state, char *message);
205        /* Implementing this function is optional. */
206        int (* send_typing)    (struct im_connection *, char *who, int flags);
207
208        /* 'name' is a handle to add/remove. For now BitlBee doesn't really
209         * handle groups, just set it to NULL, so you can ignore that
210         * parameter. */
211        void (* add_buddy)      (struct im_connection *, char *name, char *group);
212        void (* remove_buddy)   (struct im_connection *, char *name, char *group);
213
214        /* Block list stuff. Implementing these are optional. */
215        void (* add_permit)     (struct im_connection *, char *who);
216        void (* add_deny)       (struct im_connection *, char *who);
217        void (* rem_permit)     (struct im_connection *, char *who);
218        void (* rem_deny)       (struct im_connection *, char *who);
219
220        /* Request profile info. Free-formatted stuff, the IM module gives back
221           this info via imcb_log(). Implementing these are optional. */
222        void (* get_info)       (struct im_connection *, char *who);
223
224        /* Group chat stuff. */
225        /* This is called when the user uses the /invite IRC command.
226         * - 'who' may be ignored
227         * - 'message' is a handle to invite
228         */
229        void (* chat_invite)    (struct groupchat *, char *who, char *message);
230        /* This is called when the user uses the /kick IRC command.
231         * - 'who' is a handle to kick
232         * - 'message' is a kick message or NULL
233         */
234        void (* chat_kick)      (struct groupchat *, char *who, const char *message);
235        /* This is called when the user uses the /part IRC command in a group
236         * chat. You just should tell the user about it, nothing more. */
237        void (* chat_leave)     (struct groupchat *);
238        /* This is called when the user sends a message to the groupchat.
239         * 'flags' may be ignored. */
240        void (* chat_msg)       (struct groupchat *, char *message, int flags);
241        /* This is called when the user uses the /join #nick IRC command.
242         * - 'who' is the handle of the nick
243         */
244        struct groupchat *
245        (* chat_with)      (struct im_connection *, char *who);
246        /* This is used when the user uses the /join #channel IRC command.  If
247         * your protocol does not support publicly named group chats, then do
248         * not implement this. */
249        struct groupchat *
250        (* chat_join)      (struct im_connection *, const char *room,
251                            const char *nick, const char *password, set_t **sets);
252        /* Change the topic, if supported. Note that BitlBee expects the IM
253           server to confirm the topic change with a regular topic change
254           event. If it doesn't do that, you have to fake it to make it
255           visible to the user. */
256        void (* chat_topic)     (struct groupchat *, char *topic);
257
258        /* If your protocol module needs any special info for joining chatrooms
259           other than a roomname + nickname, add them here. */
260        void (* chat_add_settings)      (account_t *acc, set_t **head);
261        void (* chat_free_settings)     (account_t *acc, set_t **head);
262
263        /* You can tell what away states your protocol supports, so that
264         * BitlBee will try to map the IRC away reasons to them. If your
265         * protocol doesn't have any, just return one generic "Away". */
266        GList *(* away_states)(struct im_connection *ic);
267
268        /* Mainly for AOL, since they think "Bung hole" == "Bu ngho le". *sigh*
269         * - Most protocols will just want to set this to g_strcasecmp().*/
270        int (* handle_cmp) (const char *who1, const char *who2);
271
272        /* Implement these callbacks if you want to use imcb_ask_auth() */
273        void (* auth_allow)     (struct im_connection *, const char *who);
274        void (* auth_deny)      (struct im_connection *, const char *who);
275
276        /* Incoming transfer request */
277        void (* transfer_request) (struct im_connection *, file_transfer_t *ft, char *handle);
278
279        void (* buddy_data_add) (struct bee_user *bu);
280        void (* buddy_data_free) (struct bee_user *bu);
281
282        GList *(* buddy_action_list) (struct bee_user *bu);
283        void *(* buddy_action) (struct bee_user *bu, const char *action, char * const args[], void *data);
284
285        /* If null, equivalent to handle_cmp( ic->acc->user, who ) */
286        gboolean (* handle_is_self) (struct im_connection *, const char *who);
287
288        /* This sets/updates the im_connection->chatlist field with a
289         * bee_chat_info_t GSList. This function should ensure the
290         * bee_chat_list_finish() function gets called at some point
291         * after the chat list is completely updated.
292         */
293        void (* chat_list) (struct im_connection *, const char *server);
294
295        /* Some placeholders so eventually older plugins may cooperate with newer BitlBees. */
296        void *resv1;
297        void *resv2;
298        void *resv3;
299        void *resv4;
300        void *resv5;
301};
302
303struct plugin_info
304{
305        guint abiver;
306        const char *name;
307        const char *version;
308        const char *description;
309        const char *author;
310        const char *url;
311};
312
313#ifdef WITH_PLUGINS
314G_MODULE_EXPORT GList *get_plugins();
315#endif
316
317/* im_api core stuff. */
318void nogaim_init();
319G_MODULE_EXPORT GList *get_protocols();
320G_MODULE_EXPORT GList *get_protocols_disabled();
321G_MODULE_EXPORT GSList *get_connections();
322G_MODULE_EXPORT struct prpl *find_protocol(const char *name);
323G_MODULE_EXPORT gboolean is_protocol_disabled(const char *name);
324G_MODULE_EXPORT char *explain_unknown_protocol(const char *name);
325/* When registering a new protocol, you should allocate space for a new prpl
326 * struct, initialize it (set the function pointers to point to your
327 * functions), finally call this function. */
328G_MODULE_EXPORT void register_protocol(struct prpl *);
329
330/* Connection management. */
331/* You will need this function in prpl->login() to get an im_connection from
332 * the account_t parameter. */
333G_MODULE_EXPORT struct im_connection *imcb_new(account_t *acc);
334G_MODULE_EXPORT void imc_free(struct im_connection *ic);
335/* Once you're connected, you should call this function, so that the user will
336 * see the success. */
337G_MODULE_EXPORT void imcb_connected(struct im_connection *ic);
338/* This can be used to disconnect when something went wrong (ie. read error
339 * from the server). You probably want to set the second parameter to TRUE. */
340G_MODULE_EXPORT void imc_logout(struct im_connection *ic, int allow_reconnect);
341
342/* Communicating with the user. */
343/* A printf()-like function to tell the user anything you want. */
344G_MODULE_EXPORT void imcb_log(struct im_connection *ic, char *format, ...) G_GNUC_PRINTF(2, 3);
345/* To tell the user an error, ie. before logging out when an error occurs. */
346G_MODULE_EXPORT void imcb_error(struct im_connection *ic, char *format, ...) G_GNUC_PRINTF(2, 3);
347
348/* To ask a your about something.
349 * - 'msg' is the question.
350 * - 'data' can be your custom struct - it will be passed to the callbacks.
351 * - 'doit' or 'dont' will be called depending of the answer of the user.
352 */
353G_MODULE_EXPORT void imcb_ask(struct im_connection *ic, char *msg, void *data, query_callback doit,
354                              query_callback dont);
355G_MODULE_EXPORT void imcb_ask_with_free(struct im_connection *ic, char *msg, void *data, query_callback doit,
356                                        query_callback dont, query_callback myfree);
357
358/* Two common questions you may want to ask:
359 * - X added you to his contact list, allow?
360 * - X is not in your contact list, want to add?
361 */
362G_MODULE_EXPORT void imcb_ask_auth(struct im_connection *ic, const char *handle, const char *realname);
363G_MODULE_EXPORT void imcb_ask_add(struct im_connection *ic, const char *handle, const char *realname);
364
365/* Buddy management */
366/* This function should be called for each handle which are visible to the
367 * user, usually after a login, or if the user added a buddy and the IM
368 * server confirms that the add was successful. Don't forget to do this! */
369G_MODULE_EXPORT void imcb_add_buddy(struct im_connection *ic, const char *handle, const char *group);
370G_MODULE_EXPORT void imcb_remove_buddy(struct im_connection *ic, const char *handle, char *group);
371G_MODULE_EXPORT void imcb_rename_buddy(struct im_connection *ic, const char *handle, const char *realname);
372G_MODULE_EXPORT void imcb_buddy_nick_hint(struct im_connection *ic, const char *handle, const char *nick);
373G_MODULE_EXPORT void imcb_buddy_nick_change(struct im_connection *ic, const char *handle, const char *nick);
374G_MODULE_EXPORT void imcb_buddy_action_response(bee_user_t *bu, const char *action, char * const args[], void *data);
375
376G_MODULE_EXPORT void imcb_buddy_typing(struct im_connection *ic, const char *handle, guint32 flags);
377G_MODULE_EXPORT struct bee_user *imcb_buddy_by_handle(struct im_connection *ic, const char *handle);
378
379G_GNUC_DEPRECATED G_MODULE_EXPORT void imcb_clean_handle(struct im_connection *ic, char *handle);
380
381/* Actions, or whatever. */
382int imc_away_send_update(struct im_connection *ic);
383int imc_chat_msg(struct groupchat *c, char *msg, int flags);
384
385void imc_add_allow(struct im_connection *ic, char *handle);
386void imc_rem_allow(struct im_connection *ic, char *handle);
387void imc_add_block(struct im_connection *ic, char *handle);
388void imc_rem_block(struct im_connection *ic, char *handle);
389
390/* Misc. stuff */
391char *set_eval_timezone(set_t *set, char *value);
392gboolean auto_reconnect(gpointer data, gint fd, b_input_condition cond);
393void cancel_auto_reconnect(struct account *a);
394
395#endif
Note: See TracBrowser for help on using the repository browser.