]> git.babelmonkeys.de Git - mpdbot.git/blob - src/mpdbot.m
Stop pretending to be Objective-C 1 compatible
[mpdbot.git] / src / mpdbot.m
1 /*
2  * Copyright (c) 2010, 2011, Jonathan Schleifer <js@webkeks.org>
3  * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
4  *
5  * http://cgit.babelmonkeys.de/cgit.cgi/mpdbot/
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice is present in all copies.
10  *
11  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
12  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
15  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
16  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
17  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
18  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
19  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21  * POSSIBILITY OF SUCH DAMAGE.
22  */
23
24 #import <ObjFW/ObjFW.h>
25 #import <ObjXMPP/ObjXMPP.h>
26
27 #import "PEPThread.h"
28
29 #define NS_DISCO_INFO @"http://jabber.org/protocol/disco#info"
30
31 @interface AppDelegate: OFObject <OFApplicationDelegate, XMPPConnectionDelegate>
32 OFString *discoID;
33 PEPThread *pepper;
34 @end
35
36 OF_APPLICATION_DELEGATE(AppDelegate)
37
38 @implementation AppDelegate
39 - (void)applicationDidFinishLaunching
40 {
41         XMPPConnection *conn;
42         OFArray *arguments = [OFApplication arguments];
43
44         conn = [[XMPPConnection alloc] init];
45         conn.delegate = self;
46
47         if (arguments.count != 3) {
48                 [of_stdout writeFormat: @"Usage: %@ <server> <user> <passwd>\n",
49                     [OFApplication programName]];
50                 [OFApplication terminateWithStatus: 1];
51         }
52
53         conn.domain = [arguments objectAtIndex: 0];
54         conn.username = [arguments objectAtIndex: 1];
55         conn.password = [arguments objectAtIndex: 2];
56         conn.resource = @"ObjXMPP";
57
58         @try {
59                 [conn connect];
60                 [conn handleConnection];
61         } @catch (id e) {
62                 of_log(@"%@", e);
63         }
64 }
65
66 - (void)connectionWasAuthenticated: (XMPPConnection*)conn
67 {
68         [of_stdout writeLine: @"Auth successful"];
69 }
70
71 - (void)connection: (XMPPConnection*)conn
72      wasBoundToJID: (XMPPJID*)jid
73 {
74         XMPPPresence *pres;
75         XMPPIQ *disco;
76
77         [of_stdout writeFormat: @"Bound to JID: %@\n", [jid fullJID]];
78
79         pres = [XMPPPresence presence];
80         [pres addPriority: 0];
81         [pres addStatus: @"Hello I'm MPDbot!"];
82         [conn sendStanza: pres];
83
84         discoID = [[conn generateStanzaID] retain];
85         disco = [XMPPIQ IQWithType: @"get"
86                                 ID: discoID];
87         disco.to = [XMPPJID JIDWithString: [[conn JID] bareJID]];
88         [disco addChild: [OFXMLElement
89             elementWithName: @"query"
90                   namespace: NS_DISCO_INFO]];
91
92         [conn sendStanza: disco];
93 }
94
95 - (BOOL)connection: (XMPPConnection*)conn
96       didReceiveIQ: (XMPPIQ*)iq
97 {
98         OFXMLElement *query = [iq elementForName: @"query"
99                                        namespace: NS_DISCO_INFO];
100         if ([iq.ID isEqual: discoID]) {
101                 for (OFXMLElement *identity
102                     in [query elementsForName: @"identity"
103                                     namespace: NS_DISCO_INFO]) {
104                         if ([[[identity attributeForName: @"category"]
105                             stringValue] isEqual: @"pubsub"] &&
106                             [[[identity attributeForName: @"type"] stringValue]
107                             isEqual: @"pep"]) {
108                                 pepper = [[PEPThread alloc]
109                                     initWithObject: conn];
110                                 [pepper start];
111
112                                 return YES;
113                         }
114                 }
115         }
116
117         return NO;
118 }
119
120 -  (void)connection: (XMPPConnection*)conn
121   didReceiveMessage: (XMPPMessage*)msg
122 {
123         of_log(@"Message: %@", msg);
124 }
125
126 -   (void)connection: (XMPPConnection*)conn
127   didReceivePresence: (XMPPPresence*)pres
128 {
129         if ([pres.type isEqual: @"subscribe"]) {
130                 XMPPPresence *answer;
131                 answer = [XMPPPresence presenceWithType: @"subscribed"
132                                                      ID: pres.ID];
133                 answer.to = [XMPPJID JIDWithString: [pres.from bareJID]];
134                 [conn sendStanza: answer];
135         }
136 }
137
138 - (void)connectionWasClosed: (XMPPConnection*)conn
139 {
140         [of_stdout writeLine: @"Connection was closed!"];
141 }
142 @end