Updated with new dc_connect.
[doldaconnect.git] / clients / gaim / gaim-dolcon.c
index 2cc3731..5aad9ae 100644 (file)
@@ -1,3 +1,22 @@
+/*
+ *  Dolda Connect - Modular multiuser Direct Connect-style client
+ *  Copyright (C) 2005 Fredrik Tolf (fredrik@dolda2000.com)
+ *  
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *  
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *  
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*/
+
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
@@ -74,11 +93,14 @@ static void newpeercb(struct dc_fnetpeer *peer)
 {
     struct conndata *data;
     GaimConversation *conv;
+    char *buf;
     
     data = peer->fn->udata;
     if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
     {
-       gaim_conv_chat_add_user(GAIM_CONV_CHAT(conv), icswcstombs(peer->nick, "UTF-8", NULL), NULL, GAIM_CBFLAGS_NONE, FALSE);
+       buf = sprintf2("%s", icswcstombs(peer->nick, "UTF-8", NULL));
+       gaim_conv_chat_add_user(GAIM_CONV_CHAT(conv), buf, NULL, GAIM_CBFLAGS_NONE, TRUE);
+       free(buf);
     }
 }
 
@@ -86,11 +108,14 @@ static void delpeercb(struct dc_fnetpeer *peer)
 {
     struct conndata *data;
     GaimConversation *conv;
+    char *buf;
     
     data = peer->fn->udata;
     if((conv = gaim_find_chat(data->gc, peer->fn->id)) != NULL)
     {
-       gaim_conv_chat_remove_user(GAIM_CONV_CHAT(conv), icswcstombs(peer->nick, "UTF-8", NULL), NULL);
+       buf = sprintf2("%s", icswcstombs(peer->nick, "UTF-8", NULL));
+       gaim_conv_chat_remove_user(GAIM_CONV_CHAT(conv), buf, NULL);
+       free(buf);
     }
 }
 
@@ -125,7 +150,6 @@ static void logincb(int err, wchar_t *reason, struct conndata *data)
        return;
     }
     gaim_connection_set_state(data->gc, GAIM_CONNECTED);
-    serv_finish_login(data->gc);
     dc_queuecmd(NULL, NULL, L"notify", L"fn:chat", L"on", L"fn:act", L"on", L"fn:peer", L"on", NULL);
     dc_getfnlistasync((void (*)(int, void *))getfnlistcb, data);
 }
@@ -169,15 +193,15 @@ static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition)
                            if((conv = gaim_find_chat(data->gc, fn->id)) != NULL)
                            {
                                peer = icwcstombs(ires->argv[3].val.str, "UTF-8");
-                               msg = gaim_escape_html(icswcstombs(ires->argv[4].val.str, "UTF-8", NULL));
+                               msg = g_markup_escape_text(icswcstombs(ires->argv[4].val.str, "UTF-8", NULL), -1);
                                serv_got_chat_in(data->gc, gaim_conv_chat_get_id(GAIM_CONV_CHAT(conv)), peer, 0, msg, time(NULL));
                                g_free(msg);
                                free(peer);
                            }
                        } else {
-                           peer = icwcstombs(ires->argv[3].val.str, "UTF-8");
-                           msg = gaim_escape_html(icswcstombs(ires->argv[4].val.str, "UTF-8", NULL));
-                           if(!gaim_account_get_bool(data->gc->account, "represspm", FALSE) || (gaim_find_conversation_with_account(peer, data->gc->account) != NULL))
+                           peer = sprintf2("%i:%s", fn->id, icswcstombs(ires->argv[3].val.str, "UTF-8", NULL));
+                           msg = g_markup_escape_text(icswcstombs(ires->argv[4].val.str, "UTF-8", NULL), -1);
+                           if(!gaim_account_get_bool(data->gc->account, "represspm", FALSE) || (gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, peer, data->gc->account) != NULL))
                                serv_got_im(data->gc, peer, msg, 0, time(NULL));
                            g_free(msg);
                            free(peer);
@@ -189,6 +213,7 @@ static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition)
            case 601:
            case 602:
            case 603:
+               break;
            case 604:
                if((ires = dc_interpret(resp)) != NULL)
                {
@@ -201,15 +226,17 @@ static void dcfdcb(struct conndata *data, int fd, GaimInputCondition condition)
                    }
                    dc_freeires(ires);
                }
+               break;
            case 605:
                break;
            }
        }
+       dc_freeresp(resp);
     }
     updatewrite(data);
 }
 
-static int gi_sendchat(GaimConnection *gc, int id, const char *what)
+static int gi_sendchat(GaimConnection *gc, int id, const char *what, GaimMessageFlags flags)
 {
     struct conndata *data;
     struct dc_fnetnode *fn;
@@ -227,32 +254,32 @@ static int gi_sendchat(GaimConnection *gc, int id, const char *what)
     return(0);
 }
 
-static int gi_sendim(GaimConnection *gc, const char *who, const char *what, GaimConvImFlags flags)
+static int gi_sendim(GaimConnection *gc, const char *who, const char *what, GaimMessageFlags flags)
 {
     struct conndata *data;
-    struct dc_fnetnode *fn, *tfn;
-    struct dc_fnetpeer *peer, *tpeer;
-    wchar_t *wwho, *wwhat;
-    int en;
+    struct dc_fnetnode *fn;
+    struct dc_fnetpeer *peer;
+    wchar_t *wwho, *wwhat, *p;
+    int en, id;
     
     data = gc->proto_data;
     if((wwho = icmbstowcs((char *)who, "UTF-8")) == NULL)
        return(-errno);
-    tpeer = NULL;
-    for(fn = dc_fnetnodes; fn != NULL; fn = fn->next) {
-       for(peer = fn->peers; peer != NULL; peer = peer->next) {
-           if(!wcscmp(wwho, peer->nick)) {
-               if(tpeer == NULL) {
-                   tpeer = peer;
-                   tfn = fn;
-               } else {
-                   free(wwho);
-                   return(-ESRCH);
-               }
-           }
-       }
+    if((p = wcschr(wwho, L':')) == NULL) {
+       free(wwho);
+       return(-ESRCH);
     }
-    if(tpeer == NULL) {
+    *(p++) = L'\0';
+    id = wcstol(wwho, NULL, 10);
+    if((fn = dc_findfnetnode(id)) == NULL) {
+       free(wwho);
+       return(-ESRCH);
+    }
+    for(peer = fn->peers; peer != NULL; peer = peer->next) {
+       if(!wcscmp(peer->nick, p))
+           break;
+    }
+    if(peer == NULL) {
        free(wwho);
        return(-ESRCH);
     }
@@ -261,7 +288,7 @@ static int gi_sendim(GaimConnection *gc, const char *who, const char *what, Gaim
        free(wwho);
        return(-en);
     }
-    dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", tfn->id, L"0", L"%%ls", wwho, L"%%ls", wwhat, NULL);
+    dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", fn->id, L"0", L"%%ls", peer->nick, L"%%ls", wwhat, NULL);
     free(wwho);
     free(wwhat);
     updatewrite(data);
@@ -273,6 +300,32 @@ static const char *gi_listicon(GaimAccount *a, GaimBuddy *b)
     return("dolcon");
 }
 
+static char *gi_statustext(GaimBuddy *b)
+{
+    GaimPresence *p;
+
+    p = gaim_buddy_get_presence(b);
+    if (gaim_presence_is_online(p) && !gaim_presence_is_available(p))
+       return(g_strdup("Away"));
+    return(NULL);
+}
+
+static void gi_tiptext(GaimBuddy *b, GString *buf, gboolean full)
+{
+    /* Nothing for now */
+}
+
+static GList *gi_statustypes(GaimAccount *act)
+{
+    GList *ret;
+    
+    ret = NULL;
+    ret = g_list_append(ret, gaim_status_type_new(GAIM_STATUS_AVAILABLE, "avail", NULL, TRUE));
+    ret = g_list_append(ret, gaim_status_type_new(GAIM_STATUS_AWAY, "away", NULL, TRUE)); /* Coming up in ADC */
+    ret = g_list_append(ret, gaim_status_type_new(GAIM_STATUS_OFFLINE, "offline", NULL, TRUE));
+    return(ret);
+}
+
 static struct conndata *newconndata(void)
 {
     struct conndata *new;
@@ -312,7 +365,7 @@ static void gi_login(GaimAccount *act)
        return;
     }
     gaim_connection_update_progress(gc, "Connecting", 1, 3);
-    if((data->fd = dc_connect((char *)gaim_account_get_string(act, "server", "localhost"), -1)) < 0)
+    if((data->fd = dc_connect((char *)gaim_account_get_string(act, "server", "localhost"))) < 0)
     {
        gaim_connection_error(gc, "Could not connect to server");
        return;
@@ -376,39 +429,46 @@ static void gi_cancelgetlist(GaimRoomlist *rl)
     }
 }
 
-static void getpeerlistcb(struct dc_fnetnode *fn, int resp, struct conndata *data)
-{
-    GaimConversation *conv;
-    struct dc_fnetpeer *peer;
-    
-    if(resp == 200)
-    {
-       if(gaim_find_chat(data->gc, fn->id) != NULL)
-           return;
-       conv = serv_got_joined_chat(data->gc, fn->id, icswcstombs(fn->name, "UTF-8", NULL));
-       for(peer = fn->peers; peer != NULL; peer = peer->next)
-           gaim_conv_chat_add_user(GAIM_CONV_CHAT(conv), icswcstombs(peer->nick, "UTF-8", NULL), NULL, GAIM_CBFLAGS_NONE, FALSE);
-    }
-}
-
 static void gi_joinchat(GaimConnection *gc, GHashTable *chatdata)
 {
     struct conndata *data;
     struct dc_fnetnode *fn;
+    GaimConversation *conv;
+    struct dc_fnetpeer *peer;
+    char *buf;
+    GList *ul, *fl, *c;
     
     data = gc->proto_data;
     if((fn = dc_findfnetnode(GPOINTER_TO_INT(g_hash_table_lookup(chatdata, "id")))) == NULL)
        return;
     if(gaim_find_chat(gc, fn->id) != NULL)
        return;
-    dc_getpeerlistasync(fn, (void (*)(struct dc_fnetnode *, int, void *))getpeerlistcb, data);
-    updatewrite(data);
+    conv = serv_got_joined_chat(data->gc, fn->id, icswcstombs(fn->name, "UTF-8", NULL));
+    ul = fl = NULL;
+    for(peer = fn->peers; peer != NULL; peer = peer->next) {
+       buf = icwcstombs(peer->nick, "UTF-8");
+       ul = g_list_append(ul, buf);
+       fl = g_list_append(fl, GINT_TO_POINTER(0));
+    }
+    gaim_conv_chat_add_users(GAIM_CONV_CHAT(conv), ul, NULL, fl, FALSE);
+    for(c = ul; c != NULL; c = c->next)
+       free(c->data);
+    g_list_free(ul);
+    g_list_free(fl);
+}
+
+static char *gi_cbname(GaimConnection *gc, int id, const char *who)
+{
+    return(g_strdup_printf("%i:%s", id, who));
 }
 
 static GaimPluginProtocolInfo protinfo = {
     .options           = OPT_PROTO_PASSWORD_OPTIONAL,
     .icon_spec         = NO_BUDDY_ICONS,
     .list_icon         = gi_listicon,
+    .status_text       = gi_statustext,
+    .tooltip_text      = gi_tiptext,
+    .status_types      = gi_statustypes,
     .login             = gi_login,
     .close             = gi_close,
     .roomlist_get_list = gi_getlist,
@@ -416,6 +476,7 @@ static GaimPluginProtocolInfo protinfo = {
     .join_chat         = gi_joinchat,
     .chat_send         = gi_sendchat,
     .send_im           = gi_sendim,
+    .get_cb_real_name  = gi_cbname,
 };
 
 static GaimPluginInfo info = {
@@ -439,7 +500,7 @@ static void init(GaimPlugin *pl)
     GaimAccountOption *opt;
     
     dc_init();
-    opt = gaim_account_option_string_new("Server", "server", "localhost");
+    opt = gaim_account_option_string_new("Server", "server", "");
     protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);
     opt = gaim_account_option_int_new("Port", "port", -1);
     protinfo.protocol_options = g_list_append(protinfo.protocol_options, opt);