]> code.citadel.org Git - citadel.git/blobdiff - gaim-citadel/citadel.lua
* Move to GPL v3
[citadel.git] / gaim-citadel / citadel.lua
index 71b3110d559f0969e2f914203ffb0af9b43c07de..cfbe2b04ceb83369d3e19a32fb7495f9ed0c8907 100644 (file)
@@ -2,10 +2,10 @@
 -- Gaim Citadel plugin.
 --
 -- © 2006 David Given.
--- This code is licensed under the GPL v2. See the file COPYING in this
+-- This code is licensed under the GPL v3. See the file COPYING in this
 -- directory for the full license text.
 --
--- $Id: auth.c 4258 2006-01-29 13:34:44 +0000 (Sun, 29 Jan 2006) dothebart $
+-- $Id:citadel.lua 4326 2006-02-18 12:26:22Z hjalfi $
 
 -----------------------------------------------------------------------------
 --                                 GLOBALS                                 --
 
 local _
 local username, servername, port
+local serverfriendlyname
 local ga, gc
 local fd, gsc
 local timerhandle
+local noblist
 local buddies = {}
 
 -----------------------------------------------------------------------------
 --                                CONSTANTS                                --
 -----------------------------------------------------------------------------
 
+-- Our version number. Remember to update!
+
+local VERSION_NUMBER = "0.3.1"
+
 -- Special values returned as Citadel's response codes.
 
 local LISTING_FOLLOWS         = 100
@@ -61,8 +67,9 @@ local ASYNC_GEXP              = 02
 
 local CITADEL_DEFAULT_PORT    = 504
 local CITADEL_CONFIG_ROOM     = "My Citadel Config"
+local WAITING_ROOM            = "Sent/Received Pages"
 local CITADEL_BUDDY_MSG       = "__ Buddy List __"
-local CITADEL_POLL_INTERVAL   = 5
+local CITADEL_POLL_INTERVAL   = 60
 
 -----------------------------------------------------------------------------
 --                                UTILITIES                                --
@@ -75,7 +82,8 @@ local function log(...)
        for _, i in ipairs(arg) do
                table.insert(s, tostring(i))
        end
-       print("citadel: lua: "..table.concat(s))
+       s = table.concat(s)
+       gaim_debug_info("citadel", string.gsub(s, "%%", "%%").."\n")
 end
 
 local function unexpectederror()
@@ -283,7 +291,7 @@ local function cant_save_buddy_list()
 end
 
 local function save_buddy_list()
-       writeline("GOTO "..CITADEL_CONFIG_ROOM)
+       writeline("GOTO "..CITADEL_CONFIG_ROOM.."||1")
        local m = get_response()
        if (m.response ~= CIT_OK) then
                cant_save_buddy_list()
@@ -343,7 +351,7 @@ local function save_buddy_list()
 
        -- Go back to the lobby.
        
-       writeline("GOTO _BASEROOM_")
+       writeline("GOTO "..WAITING_ROOM)
        get_response()
 end
 
@@ -358,10 +366,45 @@ local function update_buddy_status()
        local onlinebuddies = {}
        for _, s in ipairs(m.xargs) do
                local name = unpack_citadel_data_line(s)[2]
-               onlinebuddies[name] = true
+               if (name ~= "(not logged in)") then
+                       onlinebuddies[name] = true
+               end
+       end
+
+       -- Anyone who's not online is offline.
+
+       for s, _ in pairs(buddies) do
+               if not onlinebuddies[s] then
+                       serv_got_update(gc, s, false, 0, 0, 0, 0)
+               end
        end
 
+       -- Anyone who's online is, er, online.
+
        for s, _ in pairs(onlinebuddies) do
+               -- If we're in no-buddy-list mode and this buddy isn't on our
+               -- list, add them automatically.
+
+               if noblist then
+                       if not gaim_find_buddy(ga, s) then
+                               log("trying to add new buddy ", s)
+                               local buddy = gaim_buddy_new(ga, s, s)
+                               if buddy then
+                                       -- buddy is not garbage collected! This must succeed!
+                                       local group = gaim_find_group(serverfriendlyname)
+                                       if not group then
+                                               group = gaim_group_new(serverfriendlyname)
+                                               gaim_blist_add_group(group, nil)
+                                       end
+                                       
+                                       gaim_blist_add_buddy(buddy, nil, group, nil)
+                               else
+                                       warning("Unable to add "..s.." to your buddy list. This error should never happen.")
+                                       break
+                               end
+                       end
+               end
+
                serv_got_update(gc, s, true, 0, 0, 0, 0)
        end
 end
@@ -397,11 +440,13 @@ function citadel_connect(_ga)
        gc = gaim_account_get_connection(ga)
        
        queue(function()
-               local STEPS = 13
+               local STEPS = 14
 
                username = gaim_account_get_username(ga)
                _, _, username, servername = string.find(username, "^(.*)@(.*)$")
-               port = gaim_account_get_int(ga, "port", CITADEL_DEFAULT_PORT);
+               serverfriendlyname = servername
+               port = gaim_account_get_int(ga, "port", CITADEL_DEFAULT_PORT)
+               noblist = gaim_account_get_bool(ga, "no_blist", false)
                
                log("connect to ", username, " on server ", servername, " port ", port)
                
@@ -465,12 +510,19 @@ function citadel_connect(_ga)
                -- Tell Citadel who we are.
                
                gaim_connection_update_progress(gc, "Setting up", 6, STEPS)
-               writeline("IDEN 226|0|0.2|Gaim Citadel plugin|")
+               writeline("IDEN 226|0|"..VERSION_NUMBER.."|Gaim Citadel plugin|")
                m = get_response()
                
-               -- Set asynchronous mode.
+               -- Get information about the Citadel server.
                
                gaim_connection_update_progress(gc, "Setting up", 7, STEPS)
+               writeline("INFO")
+               m = get_response()
+               serverfriendlyname = m.xargs[3]
+               
+               -- Set asynchronous mode.
+               
+               gaim_connection_update_progress(gc, "Setting up", 8, STEPS)
                writeline("ASYN 1")
                m = get_response()
                if (m.response ~= CIT_OK) then
@@ -480,8 +532,8 @@ function citadel_connect(_ga)
                (function()
                        -- Switch to private configuration room.
 
-                       gaim_connection_update_progress(gc, "Setting up", 8, STEPS)
-                       writeline("GOTO "..CITADEL_CONFIG_ROOM)
+                       gaim_connection_update_progress(gc, "Setting up", 9, STEPS)
+                       writeline("GOTO "..CITADEL_CONFIG_ROOM.."||1")
                        m = get_response()
                        if (m.response ~= CIT_OK) then
                                warning("Unable to fetch buddy list from server.")
@@ -490,7 +542,7 @@ function citadel_connect(_ga)
 
                        -- Look for our preferences.
 
-                       gaim_connection_update_progress(gc, "Setting up", 9, STEPS)
+                       gaim_connection_update_progress(gc, "Setting up", 10, STEPS)
                        writeline("MSGS ALL|0|1")
                        m = get_response()
                        if (m.response ~= START_CHAT_MODE) then
@@ -516,7 +568,7 @@ function citadel_connect(_ga)
                                return
                        end
                        
-                       gaim_connection_update_progress(gc, "Setting up", 10, STEPS)
+                       gaim_connection_update_progress(gc, "Setting up", 11, STEPS)
                        writeline("MSG0 "..m)
                        while true do
                                local s = readline()
@@ -548,13 +600,13 @@ function citadel_connect(_ga)
 
                -- Update buddy list with who's online.
 
-               gaim_connection_update_progress(gc, "Setting up", 11, STEPS)
+               gaim_connection_update_progress(gc, "Setting up", 12, STEPS)
                update_buddy_status()
 
                -- Go back to the Lobby.
 
-               gaim_connection_update_progress(gc, "Setting up", 12, STEPS)
-               writeline("GOTO _BASEROOM_")
+               gaim_connection_update_progress(gc, "Setting up", 13, STEPS)
+               writeline("GOTO "..WAITING_ROOM)
                get_response()
 
                -- Switch on the timer.
@@ -564,7 +616,7 @@ function citadel_connect(_ga)
                        
                -- Done!
                
-               gaim_connection_update_progress(gc, "Connected", 13, STEPS)
+               gaim_connection_update_progress(gc, "Connected", 14, STEPS)
                gaim_connection_set_state(gc, GAIM_CONNECTED)
        end)
 end
@@ -621,13 +673,6 @@ function citadel_get_info(name)
        end)
 end
 
-function citadel_keepalive()
-       queue(function()
-               writeline("NOOP")
-               get_response()
-       end)
-end
-
 -----------------------------------------------------------------------------
 --                                BUDDY LIST                               --
 -----------------------------------------------------------------------------