<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -32,6 +32,7 @@
 -include(&quot;ejabberd_http.hrl&quot;).
 
 -record(state, {host, db_mod}).
+-record(mod_tweet_presence, {jid, presence}).
 
 -define(PROCNAME, ?MODULE).
 
@@ -47,6 +48,14 @@ start_link(Host, Opts) -&gt;
     gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []).
 
 start(Host, Opts) -&gt;
+    mnesia:create_table(
+      mod_tweet_presence,
+      [{ram_copies, [node()]},
+       {attributes, record_info(
+                      fields,
+                      mod_tweet_presence)}]
+     ),
+
     Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
     ChildSpec =
 	{Proc,
@@ -150,6 +159,11 @@ handle_info({route, From, To, {xmlelement, &quot;iq&quot;, Attrs, _}=IQ}, State) -&gt;
             nok
     end,
     {noreply, State};
+handle_info({route, From, To, 
+             {xmlelement, &quot;presence&quot;, _, _}=Packet}, State) -&gt;
+    handle_presence(From, To, Packet),
+    {noreply, State};
+%% TODO send error-reply for bad jid
 handle_info(_Info, State) -&gt;
     {noreply, State}.
 
@@ -442,6 +456,65 @@ handle_iq_get({From, To, IQ}, State) -&gt;
                 ?ERR_FEATURE_NOT_IMPLEMENTED))
     end.
 
+handle_presence(From, To, Packet) -&gt;
+    FromS = jlib:jid_to_string(
+              jlib:jid_tolower(
+                jlib:jid_remove_resource(From))),
+    Type = xml:get_tag_attr_s(&quot;type&quot;, Packet),
+    case Type of 
+        &quot;subscribe&quot; -&gt;
+            %% send back subscription
+            {xmlelement, &quot;presence&quot;, Attrs, _} = 
+                xml:replace_tag_attr(&quot;type&quot;, &quot;subscribed&quot;, Packet),
+            ejabberd_router:route(
+              To,
+              From,
+              jlib:replace_from_to(
+                To, 
+                From, 
+                {xmlelement, &quot;presence&quot;, Attrs, []})
+             );
+        &quot;unsubscribe&quot; -&gt;
+            mnesia:dirty_delete(mod_tweet_presence, FromS),
+            %% send back unsubscribe response
+            {xmlelement, &quot;presence&quot;, Attrs, _} = 
+                xml:replace_tag_attr(&quot;type&quot;, &quot;unsubscribed&quot;, Packet),
+            ejabberd_router:route(
+              To,
+              From,
+              jlib:replace_from_to(
+                To, 
+                From, 
+                {xmlelement, &quot;presence&quot;, Attrs, []})
+	     );
+        &quot;unavailable&quot; -&gt;
+            mnesia:dirty_delete(mod_tweet_presence, FromS);
+        _ -&gt;
+            case mnesia:dirty_read(mod_tweet_presence, FromS) of 
+                [] -&gt; 
+                    %%  no presence yet, send back own presence
+                    ejabberd_router:route(
+                      To,
+                      From,
+                      {xmlelement, 
+                       &quot;presence&quot;, 
+                       [{&quot;to&quot;, jlib:jid_to_string(From)}, 
+                        {&quot;from&quot;, jlib:jid_to_string(To)}], []});
+                _ -&gt;
+                    ok
+            end,
+            P = case xml:get_subtag_cdata(Packet, &quot;show&quot;) of
+                    &quot;&quot; -&gt;
+                        &quot;available&quot;;
+                    Show -&gt;
+                        Show
+                end,
+            mnesia:dirty_write(mod_tweet_presence, 
+                               #mod_tweet_presence{
+                                 jid=FromS, 
+                                 presence=P})
+    end.
+
 get_db_mod() -&gt;
     case ets:lookup(mod_tweet_cfg, db_mod) of
         [] -&gt;</diff>
      <filename>src/mod_tweet.erl</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>a20bbd888cc1bbfb41b88d89caae8654e9a1cdf7</id>
    </parent>
  </parents>
  <author>
    <name>Stefan Strigler</name>
    <email>zeank@jwchat.org</email>
  </author>
  <url>http://github.com/sstrigler/mod_tweet/commit/a99440f7c4417ababba60c98faa6416c2c5166bb</url>
  <id>a99440f7c4417ababba60c98faa6416c2c5166bb</id>
  <committed-date>2008-09-18T04:11:17-07:00</committed-date>
  <authored-date>2008-09-18T04:11:17-07:00</authored-date>
  <message>initial support for handling presence</message>
  <tree>4bb832fbfc0b7f2db3d61fa34bb94bb5d5974992</tree>
  <committer>
    <name>Stefan Strigler</name>
    <email>zeank@jwchat.org</email>
  </committer>
</commit>
