Mercurial > prosody-hg
comparison core/rostermanager.lua @ 176:e5cd2a03891d
Outbound presence subscription
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Sat, 25 Oct 2008 21:16:08 +0500 |
| parents | f9aff1fc7e99 |
| children | 606c433955e7 |
comparison
equal
deleted
inserted
replaced
| 175:5f71d290bb44 | 176:e5cd2a03891d |
|---|---|
| 94 end | 94 end |
| 95 | 95 |
| 96 function process_inbound_subscription_approval(username, host, jid) | 96 function process_inbound_subscription_approval(username, host, jid) |
| 97 local roster = load_roster(username, host); | 97 local roster = load_roster(username, host); |
| 98 local item = roster[jid]; | 98 local item = roster[jid]; |
| 99 if item and item.ask and (item.subscription == "none" or item.subscription == "from") then | 99 if item and item.ask then |
| 100 if item.subscription == "none" then | 100 if item.subscription == "none" then |
| 101 item.subscription = "to"; | 101 item.subscription = "to"; |
| 102 else | 102 else -- subscription == from |
| 103 item.subscription = "both"; | 103 item.subscription = "both"; |
| 104 end | 104 end |
| 105 item.ask = nil; | 105 item.ask = nil; |
| 106 return datamanager.store(username, host, "roster", roster); | 106 return datamanager.store(username, host, "roster", roster); |
| 107 end | 107 end |
| 108 end | 108 end |
| 109 | 109 |
| 110 function process_inbound_subscription_cancellation(username, host, jid) | 110 function process_inbound_subscription_cancellation(username, host, jid) |
| 111 local roster = load_roster(username, host); | 111 local roster = load_roster(username, host); |
| 112 local item = roster[jid]; | 112 local item = roster[jid]; |
| 113 if item and (item.subscription == "to" or item.subscription == "both") then | 113 local changed = nil; |
| 114 if is_contact_pending_out(username, host, jid) then | |
| 115 item.ask = nil; | |
| 116 changed = true; | |
| 117 end | |
| 118 if item then | |
| 114 if item.subscription == "to" then | 119 if item.subscription == "to" then |
| 115 item.subscription = "none"; | 120 item.subscription = "none"; |
| 116 else | 121 changed = true; |
| 122 elseif item.subscription == "both" then | |
| 117 item.subscription = "from"; | 123 item.subscription = "from"; |
| 118 end | 124 changed = true; |
| 119 -- FIXME do we need to item.ask = nil;? | 125 end |
| 126 end | |
| 127 if changed then | |
| 120 return datamanager.store(username, host, "roster", roster); | 128 return datamanager.store(username, host, "roster", roster); |
| 121 end | 129 end |
| 122 end | 130 end |
| 123 | 131 |
| 124 function process_inbound_unsubscribe(username, host, jid) | 132 function process_inbound_unsubscribe(username, host, jid) |
| 125 local roster = load_roster(username, host); | 133 local roster = load_roster(username, host); |
| 126 local item = roster[jid]; | 134 local item = roster[jid]; |
| 127 if item and (item.subscription == "from" or item.subscription == "both") then | 135 local changed = nil; |
| 136 if is_contact_pending_in(username, host, jid) then | |
| 137 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty? | |
| 138 changed = true; | |
| 139 end | |
| 140 if item then | |
| 128 if item.subscription == "from" then | 141 if item.subscription == "from" then |
| 129 item.subscription = "none"; | 142 item.subscription = "none"; |
| 130 else | 143 changed = true; |
| 144 elseif item.subscription == "both" then | |
| 131 item.subscription = "to"; | 145 item.subscription = "to"; |
| 132 end | 146 changed = true; |
| 133 item.ask = nil; | 147 end |
| 148 end | |
| 149 if changed then | |
| 134 return datamanager.store(username, host, "roster", roster); | 150 return datamanager.store(username, host, "roster", roster); |
| 135 end | 151 end |
| 136 end | 152 end |
| 137 | 153 |
| 138 function is_contact_subscribed(username, host, jid) | 154 function is_contact_subscribed(username, host, jid) |
| 139 local roster = load_roster(username, host); | 155 local roster = load_roster(username, host); |
| 140 local item = roster[jid]; | 156 local item = roster[jid]; |
| 141 return item and (item.subscription == "from" or item.subscription == "both"); | 157 return item and (item.subscription == "from" or item.subscription == "both"); |
| 142 end | 158 end |
| 143 | 159 |
| 160 function is_contact_pending_in(username, host, jid) | |
| 161 local roster = load_roster(username, host); | |
| 162 return roster.pending or roster.pending[jid]; | |
| 163 end | |
| 164 function set_contact_pending_in(username, host, jid, pending) | |
| 165 local roster = load_roster(username, host); | |
| 166 local item = roster[jid]; | |
| 167 if item and (item.subscription == "from" or item.subscription == "both") then | |
| 168 return; -- false | |
| 169 end | |
| 170 if not roster.pending then roster.pending = {}; end | |
| 171 roster.pending[jid] = true; | |
| 172 return datamanager.store(username, host, "roster", roster); | |
| 173 end | |
| 174 function is_contact_pending_out(username, host, jid) | |
| 175 local roster = load_roster(username, host); | |
| 176 local item = roster[jid]; | |
| 177 return item and item.ask; | |
| 178 end | |
| 179 function set_contact_pending_out(username, host, jid) -- subscribe | |
| 180 local roster = load_roster(username, host); | |
| 181 local item = roster[jid]; | |
| 182 if item and (item.ask or item.subscription == "to" or item.subscription == "both") then | |
| 183 return true; | |
| 184 end | |
| 185 if not item then | |
| 186 item = {subscription = "none"}; | |
| 187 roster[jid] = item; | |
| 188 end | |
| 189 item.ask = "subscribe"; | |
| 190 return datamanager.store(username, host, "roster", roster); | |
| 191 end | |
| 192 function unsubscribe(username, host, jid) | |
| 193 local roster = load_roster(username, host); | |
| 194 local item = roster[jid]; | |
| 195 if not item then return false; end | |
| 196 if (item.subscription == "from" or item.subscription == "none") and not item.ask then | |
| 197 return true; | |
| 198 end | |
| 199 item.ask = nil; | |
| 200 if item.subscription == "both" then | |
| 201 item.subscription = "from"; | |
| 202 elseif item.subscription == "to" then | |
| 203 item.subscription = "none"; | |
| 204 end | |
| 205 return datamanager.store(username, host, "roster", roster); | |
| 206 end | |
| 207 function subscribed(username, host, jid) | |
| 208 if is_contact_pending_in(username, host, jid) then | |
| 209 local roster = load_roster(username, host); | |
| 210 local item = roster[jid]; | |
| 211 if item.subscription == "none" then | |
| 212 item.subscription = "from"; | |
| 213 else -- subscription == to | |
| 214 item.subsctiption = "both"; | |
| 215 end | |
| 216 roster.pending[jid] = nil; | |
| 217 -- TODO maybe remove roster.pending if empty | |
| 218 return datamanager.store(username, host, "roster", roster); | |
| 219 end -- TODO else implement optional feature pre-approval (ask = subscribed) | |
| 220 end | |
| 221 function unsubscribed(username, host, jid) | |
| 222 local roster = load_roster(username, host); | |
| 223 local item = roster[jid]; | |
| 224 local pending = is_contact_pending_in(username, host, jid); | |
| 225 local changed = nil; | |
| 226 if is_contact_pending_in(username, host, jid) then | |
| 227 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty? | |
| 228 changed = true; | |
| 229 end | |
| 230 if item then | |
| 231 if item.subscription == "from" then | |
| 232 item.subscription = "none"; | |
| 233 changed = true; | |
| 234 elseif item.subscription == both then | |
| 235 item.subscription = "to"; | |
| 236 changed = true; | |
| 237 end | |
| 238 end | |
| 239 if changed then | |
| 240 return datamanager.store(username, host, "roster", roster); | |
| 241 end | |
| 242 end | |
| 243 | |
| 244 function process_outbound_subscription_request(username, host, jid) | |
| 245 local roster = load_roster(username, host); | |
| 246 local item = roster[jid]; | |
| 247 if item and (item.subscription == "none" or item.subscription == "from") then | |
| 248 item.ask = "subscribe"; | |
| 249 return datamanager.store(username, host, "roster", roster); | |
| 250 end | |
| 251 end | |
| 252 | |
| 253 --[[function process_outbound_subscription_approval(username, host, jid) | |
| 254 local roster = load_roster(username, host); | |
| 255 local item = roster[jid]; | |
| 256 if item and (item.subscription == "none" or item.subscription == "from" then | |
| 257 item.ask = "subscribe"; | |
| 258 return datamanager.store(username, host, "roster", roster); | |
| 259 end | |
| 260 end]] | |
| 261 | |
| 262 | |
| 263 | |
| 144 return _M; | 264 return _M; |
