comparison HACKING @ 16178:d73ee2690376

merge of '3ae1c2f29b72682bad3542b9c0175438dd3309c0' and '60672db63b751e0cdc4ab40d5251c9479d3c0c79'
author Richard Laager <rlaager@wiktel.com>
date Mon, 16 Apr 2007 00:46:39 +0000
parents c4a5d8950d8c d88f0f320c9b
children
comparison
equal deleted inserted replaced
16168:cc5917d70dde 16178:d73ee2690376
1 Lots of this is pretty grossly out of date... 1 For information on hacking on Pidgin, Finch, or libpurple, see:
2 Some of it might still be useful. For coding style, your 2 http://developer.pidgin.im
3 best bet is to browse through some of the files in src and
4 emulate what you see there.
5 --Mark
6
7
8 The majority of the below was written by Eric Warmenhoven way back in
9 antiquity. I have taken the liberty of attempting to PARTIALLY update
10 it. I still think its helpful, but use it at your own risk.
11 --Luke
12
13
14 A lot of people have tried to hack gaim, but haven't been able to because
15 the code is just so horrid. Well, the code isn't getting better anytime
16 soon (I hate GNU indent), so to help all you would-be hackers help out
17 gaim, here's a brief tutorial on how gaim works. I'll quickly describe
18 the logical flow of things, then what you'll find in each of the source
19 files. As an added bonus, I'll try and describe as best I can how multiple
20 connections and multiple protocols work. Depending on how much I want to
21 avoid my final tomorrow I may even describe other parts of gaim that I
22 particularly want to brag about. Hopefully that's enough to get most of
23 you going.
24
25 If you don't know how event-driven programs work, stop right now. Gaim
26 uses GTK+'s main loop (actually GLib's but I won't talk about how GTK
27 works) and uses GLib functions for timeouts and socket notification. If
28 you don't know GTK+ you should go learn that first.
29
30 If you're going to hack gaim, PLEASE, PLEASE PLEASE PLEASE send patches
31 against the absolute latest SVN. I get really annoyed when I get patches
32 against the last released version, especially since I don't usually have
33 a copy of it on my computer, and gaim tends to change a lot between
34 versions. (I sometimes get annoyed when they're against SVN from 3 days
35 ago, but can't complain because it's usually my fault that I haven't
36 looked at the patch yet.) To get gaim from SVN (if you haven't already),
37 run the following commands:
38
39 $ svn co https://svn.sourceforge.net/svnroot/gaim/trunk gaim
40 $ cd gaim
41 $ ./autogen.sh
42
43 You'll now have your normal gaim tree with ./configure and all (which
44 ./autogen.sh takes the liberty of running for you). (If you want to make
45 your life really simple, learn how SVN works. SVN is your friend.) To make
46 a patch, just edit the files right there in that tree (don't bother with
47 two trees, or even two copies of the same file). Then when you're ready to
48 make your patch, simply run 'svn diff > my.patch' and post it on
49 sf.net/projects/gaim in the patches section.
50
51 Some Documentation is available on the Gaim api if you run the command
52 $make docs
53 after running ./configure (or ./autogen.sh). You will need doxygen and
54 graphiz dot to generate these docs.
55
56 CODING STYLE
57 ============
58
59 Coding styles are like assholes, everyone has one and no one likes anyone
60 elses. This is mine and if you want me to accept a patch from you without
61 getting annoyed you'll follow this coding style. :)
62
63 It would probably just be easier for me to include CodingStyle from the
64 linux kernel source.
65
66 Tab indents. I *HATE* 2-space indents, and I strongly dislike 8-space
67 indents. Use a tab character. I'm likely to refuse a patch if it has
68 2-space indents.
69
70 K&R style for braces. Braces always go on the same line as the if, etc.
71 that they're associated with; the only exception is functions. Braces
72 for else statements should have both braces on the same line as the else
73 (i.e. "} else {").
74
75 No functionOrVariableNamesLikeThis. Save it for Java. Underscores are your
76 friend. "tmp" is an excellent variable name. Hungarian style will not be
77 tolerated. Go back to Microsoft.
78
79 I have a 105-char wide Eterm. Deal with it.
80
81 NO goto. I'm very likely to refuse a patch if it makes use of goto. If you
82 feel the need to use goto, you need to rethink your design and flow.
83
84
85 PROGRAM FLOW (just about every function name from here on down is wrong.
86 ============ but many of the ideas still apply under different names.)
87
88 Before gaim does anything you can see, it initializes itself, which is
89 mostly just reading ~/.gaim/*.xml (handled by the functions in prefs.[ch])
90 and parsing command-line options. It then draws the login window by
91 calling show_login, and waits for input.
92
93 At the login window, when "Accounts" is clicked, account_editor() is
94 called. This then displays all of the users and various information
95 about them. (Don't ask about what happens when "Sign On" is called. It's
96 quite hackish. The only reason the login window is there anymore is to
97 make it more palatable to people so used to WinAIM that they can't accept
98 anything else.)
99
100 When the "Sign on/off" button is clicked, serv_login is passed the
101 username and the password for the account. If the password length is
102 zero (the password field is a character array rather than pointer so it
103 will not be NULL) then the Signon callback will prompt for the password
104 before calling serv_login. serv_login then signs in the user using the
105 appropriate protocol.
106
107 After you're signed in, Gaim draws the buddy list by calling
108 show_buddy_list. Assuming the user has a buddy list (all buddy list
109 functions are controlled by list.c; when you sign on do_import is called
110 and that loads the locally saved list), the protocol calls
111 gaim_prpl_got functions, which set the information in the appropriate
112 struct buddy and then passes it off to set_buddy.
113
114 set_buddy is responsible for a lot of stuff, but most of it is done
115 implicitly. It's responsible for the sounds (which is just a call to
116 play_sound), but the biggest thing it does is call new_group_show and
117 new_buddy_show if necessary. There's only one group_show per group name,
118 even between connections, and only one buddy_show per group_show per
119 buddy name, even between connections. (If that's not confusing enough,
120 wait until I really start describing how the buddy list works.)
121
122 New connections happen the exact same way as described above. Each
123 gaim_account can have one gaim_connection associated with it. gaim_account
124 and gaim_connection both have a protocol field. This is kind of confusing:
125 gaim, except for the account editor screen and when the user signs on,
126 ignores the user's protocl field, and only uses the connection's protocol
127 field. You can change the connection's protocol field once it's created
128 and been assigned a PRPL to use to change certain behavior (Oscar does
129 this because it handles both AIM and ICQ). I'll talk about the
130 gaim_connection struct more later.
131
132 When the user opens a new conversation window, new_conversation is called.
133 That's easy enough. If there isn't a conversation with the person already
134 open (checked by calling find_conversation), show_conv is called to
135 create the new window. All sorts of neat things happen there, but it's
136 mostly drawing the window. show_conv is the best place to edit the UI.
137
138 That's pretty much it for the quick tutorial. I know it wasn't much but
139 it's enough to get you started. Make sure you know GTK+ before you get too
140 involved. Most of the back-end stuff is pretty basic; most of gaim is GTK+.
141
142
143 SOURCE FILES (this should probly be utterly removed)
144 ============
145
146 about.c:
147 Not much to say here, just a few basic functions.
148
149 account.[ch]:
150 This controls the GaimAccount struct, which stores information
151 on each account a user registers with gaim. Usernames, pass-
152 words, user info, alias, user specific options, and everything
153 else controlled from within the account editor (and then some)
154 are handled via this code.
155
156 accountopt.[ch]:
157 Api and implemenation for account options. I'm not precisely
158 sure how this meshes with account.[ch]
159
160 away.c:
161 This takes care of most of the away stuff: setting the away message
162 (do_away_message); coming back (do_im_back); drawing the away window;
163 etc. Away messages work really oddly due to multiple connections and
164 multiple protocols; I think there are really only two or three people
165 who know how it works and I don't think any of us know why it works
166 that way.
167
168 blist.[ch]:
169 This takes care of the buddy list backend, the blist.xml file,
170 importing old buddy list files, and related things like
171 finding buddies and groups. buddies, contacts, and groups
172 are controlled from these files.
173
174 buddy_chat.c:
175 This takes care of the buddy chat stuff. This used to be a lot bigger
176 until the chat and IM windows got merged in the code. Now it mostly
177 just takes care of chat-specific stuff, like ignoring people and
178 keeping track of who's in the room. This is also where the chat window
179 is created.
180
181 conversation.c:
182 This is where most of the functions dealing with the IM and chat windows
183 are hidden. It tries to abstract things as much as possible, but doesn't
184 do a very good job. This is also where things like "Enter sends" and
185 "Ctrl-{B/I/U/S}" options get carried out (look for send_callback). The
186 chat and IM toolbar (with the B/I/U/S buttons) are both built from
187 the same function, build_conv_toolbar.
188
189 core.c:
190 This is the start of what will become the main() for gaim-core.
191
192 gtkdialogs.c:
193 A massive file with a lot of little utility functions. This is where all
194 of those little dialog windows are created. Things like the warn dialog
195 and the add buddy dialog are here. Not all of the dialogs in gaim are in
196 this file, though. But most of them are. This is also where do_import
197 is housed, to import buddy lists. (The actual buddy list parsing code
198 is in util.c for winaim lists and buddy.c for gaim's own lists.)
199
200 gtkimhtml.c:
201 This is gaim's HTML widget. It replaced the old widget, GtkHtml (which
202 was different than GNOME's GtkHTML). It's self-contained (it doesn't
203 use any of gaim's code) and is actually a separate project from gaim
204 (but is maintained by Eric).
205
206 idle.c:
207 This file used to be entirely #if 0'd out of existance. However, thanks
208 to some very generous people who submitted patches, this takes care of
209 reporting idle time (imagine that). It's a pretty straight-forward file.
210 This also takes care of the auto-away stuff.
211
212 gtkmain.c:
213 This is where the main() function is. It takes care of a lot of the
214 initialization stuff, and showing the buddy list or account editor.
215
216 md5.c:
217 Oscar, Yahoo, and MSN all require md5 hashing, so better to put it in
218 the core than have the same thing in three different places.
219
220 module.c:
221 This contains all of the plugin code, except for the UI. This is what
222 actually loads the plugins, makes sure they're valid, has the code for
223 setting up plugin event handlers, and contains the plugin_event method
224 that gaim calls on events.
225
226 prefs.c:
227 Read the documentation on this file. This handles the backend
228 side of prefs.
229
230 proxy.c:
231 Adam (of libfaim glory) got bored one day and rewrote this file, so
232 now everything actually works. The main function is proxy_connect,
233 which figures out which proxy you want to use (if you want to use one
234 at all) and passes off the data to the appropriate function. This file
235 should be pretty straight-forward.
236 Except I STRONGLY suspect that time has broken this file.
237
238 prpl.c:
239 This file is what lets gaim dynamically load protocols, sort of. All
240 of the actual dlopen(), dlsym() stuff is in module.c. But this contains
241 all of the functions that the protocol plugin needs to call, and manages
242 all of the protocols. It's a pretty simple file actually.
243
244 server.c:
245 This is where all of the differentiation between the different protocols
246 is done. Nearly everything that's network related goes through here
247 at one point or another. This has good things like serv_send_im. Most of
248 it should be pretty self-explanatory.
249
250 sound.c:
251 The main function in this file is play_sound, which plays one of 8
252 (maybe 9?) sounds based on preferences. All that the rest of the code
253 should have to do is call play_sound(BUDDY_ARRIVE), for example, and
254 this file will take care of determining if a sound should be played
255 and which file should be played.
256
257 util.c:
258 There's not really a lot of cohesion to this file; it's just a lot of
259 stuff that happened to be thrown into it for no apparent reason. None
260 of it is particularly tasty; it's all just utility functions. Just
261 like the name says.
262
263 plugins/ticker/gtkticker.c:
264 Syd, our resident GTK+ God, wrote a GtkWidget, GtkTicker. This is that
265 widget. It's cool, and it's tiny. This is actually a really good example
266 widget for those of you looking to write your own.
267
268 plugins/ticker/ticker.c:
269 Syd is just so cool. I really can't get over it. He let me come
270 visit him at Netscape one day, and I got to see all of their toys
271 (don't worry, I'm under an NDA). Anyway, this file is for the buddy
272 ticker. This is also a damn cool file because it's got all of the
273 functions that you'd want right up at the top. Someday I want to be
274 as cool as Syd.
275
276 For the PRPLs, the only protocol whose "main" gaim file isn't the same as
277 the name of the protocol is ICQ; for that it's gaim_icq.c. But ICQ is
278 deprecated and you should be using Oscar for ICQ anyway.
279
280 PLUGINS (read the plugins howto, this is really out of date)
281 =======
282
283 OK, so you want to load a plugin. You go through whatever UI (you
284 can read all about the UI in plugins.c or whereever). You finally get
285 to load_plugin, the meat of the plugins stuff (plugins can actually
286 call load_plugin themselves to load other plugins). load_plugin
287 is passed the full path to the plugin you want to load
288 (e.g. /usr/local/lib/gaim/irc.so).
289
290 load_plugin does a few things with that filename. The first is to see
291 if you've already loaded that plugin. If you have, load_plugin unloads
292 the one that is currently loaded. You might wonder why; it's because
293 the same plugin can't be loaded twice. If you call g_module_open on a
294 filename twice, both times it will return the same pointer, and both times
295 increment the reference count on the GModule * that it returns. This
296 means you really do have the same plugin twice, which fucks up the
297 callback system to no end. So it's better that you can only have it
298 loaded once at any given time.
299
300 Now that we're assured that we don't have this particular plugin loaded
301 yet, we better load it. g_module_open, baby. Much more portable than
302 dlopen(). In fact, for Linux it actually is the equivalent of dlopen()
303 (you can read the gmodule source and see for yourself). There's only one
304 quirk. It always logically ORs the options you pass with RTLD_GLOBAL,
305 which means that plugins share symbols. I haven't figured out yet if
306 this means just functions or variables too; but in either case make every
307 function and variable in your plugin static except for gaim_plugin_*(),
308 name(), and description(). It's good coding practice anyway.
309
310 So, assuming we didn't get NULL back from g_module_open, we then make sure
311 it's a valid gaim plugin by looking for and calling gaim_plugin_init,
312 courtesy g_module_symbol (g_module_symbol is actually what's portable
313 about gmodule as opposed to dl*; some BSD's require '_' prepended to
314 symbol names and g_module_symbol guarantees we do The Right Thing).
315
316 Assuming we've found gaim_plugin_init and it hasn't returned non-NULL
317 to us, we then add it to our list of plugins and go merrily about our way.
318
319 So when do the callbacks happen?! plugin_event, baby, plugin_event. Any
320 time you want to trigger a plugin event simply call plugin_even with the
321 parameters to be passed to any event handlers and you're set. plugin_event
322 then makes sure that any plugins waiting for the event get passed the
323 arguments properly and passes it on to perl.
324
325 Speaking of perl. If you really want to know how this works, you're
326 better off reading X-Chat's documentation of it, because it's better
327 than what I could provide.
328
329
330 MULTIPLE CONNECTIONS AND PRPLS
331 ==============================
332
333 OK, let's start with the basics. There are users. Each user is contained
334 in an gaim_account struct, and kept track of in the gaim_accounts GSList.
335 Each gaim_account has certain features: a username, a password, and
336 user_info. It also has certain options, and the protocol it uses to sign
337 on (kept as an int which is #define'd in prpl.h).
338
339 Now then, there are protocols that gaim knows about. Each protocol is
340 in a prpl struct and kept track of in the protocols GSList. The way the
341 management of the protocols is, there will only ever be one prpl per
342 numeric protocol. Each prpl defines a basic set of functions: login,
343 logout, send_im, etc. The prpl is responsible not only for handling
344 these functions, but also for calling the appropriate prpl_got functions
345 It handles each of these on a per-account basis.
346
347 So why's it called a PRPL? It stands for PRotocol PLugin. That means
348 that it's possible to dynamically add new protocols to gaim. However,
349 all protocols must be implemented the same way: by using a prpl struct
350 and being loaded, regardless of whether they are static or dynamic.
351
352 Here's how struct gaim_connection fits into all of this. At some point
353 the User (capitalized to indicate a person and not a name) will try to
354 sign on one of Their users. serv_login is then called for that user. It
355 searches for the prpl that is assigned to that user, and calls that prpl's
356 login function, passing it the gaim_account struct that is attempting to
357 sign on. The prpl is then responsible for seeing that the gaim_connection
358 is created (by calling new_gaim_connection), and registering it as
359 being online (by calling account_online and passing it the gaim_account and
360 gaim_connection structs). At that point, the gaim_account and gaim_connection
361 structs have pointers to each other, and the gaim_connection struct has
362 a pointer to the prpl struct that it is using. The gaim_connections are
363 stored in the connections GSList. The way connection management works is,
364 there will always only be one gaim_connection per user, and the prpl that
365 the gaim_connection uses will be constant for the gaim_connection's life.
366
367 So at certain points the User is going to want to do certain things,
368 like send a message. They must send the message on a connection. So the UI
369 figures out which gaim_connection the User want to send a message on (for
370 our example), and calls serv_send_im, telling it which gaim_connection to
371 use, and the necessary information (who to send it to, etc). The serv_
372 function then calls the handler of the prpl of the connection for that
373 event (that was way too many prepositions). OK, each prpl has a send_im
374 function. Each connection has a prpl. so you call gc->prpl->send_im and
375 pass it the connection and all the necessary info. And that's how things
376 get done.
377
378 I hope some of that made sense. Looking back at it it makes absolutely no
379 sense to me. Thank god I wrote the code; otherwise I'm sure I'd be lost.
380
381
382 WRITING PRPLS
383 =============
384
385 Start off with a protocol that you want to implement; make sure it has a
386 number defined in prpl.h. If it doesn't, talk to Rob or Eric about adding
387 it. *NEVER* use an unassigned number, not even for testing or personal
388 use. It's possible that number will be used later by something else and
389 that would cause quite a few head-scratchers.
390
391 Start off with the following boiler plate:
392
393 static struct prpl *my_protocol = NULL;
394
395 void newproto_init(struct prpl *ret) {
396 ret->protocol = PROTO_NEWPROTO;
397
398 my_protocol = ret;
399 }
400
401 #ifndef STATIC
402
403 char *gaim_plugin_init(GModule *handle)
404 {
405 load_protocol(newproto_init, sizeof(struct prpl));
406 return NULL;
407 }
408
409 void gaim_plugin_remove()
410 {
411 struct prpl *p = find_prpl(PROTO_NEWPROTO);
412 if (p == my_protocol)
413 unload_protocol(p);
414 }
415
416 char *name()
417 {
418 return "New Protocol";
419 }
420
421 char *description()
422 {
423 return PRPL_DESC("New Protocol");
424 }
425
426 #endif
427
428 Replace all NEWPROTO things with your protocol name (e.g. PROTO_OSCAR
429 instead of PROTO_NEWPROTO, oscar_init instead of newproto_init). Then
430 populate your struct prpl; the most important function is actually name(),
431 because without it, Gaim will most likely segfault. The second most
432 important function is login(). Not all functions need to be implemented.
433
434 There should be absolutely *ZERO* GTK+ in the PRPLs. PRPLs should *NEVER*
435 say what the UI *looks* like, only what information needs to be there.
436 There's currently an effort to get the GTK+ that is contained in the PRPLs
437 directory out of there. If you submit a patch that adds GTK+ to those
438 directories it's very likely to be refused, unless if I'm in a good mood
439 and decide to relocate things for you. That's not likely.
440
441 You're probably wondering how you can do certain things without GTK+. Well,
442 you're just going to have to make do. Rely on the UI, that's why it's
443 there. A PRPL should have absolutely ZERO interaction with the user, it
444 should all be handled by the UI.
445
446 Don't use the _options variables at all. The core should take care of all
447 of that. There are several proto_opt fields that you can use on a per-user
448 basis. Check out existing protocols for more details.