[979] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

[D-O-H] r965 - in branches/barnowl_sqlite: . owl owl/perl/lib/BarnOwl owl/perl/modules/IRC/lib/BarnOwl/Module owl/perl/modules/IRC/lib/BarnOwl/Module/IRC owl/perl/modules/Jabber/lib/BarnOwl/Module owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber

daemon@ATHENA.MIT.EDU (nelhage@MIT.EDU)
Thu Oct 29 18:11:48 2009

Resent-From: nelhage@mit.edu
Resent-To: barnowl-dev-mtg@charon.mit.edu
X-Original-To: nelhage@nelhage.com
To: dirty-owl-hackers@mit.edu
From: nelhage@MIT.EDU
Reply-to: dirty-owl-hackers@MIT.EDU
Date: Sat, 23 Feb 2008 21:32:21 -0500 (EST)

Author: nelhage
Date: 2008-02-23 21:32:20 -0500 (Sat, 23 Feb 2008)
New Revision: 965

Added:
   branches/barnowl_sqlite/owl/select.c
Modified:
   branches/barnowl_sqlite/
   branches/barnowl_sqlite/owl/
   branches/barnowl_sqlite/owl/Makefile.in
   branches/barnowl_sqlite/owl/functions.c
   branches/barnowl_sqlite/owl/global.c
   branches/barnowl_sqlite/owl/keys.c
   branches/barnowl_sqlite/owl/message.c
   branches/barnowl_sqlite/owl/owl.c
   branches/barnowl_sqlite/owl/owl.h
   branches/barnowl_sqlite/owl/perl/lib/BarnOwl/ModuleLoader.pm
   branches/barnowl_sqlite/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC.pm
   branches/barnowl_sqlite/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm
   branches/barnowl_sqlite/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber.pm
   branches/barnowl_sqlite/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber/Connection.pm
   branches/barnowl_sqlite/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber/ConnectionManager.pm
   branches/barnowl_sqlite/owl/perlconfig.c
   branches/barnowl_sqlite/owl/perlglue.xs
   branches/barnowl_sqlite/owl/zephyr.c
Log:
Merged revisions 933-964 via svnmerge from 
svn+ssh://lunatique.mit.edu/mit/barnowl/src/svn/trunk

........
  r937 | nelhage | 2008-02-11 23:09:54 -0500 (Mon, 11 Feb 2008) | 2 lines
  
  Bind M-left and M-right by default in the editor
........
  r947 | nelhage | 2008-02-18 16:45:22 -0500 (Mon, 18 Feb 2008) | 2 lines
  
  We need to stick modules on the beginning of @INC, not the end
........
  r949 | asedeno | 2008-02-18 19:43:09 -0500 (Mon, 18 Feb 2008) | 1 line
  
  Merging in the select branch.
........
  r950 | asedeno | 2008-02-18 20:54:45 -0500 (Mon, 18 Feb 2008) | 1 line
  
  Merging in the select branch, part 2. select.c
........
  r954 | asedeno | 2008-02-19 01:22:48 -0500 (Tue, 19 Feb 2008) | 1 line
  
  Fixing AIM post select()
........
  r956 | geofft | 2008-02-19 04:55:35 -0500 (Tue, 19 Feb 2008) | 4 lines
  
  Fix a bug where an explicit (local) realm foiled
  owl_message_get_cc_without_recipient(). (per quentin)
........
  r957 | asedeno | 2008-02-19 18:10:18 -0500 (Tue, 19 Feb 2008) | 4 lines
  
  owl.c:
  * Input processing - deal with all remaining input before moving on.
  * Change how we get stdin's fileno -- actually grab it from the stdin handle.
........
  r960 | chmrr | 2008-02-19 20:48:44 -0500 (Tue, 19 Feb 2008) | 3 lines
  
   r2008@utwig:  chmrr | 2008-02-19 20:48:27 -0500
    * Fix admin messages
........
  r963 | asedeno | 2008-02-22 10:58:32 -0500 (Fri, 22 Feb 2008) | 1 line
  
  resize should always refresh the editwin.
........



Property changes on: branches/barnowl_sqlite
___________________________________________________________________
Name: svnmerge-integrated
   - /trunk:1-932
   + /trunk:1-964


Property changes on: branches/barnowl_sqlite/owl
___________________________________________________________________
Name: svk:merge
   - 06e3988a-d725-0410-af47-c5dd11e74598:/local/barnowl:1876
8baf6839-b125-0410-9df9-922793c80423:/local/barnowl:20981
8baf6839-b125-0410-9df9-922793c80423:/local/owl:15641
bb873fd7-8e23-0410-944a-99ec44c633eb:/local/d-o-h/branches/par:19594
fe09232e-8620-0410-8e36-e6b4839e121d:/branches/par:688
   + 06e3988a-d725-0410-af47-c5dd11e74598:/local/barnowl:2008
8baf6839-b125-0410-9df9-922793c80423:/local/barnowl:20981
8baf6839-b125-0410-9df9-922793c80423:/local/owl:15641
bb873fd7-8e23-0410-944a-99ec44c633eb:/local/d-o-h/branches/par:19594
fe09232e-8620-0410-8e36-e6b4839e121d:/branches/par:688

Modified: branches/barnowl_sqlite/owl/Makefile.in
===================================================================
--- branches/barnowl_sqlite/owl/Makefile.in	2008-02-22 15:59:41 UTC (rev 964)
+++ branches/barnowl_sqlite/owl/Makefile.in	2008-02-24 02:32:20 UTC (rev 965)
@@ -25,7 +25,7 @@
      regex.c history.c view.c dict.c variable.c filterelement.c pair.c \
      keypress.c keymap.c keybinding.c cmd.c context.c zcrypt.c \
      aim.c buddy.c buddylist.c timer.c style.c stylefunc.c errqueue.c \
-     zbuddylist.c muxevents.c popexec.c obarray.c
+     zbuddylist.c muxevents.c popexec.c obarray.c select.c
 OWL_SRC = owl.c
 TESTER_SRC = tester.c
 

Modified: branches/barnowl_sqlite/owl/functions.c
===================================================================
--- branches/barnowl_sqlite/owl/functions.c	2008-02-22 15:59:41 UTC (rev 964)
+++ branches/barnowl_sqlite/owl/functions.c	2008-02-24 02:32:20 UTC (rev 965)
@@ -776,7 +776,7 @@
   owl_view_iterator_init_end(&it, v);
   owl_global_set_curmsg(&g, &it);
   owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
-  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
+  /* owl_mainwin_redisplay(owl_global_get_mainwin(&g)); */
   owl_global_set_direction_downwards(&g);
 }
 

Modified: branches/barnowl_sqlite/owl/global.c
===================================================================
--- branches/barnowl_sqlite/owl/global.c	2008-02-22 15:59:41 UTC (rev 964)
+++ branches/barnowl_sqlite/owl/global.c	2008-02-24 02:32:20 UTC (rev 965)
@@ -114,6 +114,7 @@
   owl_obarray_init(&(g->obarray));
 
   owl_message_init_fmtext_cache();
+  owl_list_create(&(g->dispatchlist));
 }
 
 void _owl_global_setup_windows(owl_global *g) {
@@ -444,10 +445,8 @@
   g->needrefresh=1;
   owl_mainwin_redisplay(&(g->mw));
   sepbar(NULL);
+  owl_editwin_redisplay(&(g->tw), 0);
 
-  if (owl_global_is_typwin_active(g)) {
-    owl_editwin_redisplay(&(g->tw), 0);
-  }	
   /* TODO: this should handle other forms of popwins */
   if (owl_popwin_is_active(owl_global_get_popwin(g)) 
       && owl_global_get_viewwin(g)) {
@@ -939,3 +938,8 @@
 {
   return owl_obarray_insert(&(g->obarray), string);
 }
+
+owl_list *owl_global_get_dispatchlist(owl_global *g)
+{
+  return &(g->dispatchlist);
+}

Modified: branches/barnowl_sqlite/owl/keys.c
===================================================================
--- branches/barnowl_sqlite/owl/keys.c	2008-02-22 15:59:41 UTC (rev 964)
+++ branches/barnowl_sqlite/owl/keys.c	2008-02-24 02:32:20 UTC (rev 965)
@@ -43,8 +43,10 @@
 
   BIND_CMD("M-f",         "edit:move-next-word", "");
   BIND_CMD("M-O 3 C",     "edit:move-next-word", "");
+  BIND_CMD("M-LEFT",      "edit:move-next-word", "");
   BIND_CMD("M-b",         "edit:move-prev-word", "");
   BIND_CMD("M-O 3 D",     "edit:move-prev-word", "");
+  BIND_CMD("M-RIGHT",     "edit:move-prev-word", "");
 
   BIND_CMD("LEFT",        "edit:move-left", "");
   BIND_CMD("C-b",         "edit:move-left", "");

Modified: branches/barnowl_sqlite/owl/message.c
===================================================================
--- branches/barnowl_sqlite/owl/message.c	2008-02-22 15:59:41 UTC (rev 964)
+++ branches/barnowl_sqlite/owl/message.c	2008-02-24 02:32:20 UTC (rev 965)
@@ -571,7 +571,7 @@
 /* caller must free return value */
 char *owl_message_get_cc_without_recipient(owl_message *m)
 {
-  char *cc, *out, *end, *user, *recip;
+  char *cc, *out, *end, *user, *shortuser, *recip;
 
   cc = owl_message_get_cc(m);
   if (cc == NULL)
@@ -583,11 +583,13 @@
 
   user = strtok(cc, " ");
   while (user != NULL) {
-    if (strcasecmp(user, recip) != 0) {
+    shortuser = short_zuser(user);
+    if (strcasecmp(shortuser, recip) != 0) {
       strcpy(end, user);
       end[strlen(user)] = ' ';
       end += strlen(user) + 1;
     }
+    free(shortuser);
     user = strtok(NULL, " ");
   }
   end[0] = '\0';

Modified: branches/barnowl_sqlite/owl/owl.c
===================================================================
--- branches/barnowl_sqlite/owl/owl.c	2008-02-22 15:59:41 UTC (rev 964)
+++ branches/barnowl_sqlite/owl/owl.c	2008-02-24 02:32:20 UTC (rev 965)
@@ -45,6 +45,7 @@
 #include <time.h>
 #include <sys/param.h>
 #include <sys/types.h>
+#include <sys/time.h>
 #include <termios.h>
 #include <sys/stat.h>
 #include "owl.h"
@@ -68,7 +69,7 @@
   WINDOW *recwin, *sepwin, *typwin, *msgwin;
   owl_editwin *tw;
   owl_popwin *pw;
-  int j, ret, initialsubs, debug, argcsave, followlast;
+  int ret, initialsubs, debug, argcsave, followlast;
   int newmsgs, nexttimediff;
   struct sigaction sigact;
   char *configfile, *tty, *perlout, *perlerr, **argvsave, buff[LINE], startupmsg[LINE];
@@ -205,11 +206,27 @@
   owl_global_set_startupargs(&g, argcsave, argvsave);
   owl_global_set_haveaim(&g);
 
+  /* prepare stdin dispatch */
+  {
+    owl_dispatch *d = owl_malloc(sizeof(owl_dispatch));
+    d->fd = fileno(stdin);
+    d->cfunc = &owl_process_input;
+    d->pfunc = NULL;
+    owl_select_add_dispatch(d);
+  }
+  
 #ifdef HAVE_LIBZEPHYR
   /* zephyr init */
   ret=owl_zephyr_initialize();
-  if (!ret)
-      owl_global_set_havezephyr(&g);
+  if (!ret) {
+    owl_dispatch *d = owl_malloc(sizeof(owl_dispatch));
+    d->fd = ZGetFD();
+    d->cfunc = &owl_zephyr_process_events;
+    d->pfunc = NULL;
+    owl_select_add_dispatch(d);
+    owl_global_set_havezephyr(&g);
+  }
+
 #endif
 
 #if OWL_STDERR_REDIR
@@ -433,18 +450,6 @@
 
     followlast=owl_global_should_followlast(&g);
     
-    /* Do AIM stuff */
-    if (owl_global_is_doaimevents(&g)) {
-      owl_aim_process_events();
-
-      if (owl_global_is_aimloggedin(&g)) {
-	if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) {
-	  /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */
-	  owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g));
-	}
-      }
-    }
-
     owl_perlconfig_mainloop();
 
     /* little hack */
@@ -462,8 +467,6 @@
       }
     }
 
-    owl_zephyr_process_events();
-    
     /* Grab incoming messages. */
     newmsgs=0;
     while(owl_global_messagequeue_pending(&g)) {
@@ -531,6 +534,7 @@
       } else {
 	owl_function_set_cursor(sepwin);
       }
+      owl_function_debugmsg("owl.c -- doupdate()");
       doupdate();
       owl_global_set_noneedrefresh(&g);
     }
@@ -538,40 +542,10 @@
     /* Handle all keypresses.  If no key has been pressed, sleep for a
      * little bit, but otherwise do not.  This lets input be grabbed
      * as quickly as possbile */
-    j=wgetch(typwin);
-    if (j==ERR) {
-      usleep(10000);
-    } else {
-      owl_global_set_lastinputtime(&g, now);
-      /* find and activate the current keymap.
-       * TODO: this should really get fixed by activating
-       * keymaps as we switch between windows... 
-       */
-      if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
-	owl_context_set_popless(owl_global_get_context(&g), 
-				owl_global_get_viewwin(&g));
-	owl_function_activate_keymap("popless");
-      } else if (owl_global_is_typwin_active(&g) 
-		 && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) {
-	/*
-	  owl_context_set_editline(owl_global_get_context(&g), tw);
-	  owl_function_activate_keymap("editline");
-	*/
-      } else if (owl_global_is_typwin_active(&g) 
-		 && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) {
-	owl_context_set_editmulti(owl_global_get_context(&g), tw);
-	owl_function_activate_keymap("editmulti");
-      } else {
-	owl_context_set_recv(owl_global_get_context(&g));
-	owl_function_activate_keymap("recv");
-      }
-      /* now actually handle the keypress */
-      ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
-      if (ret!=0 && ret!=1) {
-	owl_function_makemsg("Unable to handle keypress");
-      }
-    }
 
+    /* select on FDs we know about. */
+    owl_select();
+
     /* Log any error signals */
     {
       siginfo_t si;
@@ -690,6 +664,64 @@
   return 1;
 }
 
+void owl_process_aim()
+{
+  if (owl_global_is_doaimevents(&g)) {
+    owl_aim_process_events();
+    
+    if (owl_global_is_aimloggedin(&g)) {
+      if (owl_timer_is_expired(owl_global_get_aim_buddyinfo_timer(&g))) {
+        /* owl_buddylist_request_idletimes(owl_global_get_buddylist(&g)); */
+        owl_timer_reset(owl_global_get_aim_buddyinfo_timer(&g));
+      }
+    }
+  }
+}
+
+void owl_process_input()
+{
+  int ret, j;
+  owl_popwin *pw;
+  owl_editwin *tw;
+
+  while (1) {
+    j = wgetch(owl_global_get_curs_typwin(&g));
+    if (j == ERR) return;
+
+    owl_global_set_lastinputtime(&g, time(NULL));
+    pw=owl_global_get_popwin(&g);
+    tw=owl_global_get_typwin(&g);
+    
+    /* find and activate the current keymap.
+     * TODO: this should really get fixed by activating
+     * keymaps as we switch between windows... 
+     */
+    if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) {
+      owl_context_set_popless(owl_global_get_context(&g), 
+                              owl_global_get_viewwin(&g));
+      owl_function_activate_keymap("popless");
+    } else if (owl_global_is_typwin_active(&g) 
+               && owl_editwin_get_style(tw) == OWL_EDITWIN_STYLE_ONELINE) {
+      /*
+        owl_context_set_editline(owl_global_get_context(&g), tw);
+        owl_function_activate_keymap("editline");
+      */
+    } else if (owl_global_is_typwin_active(&g) 
+               && owl_editwin_get_style(tw) == OWL_EDITWIN_STYLE_MULTILINE) {
+      owl_context_set_editmulti(owl_global_get_context(&g), tw);
+      owl_function_activate_keymap("editmulti");
+    } else {
+      owl_context_set_recv(owl_global_get_context(&g));
+      owl_function_activate_keymap("recv");
+    }
+    /* now actually handle the keypress */
+    ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
+    if (ret != 0 && ret != 1) {
+      owl_function_makemsg("Unable to handle keypress");
+    }
+  }
+}
+
 void sig_handler(int sig, siginfo_t *si, void *data)
 {
   if (sig==SIGWINCH) {
@@ -704,7 +736,6 @@
   } else if (sig==SIGTERM || sig==SIGHUP) {
     owl_function_quit();
   }
-
 }
 
 void usage()

Modified: branches/barnowl_sqlite/owl/owl.h
===================================================================
--- branches/barnowl_sqlite/owl/owl.h	2008-02-22 15:59:41 UTC (rev 964)
+++ branches/barnowl_sqlite/owl/owl.h	2008-02-24 02:32:20 UTC (rev 965)
@@ -532,6 +532,12 @@
   owl_list strings;
 } owl_obarray;
 
+typedef struct _owl_dispatch {
+  int fd;           /* FD to watch for dispatch. */
+  void (*cfunc)();  /* C function to dispatch to. */
+  SV *pfunc;        /* Perl function to dispatch to. */
+} owl_dispatch;
+
 typedef struct _owl_global {
   owl_mainwin mw;
   owl_popwin pw;
@@ -602,6 +608,7 @@
   owl_timer zephyr_buddycheck_timer;
   struct termios startup_tio;
   owl_obarray obarray;
+  owl_list dispatchlist;
 } owl_global;
 
 /* globals */

Modified: branches/barnowl_sqlite/owl/perl/lib/BarnOwl/ModuleLoader.pm
===================================================================
--- branches/barnowl_sqlite/owl/perl/lib/BarnOwl/ModuleLoader.pm	2008-02-22 15:59:41 UTC (rev 964)
+++ branches/barnowl_sqlite/owl/perl/lib/BarnOwl/ModuleLoader.pm	2008-02-24 02:32:20 UTC (rev 965)
@@ -23,7 +23,7 @@
             if(-f "$dir/$f" && $f =~ /^(.+)\.par$/) {
                 $modules{$1} = 1;
             } elsif(-d "$dir/$f" && -d "$dir/$f/lib") {
-                push @INC, "$dir/$f/lib" unless grep m{^$dir/$f/lib$}, @INC;
+                unshift @INC, "$dir/$f/lib" unless grep m{^$dir/$f/lib$}, @INC;
                 $modules{$f} = 1;
             }
         }

Modified: branches/barnowl_sqlite/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm
===================================================================
--- branches/barnowl_sqlite/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm	2008-02-22 15:59:41 UTC (rev 964)
+++ branches/barnowl_sqlite/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm	2008-02-24 02:32:20 UTC (rev 965)
@@ -68,6 +68,12 @@
     return $self;
 }
 
+sub getSocket
+{
+    my $self = shift;
+    return $self->conn->socket;
+}
+
 ################################################################################
 ############################### IRC callbacks ##################################
 ################################################################################
@@ -118,7 +124,7 @@
     my ($self, $evt) = @_;
     BarnOwl::admin_message("IRC",
             BarnOwl::Style::boldify('IRC ' . $evt->type . ' message from '
-                . $evt->alias) . "\n"
+                . $self->alias) . "\n"
             . strip_irc_formatting(join '\n', cdr $evt->args));
 }
 
@@ -167,7 +173,7 @@
 sub on_disconnect {
     my $self = shift;
     delete $BarnOwl::Module::IRC::ircnets{$self->alias};
-
+    BarnOwl::remove_dispatch($self->{FD});
     BarnOwl::admin_message('IRC',
                            "[" . $self->alias . "] Disconnected from server");
 }

Modified: branches/barnowl_sqlite/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC.pm
===================================================================
--- branches/barnowl_sqlite/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC.pm	2008-02-22 15:59:41 UTC (rev 964)
+++ branches/barnowl_sqlite/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC.pm	2008-02-24 02:32:20 UTC (rev 965)
@@ -68,7 +68,15 @@
     }
 }
 
-sub mainloop_hook {
+#sub mainloop_hook {
+#    return unless defined $irc;
+#    eval {
+#        $irc->do_one_loop();
+#    };
+#    return;
+#}
+
+sub OwlProcess {
     return unless defined $irc;
     eval {
         $irc->do_one_loop();
@@ -76,6 +84,7 @@
     return;
 }
 
+
 sub register_handlers {
     if(!$irc) {
         $irc = Net::IRC->new;
@@ -108,7 +117,7 @@
 
 $BarnOwl::Hooks::startup->add(\&startup);
 $BarnOwl::Hooks::shutdown->add(\&shutdown);
-$BarnOwl::Hooks::mainLoop->add(\&mainloop_hook);
+#$BarnOwl::Hooks::mainLoop->add(\&mainloop_hook);
 
 ################################################################################
 ######################## Owl command handlers ##################################
@@ -164,6 +173,9 @@
     if ($conn->conn->connected) {
         BarnOwl::admin_message("IRC", "Connected to $alias as $nick");
         $ircnets{$alias} = $conn;
+        my $fd = $conn->getSocket()->fileno();
+        BarnOwl::add_dispatch($fd, \&OwlProcess);
+        $conn->{FD} = $fd;
     } else {
         die("IRC::Connection->connect failed: $!");
     }

Modified: branches/barnowl_sqlite/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber/Connection.pm
===================================================================
--- branches/barnowl_sqlite/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber/Connection.pm	2008-02-22 15:59:41 UTC (rev 964)
+++ branches/barnowl_sqlite/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber/Connection.pm	2008-02-24 02:32:20 UTC (rev 965)
@@ -109,6 +109,45 @@
 }
 
 
+=head2 getSID
+
+Returns the StreamID for this connection.
+
+=cut
+
+sub getStreamID {
+    my $self = shift;
+    return $self->{SESSION}->{id} || "";
+}
+
+=head2 getSocket
+
+Returns the IO::Socket for this connection.
+
+=cut
+
+sub getSocket {
+    my $self = shift;
+    my $sid = getStreamID($self);
+    return $self->{STREAM}->GetSock($sid) || -1;
+}
+
+=head2 OwlProcess
+
+Non-blocking connection processing. For use in a select loop.
+
+=cut
+
+sub OwlProcess {
+    my $self = shift;
+    my $status = $self->Process(0);
+    if ( !defined($status) ) {
+        my $jid = $self->{SESSION}->{FULLJID};
+        BarnOwl::error("Jabber account $jid disconnected!");
+        BarnOwl::Module::Jabber::do_logout($jid);
+    }
+}
+
 =head1 SEE ALSO
 
 L<Net::Jabber::Client>, L<BarnOwl::Module::Jabber>

Modified: branches/barnowl_sqlite/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber/ConnectionManager.pm
===================================================================
--- branches/barnowl_sqlite/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber/ConnectionManager.pm	2008-02-22 15:59:41 UTC (rev 964)
+++ branches/barnowl_sqlite/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber/ConnectionManager.pm	2008-02-24 02:32:20 UTC (rev 965)
@@ -36,6 +36,7 @@
     my $jidStr = shift;
     return 0 unless exists $self->{$jidStr};
 
+    BarnOwl::remove_dispatch($self->{$jidStr}->{Client}->{fileno}) if $self->{$jidStr}->{Client}->{fileno};
     $self->{$jidStr}->{Client}->Disconnect()
       if $self->{$jidStr}->{Client};
     delete $self->{$jidStr};
@@ -50,7 +51,7 @@
     return 0 unless exists $self->{$oldJidStr};
     return 0 if $oldJidStr eq $newJidStr;
 
-    $self->{$newJidStr} = $self->{$oldJidStr}; 
+    $self->{$newJidStr} = $self->{$oldJidStr};
     delete $self->{$oldJidStr};
     return 1;
 }

Modified: branches/barnowl_sqlite/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber.pm
===================================================================
--- branches/barnowl_sqlite/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber.pm	2008-02-22 15:59:41 UTC (rev 964)
+++ branches/barnowl_sqlite/owl/perl/modules/Jabber/lib/BarnOwl/Module/Jabber.pm	2008-02-24 02:32:20 UTC (rev 965)
@@ -115,7 +115,7 @@
             $conn->removeConnection($jid);
             BarnOwl::error("Connection for $jid undefined -- error in reload?");
         }
-
+        # We keep this in the mainloop hook for keep-alives
         my $status = $client->Process(0);
         if ( !defined($status) ) {
             BarnOwl::error("Jabber account $jid disconnected!");
@@ -123,8 +123,9 @@
         }
         if ($::shutdown) {
             do_logout($jid);
-            return;
+            next;
         }
+
         if ($vars{status_changed}) {
             my $p = new Net::Jabber::Presence;
             $p->SetShow($vars{show}) if $vars{show};
@@ -386,6 +387,11 @@
 		my $fullJid = $client->{SESSION}->{FULLJID} || $jidStr;
 		$conn->renameConnection($jidStr, $fullJid);
                 queue_admin_msg("Connected to jabber as $fullJid");
+                # The remove_dispatch() method is called from the
+                # ConnectionManager's removeConnection() method.
+                $client->{fileno} = $client->getSocket()->fileno();
+                #queue_admin_msg("Connected to jabber as $fullJid ($client->{fileno})");
+                BarnOwl::add_dispatch($client->{fileno}, sub { $client->OwlProcess() });
             }
         }
     }

Modified: branches/barnowl_sqlite/owl/perlconfig.c
===================================================================
--- branches/barnowl_sqlite/owl/perlconfig.c	2008-02-22 15:59:41 UTC (rev 964)
+++ branches/barnowl_sqlite/owl/perlconfig.c	2008-02-24 02:32:20 UTC (rev 965)
@@ -340,6 +340,11 @@
   SvREFCNT_dec(cmd->cmd_perl);
 }
 
+void owl_perlconfig_dispatch_free(owl_dispatch *d)
+{
+  SvREFCNT_dec(d->pfunc);
+}
+
 void owl_perlconfig_edit_callback(owl_editwin *e)
 {
   SV *cb = (SV*)(e->cbdata);
@@ -407,3 +412,28 @@
   FREETMPS;
   LEAVE;
 }
+
+void owl_perlconfig_do_dispatch(owl_dispatch *d)
+{
+  SV *cb = d->pfunc;
+  unsigned int n_a;
+  dSP;
+  if(cb == NULL) {
+    owl_function_error("Perl callback is NULL!");
+  }
+
+  ENTER;
+  SAVETMPS;
+
+  PUSHMARK(SP);
+  PUTBACK;
+  
+  call_sv(cb, G_DISCARD|G_KEEPERR|G_EVAL);
+
+  if(SvTRUE(ERRSV)) {
+    owl_function_error("%s", SvPV(ERRSV, n_a));
+  }
+
+  FREETMPS;
+  LEAVE;
+}

Modified: branches/barnowl_sqlite/owl/perlglue.xs
===================================================================
--- branches/barnowl_sqlite/owl/perlglue.xs	2008-02-22 15:59:41 UTC (rev 964)
+++ branches/barnowl_sqlite/owl/perlglue.xs	2008-02-24 02:32:20 UTC (rev 965)
@@ -358,3 +358,18 @@
 		RETVAL = newSVsv(owl_global_get_msglist(&g));
 	OUTPUT:
 		RETVAL
+
+void
+add_dispatch(fd, cb)
+	int fd
+	SV * cb
+	CODE:
+        SvREFCNT_inc(cb);
+	owl_select_add_perl_dispatch(fd, cb);
+
+void
+remove_dispatch(fd)
+	int fd
+	CODE:
+	owl_select_remove_perl_dispatch(fd);
+

Copied: branches/barnowl_sqlite/owl/select.c (from rev 963, trunk/owl/select.c)
===================================================================
--- branches/barnowl_sqlite/owl/select.c	                        (rev 0)
+++ branches/barnowl_sqlite/owl/select.c	2008-02-24 02:32:20 UTC (rev 965)
@@ -0,0 +1,236 @@
+#include "owl.h"
+
+static const char fileIdent[] = "$Id: select.c 894 2008-01-17 07:13:44Z asedeno $";
+
+/* Returns the index of the dispatch for the file descriptor. */
+int owl_select_find_dispatch(int fd)
+{
+  int i, len;
+  owl_list *dl;
+  owl_dispatch *d;
+  
+  dl = owl_global_get_dispatchlist(&g);
+  len = owl_list_get_size(dl);
+  for(i = 0; i < len; i++) {
+    d = (owl_dispatch*)owl_list_get_element(dl, i);
+    if (d->fd == fd) return i;
+  }
+  return -1;
+}
+
+/* Adds a new owl_dispatch to the list, replacing existing ones if needed. */
+void owl_select_add_dispatch(owl_dispatch *d)
+{
+  int elt;
+  owl_list *dl;
+
+  elt = owl_select_find_dispatch(d->fd);
+  dl = owl_global_get_dispatchlist(&g);
+  
+  if (elt != -1) {  /* If we have a dispatch for this FD */
+    owl_dispatch *d_old;
+    d_old = (owl_dispatch*)owl_list_get_element(dl, elt);
+    /* Ignore if we're adding the same dispatch again.  Otherwise
+       replace the old dispatch. */
+    if (d_old != d) {
+      owl_list_replace_element(dl, elt, d);
+      owl_free(d_old);
+    }
+  }
+  else {
+    owl_list_append_element(dl, d);
+  }
+}
+
+/* Removes an owl_dispatch to the list, based on it's file descriptor. */
+void owl_select_remove_dispatch(int fd)
+{
+  int elt;
+  owl_list *dl;
+
+  elt = owl_select_find_dispatch(fd);
+  dl = owl_global_get_dispatchlist(&g);
+  
+  if (elt != -1) {
+    owl_dispatch *d;
+    d = (owl_dispatch*)owl_list_get_element(dl, elt);
+    owl_list_remove_element(dl, elt);
+    if (d->pfunc) {
+      owl_perlconfig_dispatch_free(d);
+    }
+    owl_free(d);
+  }
+}
+
+int owl_select_dispatch_count()
+{
+  return owl_list_get_size(owl_global_get_dispatchlist(&g));
+}
+
+int owl_select_add_perl_dispatch(int fd, SV *cb)
+{
+  int elt;
+  owl_dispatch *d;
+  elt = owl_select_find_dispatch(fd);
+  if (elt != -1) {
+    d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt);
+    if (d->pfunc == NULL) {
+      /* don't mess with non-perl dispatch functions from here. */
+      return 1;
+    }
+  }
+
+  d = malloc(sizeof(owl_dispatch));
+  d->fd = fd;
+  d->cfunc = NULL;
+  d->pfunc = cb;
+  owl_select_add_dispatch(d);
+  return 0;
+}
+
+int owl_select_remove_perl_dispatch(int fd)
+{
+  int elt;
+  owl_dispatch *d;
+  
+  elt = owl_select_find_dispatch(fd);
+  if (elt != -1) {
+    d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt);
+    if (d->pfunc != NULL) {
+      owl_select_remove_dispatch(fd);
+      return 0;
+    }
+  }
+  return 1;
+}
+
+int owl_select_dispatch_prepare_fd_sets(fd_set *r, fd_set *e)
+{
+  int i, len, max_fd;
+  owl_dispatch *d;
+  owl_list *dl;
+
+  dl = owl_global_get_dispatchlist(&g);
+  FD_ZERO(r);
+  FD_ZERO(e);
+  max_fd = 0;
+  len = owl_select_dispatch_count(g);
+  for(i = 0; i < len; i++) {
+    d = (owl_dispatch*)owl_list_get_element(dl, i);
+    FD_SET(d->fd, r);
+    FD_SET(d->fd, e);
+    if (max_fd < d->fd) max_fd = d->fd;
+  }
+  return max_fd + 1;
+}
+
+void owl_select_dispatch(fd_set *fds, int max_fd)
+{
+  int i, len;
+  owl_dispatch *d;
+  owl_list *dl;
+
+  dl = owl_global_get_dispatchlist(&g);
+  len = owl_select_dispatch_count();
+  for(i = 0; i < len; i++) {
+    d = (owl_dispatch*)owl_list_get_element(dl, i);
+    /* While d shouldn't normally be null, the list may be altered by
+     * functions we dispatch to. */
+    if (d != NULL && FD_ISSET(d->fd, fds)) {
+      if (d->cfunc != NULL) {
+        (d->cfunc)();
+      }
+      else if (d->pfunc != NULL) {
+        owl_perlconfig_do_dispatch(d);
+      }
+    }
+  }
+}
+
+int owl_select_aim_hack(fd_set *rfds, fd_set *wfds)
+{
+  aim_conn_t *cur;
+  aim_session_t *sess;
+  int max_fd;
+
+  FD_ZERO(rfds);
+  FD_ZERO(wfds);
+  max_fd = 0;
+  sess = owl_global_get_aimsess(&g);
+  for (cur = sess->connlist, max_fd = 0; cur; cur = cur->next) {
+    if (cur->fd != -1) {
+      FD_SET(cur->fd, rfds);
+      if (cur->status & AIM_CONN_STATUS_INPROGRESS) {
+        /* Yes, we're checking writable sockets here. Without it, AIM
+           login is really slow. */
+        FD_SET(cur->fd, wfds);
+      }
+      
+      if (cur->fd > max_fd)
+        max_fd = cur->fd;
+    }
+  }
+  return max_fd;
+}
+
+void owl_select()
+{
+  int i, max_fd, aim_max_fd, aim_done;
+  fd_set r;
+  fd_set e;
+  fd_set aim_rfds, aim_wfds;
+  struct timeval timeout;
+
+  timeout.tv_sec = 1;
+  timeout.tv_usec = 0;
+
+  max_fd = owl_select_dispatch_prepare_fd_sets(&r, &e);
+
+  /* AIM HACK: 
+   *
+   *  The problem - I'm not sure where to hook into the owl/faim
+   *  interface to keep track of when the AIM socket(s) open and
+   *  close. In particular, the bosconn thing throws me off. So,
+   *  rather than register particular dispatchers for AIM, I look up
+   *  the relevant FDs and add them to select's watch lists, then
+   *  check for them individually before moving on to the other
+   *  dispatchers. --asedeno
+   */
+  aim_done = 1;
+  FD_ZERO(&aim_rfds);
+  FD_ZERO(&aim_wfds);
+  if (owl_global_is_doaimevents(&g)) {
+    aim_done = 0;
+    aim_max_fd = owl_select_aim_hack(&aim_rfds, &aim_wfds);
+    if (max_fd < aim_max_fd) max_fd = aim_max_fd;
+    for(i = 0; i <= aim_max_fd; i++) {
+      if (FD_ISSET(i, &aim_rfds)) {
+        FD_SET(i, &r);
+        FD_SET(i, &e);
+      }
+    }
+  }
+  /* END AIM HACK */
+
+  if ( select(max_fd+1, &r, &aim_wfds, &e, &timeout) ) {
+    /* Merge fd_sets and clear AIM FDs. */
+    for(i = 0; i <= max_fd; i++) {
+      /* Merge all interesting FDs into one set, since we have a
+         single dispatch per FD. */
+      if (FD_ISSET(i, &r) || FD_ISSET(i, &aim_wfds) || FD_ISSET(i, &e)) {
+        /* AIM HACK: no separate dispatch, just process here if
+           needed, and only once per run through. */
+        if (!aim_done && (FD_ISSET(i, &aim_rfds) || FD_ISSET(i, &aim_wfds))) {
+          owl_process_aim();
+          aim_done = 1;
+        }
+        else {
+          FD_SET(i, &r);
+        }
+      }
+    }
+    /* NOTE: the same dispatch function is called for both exceptional
+       and read ready FDs. */
+    owl_select_dispatch(&r, max_fd);
+  }
+}

Modified: branches/barnowl_sqlite/owl/zephyr.c
===================================================================
--- branches/barnowl_sqlite/owl/zephyr.c	2008-02-22 15:59:41 UTC (rev 964)
+++ branches/barnowl_sqlite/owl/zephyr.c	2008-02-24 02:32:20 UTC (rev 965)
@@ -29,7 +29,6 @@
   return(0);
 }
 
-
 int owl_zephyr_shutdown()
 {
 #ifdef HAVE_LIBZEPHYR


home help back first fref pref prev next nref lref last post